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

Commit 3f2c36b

Browse files
committed
Merge pull request #49 from Ocramius/hotfix/#37-catch-also-php-7-throwables
Hotfix - #37 - catch also php 7 throwables
2 parents 4ae2341 + 9f06282 commit 3f2c36b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/Dispatch.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Stratigility;
1111

1212
use Exception;
13+
use Throwable;
1314
use Psr\Http\Message\ResponseInterface;
1415
use Psr\Http\Message\ServerRequestInterface;
1516

@@ -80,8 +81,10 @@ public function __invoke(
8081
if (! $hasError && $arity < 4) {
8182
return $handler($request, $response, $next);
8283
}
83-
} catch (Exception $e) {
84-
$err = $e;
84+
} catch (Throwable $throwable) {
85+
return $next($request, $response, $throwable);
86+
} catch (Exception $exception) {
87+
return $next($request, $response, $exception);
8588
}
8689

8790
return $next($request, $response, $err);

test/DispatchTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818

1919
class DispatchTest extends TestCase
2020
{
21+
/**
22+
* @var \Zend\Stratigility\Http\Request|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $request;
25+
26+
/**
27+
* @var \Zend\Stratigility\Http\Response|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $response;
30+
2131
public function setUp()
2232
{
2333
$this->request = $this->getMockBuilder('Zend\Stratigility\Http\Request')
@@ -199,4 +209,47 @@ public function testShouldAllowDispatchingPsr7Instances()
199209
$result = $dispatch($route, $err, $request->reveal(), $response->reveal(), $next);
200210
$this->assertSame($response->reveal(), $result);
201211
}
212+
213+
/**
214+
* @requires PHP 7.0
215+
* @group 37
216+
*/
217+
public function testWillCatchPhp7Throwable()
218+
{
219+
$callableWithHint = function (\stdClass $parameter) {
220+
// will not be executed
221+
};
222+
223+
$middleware = function ($req, $res, $next) use ($callableWithHint) {
224+
$callableWithHint('not an stdClass');
225+
};
226+
227+
$errorHandler = $this->getMock('stdClass', ['__invoke']);
228+
$errorHandler
229+
->expects(self::once())
230+
->method('__invoke')
231+
->with(
232+
$this->request,
233+
$this->response,
234+
self::callback(function (\TypeError $throwable) {
235+
self::assertStringStartsWith(
236+
'Argument 1 passed to ZendTest\Stratigility\DispatchTest::ZendTest\Stratigility\{closure}()'
237+
. ' must be an instance of stdClass, string given',
238+
$throwable->getMessage()
239+
);
240+
241+
return true;
242+
})
243+
);
244+
245+
$dispatch = new Dispatch();
246+
247+
$dispatch(
248+
new Route('/foo', $middleware),
249+
null,
250+
$this->request,
251+
$this->response,
252+
$errorHandler
253+
);
254+
}
202255
}

0 commit comments

Comments
 (0)