Skip to content

Commit

Permalink
Merge pull request #949 from pestphp/fix-depends-with-describe
Browse files Browse the repository at this point in the history
[2.x] Fix the Usage of `depends` With `describe`
  • Loading branch information
nunomaduro committed Sep 6, 2023
2 parents cc6c5bf + 3927177 commit 644fade
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion overrides/Runner/TestSuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function load(string $suiteClassFile): ReflectionClass
continue;
}

if ($class->isAbstract() || ($class->getFileName() !== $suiteClassFile)) {
if ($class->isAbstract() || ($suiteClassFile !== $class->getFileName())) {
if (! str_contains($class->getFileName(), 'TestCaseFactory.php')) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function sequence(mixed ...$callbacks): self
$index = isset($callbacks[$index + 1]) ? $index + 1 : 0;
}

if (count($callbacks) > $valuesCount) {
if ($valuesCount < count($callbacks)) {
throw new OutOfRangeException('Sequence expectations are more than the iterable items.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Factories/Annotations/Depends.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Depends implements AddsAnnotations
public function __invoke(TestCaseMethodFactory $method, array $annotations): array
{
foreach ($method->depends as $depend) {
$depend = Str::evaluable($depend);
$depend = Str::evaluable($method->describing !== null ? Str::describe($method->describing, $depend) : $depend);

$annotations[] = "@depends $depend";
}
Expand Down
4 changes: 2 additions & 2 deletions src/Factories/TestCaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function hasMethod(string $methodName): bool
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
}

if (Str::evaluable($method->description) === $methodName) {
if ($methodName === Str::evaluable($method->description)) {
return true;
}
}
Expand All @@ -259,7 +259,7 @@ public function getMethod(string $methodName): TestCaseMethodFactory
throw ShouldNotHappen::fromMessage('The test description may not be empty.');
}

if (Str::evaluable($method->description) === $methodName) {
if ($methodName === Str::evaluable($method->description)) {
return $method;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ public function toThrow(callable|string|Throwable $exception, string $exceptionM
}

if (! class_exists($exception)) {
if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") {
if ($e instanceof Error && "Class \"$exception\" not found" === $e->getMessage()) {
Assert::assertTrue(true);

throw $e;
Expand Down
5 changes: 3 additions & 2 deletions src/PendingCalls/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Pest\Support\Exporter;
use Pest\Support\HigherOrderCallables;
use Pest\Support\NullClosure;
use Pest\Support\Str;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -209,7 +210,7 @@ public function skipOnLinux(): self
*/
private function skipOn(string $osFamily, string $message): self
{
return PHP_OS_FAMILY === $osFamily
return $osFamily === PHP_OS_FAMILY
? $this->skip($message)
: $this;
}
Expand Down Expand Up @@ -361,7 +362,7 @@ public function __destruct()
{
if (! is_null($this->describing)) {
$this->testCaseMethod->describing = $this->describing;
$this->testCaseMethod->description = sprintf('`%s` → %s', $this->describing, $this->testCaseMethod->description);
$this->testCaseMethod->description = Str::describe($this->describing, $this->testCaseMethod->description); // @phpstan-ignore-line
}

$this->testSuite->tests->set($this->testCaseMethod);
Expand Down
10 changes: 9 additions & 1 deletion src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function endsWith(string $target, string $search): bool
return true;
}

return substr($target, -$length) === $search;
return $search === substr($target, -$length);
}

/**
Expand Down Expand Up @@ -92,4 +92,12 @@ public static function after(string $subject, string $search): string
{
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}

/**
* Creates a describe block as `$right` → `$left` format.
*/
public static function describe(string $right, string $left): string
{
return sprintf('`%s` → %s', $right, $left);
}
}
20 changes: 20 additions & 0 deletions tests/Features/Describe.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,23 @@
expect($foo)->toBe(3);
});
})->with([3]);

describe('depends on describe', function () {
test('foo', function () {
expect('foo')->toBe('foo');
});

test('bar', function () {
expect('bar')->toBe('bar');
})->depends('foo');
});

describe('depends on describe using with', function () {
test('foo', function ($foo) {
expect($foo)->toBe(3);
});

test('bar', function ($foo) {
expect($foo + $foo)->toBe(6);
})->depends('foo');
})->with([3]);

0 comments on commit 644fade

Please sign in to comment.