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] Add assertThrown() #54046

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Draft
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 @@ -213,4 +213,31 @@ protected function assertThrows(Closure $test, string|Closure $expectedClass = T

return $this;
}

/**
* Assert that in instance of $exceptionClass was thrown and return the exception.
*
* @template TThrowable of Throwable
*
* @param Closure $test
* @param class-string<TThrowable> $exceptionClass
* @param string $noExceptionThrownMessageTemplate
* @return TThrowable
*/
protected static function assertThrown(
Closure $test,
string $exceptionClass = Throwable::class,
string $noExceptionThrownMessageTemplate = 'Did not throw expected exception %s'
)
{
try {
$test();
} catch (Throwable $t) {
if ($t instanceof $exceptionClass) {
return $t;
}
}

Assert::fail(sprintf($noExceptionThrownMessageTemplate, $exceptionClass));
}
}
74 changes: 74 additions & 0 deletions tests/Testing/Concerns/InteractsWithExceptionHandlingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Illuminate\Tests\Testing\Concerns;

use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class InteractsWithExceptionHandlingTest extends TestCase
{
use InteractsWithExceptionHandling;

public function test_assertThrown_returns_exception_when_Throwable()
{
$func = static fn(): never => throw new RuntimeException("My runtime exception");

$thrownException = $this->assertThrown($func);

$this->assertSame("My runtime exception", $thrownException->getMessage());
}

public function test_assertThrown_returns_exception_when_custom_exception()
{
$func = static function(): never {
throw (new CustomExceptionForInteractsWithExceptionHandlingTest("A message"))->setValue(1993);
};

$thrownException1 = $this->assertThrown($func, RuntimeException::class);
$thrownException2 = $this->assertThrown($func, CustomExceptionForInteractsWithExceptionHandlingTest::class);

foreach([$thrownException1, $thrownException2] as $exception) {
$this->assertSame("A message", $exception->getMessage());
$this->assertSame(1993, $exception->value);
$this->assertInstanceOf(CustomExceptionForInteractsWithExceptionHandlingTest::class, $exception);
}
}

public function test_assertThrown_fails_if_not_thrown()
{
$func = static fn(): int => 200;

try {
$this->assertThrown($func);
$this->fail("assertThrown did not raise an assertion error");
} catch (AssertionFailedError $assertionFailedError) {
$this->assertSame("Did not throw expected exception Throwable", $assertionFailedError->getMessage());
}
}

public function test_assertThrown_fails_if_not_subclass()
{
$func = static fn(): never => throw new CustomExceptionForInteractsWithExceptionHandlingTest("invalid argument exception");

try {
$this->assertThrown($func, \InvalidArgumentException::class, "abcd %s");
$this->fail("assertThrown did not raise an assertion error");
} catch (AssertionFailedError $assertionFailedError) {
$this->assertSame("abcd InvalidArgumentException", $assertionFailedError->getMessage());
}
}
}

class CustomExceptionForInteractsWithExceptionHandlingTest extends RuntimeException
{
public $value;

public function setValue($value): self
{
$this->value = $value;

return $this;
}
}
Loading