Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Inspect exception of assertThrows #52224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading