Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit cab17a2

Browse files
committed
Added tests for handling an array of middleware to be piped
1 parent 2712a99 commit cab17a2

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Mvc\Exception;
11+
12+
class ReachedFinalHandlerException extends RuntimeException
13+
{
14+
}

test/MiddlewareListenerTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Zend\Http\Request;
1818
use Zend\Http\Response;
1919
use Zend\Mvc\Application;
20+
use Zend\Mvc\Exception\ReachedFinalHandlerException;
2021
use Zend\Mvc\MiddlewareListener;
2122
use Zend\Mvc\MvcEvent;
2223
use Zend\Router\RouteMatch;
@@ -113,6 +114,59 @@ function (ServerRequestInterface $request, ResponseInterface $response) use (&$r
113114
$this->assertSame($this->routeMatch->reveal(), $routeAttribute);
114115
}
115116

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+
116170
public function testTriggersErrorForUncallableMiddleware()
117171
{
118172
$event = $this->createMvcEvent('path');
@@ -186,4 +240,37 @@ public function testCanLoadFromAbstractFactory()
186240
$this->assertSame(200, $return->getStatusCode());
187241
$this->assertEquals(TestAsset\Middleware::class, $return->getBody());
188242
}
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+
}
189276
}

0 commit comments

Comments
 (0)