diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 5f68109e..521d83a0 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -847,7 +847,7 @@ final class Expectation return $this; } - if (! class_exists($exception)) { + if (! class_exists($exception) && ! interface_exists($exception)) { if ($e instanceof Error && "Class \"$exception\" not found" === $e->getMessage()) { Assert::assertTrue(true); @@ -872,7 +872,7 @@ final class Expectation Assert::assertTrue(true); - if (! $exception instanceof Throwable && ! class_exists($exception)) { + if (! $exception instanceof Throwable && ! class_exists($exception) && ! interface_exists($exception)) { throw new ExpectationFailedException("Exception with message [$exception] not thrown."); } diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index f0389738..eb095225 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -106,7 +106,7 @@ final class TestCall { if (is_int($exception)) { $exceptionCode = $exception; - } elseif (class_exists($exception)) { + } elseif (class_exists($exception) || interface_exists($exception)) { $this->testCaseMethod ->proxies ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index a15819a2..b2d73d13 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -362,6 +362,7 @@ ✓ it catch exceptions ✓ it catch exceptions and messages ✓ it catch exceptions, messages and code + ✓ it catch exceptions by interface ✓ it can just define the message ✓ it can just define the code ✓ it not catch exceptions if given condition is false @@ -1201,8 +1202,11 @@ ✓ failures 6 ✓ failures 7 ✓ failures 8 + ✓ failures 9 + ✓ failures 10 ✓ failures with custom message ✓ not failures + ✓ not failures with interface ✓ closure missing parameter ✓ closure missing type-hint ✓ it can handle a non-defined exception @@ -2884,4 +2888,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2138 passed (4706 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2142 passed (4720 assertions) \ No newline at end of file diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index 72a1c28a..893c0ae5 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -16,6 +16,10 @@ it('catch exceptions, messages and code', function (): void { throw new Exception('Something bad happened', 1); })->throws(Exception::class, 'Something bad happened', 1); +it('catch exceptions by interface', function (): void { + throw new Exception('Something bad happened'); +})->throws(Throwable::class, 'Something bad happened'); + it('can just define the message', function (): void { throw new Exception('Something bad happened'); })->throws('Something bad happened'); diff --git a/tests/Features/Expect/toThrow.php b/tests/Features/Expect/toThrow.php index c754120d..18d3d1aa 100644 --- a/tests/Features/Expect/toThrow.php +++ b/tests/Features/Expect/toThrow.php @@ -28,7 +28,12 @@ test('passes', function (): void { ->toThrow(function (RuntimeException $e): void {}, 'actual message') ->and(function (): void { throw new CustomException('foo'); - })->toThrow(new CustomException('foo')); + })->toThrow(new CustomException('foo')) + ->and(function (): void { + throw new RuntimeException('actual message'); + })->toThrow(Throwable::class) + ->toThrow(function (Throwable $e): void {}) + ->toThrow(Throwable::class, 'actual message'); }); test('failures 1', function (): void { @@ -78,6 +83,16 @@ test('failures 8', function (): void { })->toThrow(new CustomException('expected message')); })->throws(ExpectationFailedException::class); +test('failures 9', function (): void { + expect(function (): void {})->toThrow(Throwable::class); +})->throws(ExpectationFailedException::class, 'Exception ['.Throwable::class.'] not thrown.'); + +test('failures 10', function (): void { + expect(function (): void { + throw new Exception('actual message'); + })->toThrow(JsonSerializable::class); +})->throws(ExpectationFailedException::class, 'Failed asserting that an instance of class Exception is an instance of interface JsonSerializable.'); + test('failures with custom message', function (): void { expect(function (): void { throw new RuntimeException('actual message'); @@ -90,6 +105,12 @@ test('not failures', function (): void { })->not->toThrow(RuntimeException::class); })->throws(ExpectationFailedException::class); +test('not failures with interface', function (): void { + expect(function (): void { + throw new RuntimeException; + })->not->toThrow(Throwable::class); +})->throws(ExpectationFailedException::class); + test('closure missing parameter', function (): void { expect(function (): void {})->toThrow(function (): void {}); })->throws(InvalidArgumentException::class, 'The given closure must have a single parameter type-hinted as the class string.'); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index c2859b99..3071de03 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 36 skipped, 1991 passed (4144 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 36 skipped, 1995 passed (4158 assertions)'; expect($output) ->toContain("Tests: {$expected}")