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 { try {
$callback(); $callback();
fail("Expected exception $exceptionClass was not thrown"); fail("Expected exception $exceptionClass was not thrown");
} catch (Exception $e) { } catch (Throwable $e) {
expect($e)->toBeInstanceOf($exceptionClass); expect($e)->toBeInstanceOf($exceptionClass);
if ($message) { if ($message) {
expect($e->getMessage())->toContain($message); expect($e->getMessage())->toContain($message);

View file

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