|
17 | 17 | use Zend\Http\Request;
|
18 | 18 | use Zend\Http\Response;
|
19 | 19 | use Zend\Mvc\Application;
|
| 20 | +use Zend\Mvc\Exception\ReachedFinalHandlerException; |
20 | 21 | use Zend\Mvc\MiddlewareListener;
|
21 | 22 | use Zend\Mvc\MvcEvent;
|
22 | 23 | use Zend\Router\RouteMatch;
|
@@ -113,6 +114,59 @@ function (ServerRequestInterface $request, ResponseInterface $response) use (&$r
|
113 | 114 | $this->assertSame($this->routeMatch->reveal(), $routeAttribute);
|
114 | 115 | }
|
115 | 116 |
|
| 117 | + public function testSuccessfullyDispatchesPipeOfMiddleware() |
| 118 | + { |
| 119 | + $response = new Response(); |
| 120 | + $routeMatch = $this->prophesize(RouteMatch::class); |
| 121 | + $routeMatch->getParam('middleware', false)->willReturn([ |
| 122 | + 'firstMiddleware', |
| 123 | + 'secondMiddleware', |
| 124 | + ]); |
| 125 | + |
| 126 | + $eventManager = new EventManager(); |
| 127 | + |
| 128 | + $serviceManager = $this->prophesize(ContainerInterface::class); |
| 129 | + $serviceManager->has('firstMiddleware')->willReturn(true); |
| 130 | + $serviceManager->get('firstMiddleware')->willReturn(function ($request, $response, $next) { |
| 131 | + $this->assertInstanceOf(ServerRequestInterface::class, $request); |
| 132 | + $this->assertInstanceOf(ResponseInterface::class, $response); |
| 133 | + $this->assertTrue(is_callable($next)); |
| 134 | + return $next($request->withAttribute('firstMiddlewareAttribute', 'firstMiddlewareValue'), $response); |
| 135 | + }); |
| 136 | + $serviceManager->has('secondMiddleware')->willReturn(true); |
| 137 | + $serviceManager->get('secondMiddleware')->willReturn(function ($request, $response) { |
| 138 | + $this->assertInstanceOf(ServerRequestInterface::class, $request); |
| 139 | + $this->assertInstanceOf(ResponseInterface::class, $response); |
| 140 | + $response->getBody()->write($request->getAttribute('firstMiddlewareAttribute')); |
| 141 | + return $response; |
| 142 | + }); |
| 143 | + |
| 144 | + $application = $this->prophesize(Application::class); |
| 145 | + $application->getEventManager()->willReturn($eventManager); |
| 146 | + $application->getServiceManager()->will(function () use ($serviceManager) { |
| 147 | + return $serviceManager->reveal(); |
| 148 | + }); |
| 149 | + $application->getResponse()->willReturn($response); |
| 150 | + |
| 151 | + $event = new MvcEvent(); |
| 152 | + $event->setRequest(new Request()); |
| 153 | + $event->setResponse($response); |
| 154 | + $event->setApplication($application->reveal()); |
| 155 | + $event->setRouteMatch($routeMatch->reveal()); |
| 156 | + |
| 157 | + $event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) { |
| 158 | + $this->fail(sprintf('dispatch.error triggered when it should not be: %s', var_export($e->getError(), 1))); |
| 159 | + }); |
| 160 | + |
| 161 | + $listener = new MiddlewareListener(); |
| 162 | + $return = $listener->onDispatch($event); |
| 163 | + $this->assertInstanceOf(Response::class, $return); |
| 164 | + |
| 165 | + $this->assertInstanceOf('Zend\Http\Response', $return); |
| 166 | + $this->assertSame(200, $return->getStatusCode()); |
| 167 | + $this->assertEquals('firstMiddlewareValue', $return->getBody()); |
| 168 | + } |
| 169 | + |
116 | 170 | public function testTriggersErrorForUncallableMiddleware()
|
117 | 171 | {
|
118 | 172 | $event = $this->createMvcEvent('path');
|
@@ -186,4 +240,37 @@ public function testCanLoadFromAbstractFactory()
|
186 | 240 | $this->assertSame(200, $return->getStatusCode());
|
187 | 241 | $this->assertEquals(TestAsset\Middleware::class, $return->getBody());
|
188 | 242 | }
|
| 243 | + |
| 244 | + public function testMiddlewareWithNothingPipedReachesFinalHandlerException() |
| 245 | + { |
| 246 | + $response = new Response(); |
| 247 | + $routeMatch = $this->prophesize(RouteMatch::class); |
| 248 | + $routeMatch->getParam('middleware', false)->willReturn([]); |
| 249 | + |
| 250 | + $eventManager = new EventManager(); |
| 251 | + |
| 252 | + $serviceManager = $this->prophesize(ContainerInterface::class); |
| 253 | + $application = $this->prophesize(Application::class); |
| 254 | + $application->getEventManager()->willReturn($eventManager); |
| 255 | + $application->getServiceManager()->will(function () use ($serviceManager) { |
| 256 | + return $serviceManager->reveal(); |
| 257 | + }); |
| 258 | + $application->getResponse()->willReturn($response); |
| 259 | + |
| 260 | + $event = new MvcEvent(); |
| 261 | + $event->setRequest(new Request()); |
| 262 | + $event->setResponse($response); |
| 263 | + $event->setApplication($application->reveal()); |
| 264 | + $event->setRouteMatch($routeMatch->reveal()); |
| 265 | + |
| 266 | + $event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) { |
| 267 | + $this->assertEquals(Application::ERROR_EXCEPTION, $e->getError()); |
| 268 | + $this->assertInstanceOf(ReachedFinalHandlerException::class, $e->getParam('exception')); |
| 269 | + return 'FAILED'; |
| 270 | + }); |
| 271 | + |
| 272 | + $listener = new MiddlewareListener(); |
| 273 | + $return = $listener->onDispatch($event); |
| 274 | + $this->assertEquals('FAILED', $return); |
| 275 | + } |
189 | 276 | }
|
0 commit comments