1414
1515use Bazinga \GeocoderBundle \BazingaGeocoderBundle ;
1616use Geocoder \Query \GeocodeQuery ;
17+ use Http \Message \RequestMatcher \RequestMatcher ;
18+ use Http \Mock \Client ;
1719use Nyholm \BundleTest \TestKernel ;
20+ use Psr \Http \Message \RequestInterface ;
21+ use Psr \Http \Message \ResponseInterface ;
22+ use Psr \Http \Message \StreamInterface ;
1823use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
1924use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
2025use Symfony \Component \HttpKernel \KernelInterface ;
@@ -56,6 +61,28 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
5661 $ kernel ->setClearCacheAfterShutdown (false );
5762 $ container = self ::getContainer ();
5863
64+ $ httpClient = $ container ->get (Client::class);
65+ $ httpClient ->on (new RequestMatcher (), function (RequestInterface $ request ) {
66+ if ('https://freegeoip.app/json/123.123.123.128 ' === (string ) $ request ->getUri ()) {
67+ $ stream = $ this ->createMock (StreamInterface::class);
68+ $ stream ->expects (self ::once ())
69+ ->method ('__toString ' )
70+ ->willReturn ('{"ip":"123.123.123.128","country_code":"CN","country_name":"China","region_code":"CN-BJ","region_name":"Beijing","city":"Beijing","zip_code":"100006","time_zone":"Asia\/Shanghai","latitude":39.907501220703125,"longitude":116.39710235595703,"metro_code":0} ' );
71+
72+ $ response = $ this ->createMock (ResponseInterface::class);
73+ $ response ->expects (self ::once ())
74+ ->method ('getStatusCode ' )
75+ ->willReturn (200 );
76+ $ response ->expects (self ::once ())
77+ ->method ('getBody ' )
78+ ->willReturn ($ stream );
79+
80+ return $ response ;
81+ }
82+
83+ self ::fail (sprintf ('Unexpected http call "%s %s". ' , $ request ->getMethod (), (string ) $ request ->getUri ()));
84+ });
85+
5986 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
6087 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
6188 $ country = $ result ->first ()->getCountry ()->getCode ();
@@ -75,6 +102,28 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
75102 $ kernel ->setClearCacheAfterShutdown (false );
76103 $ container = self ::getContainer ();
77104
105+ $ httpClient = $ container ->get (Client::class);
106+ $ httpClient ->on (new RequestMatcher (), function (RequestInterface $ request ) {
107+ if ('https://freegeoip.app/json/87.98.128.10 ' === (string ) $ request ->getUri ()) {
108+ $ stream = $ this ->createMock (StreamInterface::class);
109+ $ stream ->expects (self ::once ())
110+ ->method ('__toString ' )
111+ ->willReturn ('{"ip":"87.98.128.10","country_code":"FR","country_name":"France","region_code":null,"region_name":"Nord","city":"Roubaix","zip_code":"59100","time_zone":"Europe\/Paris","latitude":50.69371032714844,"longitude":3.174438953399658,"metro_code":0} ' );
112+
113+ $ response = $ this ->createMock (ResponseInterface::class);
114+ $ response ->expects (self ::once ())
115+ ->method ('getStatusCode ' )
116+ ->willReturn (200 );
117+ $ response ->expects (self ::once ())
118+ ->method ('getBody ' )
119+ ->willReturn ($ stream );
120+
121+ return $ response ;
122+ }
123+
124+ self ::fail (sprintf ('Unexpected http call "%s %s". ' , $ request ->getMethod (), (string ) $ request ->getUri ()));
125+ });
126+
78127 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
79128 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
80129 $ country = $ result ->first ()->getCountry ()->getCode ();
0 commit comments