Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4164'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 12, 2013
2 parents b3b0beb + b0afc23 commit 1380626
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/View.php
Expand Up @@ -184,6 +184,10 @@ public function render(Model $model)
$event->setRenderer($renderer);
$results = $events->trigger(ViewEvent::EVENT_RENDERER_POST, $event);

// If EVENT_RENDERER or EVENT_RENDERER_POST changed the model, make sure
// we use this new model instead of the current $model
$model = $event->getModel();

// If we have children, render them first, but only if:
// a) the renderer does not implement TreeRendererInterface, or
// b) it does, but canRenderTrees() returns false
Expand Down
28 changes: 28 additions & 0 deletions test/ViewTest.php
Expand Up @@ -22,6 +22,7 @@
use Zend\View\Resolver;
use Zend\View\Variables as ViewVariables;
use Zend\View\View;
use Zend\View\ViewEvent;

class ViewTest extends TestCase
{
Expand Down Expand Up @@ -306,4 +307,31 @@ public function testCanTriggerPostRendererEvent()
$this->view->render($this->model);
$this->assertTrue($test->flag);
}

/**
* Test the view model can be swapped out
*
* @see https://github.com/zendframework/zf2/pull/4164
*/
public function testModelFromEventIsUsedByRenderer()
{
$renderer = $this->getMock('Zend\View\Renderer\PhpRenderer', array('render'));

$model1 = new ViewModel;
$model2 = new ViewModel;

$this->view->addRenderingStrategy(function ($e) use ($renderer) {
return $renderer;
});

$this->view->getEventManager(ViewEvent::EVENT_RENDERER_POST, function($e) use ($model2) {
$e->setModel($model2);
});

$renderer->expects($this->once())
->method('render')
->with($model2);

$this->view->render($model1);
}
}

0 comments on commit 1380626

Please sign in to comment.