From c71c924736aa99fa317f49dc26ddfc1e7f512dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Fri, 6 Feb 2015 14:09:52 +0100 Subject: [PATCH] Fix isRootTemplate --- CHANGELOG.md | 4 +++ src/View/Renderer/ResourceRenderer.php | 16 ++++++---- src/View/Strategy/ResourceStrategy.php | 9 ++++-- .../View/Strategy/ResourceStrategyTest.php | 32 +++++++++++++++++++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ca1523..471c10c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/View/Renderer/ResourceRenderer.php b/src/View/Renderer/ResourceRenderer.php index b9804d8..43475f1 100644 --- a/src/View/Renderer/ResourceRenderer.php +++ b/src/View/Renderer/ResourceRenderer.php @@ -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; } diff --git a/src/View/Strategy/ResourceStrategy.php b/src/View/Strategy/ResourceStrategy.php index 56a492c..deddfcd 100644 --- a/src/View/Strategy/ResourceStrategy.php +++ b/src/View/Strategy/ResourceStrategy.php @@ -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; } diff --git a/tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php b/tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php index eb06d0f..fb0d9fe 100644 --- a/tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php +++ b/tests/ZfrRestTest/View/Strategy/ResourceStrategyTest.php @@ -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; @@ -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)); + } }