|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\DefinitionDecorator; |
| 18 | +use Symfony\Component\DependencyInjection\Reference; |
| 19 | + |
| 20 | +class CachePoolPassTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + private $cachePoolPass; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $this->cachePoolPass = new CachePoolPass(); |
| 27 | + } |
| 28 | + |
| 29 | + public function testNamespaceArgumentIsReplaced() |
| 30 | + { |
| 31 | + $container = new ContainerBuilder(); |
| 32 | + $adapter = new Definition(); |
| 33 | + $adapter->setAbstract(true); |
| 34 | + $adapter->addTag('cache.pool'); |
| 35 | + $container->setDefinition('app.cache_adapter', $adapter); |
| 36 | + $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter'); |
| 37 | + $cachePool = new DefinitionDecorator('app.cache_adapter_alias'); |
| 38 | + $cachePool->addArgument(null); |
| 39 | + $cachePool->addTag('cache.pool'); |
| 40 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 41 | + |
| 42 | + $this->cachePoolPass->process($container); |
| 43 | + |
| 44 | + $this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0)); |
| 45 | + } |
| 46 | + |
| 47 | + public function testArgsAreReplaced() |
| 48 | + { |
| 49 | + $container = new ContainerBuilder(); |
| 50 | + $cachePool = new Definition(); |
| 51 | + $cachePool->addTag('cache.pool', array( |
| 52 | + 'provider' => 'foobar', |
| 53 | + 'default_lifetime' => 3, |
| 54 | + )); |
| 55 | + $cachePool->addArgument(null); |
| 56 | + $cachePool->addArgument(null); |
| 57 | + $cachePool->addArgument(null); |
| 58 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 59 | + |
| 60 | + $this->cachePoolPass->process($container); |
| 61 | + |
| 62 | + $this->assertInstanceOf(Reference::class, $cachePool->getArgument(0)); |
| 63 | + $this->assertSame('foobar', (string) $cachePool->getArgument(0)); |
| 64 | + $this->assertSame('yRnzIIVLvL', $cachePool->getArgument(1)); |
| 65 | + $this->assertSame(3, $cachePool->getArgument(2)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @expectedException \InvalidArgumentException |
| 70 | + * @expectedExceptionMessage Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are |
| 71 | + */ |
| 72 | + public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes() |
| 73 | + { |
| 74 | + $container = new ContainerBuilder(); |
| 75 | + $adapter = new Definition(); |
| 76 | + $adapter->setAbstract(true); |
| 77 | + $adapter->addTag('cache.pool'); |
| 78 | + $container->setDefinition('app.cache_adapter', $adapter); |
| 79 | + $cachePool = new DefinitionDecorator('app.cache_adapter'); |
| 80 | + $cachePool->addTag('cache.pool', array('foobar' => 123)); |
| 81 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 82 | + |
| 83 | + $this->cachePoolPass->process($container); |
| 84 | + } |
| 85 | +} |
0 commit comments