mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #484 from fabio-ivona/fix-restore-missing-exception-in-v2
Fix toThrow expectation passing when exception doesn't exist
This commit is contained in:
@ -6,6 +6,7 @@ namespace Pest\Mixins;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Closure;
|
||||
use Error;
|
||||
use InvalidArgumentException;
|
||||
use Pest\Exceptions\InvalidExpectationValue;
|
||||
use Pest\Support\Arr;
|
||||
@ -822,8 +823,12 @@ final class Expectation
|
||||
|
||||
try {
|
||||
($this->value)();
|
||||
} catch (Throwable $e) { // @phpstan-ignore-line
|
||||
} catch (Throwable $e) {
|
||||
if (!class_exists($exception)) {
|
||||
if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Assert::assertStringContainsString($exception, $e->getMessage());
|
||||
|
||||
return $this;
|
||||
|
||||
@ -58,3 +58,19 @@ test('closure missing parameter', function () {
|
||||
test('closure missing type-hint', function () {
|
||||
expect(function () {})->toThrow(function ($e) {});
|
||||
})->throws(InvalidArgumentException::class, 'The given closure\'s parameter must be type-hinted as the class string.');
|
||||
|
||||
it('can handle a non-defined exception', function () {
|
||||
expect(function () {
|
||||
throw new NonExistingException();
|
||||
})->toThrow(NonExistingException::class);
|
||||
})->throws(Error::class, 'Class "NonExistingException" not found');
|
||||
|
||||
it('can handle a class not found Error', function () {
|
||||
expect(function () {
|
||||
throw new NonExistingException();
|
||||
})->toThrow('Class "NonExistingException" not found');
|
||||
|
||||
expect(function () {
|
||||
throw new NonExistingException();
|
||||
})->toThrow(Error::class, 'Class "NonExistingException" not found');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user