diff --git a/src/Expectation.php b/src/Expectation.php index ae7d690d..3bc3bbd8 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,6 +6,7 @@ namespace Pest; use BadMethodCallException; use Closure; +use Error; use InvalidArgumentException; use Pest\Concerns\Extendable; use Pest\Concerns\RetrievesValues; @@ -911,8 +912,12 @@ final class Expectation try { ($this->value)(); - } catch (Throwable $e) { // @phpstan-ignore-line + } catch (Throwable $e) { if (!class_exists($exception)) { + if ($e instanceof Error && (bool) preg_match("/Class [\"']{$exception}[\"'] not found/", $e->getMessage())) { + throw $e; + } + Assert::assertStringContainsString($exception, $e->getMessage()); return $this; diff --git a/src/Support/Backtrace.php b/src/Support/Backtrace.php index 0fe46c06..699a0355 100644 --- a/src/Support/Backtrace.php +++ b/src/Support/Backtrace.php @@ -26,6 +26,7 @@ final class Backtrace $current = null; foreach (debug_backtrace(self::BACKTRACE_OPTIONS) as $trace) { + assert(array_key_exists(self::FILE, $trace)); if (Str::endsWith($trace[self::FILE], (string) realpath('vendor/phpunit/phpunit/src/Util/FileLoader.php'))) { break; } @@ -45,7 +46,11 @@ final class Backtrace */ public static function file(): string { - return debug_backtrace(self::BACKTRACE_OPTIONS)[1][self::FILE]; + $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; + + assert(array_key_exists(self::FILE, $trace)); + + return $trace[self::FILE]; } /** @@ -53,7 +58,11 @@ final class Backtrace */ public static function dirname(): string { - return dirname(debug_backtrace(self::BACKTRACE_OPTIONS)[1][self::FILE]); + $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; + + assert(array_key_exists(self::FILE, $trace)); + + return dirname($trace[self::FILE]); } /** @@ -61,6 +70,10 @@ final class Backtrace */ public static function line(): int { - return debug_backtrace(self::BACKTRACE_OPTIONS)[1]['line']; + $trace = debug_backtrace(self::BACKTRACE_OPTIONS)[1]; + + assert(array_key_exists('line', $trace)); + + return $trace['line']; } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 0c6a57ab..26f896f5 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -496,6 +496,8 @@ ✓ not failures ✓ closure missing parameter ✓ closure missing type-hint + ✓ it can handle a non-defined exception + ✓ it can handle a class not found Error PASS Tests\Features\Expect\unless ✓ it pass @@ -720,5 +722,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 478 passed + Tests: 4 incompleted, 9 skipped, 480 passed \ No newline at end of file