From 36130eb7a0ba97b6d2710fbabc101525c34481ca Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 18 Feb 2022 16:40:17 +0100 Subject: [PATCH 1/3] adds a failing test --- tests/Features/Expect/toThrow.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Features/Expect/toThrow.php b/tests/Features/Expect/toThrow.php index 434ced44..b5c66abf 100644 --- a/tests/Features/Expect/toThrow.php +++ b/tests/Features/Expect/toThrow.php @@ -58,3 +58,19 @@ test('closure missing parameter', function () { test('closure missing type-hint', function () { expect(function () {})->toThrow(function ($e) {}); })->throws(InvalidArgumentException::class, 'The given closure\'s parameter must be type-hinted as the class string.'); + +it('can handle a non-defined exception', function () { + expect(function () { + throw new NonExistingException(); + })->toThrow(NonExistingException::class); +})->throws(Error::class, 'Class "NonExistingException" not found'); + +it('can handle a class not found Error', function () { + expect(function () { + throw new NonExistingException(); + })->toThrow('Class "NonExistingException" not found'); + + expect(function () { + throw new NonExistingException(); + })->toThrow(Error::class, 'Class "NonExistingException" not found'); +}); From 10b204e19d0f6721e3e2ca3c4a46bfbd7e0c4547 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 18 Feb 2022 16:40:31 +0100 Subject: [PATCH 2/3] handles toThrow exception with a "class not found" error --- src/Mixins/Expectation.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 3fbd2110..64235cfc 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -6,6 +6,7 @@ namespace Pest\Mixins; use BadMethodCallException; use Closure; +use Error; use InvalidArgumentException; use Pest\Exceptions\InvalidExpectationValue; use Pest\Support\Arr; @@ -824,6 +825,10 @@ final class Expectation ($this->value)(); } catch (Throwable $e) { // @phpstan-ignore-line if (!class_exists($exception)) { + if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") { + throw $e; + } + Assert::assertStringContainsString($exception, $e->getMessage()); return $this; From 2c3a2960406367c4438a51fd531dd4d5c7dcc329 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Fri, 18 Feb 2022 16:42:34 +0100 Subject: [PATCH 3/3] fix types --- src/Mixins/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 64235cfc..5823d61c 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -823,7 +823,7 @@ final class Expectation try { ($this->value)(); - } catch (Throwable $e) { // @phpstan-ignore-line + } catch (Throwable $e) { if (!class_exists($exception)) { if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") { throw $e;