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

Commit 6f9c271

Browse files
committed
Do not allow a null middleware
1 parent 81c5f2e commit 6f9c271

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/MiddlewareListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function (PsrServerRequestInterface $request, PsrResponseInterface $response) {
123123
* @param ResponseInterface $responsePrototype
124124
* @param array $middlewaresToBePiped
125125
* @return MiddlewarePipe
126+
* @throws \InvalidArgumentException
126127
* @throws \Zend\Mvc\Exception\MiddlewareNotCallableException
127128
*/
128129
private function createPipeFromSpec(
@@ -133,6 +134,10 @@ private function createPipeFromSpec(
133134
$pipe = new MiddlewarePipe();
134135
$pipe->setResponsePrototype($responsePrototype);
135136
foreach ($middlewaresToBePiped as $middlewareToBePiped) {
137+
if (null === $middlewareToBePiped) {
138+
throw new \InvalidArgumentException('Middleware name cannot be null');
139+
}
140+
136141
$middlewareName = is_string($middlewareToBePiped) ? $middlewareToBePiped : get_class($middlewareToBePiped);
137142

138143
if (is_string($middlewareToBePiped) && $serviceLocator->has($middlewareToBePiped)) {

test/MiddlewareListenerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,39 @@ public function testMiddlewareWithNothingPipedReachesFinalHandlerException()
275275
$return = $listener->onDispatch($event);
276276
$this->assertEquals('FAILED', $return);
277277
}
278+
279+
public function testNullMiddlewareThrowsInvalidArgument()
280+
{
281+
$response = new Response();
282+
$routeMatch = $this->prophesize(RouteMatch::class);
283+
$routeMatch->getParams()->willReturn([]);
284+
$routeMatch->getParam('middleware', false)->willReturn([null]);
285+
286+
$eventManager = new EventManager();
287+
288+
$serviceManager = $this->prophesize(ContainerInterface::class);
289+
$application = $this->prophesize(Application::class);
290+
$application->getEventManager()->willReturn($eventManager);
291+
$application->getServiceManager()->will(function () use ($serviceManager) {
292+
return $serviceManager->reveal();
293+
});
294+
$application->getResponse()->willReturn($response);
295+
296+
$event = new MvcEvent();
297+
$event->setRequest(new Request());
298+
$event->setResponse($response);
299+
$event->setApplication($application->reveal());
300+
$event->setRouteMatch($routeMatch->reveal());
301+
302+
$event->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
303+
$this->assertEquals(Application::ERROR_EXCEPTION, $e->getError());
304+
$this->assertInstanceOf(ReachedFinalHandlerException::class, $e->getParam('exception'));
305+
return 'FAILED';
306+
});
307+
308+
$listener = new MiddlewareListener();
309+
310+
$this->setExpectedException(\InvalidArgumentException::class);
311+
$listener->onDispatch($event);
312+
}
278313
}

0 commit comments

Comments
 (0)