mirror of
https://github.com/pestphp/pest.git
synced 2026-12-06 15:52:16 +01:00
fix: toThrow() and throws() with an interface (#2098)
* fix: toThrow() and throws() with an interface * test: cover not->toThrow() with an interface --------- Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -847,7 +847,7 @@ final class Expectation
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (! class_exists($exception)) {
|
||||
if (! class_exists($exception) && ! interface_exists($exception)) {
|
||||
if ($e instanceof Error && "Class \"$exception\" not found" === $e->getMessage()) {
|
||||
Assert::assertTrue(true);
|
||||
|
||||
@@ -872,7 +872,7 @@ final class Expectation
|
||||
|
||||
Assert::assertTrue(true);
|
||||
|
||||
if (! $exception instanceof Throwable && ! class_exists($exception)) {
|
||||
if (! $exception instanceof Throwable && ! class_exists($exception) && ! interface_exists($exception)) {
|
||||
throw new ExpectationFailedException("Exception with message [$exception] not thrown.");
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ final class TestCall
|
||||
{
|
||||
if (is_int($exception)) {
|
||||
$exceptionCode = $exception;
|
||||
} elseif (class_exists($exception)) {
|
||||
} elseif (class_exists($exception) || interface_exists($exception)) {
|
||||
$this->testCaseMethod
|
||||
->proxies
|
||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
|
||||
|
||||
@@ -362,6 +362,7 @@
|
||||
✓ it catch exceptions
|
||||
✓ it catch exceptions and messages
|
||||
✓ it catch exceptions, messages and code
|
||||
✓ it catch exceptions by interface
|
||||
✓ it can just define the message
|
||||
✓ it can just define the code
|
||||
✓ it not catch exceptions if given condition is false
|
||||
@@ -1201,8 +1202,11 @@
|
||||
✓ failures 6
|
||||
✓ failures 7
|
||||
✓ failures 8
|
||||
✓ failures 9
|
||||
✓ failures 10
|
||||
✓ failures with custom message
|
||||
✓ not failures
|
||||
✓ not failures with interface
|
||||
✓ closure missing parameter
|
||||
✓ closure missing type-hint
|
||||
✓ it can handle a non-defined exception
|
||||
@@ -2884,4 +2888,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, 44 skipped, 2138 passed (4706 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 44 skipped, 2142 passed (4720 assertions)
|
||||
@@ -16,6 +16,10 @@ it('catch exceptions, messages and code', function (): void {
|
||||
throw new Exception('Something bad happened', 1);
|
||||
})->throws(Exception::class, 'Something bad happened', 1);
|
||||
|
||||
it('catch exceptions by interface', function (): void {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws(Throwable::class, 'Something bad happened');
|
||||
|
||||
it('can just define the message', function (): void {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws('Something bad happened');
|
||||
|
||||
@@ -28,7 +28,12 @@ test('passes', function (): void {
|
||||
->toThrow(function (RuntimeException $e): void {}, 'actual message')
|
||||
->and(function (): void {
|
||||
throw new CustomException('foo');
|
||||
})->toThrow(new CustomException('foo'));
|
||||
})->toThrow(new CustomException('foo'))
|
||||
->and(function (): void {
|
||||
throw new RuntimeException('actual message');
|
||||
})->toThrow(Throwable::class)
|
||||
->toThrow(function (Throwable $e): void {})
|
||||
->toThrow(Throwable::class, 'actual message');
|
||||
});
|
||||
|
||||
test('failures 1', function (): void {
|
||||
@@ -78,6 +83,16 @@ test('failures 8', function (): void {
|
||||
})->toThrow(new CustomException('expected message'));
|
||||
})->throws(ExpectationFailedException::class);
|
||||
|
||||
test('failures 9', function (): void {
|
||||
expect(function (): void {})->toThrow(Throwable::class);
|
||||
})->throws(ExpectationFailedException::class, 'Exception ['.Throwable::class.'] not thrown.');
|
||||
|
||||
test('failures 10', function (): void {
|
||||
expect(function (): void {
|
||||
throw new Exception('actual message');
|
||||
})->toThrow(JsonSerializable::class);
|
||||
})->throws(ExpectationFailedException::class, 'Failed asserting that an instance of class Exception is an instance of interface JsonSerializable.');
|
||||
|
||||
test('failures with custom message', function (): void {
|
||||
expect(function (): void {
|
||||
throw new RuntimeException('actual message');
|
||||
@@ -90,6 +105,12 @@ test('not failures', function (): void {
|
||||
})->not->toThrow(RuntimeException::class);
|
||||
})->throws(ExpectationFailedException::class);
|
||||
|
||||
test('not failures with interface', function (): void {
|
||||
expect(function (): void {
|
||||
throw new RuntimeException;
|
||||
})->not->toThrow(Throwable::class);
|
||||
})->throws(ExpectationFailedException::class);
|
||||
|
||||
test('closure missing parameter', function (): void {
|
||||
expect(function (): void {})->toThrow(function (): void {});
|
||||
})->throws(InvalidArgumentException::class, 'The given closure must have a single parameter type-hinted as the class string.');
|
||||
|
||||
@@ -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, 36 skipped, 1991 passed (4144 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 36 skipped, 1995 passed (4158 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user