Skip to content

Commit

Permalink
[11.x] Inspect exception of assertThrows (#52224)
Browse files Browse the repository at this point in the history
* Inspect exception of assertThrows

* Update InteractsWithExceptionHandling.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
gdebrauwer and taylorotwell authored Jul 22, 2024
1 parent 29a58fe commit 1301d9c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Testing\Fakes\ExceptionHandlerFake;
use Illuminate\Support\Traits\ReflectsClosures;
use Illuminate\Testing\Assert;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Console\Application as ConsoleApplication;
Expand All @@ -13,6 +14,8 @@

trait InteractsWithExceptionHandling
{
use ReflectsClosures;

/**
* The original exception handler.
*
Expand Down Expand Up @@ -169,18 +172,22 @@ public function renderForConsole($output, Throwable $e)
* Assert that the given callback throws an exception with the given message when invoked.
*
* @param \Closure $test
* @param class-string<\Throwable> $expectedClass
* @param \Closure|class-string<\Throwable> $expectedClass
* @param string|null $expectedMessage
* @return $this
*/
protected function assertThrows(Closure $test, string $expectedClass = Throwable::class, ?string $expectedMessage = null)
protected function assertThrows(Closure $test, string|Closure $expectedClass = Throwable::class, ?string $expectedMessage = null)
{
[$expectedClass, $expectedClassCallback] = $expectedClass instanceof Closure
? [$this->firstClosureParameterType($expectedClass), $expectedClass]
: [$expectedClass, null];

try {
$test();

$thrown = false;
} catch (Throwable $exception) {
$thrown = $exception instanceof $expectedClass;
$thrown = $exception instanceof $expectedClass && ($expectedClassCallback === null || $expectedClassCallback($exception));

$actualMessage = $exception->getMessage();
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,42 @@ public function testAssertExceptionIsThrown()
if ($testFailed) {
Assert::fail('assertThrows failed: non matching message are thrown.');
}

$this->assertThrows(function () {
throw new CustomException('Some message.');
}, function (CustomException $exception) {
return $exception->getMessage() === 'Some message.';
});

try {
$this->assertThrows(function () {
throw new CustomException('Some message.');
}, function (CustomException $exception) {
return false;
});
$testFailed = true;
} catch (AssertionFailedError) {
$testFailed = false;
}

if ($testFailed) {
Assert::fail('assertThrows failed: exception callback succeeded.');
}

try {
$this->assertThrows(function () {
throw new Exception('Some message.');
}, function (CustomException $exception) {
return true;
});
$testFailed = true;
} catch (AssertionFailedError) {
$testFailed = false;
}

if ($testFailed) {
Assert::fail('assertThrows failed: non matching exceptions are thrown.');
}
}

public function testItReportsDuplicateExceptions()
Expand Down

0 comments on commit 1301d9c

Please sign in to comment.