feat: clarfies that high order testing does not support bound datasets

This commit is contained in:
Nuno Maduro
2024-01-25 14:08:56 +00:00
parent 1d2fe2de2d
commit 2562d36518
3 changed files with 33 additions and 4 deletions

View File

@ -290,7 +290,7 @@ trait Testable
return $arguments; return $arguments;
} }
if (in_array($testParameterTypes[0], [Closure::class, 'callable'])) { if (isset($testParameterTypes[0]) && in_array($testParameterTypes[0], [Closure::class, 'callable'])) {
return $arguments; return $arguments;
} }

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Support; namespace Pest\Support;
use Closure; use Closure;
use InvalidArgumentException;
use Pest\Exceptions\ShouldNotHappen; use Pest\Exceptions\ShouldNotHappen;
use Pest\TestSuite; use Pest\TestSuite;
use ReflectionClass; use ReflectionClass;
@ -66,9 +67,17 @@ final class Reflection
{ {
$test = TestSuite::getInstance()->test; $test = TestSuite::getInstance()->test;
return $test instanceof \PHPUnit\Framework\TestCase if (! $test instanceof \PHPUnit\Framework\TestCase) {
? Closure::fromCallable($callable)->bindTo($test)(...$test->providedData()) return self::bindCallable($callable);
: 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());
} }
/** /**

View File

@ -361,3 +361,23 @@ it('can correctly resolve a bound dataset that returns an array but wants to be
]); ]);
todo('forbids to define tests in Datasets dirs and Datasets.php files'); 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);