handles toThrow exception with a "class not found" error

This commit is contained in:
Fabio Ivona
2022-02-18 18:01:09 +01:00
parent e103623ecb
commit dccd8239dd
3 changed files with 25 additions and 5 deletions

View File

@ -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;

View File

@ -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'];
}
}

View File

@ -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