Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
oxcom committed Dec 4, 2019
2 parents cf678c4 + 026824d commit ae98593
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 44 deletions.
34 changes: 14 additions & 20 deletions src/Renderer/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
use Zend\View\Renderer\TreeRendererInterface;
use Zend\View\Resolver\ResolverInterface;
use Zend\View\View;
use ZendTwig\View\TwigModel;

class TwigRenderer implements RendererInterface, TreeRendererInterface
{
/**
* @var bool
*/
protected $canRenderTrees = true;

/**
* @var Environment
*/
Expand Down Expand Up @@ -189,7 +185,7 @@ public function render($nameOrModel, $values = null) : string
));
}

if ($model instanceof ModelInterface && $model->hasChildren() && $this->canRenderTrees()) {
if ($model instanceof ModelInterface && $model->hasChildren() && $this->canRenderTrees($model)) {
if (!isset($values[$model->captureTo()])) {
$values[$model->captureTo()] = '';
}
Expand All @@ -199,7 +195,11 @@ public function render($nameOrModel, $values = null) : string
* @var \Zend\View\Model\ViewModel $child
* @var \Twig_Template $template
*/
$result = $this->render($child, $values);
try {
$result = $this->render($child, $values);
} catch (RuntimeException $e) {
continue;
}

if ($this->isForceStandalone() || $child->terminate()) {
return $result;
Expand Down Expand Up @@ -237,23 +237,17 @@ public function canRender($name)
/**
* Indicate whether the renderer is capable of rendering trees of view models
*
* @return bool
*/
public function canRenderTrees()
{
return $this->canRenderTrees;
}

/**
* @param boolean $canRenderTrees
* @param mixed $model
*
* @return TwigRenderer
* @return bool
*/
public function setCanRenderTrees($canRenderTrees)
public function canRenderTrees($model = null)
{
$this->canRenderTrees = !!$canRenderTrees;
if (!empty($model) && $model instanceof TwigModel) {
return true;
}

return $this;
return false;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/View/TwigStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Zend\EventManager\ListenerAggregateTrait;
use Zend\View\Renderer\RendererInterface;
use Zend\View\ViewEvent;
use ZendTwig\Renderer\TwigRenderer;

class TwigStrategy implements ListenerAggregateInterface
{
Expand Down Expand Up @@ -65,6 +66,16 @@ public function selectRender(ViewEvent $e)
return $this->renderer;
}

if ($this->renderer instanceof TwigRenderer) {
try {
$tpl = $this->renderer->getResolver()->resolve($model->getTemplate());
if ($tpl instanceof \Twig\Template) {
return $this->renderer;
}
} catch (\Throwable $e) {
}
}

return null;
}

Expand Down
50 changes: 27 additions & 23 deletions tests/ZendTwig/Renderer/TwigRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\View\Http\DefaultRenderingStrategy;
use Zend\View\Model\ViewModel;
use ZendTwig\Test\Bootstrap;
use ZendTwig\Renderer\TwigRenderer;
use ZendTwig\View\TwigModel;

class TwigRendererTest extends TestCase
{
Expand Down Expand Up @@ -111,13 +115,13 @@ public function testSetGetCanRenderTrees()
*/
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);
$canRenderOld = $render->canRenderTrees();

$render->setCanRenderTrees(!$canRenderOld);
$this->assertNotEquals($canRenderOld, $render->canRenderTrees());
$model = new ViewModel();
$twigModel = new TwigModel();

$render->setCanRenderTrees($canRenderOld);
$this->assertEquals($canRenderOld, $render->canRenderTrees());
$this->assertFalse($render->canRenderTrees());
$this->assertFalse($render->canRenderTrees('Element?'));
$this->assertTrue($render->canRenderTrees($twigModel));
}

/**
Expand All @@ -132,7 +136,7 @@ public function testRenderModelExNotTemplate()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$model = new \Zend\View\Model\ViewModel([
$model = new ViewModel([
'key1' => 'value1',
'key2' => 'value2',
]);
Expand All @@ -150,7 +154,7 @@ public function testRenderModelOptions()
$view = $sm->get('View');
$viewClone = clone $view;

$model = new \Zend\View\Model\ViewModel([
$model = new ViewModel([
'key1' => 'value1',
'key2' => 'value2',
]);
Expand Down Expand Up @@ -212,22 +216,22 @@ public function testRenderChild()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel([
$modelParent = new TwigModel([
'key1' => 'value1',
]);
$modelParent->setTemplate('View/testRenderChild');

$modelChild1 = new \Zend\View\Model\ViewModel([
$modelChild1 = new TwigModel([
'key1' => 'child1-1',
'key2' => 'child1-2',
]);

$modelChild2 = new \Zend\View\Model\ViewModel([
$modelChild2 = new TwigModel([
'key1' => 'child2-1',
'key2' => 'child2-2',
]);

$modelChild3 = new \Zend\View\Model\ViewModel([
$modelChild3 = new TwigModel([
'key1' => 'child3-1',
'key2' => 'child3-2',
]);
Expand Down Expand Up @@ -262,22 +266,22 @@ public function testRenderChildNoAppend()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel([
$modelParent = new TwigModel([
'key1' => 'value1',
]);
$modelParent->setTemplate('View/testRenderChild');

$modelChild1 = new \Zend\View\Model\ViewModel([
$modelChild1 = new TwigModel([
'key1' => 'child1-1',
'key2' => 'child1-2',
]);

$modelChild2 = new \Zend\View\Model\ViewModel([
$modelChild2 = new TwigModel([
'key1' => 'child2-1',
'key2' => 'child2-2',
]);

$modelChild3 = new \Zend\View\Model\ViewModel([
$modelChild3 = new TwigModel([
'key1' => 'child3-1',
'key2' => 'child3-2',
]);
Expand Down Expand Up @@ -324,11 +328,11 @@ public function testForceStandaloneModel()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel([
$modelParent = new TwigModel([
]);
$modelParent->setTemplate('View/zend/layout');

$modelChild1 = new \Zend\View\Model\ViewModel([
$modelChild1 = new TwigModel([
'username' => 'Child007',
]);

Expand All @@ -350,11 +354,11 @@ public function testSimpleStandaloneModel()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel([
$modelParent = new TwigModel([
]);
$modelParent->setTemplate('View/zend/layout');

$modelChild1 = new \Zend\View\Model\ViewModel([
$modelChild1 = new TwigModel([
'username' => 'Child007',
]);

Expand All @@ -381,8 +385,8 @@ public function testIssue2()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel();
$modelChild1 = new \Zend\View\Model\ViewModel();
$modelParent = new TwigModel();
$modelChild1 = new TwigModel();

$modelParent->setTemplate('View/issue-2/layout');
$modelChild1->setTemplate('View/issue-2/index');
Expand All @@ -408,8 +412,8 @@ public function testIssue2Raw()
$sm = Bootstrap::getInstance()->getServiceManager();
$render = $sm->get(TwigRenderer::class);

$modelParent = new \Zend\View\Model\ViewModel();
$modelChild1 = new \Zend\View\Model\ViewModel();
$modelParent = new TwigModel();
$modelChild1 = new TwigModel();

$modelParent->setTemplate('View/issue-2/layout-raw');
$modelChild1->setTemplate('View/issue-2/index');
Expand Down
62 changes: 61 additions & 1 deletion tests/ZendTwig/View/TwigStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
namespace ZendTwig\Test\View;

use PHPUnit\Framework\TestCase;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\ViewEvent;
use ZendTwig\Renderer\TwigRenderer;
use ZendTwig\Test\Bootstrap;
use ZendTwig\View\TwigModel;
use ZendTwig\View\TwigStrategy;

class TwigStrategyTest extends TestCase
{
/**
* Check that correct render was selected
*/
public function testSelectRenderer()
public function testSelectRenderForce()
{
$model = $this->getMockBuilder('Zend\View\Model\ModelInterface')->getMock();
$model->method('getTemplate')
Expand All @@ -31,12 +34,69 @@ public function testSelectRenderer()
*/
$sm = Bootstrap::getInstance()->getServiceManager();
$strategy = $sm->get(TwigStrategy::class);
$strategy->setForceRender(true);

$renderA = $sm->get(TwigRenderer::class);
$renderB = $strategy->selectRender($event);

$this->assertSame($renderA, $renderB);
}

/**
* Check that correct render was selected
*
* @var \Zend\View\Model\ModelInterface $model
*
* @dataProvider generatorSelectRender
*/
public function testSelectRenderNoRender($model, $expected)
{
$event = new ViewEvent();
$event->setModel($model);

/**
* @var \ZendTwig\View\TwigStrategy $strategy
*/
$sm = Bootstrap::getInstance()->getServiceManager();
$strategy = $sm->get(TwigStrategy::class);
$strategy->setForceRender(false);
$render = $strategy->selectRender($event);

if (!empty($expected)) {
$expected = $sm->get($expected);
}

$this->assertSame($expected, $render);
}

/**
* @return array
*/
public function generatorSelectRender()
{
$viewModel = $this->getMockBuilder(ViewModel::class)->getMock();
$viewModel->method('getTemplate')
->will($this->returnValue('some-template-string'));

$jsonModel = $this->getMockBuilder(JsonModel::class)->getMock();
$jsonModel->method('getTemplate')
->will($this->returnValue('some-template-string'));

$twigModel = $this->getMockBuilder(TwigModel::class)->getMock();
$twigModel->method('getTemplate')
->will($this->returnValue('some-template-string'));

$viewModelTwig = new ViewModel();
$viewModelTwig->setTemplate('layout');

return [
[$viewModel, null],
[$jsonModel, null],
[$twigModel, TwigRenderer::class],
[$viewModelTwig, TwigRenderer::class],
];
}

/**
* Check that response was injected
*
Expand Down

0 comments on commit ae98593

Please sign in to comment.