1414
1515use Bazinga \GeocoderBundle \BazingaGeocoderBundle ;
1616use Geocoder \Query \GeocodeQuery ;
17- use Http \Message \RequestMatcher \RequestMatcher ;
18- use Http \Mock \Client ;
1917use Nyholm \BundleTest \TestKernel ;
20- use Psr \Http \Message \RequestInterface ;
21- use Psr \Http \Message \ResponseInterface ;
22- use Psr \Http \Message \StreamInterface ;
2318use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
2419use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
20+ use Symfony \Component \HttpClient \MockHttpClient ;
21+ use Symfony \Component \HttpClient \Response \JsonMockResponse ;
2522use Symfony \Component \HttpKernel \KernelInterface ;
2623
2724final class PluginInteractionTest extends KernelTestCase
@@ -61,27 +58,24 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
6158 $ kernel ->setClearCacheAfterShutdown (false );
6259 $ container = self ::getContainer ();
6360
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- });
61+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url , array $ options ): JsonMockResponse {
62+ self ::assertSame ('GET ' , $ method );
63+ self ::assertSame ('https://freegeoip.app/json/123.123.123.128 ' , $ url );
64+
65+ return new JsonMockResponse ([
66+ 'ip ' => '123.123.123.128 ' ,
67+ 'country_code ' => 'CN ' ,
68+ 'country_name ' => 'China ' ,
69+ 'region_code ' => 'CN-BJ ' ,
70+ 'region_name ' => 'Beijing ' ,
71+ 'city ' => 'Beijing ' ,
72+ 'zip_code ' => '100006 ' ,
73+ 'time_zone ' => 'Asia/Shanghai ' ,
74+ 'latitude ' => 39.907501220703125 ,
75+ 'longitude ' => 116.39710235595703 ,
76+ 'metro_code ' => 0 ,
77+ ]);
78+ }));
8579
8680 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
8781 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
@@ -102,27 +96,24 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
10296 $ kernel ->setClearCacheAfterShutdown (false );
10397 $ container = self ::getContainer ();
10498
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- });
99+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url , array $ options ): JsonMockResponse {
100+ self ::assertSame ('GET ' , $ method );
101+ self ::assertSame ('https://freegeoip.app/json/87.98.128.10 ' , $ url );
102+
103+ return new JsonMockResponse ([
104+ 'ip ' => '87.98.128.10 ' ,
105+ 'country_code ' => 'FR ' ,
106+ 'country_name ' => 'France ' ,
107+ 'region_code ' => null ,
108+ 'region_name ' => 'Nord ' ,
109+ 'city ' => 'Roubaix ' ,
110+ 'zip_code ' => '59100 ' ,
111+ 'time_zone ' => 'Europe/Paris ' ,
112+ 'latitude ' => 50.69371032714844 ,
113+ 'longitude ' => 3.174438953399658 ,
114+ 'metro_code ' => 0 ,
115+ ]);
116+ }));
126117
127118 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
128119 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
0 commit comments