Skip to content

Commit 6c31ab2

Browse files
committed
[Templating] Added tests for the DelegatingEngine.
1 parent 33469ea commit 6c31ab2

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
private function getEngineMock($template, $supports)
108+
{
109+
$engine = $this->getMock('Symfony\Component\Templating\EngineInterface');
110+
111+
$engine->expects($this->once())
112+
->method('supports')
113+
->with($template)
114+
->will($this->returnValue($supports));
115+
116+
return $engine;
117+
}
118+
119+
private function getStreamingEngineMock($template, $supports)
120+
{
121+
$engine = $this->getMockForAbstractClass('Symfony\Component\Templating\Tests\MyStreamingEngine');
122+
123+
$engine->expects($this->once())
124+
->method('supports')
125+
->with($template)
126+
->will($this->returnValue($supports));
127+
128+
return $engine;
129+
}
130+
}
131+
132+
interface MyStreamingEngine extends StreamingEngineInterface, EngineInterface
133+
{
134+
}

0 commit comments

Comments
 (0)