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 \DependencyInjection \Compiler \CompilerPassInterface ;
21+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
22+ use Symfony \Component \HttpClient \MockHttpClient ;
23+ use Symfony \Component \HttpClient \Response \MockResponse ;
2524use Symfony \Component \HttpKernel \KernelInterface ;
2625
2726final class PluginInteractionTest extends KernelTestCase
@@ -40,6 +39,14 @@ protected static function createKernel(array $options = []): KernelInterface
4039 */
4140 $ kernel = parent ::createKernel ($ options );
4241 $ kernel ->addTestBundle (BazingaGeocoderBundle::class);
42+ $ kernel ->addTestCompilerPass (new class implements CompilerPassInterface {
43+ public function process (ContainerBuilder $ container ): void
44+ {
45+ $ container
46+ ->getDefinition ('http_client ' )
47+ ->setPublic (true );
48+ }
49+ });
4350 $ kernel ->handleOptions ($ options );
4451
4552 return $ kernel ;
@@ -61,27 +68,12 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
6168 $ kernel ->setClearCacheAfterShutdown (false );
6269 $ container = self ::getContainer ();
6370
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- }
71+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url ): MockResponse {
72+ self ::assertSame ('GET ' , $ method );
73+ self ::assertSame ('https://freegeoip.app/json/123.123.123.128 ' , $ url );
8274
83- self :: fail ( sprintf ( ' Unexpected http call "%s %s". ' , $ request -> getMethod (), ( string ) $ request -> getUri ()) );
84- });
75+ 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 ' ]] );
76+ })) ;
8577
8678 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
8779 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
@@ -102,27 +94,12 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void
10294 $ kernel ->setClearCacheAfterShutdown (false );
10395 $ container = self ::getContainer ();
10496
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- }
97+ $ container ->set ('http_client ' , new MockHttpClient (static function (string $ method , string $ url ): MockResponse {
98+ self ::assertSame ('GET ' , $ method );
99+ self ::assertSame ('https://freegeoip.app/json/87.98.128.10 ' , $ url );
123100
124- self :: fail ( sprintf ( ' Unexpected http call "%s %s". ' , $ request -> getMethod (), ( string ) $ request -> getUri ()) );
125- });
101+ 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 ' ]] );
102+ })) ;
126103
127104 $ geoPluginGeocoder = $ container ->get ('bazinga_geocoder.provider.geoPlugin ' );
128105 $ result = $ geoPluginGeocoder ->geocodeQuery (GeocodeQuery::create ('::1 ' ));
0 commit comments