Skip to content

Commit

Permalink
feat: clarfies that high order testing does not support bound datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jan 25, 2024
1 parent 1d2fe2d commit 2562d36
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private function __resolveTestArguments(array $arguments): array
return $arguments;
}

if (in_array($testParameterTypes[0], [Closure::class, 'callable'])) {
if (isset($testParameterTypes[0]) && in_array($testParameterTypes[0], [Closure::class, 'callable'])) {
return $arguments;
}

Expand Down
15 changes: 12 additions & 3 deletions src/Support/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Pest\Support;

use Closure;
use InvalidArgumentException;
use Pest\Exceptions\ShouldNotHappen;
use Pest\TestSuite;
use ReflectionClass;
Expand Down Expand Up @@ -66,9 +67,17 @@ public static function bindCallableWithData(callable $callable): mixed
{
$test = TestSuite::getInstance()->test;

return $test instanceof \PHPUnit\Framework\TestCase
? Closure::fromCallable($callable)->bindTo($test)(...$test->providedData())
: self::bindCallable($callable);
if (! $test instanceof \PHPUnit\Framework\TestCase) {
return self::bindCallable($callable);
}

foreach ($test->providedData() as $value) {
if ($value instanceof Closure) {
throw new InvalidArgumentException('Bound datasets are not supported while doing high order testing.');
}
}

return Closure::fromCallable($callable)->bindTo($test)(...$test->providedData());
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Features/DatasetsTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,23 @@ function () {
]);

todo('forbids to define tests in Datasets dirs and Datasets.php files');

dataset('greeting-string', [
'formal' => 'Evening',
'informal' => 'yo',
]);

it('may be used with high order')
->with('greeting-string')
->expect(fn (string $greeting) => $greeting)
->throwsNoExceptions();

dataset('greeting-bound', [
'formal' => fn () => 'Evening',
'informal' => fn () => 'yo',
]);

it('may be used with high order even when bound')
->with('greeting-bound')
->expect(fn (string $greeting) => $greeting)
->throws(InvalidArgumentException::class);

0 comments on commit 2562d36

Please sign in to comment.