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

Hotfix - #37 - catch also php 7 throwables #49

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Stratigility;

use Exception;
use Throwable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -80,8 +81,10 @@ public function __invoke(
if (! $hasError && $arity < 4) {
return call_user_func($handler, $request, $response, $next);
}
} catch (Exception $e) {
$err = $e;
} catch (Throwable $throwable) {
return $next($request, $response, $throwable);
} catch (Exception $exception) {
return $next($request, $response, $exception);
}

return $next($request, $response, $err);
Expand Down
53 changes: 53 additions & 0 deletions test/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@

class DispatchTest extends TestCase
{
/**
* @var \Zend\Stratigility\Http\Request|\PHPUnit_Framework_MockObject_MockObject
*/
private $request;

/**
* @var \Zend\Stratigility\Http\Response|\PHPUnit_Framework_MockObject_MockObject
*/
private $response;

public function setUp()
{
$this->request = $this->getMockBuilder('Zend\Stratigility\Http\Request')
Expand Down Expand Up @@ -199,4 +209,47 @@ public function testShouldAllowDispatchingPsr7Instances()
$result = $dispatch($route, $err, $request->reveal(), $response->reveal(), $next);
$this->assertSame($response->reveal(), $result);
}

/**
* @requires PHP 7.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nifty; didn't know about that annotation!

* @group 37
*/
public function testWillCatchPhp7Throwable()
{
$callableWithHint = function (\stdClass $parameter) {
// will not be executed
};

$middleware = function ($req, $res, $next) use ($callableWithHint) {
$callableWithHint('not an stdClass');
};

$errorHandler = $this->getMock('stdClass', ['__invoke']);
$errorHandler
->expects(self::once())
->method('__invoke')
->with(
$this->request,
$this->response,
self::callback(function (\TypeError $throwable) {
self::assertStringStartsWith(
'Argument 1 passed to ZendTest\Stratigility\DispatchTest::ZendTest\Stratigility\{closure}()'
. ' must be an instance of stdClass, string given',
$throwable->getMessage()
);

return true;
})
);

$dispatch = new Dispatch();

$dispatch(
new Route('/foo', $middleware),
null,
$this->request,
$this->response,
$errorHandler
);
}
}