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

Commit f035215

Browse files
committed
Added tests for ErrorMiddlewarePipe behavior
This adds a test case for the ErrorMiddlewarePipe to verify that it: - aggregates a middleware pipe instance - dispatches the middleware composed in the middleware pipe instance - but only those that are error middleware
1 parent 9fb8ce0 commit f035215

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/ErrorMiddlewarePipe.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Psr\Http\Message\ServerRequestInterface as Request;
1313
use Psr\Http\Message\ResponseInterface as Response;
14+
use ReflectionProperty;
1415
use Zend\Stratigility\FinalHandler;
1516
use Zend\Stratigility\MiddlewarePipe;
1617
use Zend\Stratigility\Next;
@@ -81,7 +82,7 @@ public function __invoke($error, Request $request, Response $response, callable
8182
private function getInternalPipeline()
8283
{
8384
$r = new ReflectionProperty($this->pipeline, 'pipeline');
84-
$r->setAccessible();
85+
$r->setAccessible(true);
8586
return $r->getValue($this->pipeline);
8687
}
8788
}

test/ErrorMiddlewarePipeTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see http://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ZendTest\Expressive;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Psr\Http\Message\ServerRequestInterface as Request;
14+
use Psr\Http\Message\ResponseInterface as Response;
15+
use Psr\Http\Message\UriInterface as Uri;
16+
use Zend\Expressive\ErrorMiddlewarePipe;
17+
use Zend\Stratigility\MiddlewarePipe;
18+
19+
class ErrorMiddlewarePipeTest extends TestCase
20+
{
21+
public function setUp()
22+
{
23+
$this->internalPipe = new MiddlewarePipe();
24+
$this->errorPipe = new ErrorMiddlewarePipe($this->internalPipe);
25+
}
26+
27+
public function testWillDispatchErrorMiddlewareComposedInInternalPipeline()
28+
{
29+
$error = (object) ['error' => true];
30+
$triggered = (object) [
31+
'first' => false,
32+
'second' => false,
33+
'third' => false,
34+
];
35+
36+
$first = function ($err, $request, $response, $next) use ($error, $triggered) {
37+
$this->assertSame($error, $err);
38+
$triggered->first = true;
39+
return $next($request, $response, $err);
40+
};
41+
$second = function ($request, $response, $next) use ($triggered) {
42+
$triggered->second = true;
43+
return $next($request, $response);
44+
};
45+
$third = function ($err, $request, $response, $next) use ($error, $triggered) {
46+
$this->assertSame($error, $err);
47+
$triggered->third = true;
48+
return $response;
49+
};
50+
51+
$this->internalPipe->pipe($first);
52+
$this->internalPipe->pipe($second);
53+
$this->internalPipe->pipe($third);
54+
55+
$uri = $this->prophesize(Uri::class);
56+
$uri->getPath()->willReturn('/');
57+
$request = $this->prophesize(Request::class);
58+
$request->getUri()->willReturn($uri->reveal());
59+
$response = $this->prophesize(Response::class);
60+
61+
$final = function ($request, $response, $err = null) {
62+
$this->fail('Final handler should not be triggered');
63+
};
64+
65+
$result = $this->errorPipe->__invoke($error, $request->reveal(), $response->reveal(), $final);
66+
$this->assertSame($response->reveal(), $result);
67+
$this->assertTrue($triggered->first);
68+
$this->assertFalse($triggered->second);
69+
$this->assertTrue($triggered->third);
70+
}
71+
}

0 commit comments

Comments
 (0)