diff --git a/CHANGELOG.md b/CHANGELOG.md index a3607d4c13..e49159f86a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ a release. - Loggable: Using only PHP 8 attributes. - References: Avoid deprecations using LazyCollection with PHP 8.1 +### Changed +- In order to use a custom cache for storing configuration of an extension, the user has to call `setCacheItemPool` + on the extension listener passing an instance of `Psr\Cache\CacheItemPoolInterface`. + ## [3.4.0] - 2021-12-05 ### Added - PHP 8 Attributes support for Doctrine MongoDB to document & traits. diff --git a/composer.json b/composer.json index 436c621c73..5bcbced8dd 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,9 @@ "doctrine/collections": "^1.0", "doctrine/common": "^2.13 || ^3.0", "doctrine/event-manager": "^1.0", - "doctrine/persistence": "^1.3.3 || ^2.0" + "doctrine/persistence": "^1.3.3 || ^2.0", + "psr/cache": "^1 || ^2 || ^3", + "symfony/cache": "^4.4 || ^5.3 || ^6.0" }, "require-dev": { "doctrine/cache": "^1.11 || ^2.0", @@ -57,7 +59,6 @@ "phpstan/phpstan-doctrine": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "phpunit/phpunit": "^8.5 || ^9.5", - "symfony/cache": "^4.4 || ^5.3 || ^6.0", "symfony/console": "^4.4 || ^5.3 || ^6.0", "symfony/yaml": "^4.4 || ^5.3 || ^6.0" }, diff --git a/example/em.php b/example/em.php index 36baecbf0d..d28f703fea 100644 --- a/example/em.php +++ b/example/em.php @@ -79,27 +79,32 @@ // Sluggable extension $sluggableListener = new Gedmo\Sluggable\SluggableListener(); $sluggableListener->setAnnotationReader($annotationReader); +$sluggableListener->setCacheItemPool($cache); $eventManager->addEventSubscriber($sluggableListener); // Tree extension $treeListener = new Gedmo\Tree\TreeListener(); $treeListener->setAnnotationReader($annotationReader); +$treeListener->setCacheItemPool($cache); $eventManager->addEventSubscriber($treeListener); // Loggable extension, not used in example //$loggableListener = new Gedmo\Loggable\LoggableListener; //$loggableListener->setAnnotationReader($annotationReader); +//$loggableListener->setCacheItemPool($cache); //$loggableListener->setUsername('admin'); //$eventManager->addEventSubscriber($loggableListener); // Timestampable extension $timestampableListener = new Gedmo\Timestampable\TimestampableListener(); $timestampableListener->setAnnotationReader($annotationReader); +$timestampableListener->setCacheItemPool($cache); $eventManager->addEventSubscriber($timestampableListener); // Blameable extension $blameableListener = new \Gedmo\Blameable\BlameableListener(); $blameableListener->setAnnotationReader($annotationReader); +$blameableListener->setCacheItemPool($cache); $blameableListener->setUserValue('MyUsername'); // determine from your environment $eventManager->addEventSubscriber($blameableListener); @@ -111,11 +116,13 @@ $translatableListener->setTranslatableLocale('en'); $translatableListener->setDefaultLocale('en'); $translatableListener->setAnnotationReader($annotationReader); +$translatableListener->setCacheItemPool($cache); $eventManager->addEventSubscriber($translatableListener); // Sortable extension, not used in example //$sortableListener = new Gedmo\Sortable\SortableListener; //$sortableListener->setAnnotationReader($annotationReader); +//$sortableListener->setCacheItemPool($cache); //$eventManager->addEventSubscriber($sortableListener); // Now we will build our ORM configuration. diff --git a/src/Mapping/ExtensionMetadataFactory.php b/src/Mapping/ExtensionMetadataFactory.php index 3530ddfbe1..0819f88716 100644 --- a/src/Mapping/ExtensionMetadataFactory.php +++ b/src/Mapping/ExtensionMetadataFactory.php @@ -10,7 +10,6 @@ namespace Gedmo\Mapping; use Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver as DoctrineBundleMappingDriver; -use Doctrine\Common\Cache\Cache; use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; @@ -21,6 +20,7 @@ use Gedmo\Mapping\Driver\AttributeDriverInterface; use Gedmo\Mapping\Driver\AttributeReader; use Gedmo\Mapping\Driver\File as FileDriver; +use Psr\Cache\CacheItemPoolInterface; /** * The extension metadata factory is responsible for extension driver @@ -59,18 +59,18 @@ class ExtensionMetadataFactory protected $annotationReader; /** - * Initializes extension driver - * - * @param string $extensionNamespace - * @param object $annotationReader + * @var CacheItemPoolInterface|null */ - public function __construct(ObjectManager $objectManager, $extensionNamespace, $annotationReader) + private $cachePoolItem; + + public function __construct(ObjectManager $objectManager, string $extensionNamespace, object $annotationReader, ?CacheItemPoolInterface $cachePoolItem = null) { $this->objectManager = $objectManager; $this->annotationReader = $annotationReader; $this->extensionNamespace = $extensionNamespace; $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl(); $this->driver = $this->getDriver($omDriver); + $this->cachePoolItem = $cachePoolItem; } /** @@ -110,14 +110,7 @@ public function getExtensionMetadata($meta) $config['useObjectClass'] = $useObjectName; } - $cacheDriver = $cmf->getCacheDriver(); - - if ($cacheDriver instanceof Cache) { - // Cache the result, even if it's empty, to prevent re-parsing non-existent annotations. - $cacheId = self::getCacheId($meta->getName(), $this->extensionNamespace); - - $cacheDriver->save($cacheId, $config); - } + $this->storeConfiguration($meta->getName(), $config); return $config; } @@ -132,7 +125,7 @@ public function getExtensionMetadata($meta) */ public static function getCacheId($className, $extensionNamespace) { - return $className.'\\$'.strtoupper(str_replace('\\', '_', $extensionNamespace)).'_CLASSMETADATA'; + return str_replace('\\', '_', $className).'_$'.strtoupper(str_replace('\\', '_', $extensionNamespace)).'_CLASSMETADATA'; } /** @@ -201,4 +194,18 @@ protected function getDriver($omDriver) return $driver; } + + private function storeConfiguration(string $className, array $config): void + { + if (null === $this->cachePoolItem) { + return; + } + + // Cache the result, even if it's empty, to prevent re-parsing non-existent annotations. + $cacheId = self::getCacheId($className, $this->extensionNamespace); + + $item = $this->cachePoolItem->getItem($cacheId); + + $this->cachePoolItem->save($item->set($config)); + } } diff --git a/src/Mapping/MappedEventSubscriber.php b/src/Mapping/MappedEventSubscriber.php index 375bb85b15..e8c44095a9 100644 --- a/src/Mapping/MappedEventSubscriber.php +++ b/src/Mapping/MappedEventSubscriber.php @@ -20,6 +20,7 @@ use Doctrine\Common\EventSubscriber; use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\AdapterInterface; +use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; /** @@ -78,6 +79,11 @@ abstract class MappedEventSubscriber implements EventSubscriber */ private static $defaultAnnotationReader; + /** + * @var CacheItemPoolInterface|null + */ + private $cacheItemPool; + /** * Constructor */ @@ -97,32 +103,33 @@ public function __construct() */ public function getConfiguration(ObjectManager $objectManager, $class) { - $config = []; if (isset(self::$configurations[$this->name][$class])) { - $config = self::$configurations[$this->name][$class]; - } else { - $factory = $objectManager->getMetadataFactory(); - $cacheDriver = $factory->getCacheDriver(); - if ($cacheDriver) { - $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace()); - if (false !== ($cached = $cacheDriver->fetch($cacheId))) { - self::$configurations[$this->name][$class] = $cached; - $config = $cached; - } else { - // re-generate metadata on cache miss - $this->loadMetadataForObjectClass($objectManager, $factory->getMetadataFor($class)); - if (isset(self::$configurations[$this->name][$class])) { - $config = self::$configurations[$this->name][$class]; - } - } + return self::$configurations[$this->name][$class]; + } - $objectClass = $config['useObjectClass'] ?? $class; - if ($objectClass !== $class) { - $this->getConfiguration($objectManager, $objectClass); - } + $config = []; + + $cacheItemPool = $this->getCacheItemPool(); + + $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace()); + $cacheItem = $cacheItemPool->getItem($cacheId); + + if ($cacheItem->isHit()) { + $config = $cacheItem->get(); + self::$configurations[$this->name][$class] = $config; + } else { + // re-generate metadata on cache miss + $this->loadMetadataForObjectClass($objectManager, $objectManager->getClassMetadata($class)); + if (isset(self::$configurations[$this->name][$class])) { + $config = self::$configurations[$this->name][$class]; } } + $objectClass = $config['useObjectClass'] ?? $class; + if ($objectClass !== $class) { + $this->getConfiguration($objectManager, $objectClass); + } + return $config; } @@ -142,7 +149,8 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager) $this->extensionMetadataFactory[$oid] = new ExtensionMetadataFactory( $objectManager, $this->getNamespace(), - $this->annotationReader + $this->annotationReader, + $this->getCacheItemPool() ); } @@ -165,6 +173,11 @@ public function setAnnotationReader($reader) $this->annotationReader = $reader; } + final public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool): void + { + $this->cacheItemPool = $cacheItemPool; + } + /** * Scans the objects for extended annotations * event subscribers must subscribe to loadClassMetadata event @@ -264,4 +277,13 @@ private function getDefaultAnnotationReader(): Reader return self::$defaultAnnotationReader; } + + private function getCacheItemPool(): CacheItemPoolInterface + { + if (null === $this->cacheItemPool) { + $this->cacheItemPool = new ArrayAdapter(); + } + + return $this->cacheItemPool; + } } diff --git a/tests/Gedmo/Mapping/LoggableMappingTest.php b/tests/Gedmo/Mapping/LoggableORMMappingTest.php similarity index 72% rename from tests/Gedmo/Mapping/LoggableMappingTest.php rename to tests/Gedmo/Mapping/LoggableORMMappingTest.php index e7f12ffac6..e5a2709535 100644 --- a/tests/Gedmo/Mapping/LoggableMappingTest.php +++ b/tests/Gedmo/Mapping/LoggableORMMappingTest.php @@ -11,31 +11,33 @@ namespace Gedmo\Tests\Mapping; +use Doctrine\Common\EventManager; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Gedmo\Loggable\Entity\LogEntry; use Gedmo\Loggable\LoggableListener; use Gedmo\Mapping\ExtensionMetadataFactory; use Gedmo\Tests\Mapping\Fixture\Yaml\Category; -use Symfony\Component\Cache\Adapter\ArrayAdapter; /** * These are mapping tests for tree extension * * @author Gediminas Morkevicius */ -final class LoggableMappingTest extends \PHPUnit\Framework\TestCase +final class LoggableORMMappingTest extends ORMMappingTestCase { public const YAML_CATEGORY = Category::class; + + /** + * @var \Doctrine\ORM\EntityManager + */ private $em; protected function setUp(): void { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCache(new ArrayAdapter()); - $config->setQueryCache(new ArrayAdapter()); - $config->setProxyDir(TESTS_TEMP_DIR); - $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + parent::setUp(); + + $config = $this->getBasicConfiguration(); $chainDriverImpl = new MappingDriverChain(); $chainDriverImpl->addDriver( new YamlDriver([__DIR__.'/Driver/Yaml']), @@ -48,10 +50,10 @@ protected function setUp(): void 'memory' => true, ]; - //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); - - $evm = new \Doctrine\Common\EventManager(); - $evm->addEventSubscriber(new LoggableListener()); + $evm = new EventManager(); + $loggableListener = new LoggableListener(); + $loggableListener->setCacheItemPool($this->cache); + $evm->addEventSubscriber($loggableListener); $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); } @@ -59,7 +61,7 @@ public function testLoggableMapping(): void { $meta = $this->em->getClassMetadata(self::YAML_CATEGORY); $cacheId = ExtensionMetadataFactory::getCacheId(self::YAML_CATEGORY, 'Gedmo\Loggable'); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('loggable', $config); static::assertTrue($config['loggable']); diff --git a/tests/Gedmo/Mapping/ORMMappingTestCase.php b/tests/Gedmo/Mapping/ORMMappingTestCase.php new file mode 100644 index 0000000000..c5fbb30c01 --- /dev/null +++ b/tests/Gedmo/Mapping/ORMMappingTestCase.php @@ -0,0 +1,41 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\Mapping; + +use Doctrine\ORM\Configuration; +use PHPUnit\Framework\TestCase; +use Psr\Cache\CacheItemPoolInterface; +use Symfony\Component\Cache\Adapter\ArrayAdapter; + +abstract class ORMMappingTestCase extends TestCase +{ + /** + * @var CacheItemPoolInterface + */ + protected $cache; + + protected function setUp(): void + { + $this->cache = new ArrayAdapter(); + } + + final protected function getBasicConfiguration(): Configuration + { + $config = new Configuration(); + $config->setMetadataCache(new ArrayAdapter()); + $config->setQueryCache(new ArrayAdapter()); + $config->setProxyDir(TESTS_TEMP_DIR); + $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + + return $config; + } +} diff --git a/tests/Gedmo/Mapping/SluggableMappingTest.php b/tests/Gedmo/Mapping/SluggableMappingTest.php index 4eb2495576..967aa8811b 100644 --- a/tests/Gedmo/Mapping/SluggableMappingTest.php +++ b/tests/Gedmo/Mapping/SluggableMappingTest.php @@ -11,6 +11,9 @@ namespace Gedmo\Tests\Mapping; +use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\Common\Annotations\AnnotationRegistry; +use Doctrine\Common\EventManager; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Gedmo\Mapping\ExtensionMetadataFactory; @@ -19,33 +22,34 @@ use Gedmo\Sluggable\SluggableListener; use Gedmo\Tests\Mapping\Fixture\Sluggable; use Gedmo\Tests\Mapping\Fixture\Yaml\Category; -use Symfony\Component\Cache\Adapter\ArrayAdapter; /** * These are mapping tests for sluggable extension * * @author Gediminas Morkevicius */ -final class SluggableMappingTest extends \PHPUnit\Framework\TestCase +final class SluggableMappingTest extends ORMMappingTestCase { public const TEST_YAML_ENTITY_CLASS = Category::class; public const SLUGGABLE = Sluggable::class; + + /** + * @var \Doctrine\ORM\EntityManager + */ private $em; protected function setUp(): void { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCache(new ArrayAdapter()); - $config->setQueryCache(new ArrayAdapter()); - $config->setProxyDir(TESTS_TEMP_DIR); - $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + parent::setUp(); + + $config = $this->getBasicConfiguration(); $chainDriverImpl = new MappingDriverChain(); $chainDriverImpl->addDriver( new YamlDriver([__DIR__.'/Driver/Yaml']), 'Gedmo\Tests\Mapping\Fixture\Yaml' ); - $reader = new \Doctrine\Common\Annotations\AnnotationReader(); - \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace( + $reader = new AnnotationReader(); + AnnotationRegistry::registerAutoloadNamespace( 'Gedmo\\Mapping\\Annotation', dirname(VENDOR_PATH).'/src' ); @@ -60,8 +64,10 @@ protected function setUp(): void 'memory' => true, ]; - $evm = new \Doctrine\Common\EventManager(); - $evm->addEventSubscriber(new SluggableListener()); + $evm = new EventManager(); + $listener = new SluggableListener(); + $listener->setCacheItemPool($this->cache); + $evm->addEventSubscriber($listener); $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); } @@ -72,7 +78,7 @@ public function testShouldBeAbleToMapSluggableUsingYamlDriver(): void self::TEST_YAML_ENTITY_CLASS, 'Gedmo\Sluggable' ); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('slugs', $config); static::assertArrayHasKey('slug', $config['slugs']); @@ -120,7 +126,7 @@ public function testShouldBeAbleToMapSluggableUsingAnnotationDriver(): void self::SLUGGABLE, 'Gedmo\Sluggable' ); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('handlers', $config['slugs']['slug']); $handlers = $config['slugs']['slug']['handlers']; diff --git a/tests/Gedmo/Mapping/TimestampableMappingTest.php b/tests/Gedmo/Mapping/TimestampableMappingTest.php index 4e514ca7d7..2183419a11 100644 --- a/tests/Gedmo/Mapping/TimestampableMappingTest.php +++ b/tests/Gedmo/Mapping/TimestampableMappingTest.php @@ -11,30 +11,33 @@ namespace Gedmo\Tests\Mapping; +use Doctrine\Common\EventManager; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Gedmo\Mapping\ExtensionMetadataFactory; use Gedmo\Tests\Mapping\Fixture\Yaml\Category; use Gedmo\Timestampable\TimestampableListener; -use Symfony\Component\Cache\Adapter\ArrayAdapter; /** * These are mapping tests for timestampable extension * * @author Gediminas Morkevicius */ -final class TimestampableMappingTest extends \PHPUnit\Framework\TestCase +final class TimestampableMappingTest extends ORMMappingTestCase { public const TEST_YAML_ENTITY_CLASS = Category::class; + + /** + * @var EntityManager + */ private $em; protected function setUp(): void { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCache(new ArrayAdapter()); - $config->setQueryCache(new ArrayAdapter()); - $config->setProxyDir(TESTS_TEMP_DIR); - $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + parent::setUp(); + + $config = $this->getBasicConfiguration(); $chainDriverImpl = new MappingDriverChain(); $chainDriverImpl->addDriver( new YamlDriver([__DIR__.'/Driver/Yaml']), @@ -47,11 +50,11 @@ protected function setUp(): void 'memory' => true, ]; - //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); - - $evm = new \Doctrine\Common\EventManager(); - $evm->addEventSubscriber(new TimestampableListener()); - $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); + $evm = new EventManager(); + $listener = new TimestampableListener(); + $listener->setCacheItemPool($this->cache); + $evm->addEventSubscriber($listener); + $this->em = EntityManager::create($conn, $config, $evm); } public function testYamlMapping(): void @@ -61,7 +64,7 @@ public function testYamlMapping(): void self::TEST_YAML_ENTITY_CLASS, 'Gedmo\Timestampable' ); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('create', $config); static::assertSame('created', $config['create'][0]); static::assertArrayHasKey('update', $config); diff --git a/tests/Gedmo/Mapping/TranslatableMappingTest.php b/tests/Gedmo/Mapping/TranslatableMappingTest.php index 74bb926a7a..5482370788 100644 --- a/tests/Gedmo/Mapping/TranslatableMappingTest.php +++ b/tests/Gedmo/Mapping/TranslatableMappingTest.php @@ -11,6 +11,8 @@ namespace Gedmo\Tests\Mapping; +use Doctrine\Common\EventManager; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; @@ -18,14 +20,13 @@ use Gedmo\Tests\Mapping\Fixture\Yaml\User; use Gedmo\Tests\Translatable\Fixture\PersonTranslation; use Gedmo\Translatable\TranslatableListener; -use Symfony\Component\Cache\Adapter\ArrayAdapter; /** * These are mapping tests for translatable behavior * * @author Gediminas Morkevicius */ -final class TranslatableMappingTest extends \PHPUnit\Framework\TestCase +final class TranslatableMappingTest extends ORMMappingTestCase { public const TEST_YAML_ENTITY_CLASS = User::class; @@ -41,11 +42,9 @@ final class TranslatableMappingTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCache(new ArrayAdapter()); - $config->setQueryCache(new ArrayAdapter()); - $config->setProxyDir(TESTS_TEMP_DIR); - $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + parent::setUp(); + + $config = $this->getBasicConfiguration(); $chainDriverImpl = new MappingDriverChain(); $chainDriverImpl->addDriver( new YamlDriver([__DIR__.'/Driver/Yaml']), @@ -58,13 +57,12 @@ protected function setUp(): void 'memory' => true, ]; - //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); - - $evm = new \Doctrine\Common\EventManager(); + $evm = new EventManager(); $this->translatableListener = new TranslatableListener(); + $this->translatableListener->setCacheItemPool($this->cache); $this->translatableListener->setTranslatableLocale('en_us'); $evm->addEventSubscriber($this->translatableListener); - $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); + $this->em = EntityManager::create($conn, $config, $evm); } public function testYamlMapping(): void @@ -74,7 +72,7 @@ public function testYamlMapping(): void self::TEST_YAML_ENTITY_CLASS, 'Gedmo\Translatable' ); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('translationClass', $config); static::assertSame(PersonTranslation::class, $config['translationClass']); static::assertArrayHasKey('fields', $config); diff --git a/tests/Gedmo/Mapping/TreeMappingTest.php b/tests/Gedmo/Mapping/TreeMappingTest.php index 300dbeb8c4..70d135d249 100644 --- a/tests/Gedmo/Mapping/TreeMappingTest.php +++ b/tests/Gedmo/Mapping/TreeMappingTest.php @@ -11,6 +11,8 @@ namespace Gedmo\Tests\Mapping; +use Doctrine\Common\EventManager; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Gedmo\Mapping\ExtensionMetadataFactory; @@ -19,21 +21,20 @@ use Gedmo\Tests\Mapping\Fixture\Yaml\MaterializedPathCategory; use Gedmo\Tests\Tree\Fixture\Closure\CategoryClosure; use Gedmo\Tree\TreeListener; -use Symfony\Component\Cache\Adapter\ArrayAdapter; /** * These are mapping tests for tree extension * * @author Gediminas Morkevicius */ -final class TreeMappingTest extends \PHPUnit\Framework\TestCase +final class TreeMappingTest extends ORMMappingTestCase { public const TEST_YAML_ENTITY_CLASS = Category::class; public const YAML_CLOSURE_CATEGORY = ClosureCategory::class; public const YAML_MATERIALIZED_PATH_CATEGORY = MaterializedPathCategory::class; /** - * @var \Doctrine\ORM\EntityManager + * @var EntityManager */ private $em; @@ -44,11 +45,9 @@ final class TreeMappingTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCache(new ArrayAdapter()); - $config->setQueryCache(new ArrayAdapter()); - $config->setProxyDir(TESTS_TEMP_DIR); - $config->setProxyNamespace('Gedmo\Mapping\Proxy'); + parent::setUp(); + + $config = $this->getBasicConfiguration(); $chainDriverImpl = new MappingDriverChain(); $chainDriverImpl->addDriver( new YamlDriver([__DIR__.'/Driver/Yaml']), @@ -70,9 +69,10 @@ protected function setUp(): void ]; $this->listener = new TreeListener(); - $evm = new \Doctrine\Common\EventManager(); - $evm->addEventSubscriber(new TreeListener()); - $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); + $this->listener->setCacheItemPool($this->cache); + $evm = new EventManager(); + $evm->addEventSubscriber($this->listener); + $this->em = EntityManager::create($conn, $config, $evm); } public function testApcCached(): void @@ -94,7 +94,7 @@ public function testYamlNestedMapping(): void self::TEST_YAML_ENTITY_CLASS, 'Gedmo\Tree' ); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('left', $config); static::assertSame('left', $config['left']); static::assertArrayHasKey('right', $config); @@ -113,7 +113,7 @@ public function testYamlClosureMapping(): void { $meta = $this->em->getClassMetadata(self::YAML_CLOSURE_CATEGORY); $cacheId = ExtensionMetadataFactory::getCacheId(self::YAML_CLOSURE_CATEGORY, 'Gedmo\Tree'); - $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId); + $config = $this->cache->getItem($cacheId)->get(); static::assertArrayHasKey('parent', $config); static::assertSame('parent', $config['parent']);