From e90299a65216008ce7f9a6467e26b592aa479387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Nork=C5=ABnas?= Date: Mon, 15 Dec 2025 06:38:05 +0200 Subject: [PATCH] Fix test --- composer.json | 1 + phpstan-baseline.neon | 10 ---- tests/Functional/CustomTestKernel.php | 53 ++----------------- tests/Functional/PluginInteractionTest.php | 53 ++++++++++++++++++- ..._cache_cn.yml => fakeip_with_cache_cn.yml} | 11 ++-- ..._cache_fr.yml => fakeip_with_cache_fr.yml} | 11 ++-- tests/baseline-ignore | 7 +++ 7 files changed, 79 insertions(+), 67 deletions(-) rename tests/Functional/config/{geo_plugin_fakeip_with_cache_cn.yml => fakeip_with_cache_cn.yml} (66%) rename tests/Functional/config/{geo_plugin_fakeip_with_cache_fr.yml => fakeip_with_cache_fr.yml} (66%) diff --git a/composer.json b/composer.json index 0cf72da..d5727a8 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "php-http/discovery": "^1.14", "symfony/console": "^5.4 || ^6.4 || ^7.0", "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0", + "symfony/http-kernel": "^5.4 || ^6.4 || ^7.0", "symfony/options-resolver": "^5.4 || ^6.4 || ^7.0", "willdurand/geocoder": "^4.6|^5.0" }, diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4d7a09d..97da4e6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -115,16 +115,6 @@ parameters: count: 1 path: src/DependencyInjection/BazingaGeocoderExtension.php - - - message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:end\\(\\)\\.$#" - count: 2 - path: src/DependencyInjection/Configuration.php - - - - message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:variableNode\\(\\)\\.$#" - count: 1 - path: src/DependencyInjection/Configuration.php - - message: "#^Call to an undefined method Doctrine\\\\ORM\\\\Event\\\\OnFlushEventArgs\\:\\:getEntityManager\\(\\)\\.$#" count: 1 diff --git a/tests/Functional/CustomTestKernel.php b/tests/Functional/CustomTestKernel.php index e32e3fb..1b9a592 100644 --- a/tests/Functional/CustomTestKernel.php +++ b/tests/Functional/CustomTestKernel.php @@ -13,6 +13,7 @@ namespace Bazinga\GeocoderBundle\Tests\Functional; use Nyholm\BundleTest\TestKernel; +use Nyholm\NSA; use Symfony\Component\Config\ConfigCache; use Symfony\Component\DependencyInjection\Dumper\Preloader; use Symfony\Component\ErrorHandler\DebugClassLoader; @@ -28,15 +29,6 @@ */ class CustomTestKernel extends TestKernel { - private $warmupDir; - - public function reboot(?string $warmupDir) - { - $this->shutdown(); - $this->warmupDir = $warmupDir; - $this->boot(); - } - /* * Needed, otherwise the used cache is different on each kernel boot, which is a big issue in PluginInteractionTest */ @@ -45,45 +37,6 @@ public function getCacheDir(): string return realpath(sys_get_temp_dir()).'/NyholmBundleTest/cachePluginInteractionTest'; } - /** - * Returns the kernel parameters. - */ - protected function getKernelParameters(): array - { - $bundles = []; - $bundlesMetadata = []; - - foreach ($this->bundles as $name => $bundle) { - $bundles[$name] = \get_class($bundle); - $bundlesMetadata[$name] = [ - 'path' => $bundle->getPath(), - 'namespace' => $bundle->getNamespace(), - ]; - } - - return [ - 'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(), - 'kernel.environment' => $this->environment, - 'kernel.runtime_environment' => '%env(default:kernel.environment:APP_RUNTIME_ENV)%', - 'kernel.debug' => $this->debug, - 'kernel.build_dir' => realpath($buildDir = $this->warmupDir ?: $this->getBuildDir()) ?: $buildDir, - 'kernel.cache_dir' => realpath($cacheDir = ($this->getCacheDir() === $this->getBuildDir() ? ($this->warmupDir ?: $this->getCacheDir()) : $this->getCacheDir())) ?: $cacheDir, - 'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(), - 'kernel.bundles' => $bundles, - 'kernel.bundles_metadata' => $bundlesMetadata, - 'kernel.charset' => $this->getCharset(), - 'kernel.container_class' => $this->getContainerClass(), - ]; - } - - /** - * @internal - */ - public function setAnnotatedClassCache(array $annotatedClasses): void - { - file_put_contents(($this->warmupDir ?: $this->getBuildDir()).'/annotations.map', sprintf('getContainerClass(); - $buildDir = $this->warmupDir ?: $this->getBuildDir(); + $buildDir = $warmupDir ?: $this->getBuildDir(); $cache = new ConfigCache($buildDir.'/'.$class.'.php', $this->debug); $cachePath = $cache->getPath(); diff --git a/tests/Functional/PluginInteractionTest.php b/tests/Functional/PluginInteractionTest.php index 77a6fe7..3f7db7e 100644 --- a/tests/Functional/PluginInteractionTest.php +++ b/tests/Functional/PluginInteractionTest.php @@ -14,7 +14,12 @@ use Bazinga\GeocoderBundle\BazingaGeocoderBundle; use Geocoder\Query\GeocodeQuery; +use Http\Message\RequestMatcher\RequestMatcher; +use Http\Mock\Client; use Nyholm\BundleTest\TestKernel; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\HttpKernel\KernelInterface; @@ -51,11 +56,33 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void } $kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml'); - $kernel->addTestConfig(__DIR__.'/config/geo_plugin_fakeip_with_cache_cn.yml'); + $kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_cn.yml'); }]); $kernel->setClearCacheAfterShutdown(false); $container = self::getContainer(); + $httpClient = $container->get(Client::class); + $httpClient->on(new RequestMatcher(), function (RequestInterface $request) { + if ('https://freegeoip.app/json/123.123.123.128' === (string) $request->getUri()) { + $stream = $this->createMock(StreamInterface::class); + $stream->expects(self::once()) + ->method('__toString') + ->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}'); + + $response = $this->createMock(ResponseInterface::class); + $response->expects(self::once()) + ->method('getStatusCode') + ->willReturn(200); + $response->expects(self::once()) + ->method('getBody') + ->willReturn($stream); + + return $response; + } + + self::fail(sprintf('Unexpected http call "%s %s".', $request->getMethod(), (string) $request->getUri())); + }); + $geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin'); $result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1')); $country = $result->first()->getCountry()->getCode(); @@ -70,11 +97,33 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void } $kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml'); - $kernel->addTestConfig(__DIR__.'/config/geo_plugin_fakeip_with_cache_fr.yml'); + $kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_fr.yml'); }]); $kernel->setClearCacheAfterShutdown(false); $container = self::getContainer(); + $httpClient = $container->get(Client::class); + $httpClient->on(new RequestMatcher(), function (RequestInterface $request) { + if ('https://freegeoip.app/json/87.98.128.10' === (string) $request->getUri()) { + $stream = $this->createMock(StreamInterface::class); + $stream->expects(self::once()) + ->method('__toString') + ->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}'); + + $response = $this->createMock(ResponseInterface::class); + $response->expects(self::once()) + ->method('getStatusCode') + ->willReturn(200); + $response->expects(self::once()) + ->method('getBody') + ->willReturn($stream); + + return $response; + } + + self::fail(sprintf('Unexpected http call "%s %s".', $request->getMethod(), (string) $request->getUri())); + }); + $geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin'); $result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1')); $country = $result->first()->getCountry()->getCode(); diff --git a/tests/Functional/config/geo_plugin_fakeip_with_cache_cn.yml b/tests/Functional/config/fakeip_with_cache_cn.yml similarity index 66% rename from tests/Functional/config/geo_plugin_fakeip_with_cache_cn.yml rename to tests/Functional/config/fakeip_with_cache_cn.yml index 6bf704c..c9c0ee4 100644 --- a/tests/Functional/config/geo_plugin_fakeip_with_cache_cn.yml +++ b/tests/Functional/config/fakeip_with_cache_cn.yml @@ -2,13 +2,18 @@ bazinga_geocoder: # The local IP (127.0.0.1) will be replaced by the fake_ip # see https://github.com/geocoder-php/BazingaGeocoderBundle/blob/5.0.0/Resources/doc/index.md#fake-local-ip - fake_ip: + fake_ip: local_ip: ::1 ip: 123.123.123.128 # this ip is in china providers: geoPlugin: - factory: Bazinga\GeocoderBundle\ProviderFactory\GeoPluginFactory + factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory cache: 'app.simple_cache' cache_lifetime: 42 - cache_precision: ~ \ No newline at end of file + cache_precision: ~ + options: + http_client: '@Http\Mock\Client' + +services: + Http\Mock\Client: ~ diff --git a/tests/Functional/config/geo_plugin_fakeip_with_cache_fr.yml b/tests/Functional/config/fakeip_with_cache_fr.yml similarity index 66% rename from tests/Functional/config/geo_plugin_fakeip_with_cache_fr.yml rename to tests/Functional/config/fakeip_with_cache_fr.yml index f0b4b32..0b7e85a 100644 --- a/tests/Functional/config/geo_plugin_fakeip_with_cache_fr.yml +++ b/tests/Functional/config/fakeip_with_cache_fr.yml @@ -2,13 +2,18 @@ bazinga_geocoder: # The local IP (127.0.0.1) will be replaced by the fake_ip # see https://github.com/geocoder-php/BazingaGeocoderBundle/blob/5.0.0/Resources/doc/index.md#fake-local-ip - fake_ip: + fake_ip: local_ip: ::1 ip: 87.98.128.10 # this ip is in france providers: geoPlugin: - factory: Bazinga\GeocoderBundle\ProviderFactory\GeoPluginFactory + factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory cache: 'app.simple_cache' cache_lifetime: 42 - cache_precision: ~ \ No newline at end of file + cache_precision: ~ + options: + http_client: '@Http\Mock\Client' + +services: + Http\Mock\Client: ~ diff --git a/tests/baseline-ignore b/tests/baseline-ignore index 6642abb..8d329b8 100644 --- a/tests/baseline-ignore +++ b/tests/baseline-ignore @@ -83,3 +83,10 @@ %Geocoder\\ProviderAggregator::__construct\(\): Implicitly marking parameter \$decider as nullable is deprecated, the explicit nullable type must be used instead% %Since symfony/var-exporter 7.3: The "Symfony\\Component\\VarExporter\\LazyGhostTrait" trait is deprecated, use native lazy objects instead.% %Method "Http\\Promise\\Promise::wait\(\)" might add " Resolved value, null if \$unwrap is set to false" as a native return type declaration in the future. Do the same in implementation "Geocoder\\Plugin\\Promise\\GeocoderFulfilledPromise" now to avoid errors or add an explicit @return annotation to suppress this message.% +%The "report_fields_where_declared" configuration option is deprecated and will be removed in DoctrineBundle 3.0. When using ORM 3, report_fields_where_declared will always be true% +%Class "Doctrine\\ORM\\Proxy\\DefaultProxyClassNameResolver" is deprecated. Use native lazy objects instead% +%Calling Doctrine\\ORM\\Configuration::(getProxyDir|setProxyDir|getAutoGenerateProxyClasses|setAutoGenerateProxyClasses|setProxyNamespace|getProxyNamespace) is deprecated and will not be possible in Doctrine ORM 4.0% +%Class "Doctrine\\ORM\\Proxy\\Autoloader" is deprecated. Use native lazy objects instead% +%Not enabling native lazy objects is deprecated and will be impossible in Doctrine ORM 4.0% +%Not setting "doctrine.orm.enable_native_lazy_objects" to true is deprecated% +%Disabling native lazy objects is deprecated and will be impossible in Doctrine ORM 4.0%