diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 0d1cd17c..b2266879 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -45,9 +45,11 @@ final class TestCall /** * Asserts that the test throws the given `$exceptionClass` when called. */ - public function throws(string $exception, string $exceptionMessage = null): TestCall + public function throws(string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall { - if (class_exists($exception)) { + if (is_int($exception)) { + $exceptionCode = $exception; + } else if (class_exists($exception)) { $this->testCaseMethod ->proxies ->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]); @@ -61,6 +63,12 @@ final class TestCall ->add(Backtrace::file(), Backtrace::line(), 'expectExceptionMessage', [$exceptionMessage]); } + if (is_int($exceptionCode)) { + $this->testCaseMethod + ->proxies + ->add(Backtrace::file(), Backtrace::line(), 'expectExceptionCode', [$exceptionCode]); + } + return $this; } @@ -69,7 +77,7 @@ final class TestCall * * @param (callable(): bool)|bool $condition */ - public function throwsIf(callable|bool $condition, string $exception, string $exceptionMessage = null): TestCall + public function throwsIf(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall { $condition = is_callable($condition) ? $condition @@ -78,7 +86,7 @@ final class TestCall }; if ($condition()) { - return $this->throws($exception, $exceptionMessage); + return $this->throws($exception, $exceptionMessage, $exceptionCode); } return $this; diff --git a/tests/Features/Exceptions.php b/tests/Features/Exceptions.php index 9970c2a9..71482fbe 100644 --- a/tests/Features/Exceptions.php +++ b/tests/Features/Exceptions.php @@ -14,10 +14,18 @@ it('catch exceptions and messages', function () { throw new Exception('Something bad happened'); })->throws(Exception::class, 'Something bad happened'); +it('catch exceptions, messages and code', function () { + throw new Exception('Something bad happened', 1); +})->throws(Exception::class, 'Something bad happened', 1); + it('can just define the message', function () { throw new Exception('Something bad happened'); })->throws('Something bad happened'); +it('can just define the code', function () { + throw new Exception('Something bad happened', 1); +})->throws(1); + it('not catch exceptions if given condition is false', function () { $this->assertTrue(true); })->throwsIf(false, Exception::class); @@ -30,10 +38,22 @@ it('catch exceptions and messages if given condition is true', function () { throw new Exception('Something bad happened'); })->throwsIf(true, Exception::class, 'Something bad happened'); +it('catch exceptions, messages and code if given condition is true', function () { + throw new Exception('Something bad happened', 1); +})->throwsIf(true, Exception::class, 'Something bad happened', 1); + it('can just define the message if given condition is true', function () { throw new Exception('Something bad happened'); })->throwsIf(true, 'Something bad happened'); +it('can just define the code if given condition is true', function () { + throw new Exception('Something bad happened', 1); +})->throwsIf(true, 1); + it('can just define the message if given condition is 1', function () { throw new Exception('Something bad happened'); })->throwsIf(1, 'Something bad happened'); + +it('can just define the code if given condition is 1', function () { + throw new Exception('Something bad happened', 1); +})->throwsIf(1, 1);