|
| 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 | + |
| 19 | +class CachePoolPassTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + private $cachePoolPass; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->cachePoolPass = new CachePoolPass(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testNamespaceArgumentIsReplaced() |
| 29 | + { |
| 30 | + $container = new ContainerBuilder(); |
| 31 | + $adapter = new Definition(); |
| 32 | + $adapter->setAbstract(true); |
| 33 | + $adapter->addTag('cache.adapter', array('namespace_arg_index' => 0)); |
| 34 | + $container->setDefinition('app.cache_adapter', $adapter); |
| 35 | + $cachePool = new DefinitionDecorator('app.cache_adapter'); |
| 36 | + $cachePool->addArgument(null); |
| 37 | + $cachePool->addTag('cache.pool'); |
| 38 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 39 | + |
| 40 | + $this->cachePoolPass->process($container); |
| 41 | + |
| 42 | + $this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @expectedException \InvalidArgumentException |
| 47 | + * @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service but "app.cache_pool" has none. |
| 48 | + */ |
| 49 | + public function testThrowsExceptionWhenCachePoolHasNoParentDefinition() |
| 50 | + { |
| 51 | + $container = new ContainerBuilder(); |
| 52 | + $cachePool = new Definition(); |
| 53 | + $cachePool->addTag('cache.pool'); |
| 54 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 55 | + |
| 56 | + $this->cachePoolPass->process($container); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @expectedException \InvalidArgumentException |
| 61 | + * @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "app.cache_pool" has none. |
| 62 | + */ |
| 63 | + public function testThrowsExceptionWhenCachePoolIsNotBasedOnAdapter() |
| 64 | + { |
| 65 | + $container = new ContainerBuilder(); |
| 66 | + $container->register('app.cache_adapter'); |
| 67 | + $cachePool = new DefinitionDecorator('app.cache_adapter'); |
| 68 | + $cachePool->addTag('cache.pool'); |
| 69 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 70 | + |
| 71 | + $this->cachePoolPass->process($container); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @expectedException \InvalidArgumentException |
| 76 | + * @expectedExceptionMessage Invalid "cache.adapter" tag for service "app.cache_adapter": attribute "namespace_arg_index" is missing. |
| 77 | + */ |
| 78 | + public function testThrowsExceptionWhenCacheAdapterDefinesNoNamespaceArgument() |
| 79 | + { |
| 80 | + $container = new ContainerBuilder(); |
| 81 | + $adapter = new Definition(); |
| 82 | + $adapter->setAbstract(true); |
| 83 | + $adapter->addTag('cache.adapter'); |
| 84 | + $container->setDefinition('app.cache_adapter', $adapter); |
| 85 | + $cachePool = new DefinitionDecorator('app.cache_adapter'); |
| 86 | + $cachePool->addTag('cache.pool'); |
| 87 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 88 | + |
| 89 | + $this->cachePoolPass->process($container); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @expectedException \InvalidArgumentException |
| 94 | + * @expectedExceptionMessage Services tagged as "cache.adapter" must be abstract: "app.cache_adapter" is not. |
| 95 | + */ |
| 96 | + public function testThrowsExceptionWhenCacheAdapterIsNotAbstract() |
| 97 | + { |
| 98 | + $container = new ContainerBuilder(); |
| 99 | + $adapter = new Definition(); |
| 100 | + $adapter->addTag('cache.adapter', array('namespace_arg_index' => 0)); |
| 101 | + $container->setDefinition('app.cache_adapter', $adapter); |
| 102 | + $cachePool = new DefinitionDecorator('app.cache_adapter'); |
| 103 | + $cachePool->addTag('cache.pool'); |
| 104 | + $container->setDefinition('app.cache_pool', $cachePool); |
| 105 | + |
| 106 | + $this->cachePoolPass->process($container); |
| 107 | + } |
| 108 | +} |
0 commit comments