DelegatingEngineTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\FrameworkBundle\Tests\Templating;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class DelegatingEngineTest extends TestCase
  15. {
  16. public function testSupportsRetrievesEngineFromTheContainer()
  17. {
  18. $container = $this->getContainerMock(array(
  19. 'engine.first' => $this->getEngineMock('template.php', false),
  20. 'engine.second' => $this->getEngineMock('template.php', true),
  21. ));
  22. $delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
  23. $this->assertTrue($delegatingEngine->supports('template.php'));
  24. }
  25. public function testGetExistingEngine()
  26. {
  27. $firstEngine = $this->getEngineMock('template.php', false);
  28. $secondEngine = $this->getEngineMock('template.php', true);
  29. $container = $this->getContainerMock(array(
  30. 'engine.first' => $firstEngine,
  31. 'engine.second' => $secondEngine,
  32. ));
  33. $delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
  34. $this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php'));
  35. }
  36. /**
  37. * @expectedException \RuntimeException
  38. * @expectedExceptionMessage No engine is able to work with the template "template.php"
  39. */
  40. public function testGetInvalidEngine()
  41. {
  42. $firstEngine = $this->getEngineMock('template.php', false);
  43. $secondEngine = $this->getEngineMock('template.php', false);
  44. $container = $this->getContainerMock(array(
  45. 'engine.first' => $firstEngine,
  46. 'engine.second' => $secondEngine,
  47. ));
  48. $delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
  49. $delegatingEngine->getEngine('template.php');
  50. }
  51. public function testRenderResponseWithFrameworkEngine()
  52. {
  53. $response = new Response();
  54. $engine = $this->getFrameworkEngineMock('template.php', true);
  55. $engine->expects($this->once())
  56. ->method('renderResponse')
  57. ->with('template.php', array('foo' => 'bar'))
  58. ->will($this->returnValue($response));
  59. $container = $this->getContainerMock(array('engine' => $engine));
  60. $delegatingEngine = new DelegatingEngine($container, array('engine'));
  61. $this->assertSame($response, $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
  62. }
  63. public function testRenderResponseWithTemplatingEngine()
  64. {
  65. $engine = $this->getEngineMock('template.php', true);
  66. $container = $this->getContainerMock(array('engine' => $engine));
  67. $delegatingEngine = new DelegatingEngine($container, array('engine'));
  68. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
  69. }
  70. private function getEngineMock($template, $supports)
  71. {
  72. $engine = $this->getMockBuilder('Symfony\Component\Templating\EngineInterface')->getMock();
  73. $engine->expects($this->once())
  74. ->method('supports')
  75. ->with($template)
  76. ->will($this->returnValue($supports));
  77. return $engine;
  78. }
  79. private function getFrameworkEngineMock($template, $supports)
  80. {
  81. $engine = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
  82. $engine->expects($this->once())
  83. ->method('supports')
  84. ->with($template)
  85. ->will($this->returnValue($supports));
  86. return $engine;
  87. }
  88. private function getContainerMock($services)
  89. {
  90. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  91. $i = 0;
  92. foreach ($services as $id => $service) {
  93. $container->expects($this->at($i++))
  94. ->method('get')
  95. ->with($id)
  96. ->will($this->returnValue($service));
  97. }
  98. return $container;
  99. }
  100. }