|
| 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\TwigBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Definition; |
| 18 | +use Symfony\Component\DependencyInjection\Reference; |
| 19 | + |
| 20 | +class TwigEnvironmentPassTest extends TestCase |
| 21 | +{ |
| 22 | + public function testTwigBridgeExtensionsAreRegisteredFirst() |
| 23 | + { |
| 24 | + $twigDefinition = new Definition('twig'); |
| 25 | + |
| 26 | + $containerBuilderMock = $this->getMockBuilder(ContainerBuilder::class) |
| 27 | + ->setMethods(array('hasDefinition', 'get', 'findTaggedServiceIds', 'getDefinition')) |
| 28 | + ->getMock(); |
| 29 | + $containerBuilderMock |
| 30 | + ->expects($this->once()) |
| 31 | + ->method('hasDefinition') |
| 32 | + ->with('twig') |
| 33 | + ->will($this->returnValue(true)); |
| 34 | + $containerBuilderMock |
| 35 | + ->expects($this->once()) |
| 36 | + ->method('findTaggedServiceIds') |
| 37 | + ->with('twig.extension') |
| 38 | + ->will($this->returnValue(array( |
| 39 | + 'other_extension' => array( |
| 40 | + array() |
| 41 | + ), |
| 42 | + 'twig_bridge_extension' => array( |
| 43 | + array() |
| 44 | + ) |
| 45 | + ))); |
| 46 | + |
| 47 | + $otherExtensionDefinitionMock = $this->getMockBuilder(Definition::class) |
| 48 | + ->setMethods(array('getClass')) |
| 49 | + ->getMock(); |
| 50 | + $otherExtensionDefinitionMock |
| 51 | + ->expects($this->once()) |
| 52 | + ->method('getClass') |
| 53 | + ->will($this->returnValue('Foo\\Bar')); |
| 54 | + |
| 55 | + $twigExtensionDefinitionMock = $this->getMockBuilder(Definition::class) |
| 56 | + ->setMethods(array('getClass')) |
| 57 | + ->getMock(); |
| 58 | + $twigExtensionDefinitionMock |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('getClass') |
| 61 | + ->will($this->returnValue('Symfony\\Bridge\\Twig\\Extension\\Foo')); |
| 62 | + |
| 63 | + $containerBuilderMock |
| 64 | + ->expects($this->exactly(3)) |
| 65 | + ->method('getDefinition') |
| 66 | + ->withConsecutive(array('twig'), array('other_extension'), array('twig_bridge_extension')) |
| 67 | + ->willReturnOnConsecutiveCalls( |
| 68 | + $this->returnValue($twigDefinition), |
| 69 | + $this->returnValue($otherExtensionDefinitionMock), |
| 70 | + $this->returnValue($twigExtensionDefinitionMock) |
| 71 | + ); |
| 72 | + |
| 73 | + $twigEnvironmentPass = new TwigEnvironmentPass(); |
| 74 | + $twigEnvironmentPass->process($containerBuilderMock); |
| 75 | + |
| 76 | + $methodCalls = $twigDefinition->getMethodCalls(); |
| 77 | + $this->assertCount(2, $methodCalls); |
| 78 | + |
| 79 | + $twigBridgeExtensionReference = $methodCalls[0][1][0]; |
| 80 | + $this->assertInstanceOf(Reference::class, $twigBridgeExtensionReference); |
| 81 | + /* @var Reference $twigBridgeExtensionReference */ |
| 82 | + $this->assertEquals('twig_bridge_extension', $twigBridgeExtensionReference->__toString()); |
| 83 | + |
| 84 | + $otherExtensionReference = $methodCalls[1][1][0]; |
| 85 | + $this->assertInstanceOf(Reference::class, $otherExtensionReference); |
| 86 | + /* @var Reference $otherExtensionReference */ |
| 87 | + $this->assertEquals('other_extension', $otherExtensionReference->__toString()); |
| 88 | + } |
| 89 | +} |
0 commit comments