|
16 | 16 | use Psr\Http\Message\ResponseInterface;
|
17 | 17 | use Psr\Http\Message\ServerRequestInterface;
|
18 | 18 | use Zend\Diactoros\Response\HtmlResponse;
|
| 19 | +use Zend\Diactoros\Response as DiactorosResponse; |
19 | 20 | use Zend\EventManager\EventManager;
|
20 | 21 | use Zend\Http\Request;
|
21 | 22 | use Zend\Http\Response;
|
@@ -265,7 +266,6 @@ public function testCanLoadFromAbstractFactory()
|
265 | 266 |
|
266 | 267 | $listener = new MiddlewareListener();
|
267 | 268 | $return = $listener->onDispatch($event);
|
268 |
| - $this->assertInstanceOf(Response::class, $return); |
269 | 269 |
|
270 | 270 | $this->assertInstanceOf(Response::class, $return);
|
271 | 271 | $this->assertSame(200, $return->getStatusCode());
|
@@ -340,4 +340,42 @@ public function testNullMiddlewareThrowsInvalidMiddlewareException()
|
340 | 340 | $return = $listener->onDispatch($event);
|
341 | 341 | $this->assertEquals('FAILED', $return);
|
342 | 342 | }
|
| 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 | + } |
343 | 381 | }
|
0 commit comments