diff --git a/src/AppTesting.php b/src/AppTesting.php index c68969f..5091431 100644 --- a/src/AppTesting.php +++ b/src/AppTesting.php @@ -53,11 +53,12 @@ public function runCliWithExec(string $command) : void */ protected function suppressOutputBuffer(Closure $closure) : void { - \ob_start(static function () { - return ''; - }); - $closure($this->app); - \ob_end_clean(); + \ob_start(static fn () => ''); + try { + $closure($this->app); + } finally { + \ob_end_clean(); + } } /** diff --git a/tests/RealTestCaseTest.php b/tests/RealTestCaseTest.php index a640962..140d3e9 100644 --- a/tests/RealTestCaseTest.php +++ b/tests/RealTestCaseTest.php @@ -34,6 +34,20 @@ public function testHttp() : void self::assertMatchedRouteName('not-found'); } + public function testExceptionBeforeRouteAction() : void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid URL: invalid-url'); + $this->app->runHttp('invalid-url'); + } + + public function testExceptionInRouteAction() : void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Error in route action'); + $this->app->runHttp('http://localhost/error'); + } + public function testResponseStatus() : void { $this->app->runHttp('http://localhost'); diff --git a/tests/config/router.php b/tests/config/router.php index cba913a..3144c64 100644 --- a/tests/config/router.php +++ b/tests/config/router.php @@ -7,5 +7,17 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +use Framework\Routing\RouteCollection; +use Framework\Routing\Router; + return [ + 'default' => [ + 'callback' => static function (Router $router) : void { + $router->serve('http://localhost', static function (RouteCollection $routes) : void { + $routes->get('/error', static function () : void { + throw new \LogicException('Error in route action'); + }); + }); + }, + ], ];