From 48c1f32fd33a7d7e3793f8dc562d85607e8f7c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Nork=C5=ABnas?= Date: Mon, 5 Jan 2026 07:47:28 +0200 Subject: [PATCH] Fix some phpstan warnings --- .github/workflows/ci.yml | 2 +- .php-cs-fixer.php => .php-cs-fixer.dist.php | 3 +- composer.json | 12 ++-- .../Compiler/AddProvidersPass.php | 2 +- .../Compiler/FactoryValidatorPass.php | 2 +- .../Compiler/ProfilerPass.php | 2 +- src/DependencyInjection/Configuration.php | 7 ++- src/Plugin/ProfilingPlugin.php | 4 +- tests/Command/GeocodeCommandTest.php | 2 +- .../DependencyInjection/ConfigurationTest.php | 2 + tests/Functional/BundleInitializationTest.php | 13 ++-- tests/Functional/CustomTestKernel.php | 12 +--- .../Entity/DummyWithEmptyProperty.php | 14 ++--- .../Fixtures/Entity/DummyWithGetter.php | 31 ++++++---- .../Entity/DummyWithInvalidGetter.php | 31 ++++++---- .../Fixtures/Entity/DummyWithProperty.php | 14 ++--- .../Entity/DummyWithStringableGetter.php | 15 +++-- .../Fixtures/Entity/StringableAddress.php | 5 +- .../Functional/GeocodeEntityListenerTest.php | 21 +++++-- tests/Functional/Helper/CacheHelper.php | 62 ------------------- tests/Functional/PluginInteractionTest.php | 31 +++++----- tests/Functional/ProviderFactoryTest.php | 10 +-- tests/Functional/config/cache.yml | 11 +++- tests/Functional/config/cache_symfony.yml | 12 ---- .../config/cache_without_lifetime.yml | 10 ++- .../config/fakeip_with_cache_cn.yml | 2 +- .../config/fakeip_with_cache_fr.yml | 2 +- tests/Mapping/Driver/Fixtures/Dummy.php | 6 +- .../Constraint/AddressValidatorTest.php | 7 ++- 29 files changed, 160 insertions(+), 187 deletions(-) rename .php-cs-fixer.php => .php-cs-fixer.dist.php (89%) delete mode 100644 tests/Functional/Helper/CacheHelper.php delete mode 100644 tests/Functional/config/cache_symfony.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92d1074f..f08acfed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: composer-options: "--optimize-autoloader" - name: "Run PHP-CS-Fixer" - run: vendor/bin/php-cs-fixer fix -v --dry-run --allow-unsupported-php-version=yes --using-cache=no --format=checkstyle | cs2pr + run: vendor/bin/php-cs-fixer fix -v --dry-run --using-cache=no --format=checkstyle | cs2pr phpunit: name: PHPUnit (PHP ${{ matrix.php }}) (Symfony ${{ matrix.sf_version }}) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.dist.php similarity index 89% rename from .php-cs-fixer.php rename to .php-cs-fixer.dist.php index 5f704ffd..f6bafe89 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.dist.php @@ -7,10 +7,11 @@ ; return (new PhpCsFixer\Config()) + ->setFinder($finder) + ->setUnsupportedPhpVersionAllowed(true) ->setRules([ '@Symfony' => true, 'no_superfluous_phpdoc_tags' => false, 'phpdoc_to_comment' => ['ignored_tags' => ['var']], // phpstan errors pops up without this ]) - ->setFinder($finder) ; diff --git a/composer.json b/composer.json index afc09e2b..9cc0859e 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,8 @@ "require-dev": { "doctrine/doctrine-bundle": "^2.18 || ^3.0", "doctrine/orm": "^2.20 || ^3.0", - "fakerphp/faker": "^1.24", - "friendsofphp/php-cs-fixer": "^3.92", + "fakerphp/faker": "^1.24.1", + "friendsofphp/php-cs-fixer": "^3.92.4", "geocoder-php/algolia-places-provider": "^0.5", "geocoder-php/arcgis-online-provider": "^4.5", "geocoder-php/bing-maps-provider": "^4.4", @@ -53,11 +53,11 @@ "geocoder-php/pickpoint-provider": "^4.4", "geocoder-php/tomtom-provider": "^4.5", "geocoder-php/yandex-provider": "^4.6", - "nyholm/nsa": "^1.3", - "nyholm/psr7": "^1.8", + "nyholm/nsa": "^1.3.1", + "nyholm/psr7": "^1.8.2", "nyholm/symfony-bundle-test": "^3.1", - "phpstan/phpstan": "^2.1", - "psr/http-client": "^1.0", + "phpstan/phpstan": "^2.1.33", + "psr/http-client": "^1.0.3", "psr/simple-cache": "^1.0 || ^2.0 || ^3.0", "symfony/cache": "^6.4 || ^7.0 || ^8.0", "symfony/config": "^6.4 || ^7.0 || ^8.0", diff --git a/src/DependencyInjection/Compiler/AddProvidersPass.php b/src/DependencyInjection/Compiler/AddProvidersPass.php index a98e83fb..d5e63a89 100644 --- a/src/DependencyInjection/Compiler/AddProvidersPass.php +++ b/src/DependencyInjection/Compiler/AddProvidersPass.php @@ -20,7 +20,7 @@ /** * @author William Durand */ -class AddProvidersPass implements CompilerPassInterface +final class AddProvidersPass implements CompilerPassInterface { /** * Get all providers based on their tag (`bazinga_geocoder.provider`) and diff --git a/src/DependencyInjection/Compiler/FactoryValidatorPass.php b/src/DependencyInjection/Compiler/FactoryValidatorPass.php index 9711bf6a..b000c187 100644 --- a/src/DependencyInjection/Compiler/FactoryValidatorPass.php +++ b/src/DependencyInjection/Compiler/FactoryValidatorPass.php @@ -21,7 +21,7 @@ * * @author Tobias Nyholm */ -class FactoryValidatorPass implements CompilerPassInterface +final class FactoryValidatorPass implements CompilerPassInterface { /** * @var list diff --git a/src/DependencyInjection/Compiler/ProfilerPass.php b/src/DependencyInjection/Compiler/ProfilerPass.php index 0ad374b8..fd7b27c2 100644 --- a/src/DependencyInjection/Compiler/ProfilerPass.php +++ b/src/DependencyInjection/Compiler/ProfilerPass.php @@ -22,7 +22,7 @@ * * @author Tobias Nyholm */ -class ProfilerPass implements CompilerPassInterface +final class ProfilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 61e9b7d1..fbddef29 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -12,6 +12,7 @@ namespace Bazinga\GeocoderBundle\DependencyInjection; +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -42,7 +43,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->addDefaultsIfNotSet() ->treatFalseLike(['enabled' => false]) ->treatTrueLike(['enabled' => true]) - ->treatNullLike(['enabled' => $this->debug]) + ->treatNullLike(['enabled' => \class_exists(DoctrineBundle::class)]) ->info('Extend the debug profiler with information about requests.') ->children() ->booleanNode('enabled') @@ -54,7 +55,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->arrayNode('fake_ip') ->beforeNormalization() ->ifString() - ->then(function ($value) { + ->then(function (string $value): array { return ['ip' => $value]; }) ->end() @@ -132,7 +133,7 @@ private function createClientPluginNode(): ArrayNodeDefinition $pluginList // support having just a service id in the list ->beforeNormalization() - ->always(function ($plugin) { + ->always(function (array|string $plugin) { if (is_string($plugin)) { return [ 'reference' => [ diff --git a/src/Plugin/ProfilingPlugin.php b/src/Plugin/ProfilingPlugin.php index 358b35fc..254827ca 100644 --- a/src/Plugin/ProfilingPlugin.php +++ b/src/Plugin/ProfilingPlugin.php @@ -43,12 +43,12 @@ public function handleQuery(Query $query, callable $next, callable $first): Prom { $startTime = microtime(true); - return $next($query)->then(function (Collection $result) use ($query, $startTime) { + return $next($query)->then(function (Collection $result) use ($query, $startTime): Collection { $duration = (microtime(true) - $startTime) * 1000; $this->logQuery($query, $duration, $result); return $result; - }, function (Exception $exception) use ($query, $startTime) { + }, function (Exception $exception) use ($query, $startTime): void { $duration = (microtime(true) - $startTime) * 1000; $this->logQuery($query, $duration, $exception); diff --git a/tests/Command/GeocodeCommandTest.php b/tests/Command/GeocodeCommandTest.php index dafb43dc..3e672026 100644 --- a/tests/Command/GeocodeCommandTest.php +++ b/tests/Command/GeocodeCommandTest.php @@ -30,7 +30,7 @@ */ final class GeocodeCommandTest extends TestCase { - private static $address = '10 rue Gambetta, Paris, France'; + private static string $address = '10 rue Gambetta, Paris, France'; public function testExecute(): void { diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 858f259d..eaaa93e0 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -25,6 +25,7 @@ final class ConfigurationTest extends TestCase public function testGetConfigTreeBuilder(): void { $config = Yaml::parseFile(__DIR__.'/Fixtures/config.yml'); + self::assertIsArray($config); $configuration = new Configuration(true); $treeBuilder = $configuration->getConfigTreeBuilder(); @@ -41,6 +42,7 @@ public function testGetConfigTreeBuilder(): void public function testGetConfigTreeBuilderNoDebug(): void { $config = Yaml::parseFile(__DIR__.'/Fixtures/config.yml'); + self::assertIsArray($config); $configuration = new Configuration(false); $treeBuilder = $configuration->getConfigTreeBuilder(); diff --git a/tests/Functional/BundleInitializationTest.php b/tests/Functional/BundleInitializationTest.php index b6e80306..84402539 100644 --- a/tests/Functional/BundleInitializationTest.php +++ b/tests/Functional/BundleInitializationTest.php @@ -27,7 +27,6 @@ use Nyholm\BundleTest\TestKernel; use Nyholm\NSA; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\HttpKernel\KernelInterface; final class BundleInitializationTest extends KernelTestCase { @@ -36,11 +35,12 @@ protected static function getKernelClass(): string return TestKernel::class; } - protected static function createKernel(array $options = []): KernelInterface + /** + * @param array $options + */ + protected static function createKernel(array $options = []): TestKernel { - /** - * @var TestKernel $kernel - */ + /** @var TestKernel $kernel */ $kernel = parent::createKernel($options); $kernel->addTestBundle(BazingaGeocoderBundle::class); $kernel->handleOptions($options); @@ -93,6 +93,7 @@ public function testBundleWithCachedProvider(): void $service = $container->get('bazinga_geocoder.provider.acme'); self::assertInstanceOf(PluginProvider::class, $service); $plugins = NSA::getProperty($service, 'plugins'); + self::assertIsArray($plugins); self::assertNotEmpty($plugins); self::assertInstanceOf(CachePlugin::class, $plugins[0]); } @@ -114,6 +115,7 @@ public function testCacheLifetimeCanBeNull(): void self::assertInstanceOf(PluginProvider::class, $service); $plugins = NSA::getProperty($service, 'plugins'); + self::assertIsArray($plugins); self::assertCount(1, $plugins); $cachePlugin = array_shift($plugins); @@ -137,6 +139,7 @@ public function testBundleWithPluginsYml(): void $service = $container->get('bazinga_geocoder.provider.acme'); self::assertInstanceOf(PluginProvider::class, $service); $plugins = NSA::getProperty($service, 'plugins'); + self::assertIsArray($plugins); self::assertCount(3, $plugins); self::assertInstanceOf(LoggerPlugin::class, $plugins[0]); } diff --git a/tests/Functional/CustomTestKernel.php b/tests/Functional/CustomTestKernel.php index 5af8981f..23d8dc80 100644 --- a/tests/Functional/CustomTestKernel.php +++ b/tests/Functional/CustomTestKernel.php @@ -30,7 +30,7 @@ */ class CustomTestKernel extends TestKernel implements CompilerPassInterface { - private $warmupDir; + private ?string $warmupDir = null; public function reboot(?string $warmupDir): void { @@ -54,6 +54,8 @@ public function getShareDir(): ?string /** * Returns the kernel parameters. + * + * @return array */ protected function getKernelParameters(): array { @@ -87,14 +89,6 @@ protected function getKernelParameters(): array ] + (null !== ($dir = $this->getShareDir()) ? ['kernel.share_dir' => realpath($dir) ?: $dir] : []); } - /** - * @internal - */ - public function setAnnotatedClassCache(array $annotatedClasses): void - { - file_put_contents(($this->warmupDir ?: $this->getBuildDir()).'/annotations.map', sprintf('id; + } + + public function setAddress(string $address): void { $this->_address = $address; } #[Address] - public function getAddress() + public function getAddress(): ?string { return $this->_address; } - public function getLatitude() + public function getLatitude(): ?float { return $this->latitude; } - public function setLatitude($latitude): void + public function setLatitude(float $latitude): void { $this->latitude = $latitude; } - public function getLongitude() + public function getLongitude(): ?float { return $this->longitude; } - public function setLongitude($longitude): void + public function setLongitude(float $longitude): void { $this->longitude = $longitude; } diff --git a/tests/Functional/Fixtures/Entity/DummyWithInvalidGetter.php b/tests/Functional/Fixtures/Entity/DummyWithInvalidGetter.php index d109dc55..b82ce532 100644 --- a/tests/Functional/Fixtures/Entity/DummyWithInvalidGetter.php +++ b/tests/Functional/Fixtures/Entity/DummyWithInvalidGetter.php @@ -29,46 +29,51 @@ class DummyWithInvalidGetter #[Id] #[GeneratedValue] #[Column(type: Types::INTEGER)] - private $id; + private ?int $id = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Latitude] - private $latitude; + private ?float $latitude = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Longitude] - private $longitude; + private ?float $longitude = null; - #[Column] - private $_address; + #[Column(type: Types::STRING)] + private ?string $_address = null; - public function setAddress($address): void + public function getId(): ?int + { + return $this->id; + } + + public function setAddress(string $address): void { $this->_address = $address; } #[Address] - public function getAddress($requiredParameter) + public function getAddress(mixed $requiredParameter): ?string { return $this->_address; } - public function getLatitude() + public function getLatitude(): ?float { return $this->latitude; } - public function setLatitude($latitude): void + public function setLatitude(float $latitude): void { $this->latitude = $latitude; } - public function getLongitude() + public function getLongitude(): ?float { return $this->longitude; } - public function setLongitude($longitude): void + public function setLongitude(float $longitude): void { $this->longitude = $longitude; } diff --git a/tests/Functional/Fixtures/Entity/DummyWithProperty.php b/tests/Functional/Fixtures/Entity/DummyWithProperty.php index 04ad4af1..df222c93 100644 --- a/tests/Functional/Fixtures/Entity/DummyWithProperty.php +++ b/tests/Functional/Fixtures/Entity/DummyWithProperty.php @@ -29,17 +29,17 @@ class DummyWithProperty #[Id] #[GeneratedValue] #[Column(type: Types::INTEGER)] - public $id; + public ?int $id = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Latitude] - public $latitude; + public ?float $latitude = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Longitude] - public $longitude; + public ?float $longitude = null; - #[Column] + #[Column(type: Types::STRING)] #[Address] - public $address; + public ?string $address = null; } diff --git a/tests/Functional/Fixtures/Entity/DummyWithStringableGetter.php b/tests/Functional/Fixtures/Entity/DummyWithStringableGetter.php index c06c4a28..903922ed 100644 --- a/tests/Functional/Fixtures/Entity/DummyWithStringableGetter.php +++ b/tests/Functional/Fixtures/Entity/DummyWithStringableGetter.php @@ -30,19 +30,24 @@ class DummyWithStringableGetter #[Id] #[GeneratedValue] #[Column(type: Types::INTEGER)] - public $id; + public ?int $id = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Latitude] - public $latitude; + public ?float $latitude = null; - #[Column] + #[Column(type: Types::FLOAT)] #[Longitude] - public $longitude; + public ?float $longitude = null; #[Embedded] public StringableAddress $address; + public function __construct(StringableAddress $address) + { + $this->address = $address; + } + public function setAddress(StringableAddress $address): void { $this->address = $address; diff --git a/tests/Functional/Fixtures/Entity/StringableAddress.php b/tests/Functional/Fixtures/Entity/StringableAddress.php index efff6603..5b0f0642 100644 --- a/tests/Functional/Fixtures/Entity/StringableAddress.php +++ b/tests/Functional/Fixtures/Entity/StringableAddress.php @@ -12,6 +12,7 @@ namespace Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Embeddable; @@ -19,8 +20,8 @@ class StringableAddress implements \Stringable { public function __construct( - #[Column] - private string $address, + #[Column(type: Types::STRING)] + private readonly string $address, ) { } diff --git a/tests/Functional/GeocodeEntityListenerTest.php b/tests/Functional/GeocodeEntityListenerTest.php index 25eb3010..e55a7564 100644 --- a/tests/Functional/GeocodeEntityListenerTest.php +++ b/tests/Functional/GeocodeEntityListenerTest.php @@ -22,6 +22,7 @@ use Composer\InstalledVersions; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Tools\SchemaTool; use Nyholm\BundleTest\TestKernel; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -30,7 +31,6 @@ use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\MockResponse; use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\HttpKernel\KernelInterface; /** * @author Markus Bachmann @@ -39,6 +39,7 @@ final class GeocodeEntityListenerTest extends KernelTestCase { protected function tearDown(): void { + /** @var EntityManagerInterface $em */ $em = self::getContainer()->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -50,7 +51,10 @@ protected static function getKernelClass(): string return TestKernel::class; } - protected static function createKernel(array $options = []): KernelInterface + /** + * @param array $options + */ + protected static function createKernel(array $options = []): TestKernel { /** * @var TestKernel $kernel @@ -116,6 +120,7 @@ public function testPersistForProperty(): void $dummy = new DummyWithProperty(); $dummy->address = 'Berlin, Germany'; + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -148,6 +153,7 @@ public function testPersistForGetter(): void $container = self::getContainer(); $container->set('http_client', self::createHttpClientForBerlinQuery()); + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -183,13 +189,13 @@ public function testPersistForStringableGetter(): void $container = self::getContainer(); $container->set('http_client', self::createHttpClientForBerlinQuery()); + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); $tool->createSchema($em->getMetadataFactory()->getAllMetadata()); - $dummy = new DummyWithStringableGetter(); - $dummy->setAddress(new StringableAddress('Berlin, Germany')); + $dummy = new DummyWithStringableGetter(new StringableAddress('Berlin, Germany')); $em->persist($dummy); $em->flush(); @@ -220,6 +226,7 @@ public function testPersistForInvalidGetter(): void self::fail('I shall not be called'); })); + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -248,6 +255,7 @@ public function testPersistForEmptyProperty(): void self::fail('I shall not be called'); })); + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -276,6 +284,7 @@ public function testDoesNotGeocodeIfAddressNotChanged(): void $container = self::getContainer(); $container->set('http_client', self::createHttpClientForFrankfurtQuery($httpRequests)); + /** @var EntityManagerInterface $em */ $em = $container->get('doctrine.orm.entity_manager'); $tool = new SchemaTool($em); @@ -293,8 +302,8 @@ public function testDoesNotGeocodeIfAddressNotChanged(): void $em->flush(); self::assertSame('Frankfurt, Germany', $dummy->address); - self::assertSame(0, $dummy->latitude); - self::assertSame(0, $dummy->longitude); + self::assertSame(0.0, $dummy->latitude); + self::assertSame(0.0, $dummy->longitude); self::assertSame(1, $httpRequests); } diff --git a/tests/Functional/Helper/CacheHelper.php b/tests/Functional/Helper/CacheHelper.php deleted file mode 100644 index 9a248eff..00000000 --- a/tests/Functional/Helper/CacheHelper.php +++ /dev/null @@ -1,62 +0,0 @@ - - */ -class CacheHelper implements CacheInterface -{ - public function get(string $key, mixed $default = null): mixed - { - } - - public function set(string $key, mixed $value, int|\DateInterval|null $ttl = null): bool - { - return true; - } - - public function delete(string $key): bool - { - return true; - } - - public function clear(): bool - { - return true; - } - - public function getMultiple(iterable $keys, mixed $default = null): iterable - { - return []; - } - - public function setMultiple(iterable $values, int|\DateInterval|null $ttl = null): bool - { - return true; - } - - public function deleteMultiple(iterable $keys): bool - { - return true; - } - - public function has(string $key): bool - { - return false; - } -} diff --git a/tests/Functional/PluginInteractionTest.php b/tests/Functional/PluginInteractionTest.php index 4ee904cd..ae21ed3f 100644 --- a/tests/Functional/PluginInteractionTest.php +++ b/tests/Functional/PluginInteractionTest.php @@ -13,6 +13,7 @@ namespace Bazinga\GeocoderBundle\Tests\Functional; use Bazinga\GeocoderBundle\BazingaGeocoderBundle; +use Geocoder\Provider\GeoPlugin\GeoPlugin; use Geocoder\Query\GeocodeQuery; use Nyholm\BundleTest\TestKernel; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; @@ -21,7 +22,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\MockResponse; -use Symfony\Component\HttpKernel\KernelInterface; final class PluginInteractionTest extends KernelTestCase { @@ -32,11 +32,12 @@ protected static function getKernelClass(): string return CustomTestKernel::class; } - protected static function createKernel(array $options = []): KernelInterface + /** + * @param array $options + */ + protected static function createKernel(array $options = []): TestKernel { - /** - * @var TestKernel $kernel - */ + /** @var TestKernel $kernel */ $kernel = parent::createKernel($options); $kernel->addTestBundle(BazingaGeocoderBundle::class); $kernel->addTestCompilerPass(new class implements CompilerPassInterface { @@ -54,11 +55,12 @@ public function process(ContainerBuilder $container): void public function testCachePluginUsesIpFromFakeIpPlugin(): void { + /** @var TestKernel $kernel */ $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { $kernel->setClearCacheAfterShutdown(false); $kernel->addTestConfig(__DIR__.'/config/framework.yml'); $kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml'); - $kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml'); + $kernel->addTestConfig(__DIR__.'/config/cache.yml'); $kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_cn.yml'); }]); $kernel->setClearCacheAfterShutdown(false); @@ -68,19 +70,19 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void self::assertSame('GET', $method); self::assertSame('https://freegeoip.app/json/123.123.123.128', $url); - 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']]); + 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']]); })); - + /** @var GeoPlugin $geoPluginGeocoder */ $geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin'); $result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1')); - $country = $result->first()->getCountry()->getCode(); - self::assertEquals('CN', $country); + self::assertSame('CN', $result->first()->getCountry()?->getCode()); + /** @var TestKernel $kernel */ $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { $kernel->setClearCacheAfterShutdown(false); $kernel->addTestConfig(__DIR__.'/config/framework.yml'); $kernel->addTestConfig(__DIR__.'/config/framework_sf'.$kernel::MAJOR_VERSION.'.yml'); - $kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml'); + $kernel->addTestConfig(__DIR__.'/config/cache.yml'); $kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_fr.yml'); }]); $kernel->setClearCacheAfterShutdown(false); @@ -90,12 +92,11 @@ public function testCachePluginUsesIpFromFakeIpPlugin(): void self::assertSame('GET', $method); self::assertSame('https://freegeoip.app/json/87.98.128.10', $url); - 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']]); + 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']]); })); - + /** @var GeoPlugin $geoPluginGeocoder */ $geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin'); $result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1')); - $country = $result->first()->getCountry()->getCode(); - self::assertEquals('FR', $country); + self::assertSame('FR', $result->first()->getCountry()?->getCode()); } } diff --git a/tests/Functional/ProviderFactoryTest.php b/tests/Functional/ProviderFactoryTest.php index 0eb56958..d384bd30 100644 --- a/tests/Functional/ProviderFactoryTest.php +++ b/tests/Functional/ProviderFactoryTest.php @@ -41,7 +41,6 @@ use Nyholm\NSA; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\HttpKernel\KernelInterface; final class ProviderFactoryTest extends KernelTestCase { @@ -52,11 +51,12 @@ protected static function getKernelClass(): string return TestKernel::class; } - protected static function createKernel(array $options = []): KernelInterface + /** + * @param array $options + */ + protected static function createKernel(array $options = []): TestKernel { - /** - * @var TestKernel $kernel - */ + /** @var TestKernel $kernel */ $kernel = parent::createKernel($options); $kernel->addTestBundle(BazingaGeocoderBundle::class); $kernel->handleOptions($options); diff --git a/tests/Functional/config/cache.yml b/tests/Functional/config/cache.yml index e291985c..4fb5944e 100644 --- a/tests/Functional/config/cache.yml +++ b/tests/Functional/config/cache.yml @@ -10,4 +10,13 @@ bazinga_geocoder: services: acme.cache: - class: Bazinga\GeocoderBundle\Tests\Functional\Helper\CacheHelper + class: Symfony\Component\Cache\Psr16Cache + arguments: + - '@geocoder.cache_pool' + +framework: + cache: + pools: + geocoder.cache_pool: + adapter: cache.app + default_lifetime: 600 diff --git a/tests/Functional/config/cache_symfony.yml b/tests/Functional/config/cache_symfony.yml deleted file mode 100644 index 419c11d6..00000000 --- a/tests/Functional/config/cache_symfony.yml +++ /dev/null @@ -1,12 +0,0 @@ -framework: - cache: - app: cache.adapter.filesystem - system: cache.adapter.system - pools: - app.cache.geoPlugin: - adapter: cache.app - default_lifetime: 600 -services: - app.simple_cache: - class: Symfony\Component\Cache\Psr16Cache - arguments: ['@app.cache.geoPlugin'] \ No newline at end of file diff --git a/tests/Functional/config/cache_without_lifetime.yml b/tests/Functional/config/cache_without_lifetime.yml index 1c11bb00..7c01ac56 100644 --- a/tests/Functional/config/cache_without_lifetime.yml +++ b/tests/Functional/config/cache_without_lifetime.yml @@ -9,4 +9,12 @@ bazinga_geocoder: services: acme.cache: - class: Bazinga\GeocoderBundle\Tests\Functional\Helper\CacheHelper + class: Symfony\Component\Cache\Psr16Cache + arguments: + - '@geocoder.cache_pool' + +framework: + cache: + pools: + geocoder.cache_pool: + adapter: cache.app diff --git a/tests/Functional/config/fakeip_with_cache_cn.yml b/tests/Functional/config/fakeip_with_cache_cn.yml index 4c88e956..cc54f483 100644 --- a/tests/Functional/config/fakeip_with_cache_cn.yml +++ b/tests/Functional/config/fakeip_with_cache_cn.yml @@ -9,6 +9,6 @@ bazinga_geocoder: providers: geoPlugin: factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory - cache: 'app.simple_cache' + cache: 'acme.cache' cache_lifetime: 42 cache_precision: ~ diff --git a/tests/Functional/config/fakeip_with_cache_fr.yml b/tests/Functional/config/fakeip_with_cache_fr.yml index 4aa35993..f0eba62a 100644 --- a/tests/Functional/config/fakeip_with_cache_fr.yml +++ b/tests/Functional/config/fakeip_with_cache_fr.yml @@ -9,6 +9,6 @@ bazinga_geocoder: providers: geoPlugin: factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory - cache: 'app.simple_cache' + cache: 'acme.cache' cache_lifetime: 42 cache_precision: ~ diff --git a/tests/Mapping/Driver/Fixtures/Dummy.php b/tests/Mapping/Driver/Fixtures/Dummy.php index e0d4fcab..aa70eedb 100644 --- a/tests/Mapping/Driver/Fixtures/Dummy.php +++ b/tests/Mapping/Driver/Fixtures/Dummy.php @@ -21,11 +21,11 @@ final class Dummy { #[Latitude] - public $latitude; + public ?float $latitude; #[Longitude] - public $longitude; + public ?float $longitude; #[Address] - public $address; + public ?string $address; } diff --git a/tests/Validator/Constraint/AddressValidatorTest.php b/tests/Validator/Constraint/AddressValidatorTest.php index ebf63b0c..2e85c4d7 100644 --- a/tests/Validator/Constraint/AddressValidatorTest.php +++ b/tests/Validator/Constraint/AddressValidatorTest.php @@ -21,6 +21,9 @@ use Symfony\Component\Validator\Exception\UnexpectedValueException; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +/** + * @extends ConstraintValidatorTestCase + */ final class AddressValidatorTest extends ConstraintValidatorTestCase { protected function createValidator(): AddressValidator @@ -29,13 +32,13 @@ protected function createValidator(): AddressValidator if ('https://nominatim.openstreetmap.org/search?format=jsonv2&q=Berlin%2C%20Germany&addressdetails=1&extratags=1&limit=5' === $url) { self::assertSame('GET', $method); - return new MockResponse('[{"place_id":159647018,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright","osm_type":"relation","osm_id":62422,"lat":"52.5170365","lon":"13.3888599","category":"boundary","type":"administrative","place_rank":8,"importance":0.7875390282491362,"addresstype":"city","name":"Berlin","display_name":"Berlin, Deutschland","address":{"city":"Berlin","ISO3166-2-lvl4":"DE-BE","country":"Deutschland","country_code":"de"},"extratags":{"ele": "35", "email": "info@berlin.de", "place": "city", "capital": "yes", "website": "http://www.berlin.de", "de:place": "city", "ref:nuts": "DE3;DE30;DE300", "wikidata": "Q64", "wikipedia": "de:Berlin", "population": "3769962", "ref:LOCODE": "DEBER", "ref:nuts:1": "DE3", "ref:nuts:2": "DE30", "ref:nuts:3": "DE300", "state_code": "BE", "name:prefix": "Land und Kreisfreie Stadt", "linked_place": "city", "official_status": "Land", "contact:facebook": "http://www.facebook.com/Berlin", "name:prefix:city": "Kreisfreie Stadt", "openGeoDB:loc_id": "14356", "capital_ISO3166-1": "yes", "name:prefix:state": "Land", "source:population": "https://download.statistik-berlin-brandenburg.de/fa93e3bd19a2e885/a5ecfb2fff6a/SB_A01-05-00_2020h02_BE.pdf", "license_plate_code": "B", "official_status:de": "Land", "official_status:en": "State", "official_status:ru": "земля", "geographical_region": "Barnim;Berliner Urstromtal;Teltow;Nauener Platte", "blind:description:de": "Auf www.berlinfuerblinde.de gibt es einen kostenlosen Audioguide und weitere Informationen.", "de:regionalschluessel": "110000000000", "openGeoDB:postal_codes": "10178,10115,10117,10119,10179,10243,10245,10247,10249,10315,10317,10318,10319,10365,10367,10369,10405,10407,10409,10435,10437,10439,10551,10553,10555,10557,10559,10585,10587,10589,10623,10625,10627,10629,10707,10709,10711,10713,10715,10717,10719,10777,10", "report_problems:website": "https://ordnungsamt.berlin.de/", "TMC:cid_58:tabcd_1:Class": "Area", "openGeoDB:license_plate_code": "B", "TMC:cid_58:tabcd_1:LCLversion": "12.0", "openGeoDB:telephone_area_code": "030", "TMC:cid_58:tabcd_1:LocationCode": "266", "de:amtlicher_gemeindeschluessel": "11000000", "openGeoDB:community_identification_number": "11000000"},"boundingbox":["52.3382448","52.6755087","13.0883450","13.7611609"]}]', ['response_headers' => ['content-type' => 'application-json']]); + return new MockResponse('[{"place_id":159647018,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright","osm_type":"relation","osm_id":62422,"lat":"52.5170365","lon":"13.3888599","category":"boundary","type":"administrative","place_rank":8,"importance":0.7875390282491362,"addresstype":"city","name":"Berlin","display_name":"Berlin, Deutschland","address":{"city":"Berlin","ISO3166-2-lvl4":"DE-BE","country":"Deutschland","country_code":"de"},"extratags":{"ele": "35", "email": "info@berlin.de", "place": "city", "capital": "yes", "website": "http://www.berlin.de", "de:place": "city", "ref:nuts": "DE3;DE30;DE300", "wikidata": "Q64", "wikipedia": "de:Berlin", "population": "3769962", "ref:LOCODE": "DEBER", "ref:nuts:1": "DE3", "ref:nuts:2": "DE30", "ref:nuts:3": "DE300", "state_code": "BE", "name:prefix": "Land und Kreisfreie Stadt", "linked_place": "city", "official_status": "Land", "contact:facebook": "http://www.facebook.com/Berlin", "name:prefix:city": "Kreisfreie Stadt", "openGeoDB:loc_id": "14356", "capital_ISO3166-1": "yes", "name:prefix:state": "Land", "source:population": "https://download.statistik-berlin-brandenburg.de/fa93e3bd19a2e885/a5ecfb2fff6a/SB_A01-05-00_2020h02_BE.pdf", "license_plate_code": "B", "official_status:de": "Land", "official_status:en": "State", "official_status:ru": "земля", "geographical_region": "Barnim;Berliner Urstromtal;Teltow;Nauener Platte", "blind:description:de": "Auf www.berlinfuerblinde.de gibt es einen kostenlosen Audioguide und weitere Informationen.", "de:regionalschluessel": "110000000000", "openGeoDB:postal_codes": "10178,10115,10117,10119,10179,10243,10245,10247,10249,10315,10317,10318,10319,10365,10367,10369,10405,10407,10409,10435,10437,10439,10551,10553,10555,10557,10559,10585,10587,10589,10623,10625,10627,10629,10707,10709,10711,10713,10715,10717,10719,10777,10", "report_problems:website": "https://ordnungsamt.berlin.de/", "TMC:cid_58:tabcd_1:Class": "Area", "openGeoDB:license_plate_code": "B", "TMC:cid_58:tabcd_1:LCLversion": "12.0", "openGeoDB:telephone_area_code": "030", "TMC:cid_58:tabcd_1:LocationCode": "266", "de:amtlicher_gemeindeschluessel": "11000000", "openGeoDB:community_identification_number": "11000000"},"boundingbox":["52.3382448","52.6755087","13.0883450","13.7611609"]}]', ['response_headers' => ['content-type' => 'application/json']]); } if ('https://nominatim.openstreetmap.org/search?format=jsonv2&q=Bifrost%2C%20Nine%20Realms&addressdetails=1&extratags=1&limit=5' === $url) { self::assertSame('GET', $method); - return new MockResponse('[]', ['response_headers' => ['content-type' => 'application-json']]); + return new MockResponse('[]', ['response_headers' => ['content-type' => 'application/json']]); } self::fail(sprintf('Unexpected http call "%s %s".', $method, $url));