fix: toThrow() failure message when nothing is thrown (#2099)

Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com>
This commit is contained in:
Mr PHP
2026-12-06 20:25:45 +01:00
committed by GitHub
parent 7e06cff00f
commit cf17019a52
4 changed files with 19 additions and 5 deletions
+7 -3
View File
@@ -872,11 +872,15 @@ final class Expectation
Assert::assertTrue(true);
if (! $exception instanceof Throwable && ! class_exists($exception) && ! interface_exists($exception)) {
throw new ExpectationFailedException("Exception with message [$exception] not thrown.");
if ($exception instanceof Throwable) {
$failure = 'Exception ['.$exception::class.'] not thrown.';
} elseif (! class_exists($exception) && ! interface_exists($exception)) {
$failure = "Exception with message [$exception] not thrown.";
} else {
$failure = "Exception [$exception] not thrown.";
}
throw new ExpectationFailedException("Exception [$exception] not thrown.");
throw new ExpectationFailedException($message === '' ? $failure : $message."\n".$failure);
}
private function export(mixed $value): string
+3 -1
View File
@@ -1204,7 +1204,9 @@
✓ failures 8
✓ failures 9
✓ failures 10
✓ failures 11
✓ failures with custom message
✓ failures with custom message when nothing is thrown
✓ not failures
✓ not failures with interface
✓ closure missing parameter
@@ -2918,4 +2920,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 45 skipped, 2169 passed (4762 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 45 skipped, 2171 passed (4768 assertions)
+8
View File
@@ -93,12 +93,20 @@ test('failures 10', function (): void {
})->toThrow(JsonSerializable::class);
})->throws(ExpectationFailedException::class, 'Failed asserting that an instance of class Exception is an instance of interface JsonSerializable.');
test('failures 11', function (): void {
expect(function (): void {})->toThrow(new CustomException('expected message'));
})->throws(ExpectationFailedException::class, 'Exception ['.CustomException::class.'] not thrown.');
test('failures with custom message', function (): void {
expect(function (): void {
throw new RuntimeException('actual message');
})->toThrow(RuntimeException::class, 'expected message', 'oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');
test('failures with custom message when nothing is thrown', function (): void {
expect(function (): void {})->toThrow(RuntimeException::class, null, 'oh no!');
})->throws(ExpectationFailedException::class, "oh no!\nException [".RuntimeException::class.'] not thrown.');
test('not failures', function (): void {
expect(function (): void {
throw new RuntimeException;
+1 -1
View File
@@ -21,7 +21,7 @@ test('parallel', function () use ($run): void {
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
$profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile');
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 37 skipped, 2019 passed (4196 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 37 skipped, 2021 passed (4202 assertions)';
expect($output)
->toContain("Tests: {$expected}")