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 \MockResponse ;
2522use Symfony \Component \HttpKernel \KernelInterface ;
2623
2724final class PluginInteractionTest extends KernelTestCase
@@ -61,27 +58,12 @@ 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- }
61+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url ): MockResponse {
62+ self ::assertSame ('GET ' , $ method );
63+ self ::assertSame ('https://freegeoip.app/json/123.123.123.128 ' , $ url );
8264
83- self :: fail ( sprintf ( ' Unexpected http call "%s %s". ' , $ request -> getMethod (), ( string ) $ request -> getUri ()) );
84- });
65+ return new MockResponse ( ' {"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} ' , [ ' response_headers ' => [ ' content-type ' => ' application-json ' ]] );
66+ })) ;
8567
8668 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
8769 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
@@ -102,27 +84,12 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
10284 $ kernel ->setClearCacheAfterShutdown (false );
10385 $ container = self ::getContainer ();
10486
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- }
87+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url ): MockResponse {
88+ self ::assertSame ('GET ' , $ method );
89+ self ::assertSame ('https://freegeoip.app/json/87.98.128.10 ' , $ url );
12390
124- self :: fail ( sprintf ( ' Unexpected http call "%s %s". ' , $ request -> getMethod (), ( string ) $ request -> getUri ()) );
125- });
91+ return new MockResponse ( ' {"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} ' , [ ' response_headers ' => [ ' content-type ' => ' application-json ' ]] );
92+ })) ;
12693
12794 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
12895 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
0 commit comments