From 6c31ab2581bebab6062d206b64a1a3500eddffe7 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Fri, 14 Jun 2013 13:34:01 +0100 Subject: [PATCH] [Templating] Added tests for the DelegatingEngine. --- .../Templating/Tests/DelegatingEngineTest.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php diff --git a/src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php b/src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php new file mode 100644 index 0000000000..1c9e45faac --- /dev/null +++ b/src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php @@ -0,0 +1,134 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Templating\Tests; + +use Symfony\Component\Templating\DelegatingEngine; +use Symfony\Component\Templating\StreamingEngineInterface; +use Symfony\Component\Templating\EngineInterface; + +class DelegatingEngineTest extends \PHPUnit_Framework_TestCase +{ + public function testRenderDelegatesToSupportedEngine() + { + $firstEngine = $this->getEngineMock('template.php', false); + $secondEngine = $this->getEngineMock('template.php', true); + + $secondEngine->expects($this->once()) + ->method('render') + ->with('template.php', array('foo' => 'bar')) + ->will($this->returnValue('')); + + $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine)); + $result = $delegatingEngine->render('template.php', array('foo' => 'bar')); + + $this->assertSame('', $result); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage No engine is able to work with the template "template.php" + */ + public function testRenderWithNoSupportedEngine() + { + $firstEngine = $this->getEngineMock('template.php', false); + $secondEngine = $this->getEngineMock('template.php', false); + + $delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine)); + $delegatingEngine->render('template.php', array('foo' => 'bar')); + } + + public function testStreamDelegatesToSupportedEngine() + { + $streamingEngine = $this->getStreamingEngineMock('template.php', true); + $streamingEngine->expects($this->once()) + ->method('stream') + ->with('template.php', array('foo' => 'bar')) + ->will($this->returnValue('')); + + $delegatingEngine = new DelegatingEngine(array($streamingEngine)); + $result = $delegatingEngine->stream('template.php', array('foo' => 'bar')); + + $this->assertNull($result); + } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage Template "template.php" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface + */ + public function testStreamRequiresStreamingEngine() + { + $engine = $this->getEngineMock('template.php', true); + $engine->expects($this->never())->method('stream'); + + $delegatingEngine = new DelegatingEngine(array($engine)); + $delegatingEngine->stream('template.php', array('foo' => 'bar')); + } + + public function testExists() + { + $engine = $this->getEngineMock('template.php', true); + $engine->expects($this->once()) + ->method('exists') + ->with('template.php') + ->will($this->returnValue(true)); + + $delegatingEngine = new DelegatingEngine(array($engine)); + + $this->assertTrue($delegatingEngine->exists('template.php')); + } + + public function testSupports() + { + $engine = $this->getEngineMock('template.php', true); + + $delegatingEngine = new DelegatingEngine(array($engine)); + + $this->assertTrue($delegatingEngine->supports('template.php')); + } + + public function testSupportsWithNoSupportedEngine() + { + $engine = $this->getEngineMock('template.php', false); + + $delegatingEngine = new DelegatingEngine(array($engine)); + + $this->assertFalse($delegatingEngine->supports('template.php')); + } + + private function getEngineMock($template, $supports) + { + $engine = $this->getMock('Symfony\Component\Templating\EngineInterface'); + + $engine->expects($this->once()) + ->method('supports') + ->with($template) + ->will($this->returnValue($supports)); + + return $engine; + } + + private function getStreamingEngineMock($template, $supports) + { + $engine = $this->getMockForAbstractClass('Symfony\Component\Templating\Tests\MyStreamingEngine'); + + $engine->expects($this->once()) + ->method('supports') + ->with($template) + ->will($this->returnValue($supports)); + + return $engine; + } +} + +interface MyStreamingEngine extends StreamingEngineInterface, EngineInterface +{ +}