|
| 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 Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheAdapterPass; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +class CacheAdapterPassTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + private $cacheAdapterPass; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->cacheAdapterPass = new CacheAdapterPass(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testAdapterIsInjectedIntoConstructorArguments() |
| 29 | + { |
| 30 | + $container = $this->initializeContainer(); |
| 31 | + $this->cacheAdapterPass->process($container); |
| 32 | + $adapter = $container->getDefinition('foo')->getArgument(0); |
| 33 | + |
| 34 | + $this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter); |
| 35 | + $this->assertFalse($adapter->isAbstract()); |
| 36 | + $this->assertSame('cache.adapter.apcu_adapter', $adapter->getParent()); |
| 37 | + $this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $adapter->getArgument(0)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAdapterIsInjectedIntoMethodArguments() |
| 41 | + { |
| 42 | + $container = $this->initializeContainer(); |
| 43 | + $this->cacheAdapterPass->process($container); |
| 44 | + $methodCalls = $container->getDefinition('bar')->getMethodCalls(); |
| 45 | + $arguments = $methodCalls[0][1]; |
| 46 | + $adapter = $arguments[0]; |
| 47 | + |
| 48 | + $this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter); |
| 49 | + $this->assertFalse($adapter->isAbstract()); |
| 50 | + $this->assertSame('cache.adapter.doctrine_adapter', $adapter->getParent()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAdapterIsInjectIntoProperties() |
| 54 | + { |
| 55 | + $container = $this->initializeContainer(); |
| 56 | + $this->cacheAdapterPass->process($container); |
| 57 | + $properties = $container->getDefinition('baz')->getProperties(); |
| 58 | + $adapter = $properties['cache']; |
| 59 | + |
| 60 | + $this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter); |
| 61 | + $this->assertFalse($adapter->isAbstract()); |
| 62 | + $this->assertSame('cache.adapter.fs_adapter', $adapter->getParent()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @expectedException \InvalidArgumentException |
| 67 | + * @expectedExceptionMessage The cache adapter "bar" is not configured |
| 68 | + */ |
| 69 | + public function testThrowsExceptionWhenReferencedAdapterIsNotConfigured() |
| 70 | + { |
| 71 | + $container = new ContainerBuilder(); |
| 72 | + $container->setDefinition('foo', new Definition('Foo', array(new Reference('cache.adapter.bar')))); |
| 73 | + $this->cacheAdapterPass->process($container); |
| 74 | + } |
| 75 | + |
| 76 | + private function initializeContainer() |
| 77 | + { |
| 78 | + $container = new ContainerBuilder(); |
| 79 | + |
| 80 | + $apcuAdapter = new Definition('Symfony\Component\Cache\Adapter\ApcuAdapter'); |
| 81 | + $apcuAdapter->setAbstract(true); |
| 82 | + $container->setDefinition('cache.adapter.apcu_adapter', $apcuAdapter); |
| 83 | + |
| 84 | + $doctrineAdapter = new Definition('Symfony\Component\Cache\Adapter\DoctrineAdapter'); |
| 85 | + $doctrineAdapter->setAbstract(true); |
| 86 | + $container->setDefinition('cache.adapter.doctrine_adapter', $doctrineAdapter); |
| 87 | + |
| 88 | + $filesystemAdapter = new Definition('Symfony\Component\Cache\Adapter\FilesystemAdapter'); |
| 89 | + $filesystemAdapter->setAbstract(true); |
| 90 | + $container->setDefinition('cache.adapter.fs_adapter', $filesystemAdapter); |
| 91 | + |
| 92 | + $foo = new Definition(); |
| 93 | + $foo->setArguments(array(new Reference('cache.adapter.apcu_adapter'))); |
| 94 | + $container->setDefinition('foo', $foo); |
| 95 | + |
| 96 | + $bar = new Definition(); |
| 97 | + $bar->addMethodCall('setCache', array(new Reference('cache.adapter.doctrine_adapter'))); |
| 98 | + $container->setDefinition('bar', $bar); |
| 99 | + |
| 100 | + $baz = new Definition(); |
| 101 | + $baz->setProperty('cache', new Reference('cache.adapter.fs_adapter')); |
| 102 | + $container->setDefinition('baz', $baz); |
| 103 | + |
| 104 | + return $container; |
| 105 | + } |
| 106 | +} |
0 commit comments