|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPrunerPass; |
| 16 | +use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 17 | +use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
| 18 | +use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
| 21 | + |
| 22 | +class CachePoolPrunerPassTest extends TestCase |
| 23 | +{ |
| 24 | + public function testCompilerPassReplacesCommandArgument() |
| 25 | + { |
| 26 | + $container = new ContainerBuilder(); |
| 27 | + $container->register('cache.command.pool_pruner')->addArgument(array()); |
| 28 | + $container->register('pool.foo', FilesystemAdapter::class)->addTag('cache.pool'); |
| 29 | + $container->register('pool.bar', PhpFilesAdapter::class)->addTag('cache.pool'); |
| 30 | + |
| 31 | + $pass = new CachePoolPrunerPass(); |
| 32 | + $pass->process($container); |
| 33 | + |
| 34 | + $expected = array( |
| 35 | + 'pool.foo' => new Reference('pool.foo'), |
| 36 | + 'pool.bar' => new Reference('pool.bar'), |
| 37 | + ); |
| 38 | + $argument = $container->getDefinition('cache.command.pool_pruner')->getArgument(0); |
| 39 | + |
| 40 | + $this->assertInstanceOf(IteratorArgument::class, $argument); |
| 41 | + $this->assertEquals($expected, $argument->getValues()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testCompilePassIsIgnoredIfCommandDoesNotExist() |
| 45 | + { |
| 46 | + $container = $this |
| 47 | + ->getMockBuilder(ContainerBuilder::class) |
| 48 | + ->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds')) |
| 49 | + ->getMock(); |
| 50 | + |
| 51 | + $container |
| 52 | + ->expects($this->atLeastOnce()) |
| 53 | + ->method('hasDefinition') |
| 54 | + ->with('cache.command.pool_pruner') |
| 55 | + ->will($this->returnValue(false)); |
| 56 | + |
| 57 | + $container |
| 58 | + ->expects($this->never()) |
| 59 | + ->method('getDefinition'); |
| 60 | + |
| 61 | + $container |
| 62 | + ->expects($this->never()) |
| 63 | + ->method('findTaggedServiceIds'); |
| 64 | + |
| 65 | + $pass = new CachePoolPrunerPass(); |
| 66 | + $pass->process($container); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
| 71 | + * @expectedExceptionMessage Class "Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\NotFound" used for service "pool.not-found" cannot be found. |
| 72 | + */ |
| 73 | + public function testCompilerPassThrowsOnInvalidDefinitionClass() |
| 74 | + { |
| 75 | + $container = new ContainerBuilder(); |
| 76 | + $container->register('cache.command.pool_pruner')->addArgument(array()); |
| 77 | + $container->register('pool.not-found', NotFound::class)->addTag('cache.pool'); |
| 78 | + |
| 79 | + $pass = new CachePoolPrunerPass(); |
| 80 | + $pass->process($container); |
| 81 | + } |
| 82 | +} |
0 commit comments