TwigLoaderPassTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;
  13. class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $builder;
  19. /**
  20. * @var Definition
  21. */
  22. private $chainLoader;
  23. /**
  24. * @var TwigLoaderPass
  25. */
  26. private $pass;
  27. protected function setUp()
  28. {
  29. $this->builder = $this->getMock(
  30. 'Symfony\Component\DependencyInjection\ContainerBuilder',
  31. array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition')
  32. );
  33. $this->chainLoader = new Definition('loader');
  34. $this->pass = new TwigLoaderPass();
  35. }
  36. public function testMapperPassWithOneTaggedLoaders()
  37. {
  38. $serviceIds = array(
  39. 'test_loader_1' => array(
  40. array(),
  41. ),
  42. );
  43. $this->builder->expects($this->once())
  44. ->method('hasDefinition')
  45. ->with('twig')
  46. ->will($this->returnValue(true));
  47. $this->builder->expects($this->once())
  48. ->method('findTaggedServiceIds')
  49. ->with('twig.loader')
  50. ->will($this->returnValue($serviceIds));
  51. $this->builder->expects($this->once())
  52. ->method('setAlias')
  53. ->with('twig.loader', 'test_loader_1');
  54. $this->pass->process($this->builder);
  55. }
  56. public function testMapperPassWithTwoTaggedLoaders()
  57. {
  58. $serviceIds = array(
  59. 'test_loader_1' => array(
  60. array(),
  61. ),
  62. 'test_loader_2' => array(
  63. array(),
  64. ),
  65. );
  66. $this->builder->expects($this->once())
  67. ->method('hasDefinition')
  68. ->with('twig')
  69. ->will($this->returnValue(true));
  70. $this->builder->expects($this->once())
  71. ->method('findTaggedServiceIds')
  72. ->with('twig.loader')
  73. ->will($this->returnValue($serviceIds));
  74. $this->builder->expects($this->once())
  75. ->method('getDefinition')
  76. ->with('twig.loader.chain')
  77. ->will($this->returnValue($this->chainLoader));
  78. $this->builder->expects($this->once())
  79. ->method('setAlias')
  80. ->with('twig.loader', 'twig.loader.chain');
  81. $this->pass->process($this->builder);
  82. $calls = $this->chainLoader->getMethodCalls();
  83. $this->assertCount(2, $calls);
  84. $this->assertEquals('addLoader', $calls[0][0]);
  85. $this->assertEquals('addLoader', $calls[1][0]);
  86. $this->assertEquals('test_loader_1', (string) $calls[0][1][0]);
  87. $this->assertEquals('test_loader_2', (string) $calls[1][1][0]);
  88. }
  89. public function testMapperPassWithTwoTaggedLoadersWithPriority()
  90. {
  91. $serviceIds = array(
  92. 'test_loader_1' => array(
  93. array('priority' => 100),
  94. ),
  95. 'test_loader_2' => array(
  96. array('priority' => 200),
  97. ),
  98. );
  99. $this->builder->expects($this->once())
  100. ->method('hasDefinition')
  101. ->with('twig')
  102. ->will($this->returnValue(true));
  103. $this->builder->expects($this->once())
  104. ->method('findTaggedServiceIds')
  105. ->with('twig.loader')
  106. ->will($this->returnValue($serviceIds));
  107. $this->builder->expects($this->once())
  108. ->method('getDefinition')
  109. ->with('twig.loader.chain')
  110. ->will($this->returnValue($this->chainLoader));
  111. $this->builder->expects($this->once())
  112. ->method('setAlias')
  113. ->with('twig.loader', 'twig.loader.chain');
  114. $this->pass->process($this->builder);
  115. $calls = $this->chainLoader->getMethodCalls();
  116. $this->assertCount(2, $calls);
  117. $this->assertEquals('addLoader', $calls[0][0]);
  118. $this->assertEquals('addLoader', $calls[1][0]);
  119. $this->assertEquals('test_loader_2', (string) $calls[0][1][0]);
  120. $this->assertEquals('test_loader_1', (string) $calls[1][1][0]);
  121. }
  122. /**
  123. * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
  124. */
  125. public function testMapperPassWithZeroTaggedLoaders()
  126. {
  127. $this->builder->expects($this->once())
  128. ->method('hasDefinition')
  129. ->with('twig')
  130. ->will($this->returnValue(true));
  131. $this->builder->expects($this->once())
  132. ->method('findTaggedServiceIds')
  133. ->with('twig.loader')
  134. ->will($this->returnValue(array()));
  135. $this->pass->process($this->builder);
  136. }
  137. }