forked from zendframework/zend-mvc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForwardTest.php
223 lines (196 loc) · 8.55 KB
/
ForwardTest.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Mvc\Controller\Plugin;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\EventManager\StaticEventManager;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Controller\ControllerManager;
use Zend\Mvc\Controller\PluginManager;
use Zend\Mvc\Controller\Plugin\Forward as ForwardPlugin;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Stdlib\CallbackHandler;
use ZendTest\Mvc\Controller\TestAsset\ForwardController;
use ZendTest\Mvc\Controller\TestAsset\SampleController;
use ZendTest\Mvc\Controller\TestAsset\UneventfulController;
use ZendTest\Mvc\TestAsset\Locator;
class ForwardTest extends TestCase
{
/**
* @var PluginManager
*/
private $plugins;
/**
* @var ControllerManager
*/
private $controllers;
/**
* @var SampleController
*/
private $controller;
/**
* @var \Zend\Mvc\Controller\Plugin\Forward
*/
private $plugin;
public function setUp()
{
StaticEventManager::resetInstance();
$mockSharedEventManager = $this->getMock('Zend\EventManager\SharedEventManagerInterface');
$mockSharedEventManager->expects($this->any())->method('getListeners')->will($this->returnValue([]));
$mockEventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
$mockEventManager->expects($this->any())->method('getSharedManager')->will($this->returnValue($mockSharedEventManager));
$mockApplication = $this->getMock('Zend\Mvc\ApplicationInterface');
$mockApplication->expects($this->any())->method('getEventManager')->will($this->returnValue($mockEventManager));
$event = new MvcEvent();
$event->setApplication($mockApplication);
$event->setRequest(new Request());
$event->setResponse(new Response());
$routeMatch = new RouteMatch(['action' => 'test']);
$routeMatch->setMatchedRouteName('some-route');
$event->setRouteMatch($routeMatch);
$services = new Locator();
$plugins = $this->plugins = new PluginManager();
$plugins->setServiceLocator($services);
$controllers = $this->controllers = new ControllerManager();
$controllers->setFactory('forward', function () use ($plugins) {
$controller = new ForwardController();
$controller->setPluginManager($plugins);
return $controller;
});
$controllers->setServiceLocator($services);
$controllerManager = function () use ($controllers) {
return $controllers;
};
$services->add('ControllerManager', $controllerManager);
$services->add('ControllerPluginManager', function () use ($plugins) {
return $plugins;
});
$services->add('Zend\ServiceManager\ServiceLocatorInterface', function () use ($services) {
return $services;
});
$services->add('EventManager', function () use ($mockEventManager) {
return $mockEventManager;
});
$services->add('SharedEventManager', function () use ($mockSharedEventManager) {
return $mockSharedEventManager;
});
$this->controller = new SampleController();
$this->controller->setEvent($event);
$this->controller->setServiceLocator($services);
$this->controller->setPluginManager($plugins);
$this->plugin = $this->controller->plugin('forward');
}
public function tearDown()
{
StaticEventManager::resetInstance();
}
public function testPluginWithoutEventAwareControllerRaisesDomainException()
{
$controller = new UneventfulController();
$plugin = new ForwardPlugin($this->controllers);
$plugin->setController($controller);
$this->setExpectedException('Zend\Mvc\Exception\DomainException', 'InjectApplicationEventInterface');
$plugin->dispatch('forward');
}
public function testPluginWithoutControllerLocatorRaisesServiceNotCreatedException()
{
$controller = new SampleController();
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotCreatedException');
$plugin = $controller->plugin('forward');
}
public function testDispatchRaisesDomainExceptionIfDiscoveredControllerIsNotDispatchable()
{
$locator = $this->controller->getServiceLocator();
$locator->add('bogus', function () {
return new stdClass;
});
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->plugin->dispatch('bogus');
}
public function testDispatchRaisesDomainExceptionIfCircular()
{
$this->setExpectedException('Zend\Mvc\Exception\DomainException', 'Circular forwarding');
$sampleController = $this->controller;
$this->controllers->setService('sample', $sampleController);
$this->plugin->dispatch('sample', ['action' => 'test-circular']);
}
public function testPluginDispatchsRequestedControllerWhenFound()
{
$result = $this->plugin->dispatch('forward');
$this->assertInternalType('array', $result);
$this->assertEquals(['content' => 'ZendTest\Mvc\Controller\TestAsset\ForwardController::testAction'], $result);
}
/**
* @group 5432
*/
public function testNonArrayListenerDoesNotRaiseErrorWhenPluginDispatchsRequestedController()
{
$services = $this->plugins->getServiceLocator();
$events = $services->get('EventManager');
$sharedEvents = $this->getMock('Zend\EventManager\SharedEventManagerInterface');
$sharedEvents->expects($this->any())->method('getListeners')->will($this->returnValue([
new CallbackHandler(function ($e) {})
]));
$events = $this->getMock('Zend\EventManager\EventManagerInterface');
$events->expects($this->any())->method('getSharedManager')->will($this->returnValue($sharedEvents));
$application = $this->getMock('Zend\Mvc\ApplicationInterface');
$application->expects($this->any())->method('getEventManager')->will($this->returnValue($events));
$event = $this->controller->getEvent();
$event->setApplication($application);
$result = $this->plugin->dispatch('forward');
$this->assertInternalType('array', $result);
$this->assertEquals(['content' => 'ZendTest\Mvc\Controller\TestAsset\ForwardController::testAction'], $result);
}
public function testDispatchWillSeedRouteMatchWithPassedParameters()
{
$result = $this->plugin->dispatch('forward', [
'action' => 'test-matches',
'param1' => 'foobar',
]);
$this->assertInternalType('array', $result);
$this->assertTrue(isset($result['action']));
$this->assertEquals('test-matches', $result['action']);
$this->assertTrue(isset($result['param1']));
$this->assertEquals('foobar', $result['param1']);
}
public function testRouteMatchObjectRemainsSameFollowingForwardDispatch()
{
$routeMatch = $this->controller->getEvent()->getRouteMatch();
$matchParams = $routeMatch->getParams();
$matchMatchedRouteName = $routeMatch->getMatchedRouteName();
$result = $this->plugin->dispatch('forward', [
'action' => 'test-matches',
'param1' => 'foobar',
]);
$testMatch = $this->controller->getEvent()->getRouteMatch();
$testParams = $testMatch->getParams();
$testMatchedRouteName = $testMatch->getMatchedRouteName();
$this->assertSame($routeMatch, $testMatch);
$this->assertEquals($matchParams, $testParams);
$this->assertEquals($matchMatchedRouteName, $testMatchedRouteName);
}
public function testAllowsPassingEmptyArrayOfRouteParams()
{
$result = $this->plugin->dispatch('forward', []);
$this->assertInternalType('array', $result);
$this->assertTrue(isset($result['status']));
$this->assertEquals('not-found', $result['status']);
$this->assertTrue(isset($result['params']));
$this->assertEquals([], $result['params']);
}
/**
* @group 6398
*/
public function testSetListenersToDetachIsFluent()
{
$this->assertSame($this->plugin, $this->plugin->setListenersToDetach([]));
}
}