From 99ea9f42e585a13013ece7f28e9ce6f31211c62a Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 8 Jul 2021 18:32:02 +0100 Subject: [PATCH 1/2] Allows you to just specify an exception message when calling `throws`. --- src/PendingObjects/TestCall.php | 10 +++++++--- tests/Features/Exceptions.php | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index 41d85826..4ad60bde 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -61,9 +61,13 @@ final class TestCall */ public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall { - $this->testCaseFactory - ->proxies - ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]); + if (class_exists($exceptionClass)) { + $this->testCaseFactory + ->proxies + ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]); + } else { + $exceptionMessage = $exceptionClass; + } if (is_string($exceptionMessage)) { $this->testCaseFactory diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index 5e7e51a9..37eaaeb9 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -13,3 +13,7 @@ it('catch exceptions', function () { it('catch exceptions and messages', function () { throw new Exception('Something bad happened'); })->throws(Exception::class, 'Something bad happened'); + +it('can just define the message', function () { + throw new Exception('Something bad happened'); +})->throws('Something bad happened'); From dd05452eddc4e7ca761653c6168067f19fdef0bc Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 8 Jul 2021 18:39:09 +0100 Subject: [PATCH 2/2] Renames a property to be more inclusive. --- src/PendingObjects/TestCall.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index 4ad60bde..81349be1 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -59,14 +59,14 @@ final class TestCall /** * Asserts that the test throws the given `$exceptionClass` when called. */ - public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall + public function throws(string $exception, string $exceptionMessage = null): TestCall { - if (class_exists($exceptionClass)) { + if (class_exists($exception)) { $this->testCaseFactory ->proxies - ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]); + ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]); } else { - $exceptionMessage = $exceptionClass; + $exceptionMessage = $exception; } if (is_string($exceptionMessage)) {