Skip to content

Commit

Permalink
doctrine#774 added missing annotation drivers to Driver Factory
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Paulis <[email protected]>
  • Loading branch information
ppaulis committed Apr 21, 2022
1 parent 7a8ee47 commit c9b5917
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"require-dev": {
"doctrine/coding-standard": "^9.0.0",
"doctrine/mongodb-odm": "^2.3.3",
"doctrine/orm": "^2.11.1",
"jangregor/phpstan-prophecy": "^1.0.0",
"laminas/laminas-cache-storage-adapter-blackhole": "^2.0.0",
Expand Down Expand Up @@ -100,7 +101,10 @@
"bin/doctrine-module"
],
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
},
"scripts": {
"check": [
Expand Down
10 changes: 8 additions & 2 deletions src/Service/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace DoctrineModule\Service;

use Doctrine\Common\Annotations;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver as AnnotationDriverODM;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver as AnnotationDriverORM;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AnnotationDriverPersistence;
use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
Expand Down Expand Up @@ -73,7 +75,11 @@ protected function createDriver(ContainerInterface $container, Driver $options):
// Special options for AnnotationDrivers.
if (
$class !== AttributeDriver::class &&
($class === AnnotationDriver::class || is_subclass_of($class, AnnotationDriver::class))
(
($class === AnnotationDriverPersistence::class || is_subclass_of($class, AnnotationDriverPersistence::class)) ||
($class === AnnotationDriverORM::class || is_subclass_of($class, AnnotationDriverORM::class)) ||
($class === AnnotationDriverODM::class || is_subclass_of($class, AnnotationDriverODM::class))
)
) {
$reader = new Annotations\AnnotationReader();
$reader = new Annotations\CachedReader(
Expand Down
48 changes: 48 additions & 0 deletions tests/Service/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace DoctrineModuleTest\Service;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver as AnnotationDriverODM;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver as AnnotationDriverORM;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use DoctrineModule\Service\DriverFactory;
use DoctrineModuleTest\Service\Mock\MetadataDriverMock;
use DoctrineModuleTest\Service\TestAsset\DummyAnnotationDriver;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase as BaseTestCase;

use function assert;
use function method_exists;

/**
* Base test case to be used when a service manager instance is required
Expand Down Expand Up @@ -40,6 +46,7 @@ public function testCreateDriver(): void
public function testCreateDriverChain(): void
{
$serviceManager = new ServiceManager();
//$serviceManager->setService('doctrine.cache.array', new ArrayCa);
$serviceManager->setService(
'config',
[
Expand Down Expand Up @@ -90,4 +97,45 @@ public function testCreateAttributeDriver(): void
$driver = $factory->__invoke($serviceManager, AttributeDriver::class);
$this->assertInstanceOf(AttributeDriver::class, $driver);
}

/**
* @return array<int, array<string>>
*/
public function dataProviderAnnotationDrivers(): array
{
return [
[DummyAnnotationDriver::class],
[AnnotationDriverORM::class],
[AnnotationDriverODM::class],
];
}

/**
* @dataProvider dataProviderAnnotationDrivers
*/
public function testCreateAnnotationDrivers(string $annotationDriverClassName): void
{
$serviceManager = new ServiceManager();
$serviceManager->setService('doctrine.cache.array', new ArrayCache());
$serviceManager->setService(
'config',
[
'doctrine' => [
'driver' => [
'testDriver' => ['class' => $annotationDriverClassName],
],
],
]
);

$factory = new DriverFactory('testDriver');
$driver = $factory->__invoke($serviceManager, $annotationDriverClassName);
$this->assertInstanceOf($annotationDriverClassName, $driver);

if (! method_exists($driver, 'getReader')) {
return;
}

$this->assertInstanceOf(Reader::class, $driver->getReader());
}
}
18 changes: 18 additions & 0 deletions tests/Service/TestAsset/DummyAnnotationDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DoctrineModuleTest\Service\TestAsset;

use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AnnotationDriverPersistence;

class DummyAnnotationDriver extends AnnotationDriverPersistence
{
/**
* {@inheritDoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata): void
{
}
}
12 changes: 12 additions & 0 deletions tests/Service/TestAsset/DummyEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace DoctrineModuleTest\Service\TestAsset;

/**
* @Annotation
*/
class DummyEntity
{
}

0 comments on commit c9b5917

Please sign in to comment.