refactor(tests): update exception handling to use Throwable

- Changed exception handling in Pest.php to catch Throwable instead of Exception.
- Updated BootstrapTest.php and ContainerTest.php to expect Throwable for error handling in tests.
- Refactored tests to utilize expectException helper for cleaner syntax and improved readability.
This commit is contained in:
Yury Pikhtarev 2025-06-21 22:46:37 +04:00
commit becc09ac10
No known key found for this signature in database
3 changed files with 17 additions and 22 deletions

View file

@ -86,7 +86,7 @@ function expectException(callable $callback, string $exceptionClass, ?string $me
try {
$callback();
fail("Expected exception $exceptionClass was not thrown");
} catch (Exception $e) {
} catch (Throwable $e) {
expect($e)->toBeInstanceOf($exceptionClass);
if ($message) {
expect($e->getMessage())->toContain($message);

View file

@ -184,7 +184,7 @@ describe('Bootstrap', function () {
// Should not throw fatal error for non-existent path
expect(function () {
Bootstrap::init('/non/existent/path');
})->not->toThrow(Error::class);
})->not->toThrow(Throwable::class);
});
});
});

View file

@ -36,13 +36,11 @@ describe('Container', function () {
});
it('throws NotFoundExceptionInterface for non-existent services', function () {
try {
$this->container->get('non.existent.service');
fail('Expected exception to be thrown');
} catch (Exception $e) {
expect($e)->toBeInstanceOf(NotFoundExceptionInterface::class);
expect($e->getMessage())->toContain('non.existent.service');
}
expectException(
fn() => $this->container->get('non.existent.service'),
NotFoundExceptionInterface::class,
'non.existent.service'
);
});
it('returns same instance for singleton services', function () {
@ -140,12 +138,11 @@ describe('Container', function () {
describe('error handling', function () {
it('provides meaningful error messages for missing services', function () {
try {
$this->container->get('missing.service');
fail('Expected NotFoundExceptionInterface to be thrown');
} catch (NotFoundExceptionInterface $e) {
expect($e->getMessage())->toContain('missing.service');
}
expectException(
fn() => $this->container->get('missing.service'),
NotFoundExceptionInterface::class,
'missing.service'
);
});
it('handles circular dependencies gracefully', function () {
@ -158,13 +155,11 @@ describe('Container', function () {
}),
]);
try {
$container->get('service.a');
fail('Expected circular dependency exception');
} catch (Exception $e) {
expect($e)->toBeInstanceOf(ContainerExceptionInterface::class);
expect($e->getMessage())->toContain('Circular dependency');
}
expectException(
fn() => $container->get('service.a'),
ContainerExceptionInterface::class,
'Circular dependency'
);
});
});
});