From 9b5d144c193b0bf71c1969b5d99faab5a8bb83e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 20 Apr 2022 23:16:59 +0200 Subject: [PATCH 1/2] Adapt to new type hierarchy In recent versions of ORM and ODM, annotation driver no longer extend a base class in doctrine/persistence. Instead of testing for that base class, detecting the number of constructor arguments seems like a more robust way to figure out what arguments to pass. --- src/Service/DriverFactory.php | 8 +++----- tests/Service/DriverFactoryTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Service/DriverFactory.php b/src/Service/DriverFactory.php index 48b35e21..f0229a9c 100644 --- a/src/Service/DriverFactory.php +++ b/src/Service/DriverFactory.php @@ -5,8 +5,6 @@ namespace DoctrineModule\Service; use Doctrine\Common\Annotations; -use Doctrine\ORM\Mapping\Driver\AttributeDriver; -use Doctrine\Persistence\Mapping\Driver\AnnotationDriver; use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator; use Doctrine\Persistence\Mapping\Driver\FileDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; @@ -14,12 +12,12 @@ use DoctrineModule\Options\Driver; use Interop\Container\ContainerInterface; use InvalidArgumentException; +use ReflectionClass; use RuntimeException; use function class_exists; use function get_class; use function interface_exists; -use function is_subclass_of; use function sprintf; /** @@ -70,10 +68,10 @@ protected function createDriver(ContainerInterface $container, Driver $options): // Not all drivers (DriverChain) require paths. $paths = $options->getPaths(); + $constructor = (new ReflectionClass($class))->getConstructor(); // Special options for AnnotationDrivers. if ( - $class !== AttributeDriver::class && - ($class === AnnotationDriver::class || is_subclass_of($class, AnnotationDriver::class)) + $constructor !== null && $constructor->getNumberOfParameters() === 2 ) { $reader = new Annotations\AnnotationReader(); $reader = new Annotations\CachedReader( diff --git a/tests/Service/DriverFactoryTest.php b/tests/Service/DriverFactoryTest.php index b9d05503..c34a92f2 100644 --- a/tests/Service/DriverFactoryTest.php +++ b/tests/Service/DriverFactoryTest.php @@ -4,6 +4,9 @@ namespace DoctrineModuleTest\Service; +use Doctrine\Common\Annotations\Reader; +use Doctrine\Common\Cache\ArrayCache; +use Doctrine\ORM\Mapping\Driver\AnnotationDriver; use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use DoctrineModule\Service\DriverFactory; @@ -90,4 +93,29 @@ public function testCreateAttributeDriver(): void $driver = $factory->__invoke($serviceManager, AttributeDriver::class); $this->assertInstanceOf(AttributeDriver::class, $driver); } + + public function testCreateAnnotationDriver(): void + { + $serviceManager = new ServiceManager(); + $serviceManager->setService( + 'config', + [ + 'doctrine' => [ + 'driver' => [ + 'testDriver' => ['class' => AnnotationDriver::class], + ], + ], + ] + ); + $serviceManager->setService( + 'doctrine.cache.array', + new ArrayCache() + ); + + $factory = new DriverFactory('testDriver'); + $driver = $factory->__invoke($serviceManager, AnnotationDriver::class); + $this->assertInstanceOf(AnnotationDriver::class, $driver); + assert($driver instanceof AnnotationDriver); + $this->assertInstanceOf(Reader::class, $driver->getReader()); + } } From 1d0e401f3f0ebe131312aef85922d99e86246d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 21 Apr 2022 22:32:18 +0200 Subject: [PATCH 2/2] Leverage phpstan-phpunit's extension --- phpstan.neon | 1 + tests/Service/DriverFactoryTest.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 676b1733..fa61ab43 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,5 +5,6 @@ parameters: - src - tests includes: + - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon - vendor/jangregor/phpstan-prophecy/extension.neon diff --git a/tests/Service/DriverFactoryTest.php b/tests/Service/DriverFactoryTest.php index c34a92f2..5060ae39 100644 --- a/tests/Service/DriverFactoryTest.php +++ b/tests/Service/DriverFactoryTest.php @@ -115,7 +115,6 @@ public function testCreateAnnotationDriver(): void $factory = new DriverFactory('testDriver'); $driver = $factory->__invoke($serviceManager, AnnotationDriver::class); $this->assertInstanceOf(AnnotationDriver::class, $driver); - assert($driver instanceof AnnotationDriver); $this->assertInstanceOf(Reader::class, $driver->getReader()); } }