8000 merged branch jakzal/templating-delegating-engine (PR #8280) · symfony/symfony@d744ffa · GitHub
[go: up one dir, main page]

Skip to content

Commit d744ffa

Browse files
committed
merged branch jakzal/templating-delegating-engine (PR #8280)
This PR was merged into the master branch. Discussion ---------- [Templating|FrameworkBundle] Made DelegatingEngine::getEngine() public | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7254 | License | MIT | Doc PR | - As a bonus I've covered both classes with tests. Commits ------- a54cbff [FrameworkBundle] Made DelegatingEngine::getEngine() public. 3f84cd3 [Templating] Made DelegatingEngine::getEngine() public. 0a72a99 [FrameworkBundle] Added tests for the DelegatingEngine. 6c31ab2 [Templating] Added tests for the DelegatingEngine.
2 parents ded2984 + a54cbff commit d744ffa

File tree

4 files changed

+273
-2
lines changed

4 files changed

+273
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function supports($name)
5858
/**
5959
* {@inheritdoc}
6060
*/
61-
protected function getEngine($name)
61+
public function getEngine($name)
6262
{
6363
foreach ($this->engines as $i => $engine) {
6464
if (is_string($engine)) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\Bundle\FrameworkBundle\Tests\Templating;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
15+
16+
class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testSupportsRetrievesEngineFromTheContainer()
19+
{
20+
$container = $this->getContainerMock(array(
21+
'engine.first' => $this->getEngineMock('template.php', false),
22+
'engine.second' => $this->getEngineMock('template.php', true)
23+
));
24+
25+
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
26+
27+
$this->assertTrue($delegatingEngine->supports('template.php'));
28+
}
29+
30+
public function testGetExistingEngine()
31+
{
32+
$firstEngine = $this->getEngineMock('template.php', false);
33+
$secondEngine = $this->getEngineMock('template.php', true);
34+
$container = $this->getContainerMock(array(
35+
'engine.first' => $firstEngine,
36+
'engine.second' => $secondEngine
37+
));
38+
39+
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
40+
41+
$this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php', array('foo' => 'bar')));
42+
}
43+
44+
/**
45+
* @expectedException \RuntimeException
46+
* @expectedExceptionMessage No engine is able to work with the template "template.php"
47+
*/
48+
public function testGetInvalidEngine()
49+
{
50+
$firstEngine = $this->getEngineMock('template.php', false);
51+
$secondEngine = $this->getEngineMock('template.php', false);
52+
$container = $this->getContainerMock(array(
53+
'engine.first' => $firstEngine,
54+
'engine.second' => $secondEngine
55+
));
56+
57+
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
58+
$delegatingEngine->getEngine('template.php', array('foo' => 'bar'));
59+
}
60+
61+
public function testRenderResponse()
62+
{
63+
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
64+
$engine = $this->getFrameworkEngineMock('template.php', true);
65+
$engine->expects($this->once())
66+
->method('renderResponse')
67+
->with('template.php', array('foo' => 'bar'))
68+
->will($this->returnValue($response));
69+
$container = $this->getContainerMock(array('engine' => $engine));
70+
71+
$delegatingEngine = new DelegatingEngine($container, array('engine'));
72+
73+
$this->assertSame($response, $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
74+
}
75+
76+
private function getEngineMock($template, $supports)
77+
{
78+
$engine = $this->getMock('Symfony\Component\Templating\EngineInterface');
79+
80+
$engine->expects($this->once())
81+
->method('supports')
82+
->with($template)
83+
->will($this->returnValue($supports));
84+
85+
return $engine;
86+
}
87+
88+
private function getFrameworkEngineMock($template, $supports)
89+
{
90+
$engine = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
91+
92+
$engine->expects($this->once())
93+
->method('supports')
94+
->with($template)
95+
->will($this->returnValue($supports));
96+
97+
return $engine;
98+
}
99+
100+
private function getContainerMock($services)
101+
{
102+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
103+
104+
$i = 0;
105+
foreach ($services as $id => $service) {
106+
$container->expects($this->at($i++))
107+
->method('get')
108+
->with($id)
109+
->will($this->returnValue($service));
110+
}
111+
112+
return $container;
113+
}
114+
}

src/Symfony/Component/Templating/DelegatingEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function supports($name)
114114
*
115115
* @api
116116
*/
117-
protected function getEngine($name)
117+
public function getEngine($name)
118118
{
119119
foreach ($this->engines as $engine) {
120120
if ($engine->supports($name)) {
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)
0