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