CachePoolPrunerPassTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\FilesystemAdapter;
  13. use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
  14. use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass;
  15. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. class CachePoolPrunerPassTest extends TestCase
  19. {
  20. public function testCompilerPassReplacesCommandArgument()
  21. {
  22. $container = new ContainerBuilder();
  23. $container->register('console.command.cache_pool_prune')->addArgument([]);
  24. $container->register('pool.foo', FilesystemAdapter::class)->addTag('cache.pool');
  25. $container->register('pool.bar', PhpFilesAdapter::class)->addTag('cache.pool');
  26. $pass = new CachePoolPrunerPass();
  27. $pass->process($container);
  28. $expected = [
  29. 'pool.foo' => new Reference('pool.foo'),
  30. 'pool.bar' => new Reference('pool.bar'),
  31. ];
  32. $argument = $container->getDefinition('console.command.cache_pool_prune')->getArgument(0);
  33. $this->assertInstanceOf(IteratorArgument::class, $argument);
  34. $this->assertEquals($expected, $argument->getValues());
  35. }
  36. public function testCompilePassIsIgnoredIfCommandDoesNotExist()
  37. {
  38. $container = new ContainerBuilder();
  39. $definitionsBefore = \count($container->getDefinitions());
  40. $aliasesBefore = \count($container->getAliases());
  41. $pass = new CachePoolPrunerPass();
  42. $pass->process($container);
  43. // the container is untouched (i.e. no new definitions or aliases)
  44. $this->assertCount($definitionsBefore, $container->getDefinitions());
  45. $this->assertCount($aliasesBefore, $container->getAliases());
  46. }
  47. public function testCompilerPassThrowsOnInvalidDefinitionClass()
  48. {
  49. $this->expectException('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException');
  50. $this->expectExceptionMessage('Class "Symfony\Component\Cache\Tests\DependencyInjection\NotFound" used for service "pool.not-found" cannot be found.');
  51. $container = new ContainerBuilder();
  52. $container->register('console.command.cache_pool_prune')->addArgument([]);
  53. $container->register('pool.not-found', NotFound::class)->addTag('cache.pool');
  54. $pass = new CachePoolPrunerPass();
  55. $pass->process($container);
  56. }
  57. }