From e4ba833a702b0e6dd814ed8fc610975d87f7b540 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 1 Jun 2021 17:56:39 +0200 Subject: [PATCH] Fix compatibility layer for cache services (#1352) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gabriel Ostrolucký --- .../Compiler/CacheCompatibilityPass.php | 67 ++++++++++++++ DependencyInjection/Configuration.php | 5 +- DependencyInjection/DoctrineExtension.php | 91 ++++++------------- DoctrineBundle.php | 2 + .../AbstractDoctrineExtensionTest.php | 13 ++- .../Compiler/CacheCompatibilityPassTest.php | 86 ++++++++++++++++++ .../Compiler/IdGeneratorPassTest.php} | 8 +- .../DoctrineExtensionTest.php | 48 +++++----- .../Fixtures/TestKernel.php | 4 +- .../DisconnectedMetadataFactoryTest.php | 1 + Tests/ServiceRepositoryTest.php | 2 + Tests/TestCase.php | 10 +- 12 files changed, 241 insertions(+), 96 deletions(-) create mode 100644 DependencyInjection/Compiler/CacheCompatibilityPass.php create mode 100644 Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php rename Tests/{CustomIdGeneratorTest.php => DependencyInjection/Compiler/IdGeneratorPassTest.php} (90%) diff --git a/DependencyInjection/Compiler/CacheCompatibilityPass.php b/DependencyInjection/Compiler/CacheCompatibilityPass.php new file mode 100644 index 000000000..64b02a770 --- /dev/null +++ b/DependencyInjection/Compiler/CacheCompatibilityPass.php @@ -0,0 +1,67 @@ + true, + 'setQueryCacheImpl' => false, + 'setResultCacheImpl' => false, + ]; + + public function process(ContainerBuilder $container): void + { + foreach (array_keys($container->findTaggedServiceIds(self::CONFIGURATION_TAG)) as $id) { + foreach ($container->getDefinition($id)->getMethodCalls() as $methodCall) { + if (! isset(self::CACHE_METHODS_PSR6_SUPPORT_MAP[$methodCall[0]])) { + continue; + } + + $aliasId = (string) $methodCall[1][0]; + $definitionId = (string) $container->getAlias($aliasId); + $isPsr6 = is_a($container->getDefinition($definitionId)->getClass(), CacheItemPoolInterface::class, true); + $shouldBePsr6 = self::CACHE_METHODS_PSR6_SUPPORT_MAP[$methodCall[0]]; + + if ($shouldBePsr6 === $isPsr6) { + continue; + } + + $targetClass = CacheProvider::class; + $targetFactory = DoctrineProvider::class; + + if ($shouldBePsr6) { + $targetClass = CacheItemPoolInterface::class; + $targetFactory = CacheAdapter::class; + + trigger_deprecation( + 'doctrine/doctrine-bundle', + '2.4', + 'Configuring doctrine/cache is deprecated. Please update the cache service "%s" to use a PSR-6 cache.', + $definitionId + ); + } + + $compatibilityLayerId = $definitionId . '.compatibility_layer'; + $container->setAlias($aliasId, $compatibilityLayerId); + $container->register($compatibilityLayerId, $targetClass) + ->setFactory([$targetFactory, 'wrap']) + ->addArgument(new Reference($definitionId)); + } + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9fda8bae9..6ff8882ef 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -736,7 +736,6 @@ private function getOrmCacheDriverNode(string $name): ArrayNodeDefinition $node = $treeBuilder->getRootNode(); $node - ->addDefaultsIfNotSet() ->beforeNormalization() ->ifString() ->then(static function ($v): array { @@ -751,9 +750,11 @@ private function getOrmCacheDriverNode(string $name): ArrayNodeDefinition if ($name === 'metadata_cache_driver') { $node->setDeprecated(...$this->getDeprecationMsg( - 'The "metadata_cache_driver" configuration key is deprecated. PHP Array cache is now automatically registered when %kernel.debug% is false.', + 'The "metadata_cache_driver" configuration key is deprecated. Remove the configuration to have the cache created automatically.', '2.3' )); + } else { + $node->addDefaultsIfNotSet(); } return $node; diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index 0c627b62f..d6a83fc38 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -11,9 +11,6 @@ use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; -use Doctrine\Common\Cache\CacheProvider; -use Doctrine\Common\Cache\Psr6\CacheAdapter; -use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connections\MasterSlaveConnection; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; @@ -26,7 +23,6 @@ use Doctrine\ORM\Proxy\Autoloader; use Doctrine\ORM\UnitOfWork; use LogicException; -use Psr\Cache\CacheItemPoolInterface; use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension; use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator; use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator; @@ -56,7 +52,6 @@ use function array_keys; use function class_exists; use function interface_exists; -use function is_a; use function method_exists; use function reset; use function sprintf; @@ -883,21 +878,17 @@ protected function getMappingResourceExtension(): string /** * {@inheritDoc} */ - protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriver, ContainerBuilder $container, bool $usePsr6 = false): string + protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriver, ContainerBuilder $container): string { $aliasId = $this->getObjectManagerElementName(sprintf('%s_%s', $objectManagerName, $cacheName)); - $isPsr6 = null; switch ($cacheDriver['type'] ?? 'pool') { case 'service': $serviceId = $cacheDriver['id']; - break; case 'pool': - $pool = $cacheDriver['pool'] ?? $this->createArrayAdapterCachePool($container, $objectManagerName, $cacheName); - $serviceId = $pool; - $isPsr6 = true; + $serviceId = $cacheDriver['pool'] ?? $this->createArrayAdapterCachePool($container, $objectManagerName, $cacheName); break; default: @@ -909,32 +900,6 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD )); } - if ($isPsr6 === null) { - $definition = $container->getDefinition($serviceId); - $isPsr6 = is_a($definition->getClass(), CacheItemPoolInterface::class, true); - } - - $cacheName = str_replace('_cache', '', $cacheName); - - // Create a wrapper as required - if ($isPsr6 && ! $usePsr6) { - $wrappedServiceId = sprintf('doctrine.orm.cache.provider.%s.%s', $objectManagerName, $cacheName); - - $definition = $container->register($wrappedServiceId, CacheProvider::class); - $definition->setFactory([DoctrineProvider::class, 'wrap']); - $definition->addArgument(new Reference($serviceId)); - - $serviceId = $wrappedServiceId; - } elseif (! $isPsr6 && $usePsr6) { - $wrappedServiceId = sprintf('cache.doctrine.orm.adapter.%s.%s', $objectManagerName, $cacheName); - - $definition = $container->register($wrappedServiceId, CacheItemPoolInterface::class); - $definition->setFactory([CacheAdapter::class, 'wrap']); - $definition->addArgument(new Reference($serviceId)); - - $serviceId = $wrappedServiceId; - } - $container->setAlias($aliasId, new Alias($serviceId, false)); return $aliasId; @@ -947,15 +912,36 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD */ protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $container) { - $this->loadCacheDriver('metadata_cache', $entityManager['name'], $entityManager['metadata_cache_driver'], $container, true); - $this->loadCacheDriver('result_cache', $entityManager['name'], $entityManager['result_cache_driver'], $container, false); - $this->loadCacheDriver('query_cache', $entityManager['name'], $entityManager['query_cache_driver'], $container, false); + if (isset($entityManager['metadata_cache_driver'])) { + $this->loadCacheDriver('metadata_cache', $entityManager['name'], $entityManager['metadata_cache_driver'], $container); + } else { + $this->createMetadataCache($entityManager['name'], $container); + } - if ($container->getParameter('kernel.debug')) { - return; + $this->loadCacheDriver('result_cache', $entityManager['name'], $entityManager['result_cache_driver'], $container); + $this->loadCacheDriver('query_cache', $entityManager['name'], $entityManager['query_cache_driver'], $container); + } + + private function createMetadataCache(string $objectManagerName, ContainerBuilder $container): void + { + $aliasId = $this->getObjectManagerElementName(sprintf('%s_%s', $objectManagerName, 'metadata_cache')); + $cacheId = sprintf('cache.doctrine.orm.%s.%s', $objectManagerName, 'metadata'); + + $cache = new Definition(ArrayAdapter::class); + + if (! $container->getParameter('kernel.debug')) { + $phpArrayFile = '%kernel.cache_dir%' . sprintf('/doctrine/orm/%s_metadata.php', $objectManagerName); + $cacheWarmerServiceId = $this->getObjectManagerElementName(sprintf('%s_%s', $objectManagerName, 'metadata_cache_warmer')); + + $container->register($cacheWarmerServiceId, DoctrineMetadataCacheWarmer::class) + ->setArguments([new Reference(sprintf('doctrine.orm.%s_entity_manager', $objectManagerName)), $phpArrayFile]) + ->addTag('kernel.cache_warmer', ['priority' => 1000]); // priority should be higher than ProxyCacheWarmer + + $cache = new Definition(PhpArrayAdapter::class, [$phpArrayFile, $cache]); } - $this->registerMetadataPhpArrayCaching($entityManager['name'], $container); + $container->setDefinition($cacheId, $cache); + $container->setAlias($aliasId, $cacheId); } /** @@ -1062,23 +1048,4 @@ private function createArrayAdapterCachePool(ContainerBuilder $container, string return $id; } - - private function registerMetadataPhpArrayCaching(string $entityManagerName, ContainerBuilder $container): void - { - $metadataCacheAlias = $this->getObjectManagerElementName($entityManagerName . '_metadata_cache'); - $decoratedMetadataCacheServiceId = (string) $container->getAlias($metadataCacheAlias); - $phpArrayCacheDecoratorServiceId = $decoratedMetadataCacheServiceId . '.php_array'; - $phpArrayFile = '%kernel.cache_dir%' . sprintf('/doctrine/orm/%s_metadata.php', $entityManagerName); - $cacheWarmerServiceId = $this->getObjectManagerElementName($entityManagerName . '_metadata_cache_warmer'); - - $container->register($cacheWarmerServiceId, DoctrineMetadataCacheWarmer::class) - ->setArguments([new Reference(sprintf('doctrine.orm.%s_entity_manager', $entityManagerName)), $phpArrayFile]) - ->addTag('kernel.cache_warmer', ['priority' => 1000]); // priority should be higher than ProxyCacheWarmer - - $container->setAlias($metadataCacheAlias, $phpArrayCacheDecoratorServiceId); - - $container->register($phpArrayCacheDecoratorServiceId, PhpArrayAdapter::class) - ->addArgument($phpArrayFile) - ->addArgument(new Reference($decoratedMetadataCacheServiceId)); - } } diff --git a/DoctrineBundle.php b/DoctrineBundle.php index 2088d0fff..ec06ecbd2 100644 --- a/DoctrineBundle.php +++ b/DoctrineBundle.php @@ -2,6 +2,7 @@ namespace Doctrine\Bundle\DoctrineBundle; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass; @@ -48,6 +49,7 @@ public function build(ContainerBuilder $container) } } + $container->addCompilerPass(new CacheCompatibilityPass()); $container->addCompilerPass(new DoctrineValidationPass('orm')); $container->addCompilerPass(new EntityListenerPass()); $container->addCompilerPass(new ServiceRepositoryCompilerPass()); diff --git a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php index d92bd4125..1ee2210e4 100644 --- a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php +++ b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php @@ -3,10 +3,12 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection; use Doctrine\Bundle\DoctrineBundle\Dbal\BlacklistSchemaAssetFilter; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; +use Doctrine\Common\Cache\CacheProvider; use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connections\MasterSlaveConnection; @@ -445,12 +447,14 @@ public function testLoadMultipleConnections(): void $this->assertEquals(PhpArrayAdapter::class, $definition->getClass()); $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_query_cache')); + $this->assertEquals(CacheProvider::class, $definition->getClass()); $this->assertEquals([DoctrineProvider::class, 'wrap'], $definition->getFactory()); $arguments = $definition->getArguments(); $this->assertInstanceOf(Reference::class, $arguments[0]); $this->assertEquals('cache.doctrine.orm.em1.query', (string) $arguments[0]); $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_result_cache')); + $this->assertEquals(CacheProvider::class, $definition->getClass()); $this->assertEquals([DoctrineProvider::class, 'wrap'], $definition->getFactory()); $arguments = $definition->getArguments(); $this->assertInstanceOf(Reference::class, $arguments[0]); @@ -731,7 +735,7 @@ public function testSecondLevelCache(): void $this->assertDICDefinitionClass($myEntityRegionDef, '%doctrine.orm.second_level_cache.default_region.class%'); $this->assertDICDefinitionClass($loggerChainDef, '%doctrine.orm.second_level_cache.logger_chain.class%'); $this->assertDICDefinitionClass($loggerStatisticsDef, '%doctrine.orm.second_level_cache.logger_statistics.class%'); - $this->assertEquals([DoctrineProvider::class, 'wrap'], $cacheDriverDef->getFactory()); + $this->assertDICDefinitionClass($cacheDriverDef, ArrayAdapter::class); $this->assertDICDefinitionMethodCallOnce($configDef, 'setSecondLevelCacheConfiguration'); $this->assertDICDefinitionMethodCallCount($slcFactoryDef, 'setRegion', [], 3); $this->assertDICDefinitionMethodCallCount($loggerChainDef, 'setLogger', [], 3); @@ -1277,6 +1281,7 @@ private function loadContainer( ): ContainerBuilder { $container = $this->getContainer($bundles); $container->registerExtension(new DoctrineExtension()); + $container->addCompilerPass(new CacheCompatibilityPass()); $this->loadFromFile($container, $fixture); @@ -1450,8 +1455,10 @@ private function assertDICDefinitionNoMethodCall( private function compileContainer(ContainerBuilder $container): void { - $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]); - $container->getCompilerPassConfig()->setRemovingPasses([]); + $passConfig = $container->getCompilerPassConfig(); + $passConfig->setOptimizationPasses([new ResolveChildDefinitionsPass()]); + $passConfig->setRemovingPasses([]); + $passConfig->addPass(new CacheCompatibilityPass()); $container->compile(); } } diff --git a/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php b/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php new file mode 100644 index 000000000..f9b1ee544 --- /dev/null +++ b/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php @@ -0,0 +1,86 @@ +expectNotToPerformAssertions(); + (new class () extends TestKernel { + public function registerContainerConfiguration(LoaderInterface $loader): void + { + parent::registerContainerConfiguration($loader); + $loader->load(static function (ContainerBuilder $containerBuilder): void { + $containerBuilder->loadFromExtension( + 'doctrine', + ['orm' => ['query_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] + ); + $containerBuilder->setDefinition( + 'custom_cache_service', + (new Definition(DoctrineProvider::class)) + ->setArguments([new Definition(ArrayAdapter::class)]) + ->setFactory([DoctrineProvider::class, 'wrap']) + ); + }); + } + })->boot(); + } + + /** @group legacy */ + public function testMetadataCacheConfigUsingPsr6ServiceDefinedByApplication(): void + { + $this->expectDeprecation('%aThe "metadata_cache_driver" configuration key is deprecated.%a'); + (new class (false) extends TestKernel { + public function registerContainerConfiguration(LoaderInterface $loader): void + { + parent::registerContainerConfiguration($loader); + $loader->load(static function (ContainerBuilder $containerBuilder): void { + $containerBuilder->loadFromExtension( + 'doctrine', + ['orm' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] + ); + $containerBuilder->setDefinition( + 'custom_cache_service', + new Definition(ArrayAdapter::class) + ); + }); + } + })->boot(); + } + + /** @group legacy */ + public function testMetdataCacheConfigUsingNonPsr6ServiceDefinedByApplication(): void + { + $this->expectDeprecation('Since doctrine/doctrine-bundle 2.4: Configuring doctrine/cache is deprecated. Please update the cache service "custom_cache_service" to use a PSR-6 cache.'); + (new class (false) extends TestKernel { + public function registerContainerConfiguration(LoaderInterface $loader): void + { + parent::registerContainerConfiguration($loader); + $loader->load(static function (ContainerBuilder $containerBuilder): void { + $containerBuilder->loadFromExtension( + 'doctrine', + ['orm' => ['metadata_cache_driver' => ['type' => 'service', 'id' => 'custom_cache_service']]] + ); + $containerBuilder->setDefinition( + 'custom_cache_service', + (new Definition(DoctrineProvider::class)) + ->setArguments([new Definition(ArrayAdapter::class)]) + ->setFactory([DoctrineProvider::class, 'wrap']) + ); + }); + } + })->boot(); + } +} diff --git a/Tests/CustomIdGeneratorTest.php b/Tests/DependencyInjection/Compiler/IdGeneratorPassTest.php similarity index 90% rename from Tests/CustomIdGeneratorTest.php rename to Tests/DependencyInjection/Compiler/IdGeneratorPassTest.php index 217f5f67e..a9d278cda 100644 --- a/Tests/CustomIdGeneratorTest.php +++ b/Tests/DependencyInjection/Compiler/IdGeneratorPassTest.php @@ -1,7 +1,8 @@ [ 'AnnotationsBundle' => [ 'type' => 'annotation', - 'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Entity', + 'dir' => __DIR__ . '/../Fixtures/Bundles/AnnotationsBundle/Entity', 'prefix' => 'Fixtures\Bundles\AnnotationsBundle\Entity', ], ], @@ -78,6 +79,7 @@ public function testRepositoryServiceWiring(): void $def->setAutoconfigured(true); + $container->addCompilerPass(new CacheCompatibilityPass()); $container->addCompilerPass(new IdGeneratorPass()); $container->compile(); diff --git a/Tests/DependencyInjection/DoctrineExtensionTest.php b/Tests/DependencyInjection/DoctrineExtensionTest.php index 7711d727a..fb387eaa7 100644 --- a/Tests/DependencyInjection/DoctrineExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -5,6 +5,7 @@ use Closure; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; use Doctrine\Bundle\DoctrineBundle\CacheWarmer\DoctrineMetadataCacheWarmer; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; use Doctrine\Bundle\DoctrineBundle\Tests\Builder\BundleConfigurationBuilder; use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\Php8EntityListener; @@ -454,11 +455,8 @@ public function testDependencyInjectionConfigurationDefaults(): void $arguments = $definition->getArguments(); $this->assertSame('%kernel.cache_dir%/doctrine/orm/default_metadata.php', $arguments[0]); - $this->assertInstanceOf(Reference::class, $arguments[1]); - $wrappedDefinition = (string) $arguments[1]; - - $definition = $container->getDefinition((string) $wrappedDefinition); - $this->assertSame(ArrayAdapter::class, $definition->getClass()); + $wrappedDefinition = $arguments[1]; + $this->assertSame(ArrayAdapter::class, $wrappedDefinition->getClass()); $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_query_cache')); $this->assertEquals([DoctrineProvider::class, 'wrap'], $definition->getFactory()); @@ -577,6 +575,7 @@ public function testSingleEntityManagerWithDefaultSecondLevelCacheConfiguration( $this->assertEquals('%doctrine.orm.second_level_cache.default_cache_factory.class%', $slcDefinition->getClass()); } + /** @group legacy */ public function testSingleEntityManagerWithCustomSecondLevelCacheConfiguration(): void { if (! interface_exists(EntityManagerInterface::class)) { @@ -932,7 +931,7 @@ public function testInvalidCacheConfiguration(): void * * @dataProvider cacheConfigurationProvider */ - public function testCacheConfiguration(string $expectedAliasName, string $expectedAliasTarget, string $cacheName, $cacheConfig): void + public function testCacheConfiguration(string $expectedAliasName, string $expectedTarget, string $cacheName, $cacheConfig): void { if (! interface_exists(EntityManagerInterface::class)) { self::markTestSkipped('This test requires ORM'); @@ -950,7 +949,7 @@ public function testCacheConfiguration(string $expectedAliasName, string $expect $this->assertTrue($container->hasAlias($expectedAliasName)); $alias = $container->getAlias($expectedAliasName); - $this->assertEquals($expectedAliasTarget, (string) $alias); + $this->assertEquals($expectedTarget, (string) $alias); } /** @@ -970,27 +969,33 @@ public static function legacyCacheConfigurationProvider(): array return [ 'metadata_cache_default' => [ 'expectedAliasName' => 'doctrine.orm.default_metadata_cache', - 'expectedAliasTarget' => 'cache.doctrine.orm.default.metadata.php_array', + 'expectedAliasTarget' => 'cache.doctrine.orm.default.metadata', 'cacheName' => 'metadata_cache_driver', 'cacheConfig' => ['type' => null], ], 'metadata_cache_pool' => [ 'expectedAliasName' => 'doctrine.orm.default_metadata_cache', - 'expectedAliasTarget' => 'metadata_cache_pool.php_array', + 'expectedAliasTarget' => 'metadata_cache_pool', 'cacheName' => 'metadata_cache_driver', 'cacheConfig' => ['type' => 'pool', 'pool' => 'metadata_cache_pool'], ], 'metadata_cache_service' => [ 'expectedAliasName' => 'doctrine.orm.default_metadata_cache', - 'expectedAliasTarget' => 'cache.doctrine.orm.adapter.default.metadata.php_array', + 'expectedAliasTarget' => 'service_target_metadata', 'cacheName' => 'metadata_cache_driver', 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_metadata'], ], - 'metadata_psr6_service' => [ - 'expectedAliasName' => 'doctrine.orm.default_metadata_cache', - 'expectedAliasTarget' => 'service_target_metadata_psr6.php_array', - 'cacheName' => 'metadata_cache_driver', - 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_metadata_psr6'], + 'query_cache_service' => [ + 'expectedAliasName' => 'doctrine.orm.default_query_cache', + 'expectedAliasTarget' => 'service_target_query', + 'cacheName' => 'query_cache_driver', + 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_query'], + ], + 'result_cache_service' => [ + 'expectedAliasName' => 'doctrine.orm.default_result_cache', + 'expectedAliasTarget' => 'service_target_result', + 'cacheName' => 'result_cache_driver', + 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_result'], ], ]; } @@ -1001,37 +1006,37 @@ public static function cacheConfigurationProvider(): array return [ 'query_cache_default' => [ 'expectedAliasName' => 'doctrine.orm.default_query_cache', - 'expectedAliasTarget' => 'doctrine.orm.cache.provider.default.query', + 'expectedTarget' => 'cache.doctrine.orm.default.query', 'cacheName' => 'query_cache_driver', 'cacheConfig' => ['type' => null], ], 'result_cache_default' => [ 'expectedAliasName' => 'doctrine.orm.default_result_cache', - 'expectedAliasTarget' => 'doctrine.orm.cache.provider.default.result', + 'expectedTarget' => 'cache.doctrine.orm.default.result', 'cacheName' => 'result_cache_driver', 'cacheConfig' => ['type' => null], ], 'query_cache_pool' => [ 'expectedAliasName' => 'doctrine.orm.default_query_cache', - 'expectedAliasTarget' => 'doctrine.orm.cache.provider.default.query', + 'expectedTarget' => 'query_cache_pool', 'cacheName' => 'query_cache_driver', 'cacheConfig' => ['type' => 'pool', 'pool' => 'query_cache_pool'], ], 'result_cache_pool' => [ 'expectedAliasName' => 'doctrine.orm.default_result_cache', - 'expectedAliasTarget' => 'doctrine.orm.cache.provider.default.result', + 'expectedTarget' => 'result_cache_pool', 'cacheName' => 'result_cache_driver', 'cacheConfig' => ['type' => 'pool', 'pool' => 'result_cache_pool'], ], 'query_cache_service' => [ 'expectedAliasName' => 'doctrine.orm.default_query_cache', - 'expectedAliasTarget' => 'service_target_query', + 'expectedTarget' => 'service_target_query', 'cacheName' => 'query_cache_driver', 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_query'], ], 'result_cache_service' => [ 'expectedAliasName' => 'doctrine.orm.default_result_cache', - 'expectedAliasTarget' => 'service_target_result', + 'expectedTarget' => 'service_target_result', 'cacheName' => 'result_cache_driver', 'cacheConfig' => ['type' => 'service', 'id' => 'service_target_result'], ], @@ -1202,6 +1207,7 @@ private function compileContainer(ContainerBuilder $container): void { $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]); $container->getCompilerPassConfig()->setRemovingPasses([]); + $container->addCompilerPass(new CacheCompatibilityPass()); $container->compile(); } } diff --git a/Tests/DependencyInjection/Fixtures/TestKernel.php b/Tests/DependencyInjection/Fixtures/TestKernel.php index 8215e1799..9660a9ada 100644 --- a/Tests/DependencyInjection/Fixtures/TestKernel.php +++ b/Tests/DependencyInjection/Fixtures/TestKernel.php @@ -19,9 +19,9 @@ class TestKernel extends Kernel /** @var string|null */ private $projectDir; - public function __construct() + public function __construct(bool $debug = true) { - parent::__construct('test', true); + parent::__construct('test', $debug); } /** @return iterable */ diff --git a/Tests/Mapping/DisconnectedMetadataFactoryTest.php b/Tests/Mapping/DisconnectedMetadataFactoryTest.php index f52ccc402..ab61fffbb 100644 --- a/Tests/Mapping/DisconnectedMetadataFactoryTest.php +++ b/Tests/Mapping/DisconnectedMetadataFactoryTest.php @@ -41,6 +41,7 @@ public function testCannotFindNamespaceAndPathForMetadata(): void public function testFindNamespaceAndPathForMetadata(): void { + /** @psalm-suppress UndefinedClass */ $class = new ClassMetadataInfo('\Vendor\Package\Class'); $collection = new ClassMetadataCollection([$class]); diff --git a/Tests/ServiceRepositoryTest.php b/Tests/ServiceRepositoryTest.php index a55658379..c8e6f8859 100644 --- a/Tests/ServiceRepositoryTest.php +++ b/Tests/ServiceRepositoryTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; use Doctrine\Common\Annotations\AnnotationReader; @@ -87,6 +88,7 @@ public function testRepositoryServiceWiring(): void $def->setAutoconfigured(true); $container->addCompilerPass(new ServiceRepositoryCompilerPass()); + $container->addCompilerPass(new CacheCompatibilityPass()); $container->compile(); $em = $container->get('doctrine.orm.default_entity_manager'); diff --git a/Tests/TestCase.php b/Tests/TestCase.php index a5a5117cf..0ad1241f5 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -2,6 +2,7 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests; +use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType; use Doctrine\Common\Annotations\AnnotationReader; @@ -74,10 +75,13 @@ public function createXmlBundleTestContainer(): ContainerBuilder $container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true)); $container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true)); - $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]); - $container->getCompilerPassConfig()->setRemovingPasses([]); + $compilerPassConfig = $container->getCompilerPassConfig(); + + $compilerPassConfig->setOptimizationPasses([new ResolveChildDefinitionsPass()]); + $compilerPassConfig->setRemovingPasses([]); + $compilerPassConfig->addPass(new CacheCompatibilityPass()); // make all Doctrine services public, so we can fetch them in the test - $container->getCompilerPassConfig()->addPass(new TestCaseAllPublicCompilerPass()); + $compilerPassConfig->addPass(new TestCaseAllPublicCompilerPass()); $container->compile(); return $container;