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.
This commit is contained in:
commit
d744ffaaeb
@ -58,7 +58,7 @@ class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function getEngine($name)
|
public function getEngine($name)
|
||||||
{
|
{
|
||||||
foreach ($this->engines as $i => $engine) {
|
foreach ($this->engines as $i => $engine) {
|
||||||
if (is_string($engine)) {
|
if (is_string($engine)) {
|
||||||
|
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
|
||||||
|
|
||||||
|
class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testSupportsRetrievesEngineFromTheContainer()
|
||||||
|
{
|
||||||
|
$container = $this->getContainerMock(array(
|
||||||
|
'engine.first' => $this->getEngineMock('template.php', false),
|
||||||
|
'engine.second' => $this->getEngineMock('template.php', true)
|
||||||
|
));
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
|
||||||
|
|
||||||
|
$this->assertTrue($delegatingEngine->supports('template.php'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetExistingEngine()
|
||||||
|
{
|
||||||
|
$firstEngine = $this->getEngineMock('template.php', false);
|
||||||
|
$secondEngine = $this->getEngineMock('template.php', true);
|
||||||
|
$container = $this->getContainerMock(array(
|
||||||
|
'engine.first' => $firstEngine,
|
||||||
|
'engine.second' => $secondEngine
|
||||||
|
));
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
|
||||||
|
|
||||||
|
$this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php', array('foo' => 'bar')));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
* @expectedExceptionMessage No engine is able to work with the template "template.php"
|
||||||
|
*/
|
||||||
|
public function testGetInvalidEngine()
|
||||||
|
{
|
||||||
|
$firstEngine = $this->getEngineMock('template.php', false);
|
||||||
|
$secondEngine = $this->getEngineMock('template.php', false);
|
||||||
|
$container = $this->getContainerMock(array(
|
||||||
|
'engine.first' => $firstEngine,
|
||||||
|
'engine.second' => $secondEngine
|
||||||
|
));
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine($container, array('engine.first', 'engine.second'));
|
||||||
|
$delegatingEngine->getEngine('template.php', array('foo' => 'bar'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRenderResponse()
|
||||||
|
{
|
||||||
|
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
|
||||||
|
$engine = $this->getFrameworkEngineMock('template.php', true);
|
||||||
|
$engine->expects($this->once())
|
||||||
|
->method('renderResponse')
|
||||||
|
->with('template.php', array('foo' => 'bar'))
|
||||||
|
->will($this->returnValue($response));
|
||||||
|
$container = $this->getContainerMock(array('engine' => $engine));
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine($container, array('engine'));
|
||||||
|
|
||||||
|
$this->assertSame($response, $delegatingEngine->renderResponse('template.php', array('foo' => 'bar')));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 getFrameworkEngineMock($template, $supports)
|
||||||
|
{
|
||||||
|
$engine = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
|
||||||
|
|
||||||
|
$engine->expects($this->once())
|
||||||
|
->method('supports')
|
||||||
|
->with($template)
|
||||||
|
->will($this->returnValue($supports));
|
||||||
|
|
||||||
|
return $engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getContainerMock($services)
|
||||||
|
{
|
||||||
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
foreach ($services as $id => $service) {
|
||||||
|
$container->expects($this->at($i++))
|
||||||
|
->method('get')
|
||||||
|
->with($id)
|
||||||
|
->will($this->returnValue($service));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
}
|
@ -114,7 +114,7 @@ class DelegatingEngine implements EngineInterface, StreamingEngineInterface
|
|||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
protected function getEngine($name)
|
public function getEngine($name)
|
||||||
{
|
{
|
||||||
foreach ($this->engines as $engine) {
|
foreach ($this->engines as $engine) {
|
||||||
if ($engine->supports($name)) {
|
if ($engine->supports($name)) {
|
||||||
|
157
src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php
Normal file
157
src/Symfony/Component/Templating/Tests/DelegatingEngineTest.php
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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('<html />'));
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
|
||||||
|
$result = $delegatingEngine->render('template.php', array('foo' => 'bar'));
|
||||||
|
|
||||||
|
$this->assertSame('<html />', $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('<html />'));
|
||||||
|
|
||||||
|
$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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetExistingEngine()
|
||||||
|
{
|
||||||
|
$firstEngine = $this->getEngineMock('template.php', false);
|
||||||
|
$secondEngine = $this->getEngineMock('template.php', true);
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
|
||||||
|
|
||||||
|
$this->assertSame($secondEngine, $delegatingEngine->getEngine('template.php', array('foo' => 'bar')));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
* @expectedExceptionMessage No engine is able to work with the template "template.php"
|
||||||
|
*/
|
||||||
|
public function testGetInvalidEngine()
|
||||||
|
{
|
||||||
|
$firstEngine = $this->getEngineMock('template.php', false);
|
||||||
|
$secondEngine = $this->getEngineMock('template.php', false);
|
||||||
|
|
||||||
|
$delegatingEngine = new DelegatingEngine(array($firstEngine, $secondEngine));
|
||||||
|
$delegatingEngine->getEngine('template.php', array('foo' => 'bar'));
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
}
|
Reference in New Issue
Block a user