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

Commit

Permalink
Merge branch 'feature/49' into develop
Browse files Browse the repository at this point in the history
Close #49
Close #37
  • Loading branch information
weierophinney committed Mar 17, 2016
2 parents 4ae2341 + f933a67 commit f86da79
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ This release contains two potential backwards compatibility breaks:
- [#46](https://github.com/zendframework/zend-stratigility/pull/46) adds
`FinalHandler::setOriginalResponse()`, allowing you to alter the response used
for comparisons when the `FinalHandler` is invoked.
- [#37](https://github.com/zendframework/zend-stratigility/pull/37) and
[#49](https://github.com/zendframework/zend-stratigility/pull/49) add
support in `Zend\Stratigility\Dispatch` to catch PHP 7 `Throwable`s.

### Deprecated

Expand Down
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
55 changes: 55 additions & 0 deletions test/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use stdClass;
use TypeError;
use Zend\Stratigility\Dispatch;
use Zend\Stratigility\Route;

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 +211,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 f86da79

Please sign in to comment.