123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
- use Symfony\Component\DependencyInjection\Definition;
- use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;
- class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $builder;
- /**
- * @var Definition
- */
- private $chainLoader;
- /**
- * @var TwigLoaderPass
- */
- private $pass;
- protected function setUp()
- {
- $this->builder = $this->getMock(
- 'Symfony\Component\DependencyInjection\ContainerBuilder',
- array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition')
- );
- $this->chainLoader = new Definition('loader');
- $this->pass = new TwigLoaderPass();
- }
- public function testMapperPassWithOneTaggedLoaders()
- {
- $serviceIds = array(
- 'test_loader_1' => array(
- array(),
- ),
- );
- $this->builder->expects($this->once())
- ->method('hasDefinition')
- ->with('twig')
- ->will($this->returnValue(true));
- $this->builder->expects($this->once())
- ->method('findTaggedServiceIds')
- ->with('twig.loader')
- ->will($this->returnValue($serviceIds));
- $this->builder->expects($this->once())
- ->method('setAlias')
- ->with('twig.loader', 'test_loader_1');
- $this->pass->process($this->builder);
- }
- public function testMapperPassWithTwoTaggedLoaders()
- {
- $serviceIds = array(
- 'test_loader_1' => array(
- array(),
- ),
- 'test_loader_2' => array(
- array(),
- ),
- );
- $this->builder->expects($this->once())
- ->method('hasDefinition')
- ->with('twig')
- ->will($this->returnValue(true));
- $this->builder->expects($this->once())
- ->method('findTaggedServiceIds')
- ->with('twig.loader')
- ->will($this->returnValue($serviceIds));
- $this->builder->expects($this->once())
- ->method('getDefinition')
- ->with('twig.loader.chain')
- ->will($this->returnValue($this->chainLoader));
- $this->builder->expects($this->once())
- ->method('setAlias')
- ->with('twig.loader', 'twig.loader.chain');
- $this->pass->process($this->builder);
- $calls = $this->chainLoader->getMethodCalls();
- $this->assertCount(2, $calls);
- $this->assertEquals('addLoader', $calls[0][0]);
- $this->assertEquals('addLoader', $calls[1][0]);
- $this->assertEquals('test_loader_1', (string) $calls[0][1][0]);
- $this->assertEquals('test_loader_2', (string) $calls[1][1][0]);
- }
- public function testMapperPassWithTwoTaggedLoadersWithPriority()
- {
- $serviceIds = array(
- 'test_loader_1' => array(
- array('priority' => 100),
- ),
- 'test_loader_2' => array(
- array('priority' => 200),
- ),
- );
- $this->builder->expects($this->once())
- ->method('hasDefinition')
- ->with('twig')
- ->will($this->returnValue(true));
- $this->builder->expects($this->once())
- ->method('findTaggedServiceIds')
- ->with('twig.loader')
- ->will($this->returnValue($serviceIds));
- $this->builder->expects($this->once())
- ->method('getDefinition')
- ->with('twig.loader.chain')
- ->will($this->returnValue($this->chainLoader));
- $this->builder->expects($this->once())
- ->method('setAlias')
- ->with('twig.loader', 'twig.loader.chain');
- $this->pass->process($this->builder);
- $calls = $this->chainLoader->getMethodCalls();
- $this->assertCount(2, $calls);
- $this->assertEquals('addLoader', $calls[0][0]);
- $this->assertEquals('addLoader', $calls[1][0]);
- $this->assertEquals('test_loader_2', (string) $calls[0][1][0]);
- $this->assertEquals('test_loader_1', (string) $calls[1][1][0]);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
- */
- public function testMapperPassWithZeroTaggedLoaders()
- {
- $this->builder->expects($this->once())
- ->method('hasDefinition')
- ->with('twig')
- ->will($this->returnValue(true));
- $this->builder->expects($this->once())
- ->method('findTaggedServiceIds')
- ->with('twig.loader')
- ->will($this->returnValue(array()));
- $this->pass->process($this->builder);
- }
- }
|