From becc09ac10e216874d6edf92716b117f5afa727f Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Sat, 21 Jun 2025 22:46:37 +0400 Subject: [PATCH] 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. --- tests/Pest.php | 2 +- .../DependencyInjection/BootstrapTest.php | 2 +- .../DependencyInjection/ContainerTest.php | 35 ++++++++----------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/tests/Pest.php b/tests/Pest.php index 91c770acf..f1c9a7613 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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); diff --git a/tests/Unit/Infrastructure/DependencyInjection/BootstrapTest.php b/tests/Unit/Infrastructure/DependencyInjection/BootstrapTest.php index 39041a91a..1a0fbaf16 100644 --- a/tests/Unit/Infrastructure/DependencyInjection/BootstrapTest.php +++ b/tests/Unit/Infrastructure/DependencyInjection/BootstrapTest.php @@ -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); }); }); }); diff --git a/tests/Unit/Infrastructure/DependencyInjection/ContainerTest.php b/tests/Unit/Infrastructure/DependencyInjection/ContainerTest.php index 8ba160370..d751ecf67 100644 --- a/tests/Unit/Infrastructure/DependencyInjection/ContainerTest.php +++ b/tests/Unit/Infrastructure/DependencyInjection/ContainerTest.php @@ -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' + ); }); }); });