DelegatingEngineTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Component\Templating\Tests;
  11. use Symfony\Component\Templating\DelegatingEngine;
  12. use Symfony\Component\Templating\StreamingEngineInterface;
  13. use Symfony\Component\Templating\EngineInterface;
  14. class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testRenderDelegatesToSupportedEngine()
  17. {
  18. $firstEngine = $this->getEngineMock('template.php', false);
  19. $secondEngine = $this->getEngineMock('template.php', true);
  20. $secondEngine->expects($this->once())
  21. ->method('render')
  22. ->with('template.php', array('foo' => 'bar'))
  23. ->will($this->returnValue('<html />'));
  24. $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
  25. $result = $delegatingEngine->render('template.php', array('foo' => 'bar'));
  26. $this->assertSame('<html />', $result);
  27. }
  28. /**
  29. * @expectedException \RuntimeException
  30. * @expectedExceptionMessage No engine is able to work with the template "template.php"
  31. */
  32. public function testRenderWithNoSupportedEngine()
  33. {
  34. $firstEngine = $this->getEngineMock('template.php', false);
  35. $secondEngine = $this->getEngineMock('template.php', false);
  36. $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
  37. $delegatingEngine->render('template.php', array('foo' => 'bar'));
  38. }
  39. public function testStreamDelegatesToSupportedEngine()
  40. {
  41. $streamingEngine = $this->getStreamingEngineMock('template.php', true);
  42. $streamingEngine->expects($this->once())
  43. ->method('stream')
  44. ->with('template.php', array('foo' => 'bar'))
  45. ->will($this->returnValue('<html />'));
  46. $delegatingEngine = new DelegatingEngine(array($streamingEngine));
  47. $result = $delegatingEngine->stream('template.php', array('foo' => 'bar'));
  48. $this->assertNull($result);
  49. }
  50. /**
  51. * @expectedException \LogicException
  52. * @expectedExceptionMessage Template "template.php" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface
  53. */
  54. public function testStreamRequiresStreamingEngine()
  55. {
  56. $delegatingEngine = new DelegatingEngine(array(new TestEngine()));
  57. $delegatingEngine->stream('template.php', array('foo' => 'bar'));
  58. }
  59. public function testExists()
  60. {
  61. $engine = $this->getEngineMock('template.php', true);
  62. $engine->expects($this->once())
  63. ->method('exists')
  64. ->with('template.php')
  65. ->will($this->returnValue(true));
  66. $delegatingEngine = new DelegatingEngine(array($engine));
  67. $this->assertTrue($delegatingEngine->exists('template.php'));
  68. }
  69. public function testSupports()
  70. {
  71. $engine = $this->getEngineMock('template.php', true);
  72. $delegatingEngine = new DelegatingEngine(array($engine));
  73. $this->assertTrue($delegatingEngine->supports('template.php'));
  74. }
  75. public function testSupportsWithNoSupportedEngine()
  76. {
  77. $engine = $this->getEngineMock('template.php', false);
  78. $delegatingEngine = new DelegatingEngine(array($engine));
  79. $this->assertFalse($delegatingEngine->supports('template.php'));
  80. }
  81. public function testGetExistingEngine()
  82. {
  83. $firstEngine = $this->getEngineMock('template.php', false);
  84. $secondEngine = $this->getEngineMock('template.php', true);
  85. $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
  86. $this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php'));
  87. }
  88. /**
  89. * @expectedException \RuntimeException
  90. * @expectedExceptionMessage No engine is able to work with the template "template.php"
  91. */
  92. public function testGetInvalidEngine()
  93. {
  94. $firstEngine = $this->getEngineMock('template.php', false);
  95. $secondEngine = $this->getEngineMock('template.php', false);
  96. $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
  97. $delegatingEngine->getEngine('template.php');
  98. }
  99. private function getEngineMock($template, $supports)
  100. {
  101. $engine = $this->getMock('Symfony\Component\Templating\EngineInterface');
  102. $engine->expects($this->once())
  103. ->method('supports')
  104. ->with($template)
  105. ->will($this->returnValue($supports));
  106. return $engine;
  107. }
  108. private function getStreamingEngineMock($template, $supports)
  109. {
  110. $engine = $this->getMockForAbstractClass('Symfony\Component\Templating\Tests\MyStreamingEngine');
  111. $engine->expects($this->once())
  112. ->method('supports')
  113. ->with($template)
  114. ->will($this->returnValue($supports));
  115. return $engine;
  116. }
  117. }
  118. interface MyStreamingEngine extends StreamingEngineInterface, EngineInterface
  119. {
  120. }
  121. class TestEngine implements EngineInterface
  122. {
  123. public function render($name, array $parameters = array())
  124. {
  125. }
  126. public function exists($name)
  127. {
  128. }
  129. public function supports($name)
  130. {
  131. return true;
  132. }
  133. public function stream()
  134. {
  135. }
  136. }