mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Merge pull request #424 from pestphp/better-dataset-support
[2.x] Vastly improves the logic around bound datasets to make them more user friendly
This commit is contained in:
@ -7,6 +7,7 @@ namespace Pest\Concerns;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Pest\Support\ChainableClosure;
|
use Pest\Support\ChainableClosure;
|
||||||
use Pest\Support\ExceptionTrace;
|
use Pest\Support\ExceptionTrace;
|
||||||
|
use Pest\Support\Reflection;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -210,7 +211,28 @@ trait Testable
|
|||||||
*/
|
*/
|
||||||
private function __resolveTestArguments(array $arguments): array
|
private function __resolveTestArguments(array $arguments): array
|
||||||
{
|
{
|
||||||
return array_map(fn ($data) => $data instanceof Closure ? $this->__callClosure($data, []) : $data, $arguments);
|
if (count($arguments) !== 1) {
|
||||||
|
return $arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$arguments[0] instanceof Closure) {
|
||||||
|
return $arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
|
||||||
|
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
|
||||||
|
|
||||||
|
if (in_array($testParameterTypes[0], ['Closure', 'callable'])) {
|
||||||
|
return $arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
$boundDatasetResult = $this->__callClosure($arguments[0], []);
|
||||||
|
|
||||||
|
if (count($testParameterTypes) === 1 || !is_array($boundDatasetResult)) {
|
||||||
|
return [$boundDatasetResult];
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values($boundDatasetResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -205,4 +205,12 @@ final class Reflection
|
|||||||
|
|
||||||
return $arguments;
|
return $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function getFunctionVariable(Closure $function, string $key)
|
||||||
|
{
|
||||||
|
return (new ReflectionFunction($function))->getStaticVariables()[$key] ?? null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,11 +96,20 @@
|
|||||||
✓ more than two datasets with (2) / (4) / (5)
|
✓ more than two datasets with (2) / (4) / (5)
|
||||||
✓ more than two datasets with (2) / (4) / (6)
|
✓ more than two datasets with (2) / (4) / (6)
|
||||||
✓ more than two datasets did the job right
|
✓ more than two datasets did the job right
|
||||||
✓ it can resolve a dataset after the test case is available with (Closure Object (...))
|
✓ it can resolve a dataset after the test case is available with (Closure Object (...)) #1
|
||||||
|
✓ it can resolve a dataset after the test case is available with (Closure Object (...)) #2
|
||||||
✓ it can resolve a dataset after the test case is available with shared yield sets with (Closure Object (...)) #1
|
✓ it can resolve a dataset after the test case is available with shared yield sets with (Closure Object (...)) #1
|
||||||
✓ it can resolve a dataset after the test case is available with shared yield sets with (Closure Object (...)) #2
|
✓ it can resolve a dataset after the test case is available with shared yield sets with (Closure Object (...)) #2
|
||||||
✓ it can resolve a dataset after the test case is available with shared array sets with (Closure Object (...)) #1
|
✓ it can resolve a dataset after the test case is available with shared array sets with (Closure Object (...)) #1
|
||||||
✓ it can resolve a dataset after the test case is available with shared array sets with (Closure Object (...)) #2
|
✓ it can resolve a dataset after the test case is available with shared array sets with (Closure Object (...)) #2
|
||||||
|
✓ it resolves a potential bound dataset logically with ('foo', Closure Object (...))
|
||||||
|
✓ it resolves a potential bound dataset logically even when the closure comes first with (Closure Object (...), 'bar')
|
||||||
|
✓ it will not resolve a closure if it is type hinted as a closure with (Closure Object (...)) #1
|
||||||
|
✓ it will not resolve a closure if it is type hinted as a closure with (Closure Object (...)) #2
|
||||||
|
✓ it will not resolve a closure if it is type hinted as a callable with (Closure Object (...)) #1
|
||||||
|
✓ it will not resolve a closure if it is type hinted as a callable with (Closure Object (...)) #2
|
||||||
|
✓ it can correctly resolve a bound dataset that returns an array with (Closure Object (...))
|
||||||
|
✓ it can correctly resolve a bound dataset that returns an array but wants to be spread with (Closure Object (...))
|
||||||
|
|
||||||
PASS Tests\Features\Exceptions
|
PASS Tests\Features\Exceptions
|
||||||
✓ it gives access the the underlying expectException
|
✓ it gives access the the underlying expectException
|
||||||
@ -720,5 +729,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 9 skipped, 478 passed
|
Tests: 4 incompleted, 9 skipped, 487 passed
|
||||||
|
|
||||||
@ -234,6 +234,7 @@ it('can resolve a dataset after the test case is available', function ($result)
|
|||||||
expect($result)->toBe('bar');
|
expect($result)->toBe('bar');
|
||||||
})->with([
|
})->with([
|
||||||
function () { return $this->foo; },
|
function () { return $this->foo; },
|
||||||
|
[function () { return $this->foo; }],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
it('can resolve a dataset after the test case is available with shared yield sets', function ($result) {
|
it('can resolve a dataset after the test case is available with shared yield sets', function ($result) {
|
||||||
@ -243,3 +244,43 @@ it('can resolve a dataset after the test case is available with shared yield set
|
|||||||
it('can resolve a dataset after the test case is available with shared array sets', function ($result) {
|
it('can resolve a dataset after the test case is available with shared array sets', function ($result) {
|
||||||
expect($result)->toBeInt()->toBeLessThan(3);
|
expect($result)->toBeInt()->toBeLessThan(3);
|
||||||
})->with('bound.array');
|
})->with('bound.array');
|
||||||
|
|
||||||
|
it('resolves a potential bound dataset logically', function ($foo, $bar) {
|
||||||
|
expect($foo)->toBe('foo');
|
||||||
|
expect($bar())->toBe('bar');
|
||||||
|
})->with([
|
||||||
|
['foo', function () { return 'bar'; }], // This should be passed as a closure because we've passed multiple arguments
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('resolves a potential bound dataset logically even when the closure comes first', function ($foo, $bar) {
|
||||||
|
expect($foo())->toBe('foo');
|
||||||
|
expect($bar)->toBe('bar');
|
||||||
|
})->with([
|
||||||
|
[function () { return 'foo'; }, 'bar'], // This should be passed as a closure because we've passed multiple arguments
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('will not resolve a closure if it is type hinted as a closure', function (Closure $data) {
|
||||||
|
expect($data())->toBeString();
|
||||||
|
})->with([
|
||||||
|
function () { return 'foo'; },
|
||||||
|
function () { return 'bar'; },
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('will not resolve a closure if it is type hinted as a callable', function (callable $data) {
|
||||||
|
expect($data())->toBeString();
|
||||||
|
})->with([
|
||||||
|
function () { return 'foo'; },
|
||||||
|
function () { return 'bar'; },
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('can correctly resolve a bound dataset that returns an array', function (array $data) {
|
||||||
|
expect($data)->toBe(['foo', 'bar', 'baz']);
|
||||||
|
})->with([
|
||||||
|
function () { return ['foo', 'bar', 'baz']; },
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('can correctly resolve a bound dataset that returns an array but wants to be spread', function (string $foo, string $bar, string $baz) {
|
||||||
|
expect([$foo, $bar, $baz])->toBe(['foo', 'bar', 'baz']);
|
||||||
|
})->with([
|
||||||
|
function () { return ['foo', 'bar', 'baz']; },
|
||||||
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user