Skip to content

Commit 058b9a3

Browse files
committed
test: application exception handler
1 parent cb58bed commit 058b9a3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tests/ApplicationTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Ahc\Cli\IO\Interactor;
1717
use InvalidArgumentException;
1818
use PHPUnit\Framework\TestCase;
19+
use Throwable;
1920

2021
class ApplicationTest extends TestCase
2122
{
@@ -288,11 +289,19 @@ public function test_io_returns_new_instance_if_not_provided(): void
288289
);
289290
}
290291

292+
public function test_on_exception()
293+
{
294+
$this->expectException(InvalidArgumentException::class);
295+
$this->expectExceptionMessage($msg = 'this will be rethrown and propagated');
296+
297+
$cmd = (new Command('cmd'))->action(fn () => throw new InvalidArgumentException($msg));
298+
$app = $this->newApp('test')->add($cmd)->onException(fn (Throwable $e) => throw $e);
299+
$app->handle(['test', 'cmd']);
300+
}
301+
291302
protected function newApp(string $name, string $version = '')
292303
{
293-
$app = new Application($name, $version ?: '0.0.1', function () {
294-
return false;
295-
});
304+
$app = new Application($name, $version ?: '0.0.1', fn () => false);
296305

297306
return $app->io(new Interactor(static::$in, static::$ou));
298307
}

0 commit comments

Comments
 (0)