Skip to content

Commit

Permalink
Fix isRootTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Feb 6, 2015
1 parent 21e6550 commit c71c924
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.4.3

* `isRootTemplate` now work even if you have called the `renderResource` helper inside a view

## 0.4.2

* Does not modify status code response if an exception is contained into the MvcEvent
Expand Down
16 changes: 10 additions & 6 deletions src/View/Renderer/ResourceRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,24 @@ public function isRootTemplate()
*/
public function render($nameOrModel, $values = null)
{
// We set the currently rendered view model into the viewModel helper. This allows to render additional
// properties in the view by comparing the root and nested view model
$this->viewModel()->setCurrent($nameOrModel);
/** @var \Zend\View\Helper\ViewModel $viewModelHelper */
$viewModelHelper = $this->helperPluginManager->get('viewModel');

// Because templates can be rendered recursively, we need to save the current context
$previousViewModel = $viewModelHelper->getCurrent();

$template = $this->resolver->resolve($nameOrModel->getTemplate());

// We need to save and restore the previous variables, because the same renderer can be used inside
// multiple contexts
$previousTemplateVariables = $this->templateVariables;
$this->templateVariables = $nameOrModel->getVariables();
$viewModelHelper->setCurrent($nameOrModel);
$this->templateVariables = $nameOrModel->getVariables();

$result = include $template;

$this->templateVariables = $previousTemplateVariables;
// Restore the previous context
$this->templateVariables = $previousViewModel->getVariables();
$viewModelHelper->setCurrent($previousViewModel);

return $result;
}
Expand Down
9 changes: 6 additions & 3 deletions src/View/Strategy/ResourceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ public function selectRenderer(ViewEvent $event)

// If we have a ResourceViewModel, we set it as the "root" view model in the view model helper. This allows
// to differentiate between a nested context or not, in the view
/** @var \Zend\View\Helper\ViewModel $viewModel */
$viewModel = $this->renderer->viewModel();
$viewModel->setRoot($event->getModel());
/** @var \Zend\View\Helper\ViewModel $viewModelHelper */
$viewModelHelper = $helperPluginManager->get('viewModel');
$viewModel = $event->getModel();

$viewModelHelper->setRoot($viewModel);
$viewModelHelper->setCurrent($viewModel);

return $this->renderer;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Zend\EventManager\SharedEventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\DispatchableInterface;
use Zend\View\Helper\ViewModel;
use Zend\View\HelperPluginManager;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ModelInterface;
use Zend\View\ViewEvent;
Expand Down Expand Up @@ -81,4 +83,34 @@ public function testDoNotSetTemplateIfNotResourceViewModel()

$this->resourceStrategy->setTemplate($mvcEvent);
}

public function testDoNotSelectRenderIfNotResourceViewModel()
{
$model = $this->getMock(ModelInterface::class);

$viewEvent = new ViewEvent();
$viewEvent->setModel($model);

$this->assertNull($this->resourceStrategy->selectRenderer($viewEvent));
}

public function testProperlyFillViewModelHelperIfRendererIsSelected()
{
$model = new ResourceViewModel();

$viewEvent = new ViewEvent();
$viewEvent->setModel($model);

$viewModelHelper = $this->getMock(ViewModel::class, [], [], '', false);
$viewModelHelper->expects($this->once())->method('setRoot')->with($model);
$viewModelHelper->expects($this->once())->method('setCurrent')->with($model);

$helperManager = $this->getMock(HelperPluginManager::class, [], [], '', false);
$helperManager->expects($this->once())->method('setRenderer')->with($this->resourceRenderer);
$helperManager->expects($this->once())->method('get')->with('viewModel')->willReturn($viewModelHelper);

$this->resourceRenderer->expects($this->once())->method('getHelperPluginManager')->willReturn($helperManager);

$this->assertSame($this->resourceRenderer, $this->resourceStrategy->selectRenderer($viewEvent));
}
}

0 comments on commit c71c924

Please sign in to comment.