CachePoolClearerPassTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass;
  13. use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
  14. use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
  15. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Definition;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
  20. class CachePoolClearerPassTest extends TestCase
  21. {
  22. public function testPoolRefsAreWeak()
  23. {
  24. $container = new ContainerBuilder();
  25. $container->setParameter('kernel.container_class', 'app');
  26. $container->setParameter('kernel.project_dir', 'foo');
  27. $globalClearer = new Definition(Psr6CacheClearer::class);
  28. $container->setDefinition('cache.global_clearer', $globalClearer);
  29. $publicPool = new Definition();
  30. $publicPool->addArgument('namespace');
  31. $publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
  32. $container->setDefinition('public.pool', $publicPool);
  33. $publicPool = new Definition();
  34. $publicPool->addArgument('namespace');
  35. $publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias', 'name' => 'pool2']);
  36. $container->setDefinition('public.pool2', $publicPool);
  37. $privatePool = new Definition();
  38. $privatePool->setPublic(false);
  39. $privatePool->addArgument('namespace');
  40. $privatePool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
  41. $container->setDefinition('private.pool', $privatePool);
  42. $clearer = new Definition();
  43. $container->setDefinition('clearer', $clearer);
  44. $container->setAlias('clearer_alias', 'clearer');
  45. $pass = new RemoveUnusedDefinitionsPass();
  46. foreach ($container->getCompiler()->getPassConfig()->getRemovingPasses() as $removingPass) {
  47. if ($removingPass instanceof RepeatedPass) {
  48. $pass->setRepeatedPass(new RepeatedPass([$pass]));
  49. break;
  50. }
  51. }
  52. foreach ([new CachePoolPass(), $pass, new CachePoolClearerPass()] as $pass) {
  53. $pass->process($container);
  54. }
  55. $expected = [[
  56. 'public.pool' => new Reference('public.pool'),
  57. 'pool2' => new Reference('public.pool2'),
  58. ]];
  59. $this->assertEquals($expected, $clearer->getArguments());
  60. $this->assertEquals($expected, $globalClearer->getArguments());
  61. }
  62. }