This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
InjectTemplateListenerTest.php
165 lines (131 loc) · 5.62 KB
/
InjectTemplateListenerTest.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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 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\EventManager;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\View\Http\InjectTemplateListener;
use Zend\View\Model\ViewModel;
/**
* @category Zend
* @package Zend_Mvc
* @subpackage UnitTest
*/
class InjectTemplateListenerTest extends TestCase
{
public function setUp()
{
$this->listener = new InjectTemplateListener();
$this->event = new MvcEvent();
$this->routeMatch = new RouteMatch(array());
$this->event->setRouteMatch($this->routeMatch);
}
public function testSetsTemplateBasedOnRouteMatchIfNoTemplateIsSetOnViewModel()
{
$this->routeMatch->setParam('controller', 'Foo\Controller\SomewhatController');
$this->routeMatch->setParam('action', 'useful');
$model = new ViewModel();
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('foo/somewhat/useful', $model->getTemplate());
}
public function testUsesModuleAndControllerOnlyIfNoActionInRouteMatch()
{
$this->routeMatch->setParam('controller', 'Foo\Controller\SomewhatController');
$model = new ViewModel();
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('foo/somewhat', $model->getTemplate());
}
public function testNormalizesLiteralControllerNameIfNoNamespaceSeparatorPresent()
{
$this->routeMatch->setParam('controller', 'SomewhatController');
$model = new ViewModel();
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('somewhat', $model->getTemplate());
}
public function testNormalizesNamesToLowercase()
{
$this->routeMatch->setParam('controller', 'Somewhat.DerivedController');
$this->routeMatch->setParam('action', 'some-UberCool');
$model = new ViewModel();
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('somewhat.derived/some-uber-cool', $model->getTemplate());
}
public function testLackOfViewModelInResultBypassesTemplateInjection()
{
$this->assertNull($this->listener->injectTemplate($this->event));
$this->assertNull($this->event->getResult());
}
public function testBypassesTemplateInjectionIfResultViewModelAlreadyHasATemplate()
{
$this->routeMatch->setParam('controller', 'Foo\Controller\SomewhatController');
$this->routeMatch->setParam('action', 'useful');
$model = new ViewModel();
$model->setTemplate('custom');
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('custom', $model->getTemplate());
}
public function testMapsSubNamespaceToSubDirectoryWithControllerFromRouteMatch()
{
$this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'Aj\Controller\SweetAppleAcres\Reports');
$this->routeMatch->setParam('controller', 'CiderSales');
$this->routeMatch->setParam('action', 'PinkiePieRevenue');
$model = new ViewModel();
$this->event->setResult($model);
$this->listener->injectTemplate($this->event);
$this->assertEquals('sweet-apple-acres/reports/cider-sales/pinkie-pie-revenue', $model->getTemplate());
}
public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget()
{
$this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'ZendTest\Mvc\Controller\TestAsset');
$this->routeMatch->setParam('action', 'test');
$myViewModel = new ViewModel();
$myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
$this->event->setTarget($myController);
$this->event->setResult($myViewModel);
$this->listener->injectTemplate($this->event);
$this->assertEquals('zend-test/controller/test-asset/sample/test', $myViewModel->getTemplate());
}
public function testAttachesListenerAtExpectedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->listener);
$listeners = $events->getListeners(MvcEvent::EVENT_DISPATCH);
$expectedCallback = array($this->listener, 'injectTemplate');
$expectedPriority = -90;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Listener not found');
}
public function testDetachesListeners()
{
$events = new EventManager();
$events->attachAggregate($this->listener);
$listeners = $events->getListeners(MvcEvent::EVENT_DISPATCH);
$this->assertEquals(1, count($listeners));
$events->detachAggregate($this->listener);
$listeners = $events->getListeners(MvcEvent::EVENT_DISPATCH);
$this->assertEquals(0, count($listeners));
}
}