allows to check toThrow against an exception instance

This commit is contained in:
Fabio Ivona
2023-05-01 21:42:47 +02:00
parent 2e25eb59b8
commit f8930d20ae
2 changed files with 24 additions and 2 deletions

View File

@ -842,7 +842,7 @@ final class Expectation
* @param (Closure(Throwable): mixed)|string $exception * @param (Closure(Throwable): mixed)|string $exception
* @return self<TValue> * @return self<TValue>
*/ */
public function toThrow(callable|string $exception, string $exceptionMessage = null, string $message = ''): self public function toThrow(callable|string|Throwable $exception, string $exceptionMessage = null, string $message = ''): self
{ {
$callback = NullClosure::create(); $callback = NullClosure::create();
@ -864,6 +864,15 @@ final class Expectation
try { try {
($this->value)(); ($this->value)();
} catch (Throwable $e) { } catch (Throwable $e) {
if ($exception instanceof Throwable) {
expect($e)
->toBeInstanceOf($exception::class, $message)
->and($e->getMessage())->toBe($exceptionMessage ?? $exception->getMessage(), $message);
return $this;
}
if (! class_exists($exception)) { if (! class_exists($exception)) {
if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") { if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") {
Assert::assertTrue(true); Assert::assertTrue(true);
@ -888,7 +897,7 @@ final class Expectation
Assert::assertTrue(true); Assert::assertTrue(true);
if (! class_exists($exception)) { if (! $exception instanceof Throwable && ! class_exists($exception)) {
throw new ExpectationFailedException("Exception with message \"$exception\" not thrown."); throw new ExpectationFailedException("Exception with message \"$exception\" not thrown.");
} }

View File

@ -2,6 +2,10 @@
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
class CustomException extends Exception
{
}
test('passes', function () { test('passes', function () {
expect(function () { expect(function () {
throw new RuntimeException(); throw new RuntimeException();
@ -33,6 +37,9 @@ test('passes', function () {
throw new RuntimeException('actual message'); throw new RuntimeException('actual message');
})->toThrow(function (RuntimeException $e) { })->toThrow(function (RuntimeException $e) {
}, 'actual message'); }, 'actual message');
expect(function () {
throw new CustomException('foo');
})->toThrow(new CustomException('foo'));
}); });
test('failures 1', function () { test('failures 1', function () {
@ -79,6 +86,12 @@ test('failures 7', function () {
})->toThrow(RuntimeException::class, 'expected message'); })->toThrow(RuntimeException::class, 'expected message');
})->throws(ExpectationFailedException::class); })->throws(ExpectationFailedException::class);
test('failures 8', function () {
expect(function () {
throw new CustomException('actual message');
})->toThrow(new CustomException('expected message'));
})->throws(ExpectationFailedException::class);
test('failures with custom message', function () { test('failures with custom message', function () {
expect(function () { expect(function () {
throw new RuntimeException('actual message'); throw new RuntimeException('actual message');