Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Actions/LoadStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static function in(string $rootPath): void
$directory = new RecursiveDirectoryIterator($filename);
$iterator = new RecursiveIteratorIterator($directory);
foreach ($iterator as $file) {
/* @phpstan-ignore-next-line */
$filename = $file->__toString();
if (Str::endsWith($filename, '.php') && file_exists($filename)) {
require_once $filename;
Expand Down
7 changes: 6 additions & 1 deletion src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BadMethodCallException;
use Closure;
use Error;
use InvalidArgumentException;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
Expand Down Expand Up @@ -911,8 +912,12 @@ public function toThrow($exception, string $exceptionMessage = null): Expectatio

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;
Expand Down
2 changes: 0 additions & 2 deletions src/Logging/JUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public function addSkippedTest(Test $test, Throwable $t, float $time): void
$this->doAddSkipped();
}

/** @phpstan-ignore-next-line */
public function startTestSuite(TestSuite $suite): void
{
$testSuite = $this->document->createElement('testsuite');
Expand Down Expand Up @@ -212,7 +211,6 @@ public function startTestSuite(TestSuite $suite): void
$this->testSuiteTimes[$this->testSuiteLevel] = 0;
}

/** @phpstan-ignore-next-line */
public function endTestSuite(TestSuite $suite): void
{
$this->testSuites[$this->testSuiteLevel]->setAttribute(
Expand Down
2 changes: 0 additions & 2 deletions src/Logging/TeamCity.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ private function successfulTestCount(TestResult $result): int
- $result->riskyCount();
}

/** @phpstan-ignore-next-line */
public function startTestSuite(TestSuite $suite): void
{
$suiteName = $suite->getName();
Expand Down Expand Up @@ -164,7 +163,6 @@ private static function escapeValue(string $text): string
);
}

/** @phpstan-ignore-next-line */
public function endTestSuite(TestSuite $suite): void
{
$suiteName = $suite->getName();
Expand Down
19 changes: 16 additions & 3 deletions src/Support/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function testFile(): string
$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;
}
Expand All @@ -45,22 +46,34 @@ public static function testFile(): string
*/
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];
}

/**
* Returns the dirname that called the current function/method.
*/
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]);
}

/**
* Returns the line that called the current function/method.
*/
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'];
}
}
4 changes: 3 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

12 changes: 12 additions & 0 deletions tests/Features/Expect/toThrow.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@
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);

it('can handle a class not found Error', function () {
expect(function () {
throw new NonExistingException();
})->toThrow(Error::class);
});