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

Commit c27997f

Browse files
committed
MiddlewareListener should cancel any previous dispatch errors set in the given MvcEvent
1 parent 9935a57 commit c27997f

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

test/MiddlewareListenerTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Http\Message\ResponseInterface;
1717
use Psr\Http\Message\ServerRequestInterface;
1818
use Zend\Diactoros\Response\HtmlResponse;
19+
use Zend\Diactoros\Response as DiactorosResponse;
1920
use Zend\EventManager\EventManager;
2021
use Zend\Http\Request;
2122
use Zend\Http\Response;
@@ -265,7 +266,6 @@ public function testCanLoadFromAbstractFactory()
265266

266267
$listener = new MiddlewareListener();
267268
$return = $listener->onDispatch($event);
268-
$this->assertInstanceOf(Response::class, $return);
269269

270270
$this->assertInstanceOf(Response::class, $return);
271271
$this->assertSame(200, $return->getStatusCode());
@@ -340,4 +340,42 @@ public function testNullMiddlewareThrowsInvalidMiddlewareException()
340340
$return = $listener->onDispatch($event);
341341
$this->assertEquals('FAILED', $return);
342342
}
343+
344+
public function testValidMiddlewareDispatchCancelsPreviousDispatchFailures()
345+
{
346+
$middlewareName = uniqid('middleware', true);
347+
/* @var $routeMatch RouteMatch|\PHPUnit_Framework_MockObject_MockObject */
348+
$routeMatch = $this->createMock(RouteMatch::class);
349+
$response = new DiactorosResponse();
350+
/* @var $application Application|\PHPUnit_Framework_MockObject_MockObject */
351+
$application = $this->createMock(Application::class);
352+
$serviceManager = $this->createMock(ServiceManager::class);
353+
$eventManager = new EventManager();
354+
$middleware = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
355+
356+
$application->expects(self::any())->method('getRequest')->willReturn(new Request());
357+
$application->expects(self::any())->method('getEventManager')->willReturn($eventManager);
358+
$application->expects(self::any())->method('getServiceManager')->willReturn($serviceManager);
359+
$application->expects(self::any())->method('getResponse')->willReturn(new Response());
360+
$middleware->expects(self::once())->method('__invoke')->willReturn($response);
361+
362+
$serviceManager->expects(self::any())->method('has')->with($middlewareName)->willReturn(true);
363+
$serviceManager->expects(self::any())->method('get')->with($middlewareName)->willReturn($middleware);
364+
$routeMatch->expects(self::any())->method('getParam')->with('middleware')->willReturn($middlewareName);
365+
$routeMatch->expects(self::any())->method('getParams')->willReturn([]);
366+
367+
$event = new MvcEvent();
368+
369+
$event->setRequest(new Request());
370+
$event->setApplication($application);
371+
$event->setError(Application::ERROR_CONTROLLER_CANNOT_DISPATCH);
372+
$event->setRouteMatch($routeMatch);
373+
374+
$listener = new MiddlewareListener();
375+
$result = $listener->onDispatch($event);
376+
377+
self::assertInstanceOf(Response::class, $result);
378+
self::assertInstanceOf(Response::class, $event->getResult());
379+
self::assertNull($event->getError(), 'Previously set MVC errors are canceled by a successful dispatch');
380+
}
343381
}

0 commit comments

Comments
 (0)