Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to extend TestCall to create custom chained helpers #1267

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions src/PendingCalls/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Pest\PendingCalls;

use Closure;
use Pest\Concerns\Extendable;
use Pest\Concerns\Testable;
use Pest\Exceptions\InvalidArgumentException;
use Pest\Exceptions\TestDescriptionMissing;
Expand All @@ -31,6 +32,9 @@
final class TestCall // @phpstan-ignore-line
{
use Describable;
use Extendable {
extend as traitExtend;
}

/**
* The list of test case factory attributes.
Expand Down Expand Up @@ -643,9 +647,25 @@ public function __get(string $name): self
*/
public function __call(string $name, array $arguments): self
{
if (self::hasExtend($name)) {
$extend = self::$extends[$name]->bindTo($this, TestCall::class);

if ($extend != false) { // @pest-arch-ignore-line
return $extend(...$arguments);
}
}

return $this->addChain(Backtrace::file(), Backtrace::line(), $name, $arguments);
}

public function extend(string $name, Closure $extend): void
{
$this->traitExtend($name, $extend);

$this->description = "extend: $name";
$this->testCaseMethod->closure = fn (): null => null;
Comment on lines +665 to +666
Copy link
Contributor Author

@JHWelch JHWelch Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required because extend is being called non-statically. With this combination no errors are thrown and no output is included from a test()->extend(...) call.

I would need to add a different StaticallyExtendable or something like it to be able to use TestCall::extend(...) which is another possible API.

}

/**
* Add a chain to the test case factory. Omitting the arguments will treat it as a property accessor.
*
Expand Down
6 changes: 5 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,10 @@
✓ a test
✓ higher order message test

PASS Tests\Features\TestCallExtend
✓ it uses fooBar extension with ('foo')
✓ it uses fooBar extension with ('bar')

PASS Tests\Features\ThrowsNoExceptions
✓ it allows access to the underlying expectNotToPerformAssertions method
✓ it allows performing no expectations without being risky
Expand Down Expand Up @@ -1698,4 +1702,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1144 passed (2736 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 33 skipped, 1146 passed (2738 assertions)
17 changes: 17 additions & 0 deletions tests/Features/TestCallExtend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use function PHPUnit\Framework\assertTrue;

test()->extend('fooBar', function () {
return $this->with([
'foo',
'bar',
]);
});

it('uses fooBar extension', function ($value) {
assertTrue(in_array($value, [
'foo',
'bar',
]));
})->fooBar();
2 changes: 1 addition & 1 deletion tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1134 passed (2712 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 38 todos, 24 skipped, 1136 passed (2714 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

Expand Down