CachePoolPassTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Adapter\ArrayAdapter;
  13. use Symfony\Component\Cache\Adapter\RedisAdapter;
  14. use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
  15. use Symfony\Component\DependencyInjection\ChildDefinition;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Definition;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. class CachePoolPassTest extends TestCase
  20. {
  21. private $cachePoolPass;
  22. protected function setUp(): void
  23. {
  24. $this->cachePoolPass = new CachePoolPass();
  25. }
  26. public function testNamespaceArgumentIsReplaced()
  27. {
  28. $container = new ContainerBuilder();
  29. $container->setParameter('kernel.container_class', 'app');
  30. $container->setParameter('kernel.project_dir', 'foo');
  31. $adapter = new Definition();
  32. $adapter->setAbstract(true);
  33. $adapter->addTag('cache.pool');
  34. $container->setDefinition('app.cache_adapter', $adapter);
  35. $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
  36. $cachePool = new ChildDefinition('app.cache_adapter_alias');
  37. $cachePool->addArgument(null);
  38. $cachePool->addTag('cache.pool');
  39. $container->setDefinition('app.cache_pool', $cachePool);
  40. $this->cachePoolPass->process($container);
  41. $this->assertSame('z3X945Jbf5', $cachePool->getArgument(0));
  42. }
  43. public function testNamespaceArgumentIsSeededWithAdapterClassName()
  44. {
  45. $container = new ContainerBuilder();
  46. $container->setParameter('kernel.container_class', 'app');
  47. $container->setParameter('kernel.project_dir', 'foo');
  48. $adapter = new Definition();
  49. $adapter->setAbstract(true);
  50. $adapter->addTag('cache.pool');
  51. $adapter->setClass(RedisAdapter::class);
  52. $container->setDefinition('app.cache_adapter', $adapter);
  53. $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
  54. $cachePool = new ChildDefinition('app.cache_adapter_alias');
  55. $cachePool->addArgument(null);
  56. $cachePool->addTag('cache.pool');
  57. $container->setDefinition('app.cache_pool', $cachePool);
  58. $this->cachePoolPass->process($container);
  59. $this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
  60. }
  61. public function testNamespaceArgumentIsSeededWithAdapterClassNameWithoutAffectingOtherCachePools()
  62. {
  63. $container = new ContainerBuilder();
  64. $container->setParameter('kernel.container_class', 'app');
  65. $container->setParameter('kernel.project_dir', 'foo');
  66. $adapter = new Definition();
  67. $adapter->setAbstract(true);
  68. $adapter->addTag('cache.pool');
  69. $adapter->setClass(RedisAdapter::class);
  70. $container->setDefinition('app.cache_adapter', $adapter);
  71. $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
  72. $otherCachePool = new ChildDefinition('app.cache_adapter_alias');
  73. $otherCachePool->addArgument(null);
  74. $otherCachePool->addTag('cache.pool');
  75. $container->setDefinition('app.other_cache_pool', $otherCachePool);
  76. $cachePool = new ChildDefinition('app.cache_adapter_alias');
  77. $cachePool->addArgument(null);
  78. $cachePool->addTag('cache.pool');
  79. $container->setDefinition('app.cache_pool', $cachePool);
  80. $this->cachePoolPass->process($container);
  81. $this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
  82. }
  83. public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
  84. {
  85. $container = new ContainerBuilder();
  86. $container->setParameter('kernel.container_class', 'app');
  87. $container->setParameter('kernel.project_dir', 'foo');
  88. $container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
  89. $cachePool = new ChildDefinition('cache.adapter.array');
  90. $cachePool->addTag('cache.pool');
  91. $container->setDefinition('app.cache_pool', $cachePool);
  92. $this->cachePoolPass->process($container);
  93. $this->assertCount(0, $container->getDefinition('app.cache_pool')->getArguments());
  94. }
  95. public function testArgsAreReplaced()
  96. {
  97. $container = new ContainerBuilder();
  98. $container->setParameter('kernel.container_class', 'app');
  99. $container->setParameter('cache.prefix.seed', 'foo');
  100. $cachePool = new Definition();
  101. $cachePool->addTag('cache.pool', [
  102. 'provider' => 'foobar',
  103. 'default_lifetime' => 3,
  104. ]);
  105. $cachePool->addArgument(null);
  106. $cachePool->addArgument(null);
  107. $cachePool->addArgument(null);
  108. $container->setDefinition('app.cache_pool', $cachePool);
  109. $this->cachePoolPass->process($container);
  110. $this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
  111. $this->assertSame('foobar', (string) $cachePool->getArgument(0));
  112. $this->assertSame('tQNhcV-8xa', $cachePool->getArgument(1));
  113. $this->assertSame(3, $cachePool->getArgument(2));
  114. }
  115. public function testWithNameAttribute()
  116. {
  117. $container = new ContainerBuilder();
  118. $container->setParameter('kernel.container_class', 'app');
  119. $container->setParameter('cache.prefix.seed', 'foo');
  120. $cachePool = new Definition();
  121. $cachePool->addTag('cache.pool', [
  122. 'name' => 'foobar',
  123. 'provider' => 'foobar',
  124. ]);
  125. $cachePool->addArgument(null);
  126. $cachePool->addArgument(null);
  127. $cachePool->addArgument(null);
  128. $container->setDefinition('app.cache_pool', $cachePool);
  129. $this->cachePoolPass->process($container);
  130. $this->assertSame('+naTpPa4Sm', $cachePool->getArgument(1));
  131. }
  132. public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
  133. {
  134. $this->expectException('InvalidArgumentException');
  135. $this->expectExceptionMessage('Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are');
  136. $container = new ContainerBuilder();
  137. $container->setParameter('kernel.container_class', 'app');
  138. $container->setParameter('kernel.project_dir', 'foo');
  139. $adapter = new Definition();
  140. $adapter->setAbstract(true);
  141. $adapter->addTag('cache.pool');
  142. $container->setDefinition('app.cache_adapter', $adapter);
  143. $cachePool = new ChildDefinition('app.cache_adapter');
  144. $cachePool->addTag('cache.pool', ['foobar' => 123]);
  145. $container->setDefinition('app.cache_pool', $cachePool);
  146. $this->cachePoolPass->process($container);
  147. }
  148. }