Skip to content
Open
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: 0 additions & 1 deletion overrides/Runner/TestSuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ public function load(string $suiteClassFile): ReflectionClass
if (! class_exists($suiteClassName, false)) {
return $this->exceptionFor($suiteClassName, $suiteClassFile);
}

// @codeCoverageIgnoreEnd

if ($class->isSubclassOf(TestCase::class) && ! $class->isAbstract()) {
Expand Down
30 changes: 29 additions & 1 deletion src/Factories/TestCaseMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -102,6 +103,19 @@ final class TestCaseMethodFactory
*/
public array $depends = [];

/**
* The test's external dependencies (tests in other Pest test files).
*
* Example:
* [
* 'testCase' => 'Tests\Features\Depends',
* 'test' => 'first',
* ]
*
* @var array<int, array{testCase: string, test: string}>
*/
public array $dependsExternal = [];

/**
* The test's groups.
*
Expand Down Expand Up @@ -183,7 +197,7 @@ public function getClosure(): Closure
*/
public function receivesArguments(): bool
{
return $this->datasets !== [] || $this->depends !== [] || $this->repetitions > 1;
return $this->datasets !== [] || $this->depends !== [] || $this->dependsExternal !== [] || $this->repetitions > 1;
}

/**
Expand Down Expand Up @@ -220,6 +234,20 @@ public function buildForEvaluation(): string
);
}

foreach ($this->dependsExternal as $externalDepend) {
$depend = Str::evaluable(
$this->describing === []
? $externalDepend['test']
: Str::describe($this->describing, $externalDepend['test'])
);

$className = 'P\\'.$externalDepend['testCase'];
$this->attributes[] = new Attribute(
DependsExternal::class,
[$className, $depend],
);
}

if ($this->datasets !== [] || $this->repetitions > 1) {
$dataProviderName = $methodName.'_dataset';
$this->attributes[] = new Attribute(
Expand Down
18 changes: 18 additions & 0 deletions src/PendingCalls/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ public function depends(string ...$depends): self
return $this;
}

/**
* Sets external test dependencies from another Pest test file reference.
*
* @param string $testCase The Pest test reference (e.g. "Tests\Features\Depends")
* @param string ...$depends One or more test descriptions to depend on
*/
public function dependsExternal(string $testCase, string ...$depends): self
{
foreach ($depends as $depend) {
$this->testCaseMethod->dependsExternal[] = [
'testCase' => $testCase,
'test' => $depend,
];
}

return $this;
}

/**
* Sets the test group(s).
*/
Expand Down
10 changes: 9 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@
✓ describe block → nested describe → third in nested describe
✓ depends on test after describe block

PASS Tests\Features\DependsExternal
✓ dependsExternal
✓ dependsExternal chained
✓ dependsExternal chained, reversed
✓ depends on tests in an external file
✓ depends on tests in an external file with spread arguments
✓ depends on tests in an external file with defined arguments

PASS Tests\Features\DependsInheritance
✓ it is a test
✓ it uses correct parent class
Expand Down Expand Up @@ -1901,4 +1909,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1294 passed (2971 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1300 passed (2980 assertions)
30 changes: 30 additions & 0 deletions tests/Features/DependsExternal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

test('dependsExternal', function () {
expect(func_get_args())->toBe(['first', 'second']);
})->dependsExternal('Tests\Features\Depends', 'first', 'second');

test('dependsExternal chained', function () {
expect(func_get_args())->toBe(['first', 'second']);
})->dependsExternal('Tests\Features\Depends', 'first')
->dependsExternal('Tests\Features\Depends', 'second');

test('dependsExternal chained, reversed', function () {
expect(func_get_args())->toBe(['second', 'first']);
})->dependsExternal('Tests\Features\Depends', 'second')
->dependsExternal('Tests\Features\Depends', 'first');

test('depends on tests in an external file', function (string $first, string $second) {
expect($first)->toBe('first');
expect($second)->toBe('second');
})->dependsExternal('Tests\Features\Depends', 'first', 'second');

test('depends on tests in an external file with spread arguments', function (string ...$params) {
expect(func_get_args())->toBe($params);
expect($params)->toBe(['first', 'second']);
})->dependsExternal('Tests\Features\Depends', 'first', 'second');

test('depends on tests in an external file with defined arguments', function (string $first, string $second) {
expect($first)->toBe('first');
expect($second)->toBe('second');
})->dependsExternal('Tests\Features\Depends', 'first', 'second');
4 changes: 2 additions & 2 deletions tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
$file = file_get_contents(__FILE__);
$file = preg_replace(
'/\$expected = \'.*?\';/',
"\$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1278 passed (2920 assertions)';",
"\$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1284 passed (2929 assertions)';",
$file,
);
file_put_contents(__FILE__, $file);
}

$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1278 passed (2920 assertions)';
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1284 passed (2929 assertions)';

expect($output)
->toContain("Tests: {$expected}")
Expand Down