Skip to content

Commit 1f28941

Browse files
committed
Add ability to extend TestCall to create custom chained helpers
1 parent 48a1de2 commit 1f28941

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/PendingCalls/TestCall.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Pest\PendingCalls;
66

77
use Closure;
8+
use Pest\Concerns\Extendable;
89
use Pest\Exceptions\InvalidArgumentException;
910
use Pest\Exceptions\TestDescriptionMissing;
1011
use Pest\Factories\Attribute;
@@ -30,6 +31,9 @@
3031
final class TestCall
3132
{
3233
use Describable;
34+
use Extendable {
35+
extend as traitExtend;
36+
}
3337

3438
/**
3539
* The list of test case factory attributes.
@@ -642,9 +646,25 @@ public function __get(string $name): self
642646
*/
643647
public function __call(string $name, array $arguments): self
644648
{
649+
if (self::hasExtend($name)) {
650+
$extend = self::$extends[$name]->bindTo($this, TestCall::class);
651+
652+
if ($extend != false) { // @pest-arch-ignore-line
653+
return $extend(...$arguments);
654+
}
655+
}
656+
645657
return $this->addChain(Backtrace::file(), Backtrace::line(), $name, $arguments);
646658
}
647659

660+
public function extend(string $name, Closure $extend): void
661+
{
662+
$this->traitExtend($name, $extend);
663+
664+
$this->description = "extend: $name";
665+
$this->testCaseMethod->closure = fn (): null => null;
666+
}
667+
648668
/**
649669
* Add a chain to the test case factory. Omitting the arguments will treat it as a property accessor.
650670
*

tests/.snapshots/success.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,10 @@
12611261
✓ a test
12621262
✓ higher order message test
12631263

1264+
PASS Tests\Features\TestCallExtend
1265+
✓ it uses fooBar extension with ('foo')
1266+
✓ it uses fooBar extension with ('bar')
1267+
12641268
PASS Tests\Features\ThrowsNoExceptions
12651269
✓ it allows access to the underlying expectNotToPerformAssertions method
12661270
✓ it allows performing no expectations without being risky
@@ -1580,4 +1584,4 @@
15801584
WARN Tests\Visual\Version
15811585
- visual snapshot of help command output
15821586

1583-
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1093 passed (2644 assertions)
1587+
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 28 skipped, 1095 passed (2646 assertions)

tests/Features/TestCallExtend.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use function PHPUnit\Framework\assertTrue;
4+
5+
test()->extend('fooBar', function () {
6+
return $this->with([
7+
'foo',
8+
'bar',
9+
]);
10+
});
11+
12+
it('uses fooBar extension', function ($value) {
13+
assertTrue(in_array($value, [
14+
'foo',
15+
'bar',
16+
]));
17+
})->fooBar();

tests/Visual/Parallel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
test('parallel', function () use ($run) {
1818
expect($run('--exclude-group=integration'))
19-
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1083 passed (2620 assertions)')
19+
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 17 todos, 19 skipped, 1085 passed (2622 assertions)')
2020
->toContain('Parallel: 3 processes');
2121
})->skipOnWindows();
2222

0 commit comments

Comments
 (0)