Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

176 lines (149 sloc) 6.177 kb
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/
namespace ZendTest\Mvc\View;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\Event;
use Zend\EventManager\EventManager;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\View\Http\DefaultRenderingStrategy;
use Zend\ServiceManager\ServiceManager;
use Zend\View\Model\ModelInterface as Model;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\View;
use Zend\View\Model\ViewModel;
use Zend\View\Resolver\TemplateMapResolver;
use Zend\View\Strategy\PhpRendererStrategy;
/**
* @category Zend
* @package Zend_Mvc
* @subpackage UnitTest
*/
class DefaultRenderingStrategyTest extends TestCase
{
protected $event;
protected $request;
protected $response;
protected $view;
protected $renderer;
protected $strategy;
public function setUp()
{
$this->view = new View();
$this->request = new Request();
$this->response = new Response();
$this->event = new MvcEvent();
$this->renderer = new PhpRenderer();
$this->event->setRequest($this->request)
->setResponse($this->response);
$this->strategy = new DefaultRenderingStrategy($this->view);
}
public function testAttachesRendererAtExpectedPriority()
{
$evm = new EventManager();
$evm->attachAggregate($this->strategy);
$events = array(MvcEvent::EVENT_RENDER, MvcEvent::EVENT_RENDER_ERROR);
foreach ($events as $event) {
$listeners = $evm->getListeners($event);
$expectedCallback = array($this->strategy, 'render');
$expectedPriority = -10000;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Renderer not found');
}
}
public function testCanDetachListenersFromEventManager()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$this->assertEquals(1, count($events->getListeners(MvcEvent::EVENT_RENDER)));
$events->detachAggregate($this->strategy);
$this->assertEquals(0, count($events->getListeners(MvcEvent::EVENT_RENDER)));
}
public function testWillRenderAlternateStrategyWhenSelected()
{
$renderer = new TestAsset\DumbStrategy();
$this->view->addRenderingStrategy(function ($e) use ($renderer) {
return $renderer;
}, 100);
$model = new ViewModel(array('foo' => 'bar'));
$model->setOption('template', 'content');
$this->event->setResult($model);
$result = $this->strategy->render($this->event);
$this->assertSame($this->response, $result);
$expected = sprintf('content (%s): %s', json_encode(array('template' => 'content')), json_encode(array('foo' => 'bar')));
}
public function testLayoutTemplateIsLayoutByDefault()
{
$this->assertEquals('layout', $this->strategy->getLayoutTemplate());
}
public function testLayoutTemplateIsMutable()
{
$this->strategy->setLayoutTemplate('alternate/layout');
$this->assertEquals('alternate/layout', $this->strategy->getLayoutTemplate());
}
public function testBypassesRenderingIfResultIsAResponse()
{
$renderer = new TestAsset\DumbStrategy();
$this->view->addRenderingStrategy(function ($e) use ($renderer) {
return $renderer;
}, 100);
$model = new ViewModel(array('foo' => 'bar'));
$model->setOption('template', 'content');
$this->event->setViewModel($model);
$this->event->setResult($this->response);
$result = $this->strategy->render($this->event);
$this->assertSame($this->response, $result);
}
public function testTriggersRenderErrorEventInCaseOfRenderingException()
{
$resolver = new TemplateMapResolver();
$resolver->add('exception', __DIR__ . '/_files/exception.phtml');
$this->renderer->setResolver($resolver);
$strategy = new PhpRendererStrategy($this->renderer);
$this->view->getEventManager()->attach($strategy);
$model = new ViewModel();
$model->setTemplate('exception');
$this->event->setViewModel($model);
$services = new ServiceManager();
$services->setService('Request', $this->request);
$services->setService('Response', $this->response);
$services->setInvokableClass('SharedEventManager', 'Zend\EventManager\SharedEventManager');
$services->setFactory('EventManager', function ($services) {
$sharedEvents = $services->get('SharedEventManager');
$events = new EventManager();
$events->setSharedManager($sharedEvents);
return $events;
}, false);
$application = new Application(array(), $services);
$this->event->setApplication($application);
$test = (object) array('flag' => false);
$application->getEventManager()->attach('render.error', function ($e) use ($test) {
$test->flag = true;
$test->error = $e->getError();
$test->exception = $e->getParam('exception');
});
$this->strategy->render($this->event);
$this->assertTrue($test->flag);
$this->assertEquals(Application::ERROR_EXCEPTION, $test->error);
$this->assertInstanceOf('Exception', $test->exception);
$this->assertContains('script', $test->exception->getMessage());
}
}
Jump to Line
Something went wrong with that request. Please try again.