Skip to content

Commit

Permalink
Finalizes output buffer when an exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Jan 26, 2024
1 parent 2a3a6f7 commit 7f895af
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/AppTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/RealTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions tests/config/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
},
],
];

0 comments on commit 7f895af

Please sign in to comment.