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

Commit

Permalink
Merge pull request #49 from Ocramius/hotfix/#37-catch-also-php-7-thro…
Browse files Browse the repository at this point in the history
…wables

Hotfix - #37 - catch also php 7 throwables
  • Loading branch information
weierophinney committed Mar 17, 2016
2 parents 4ae2341 + 9f06282 commit 3f2c36b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
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 $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
* @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
);
}
}

0 comments on commit 3f2c36b

Please sign in to comment.