forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultRendereringStrategyTest.php
175 lines (149 loc) · 6.03 KB
/
DefaultRendereringStrategyTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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());
}
}