Vastly improves the logic around bound datasets to make them more user friendly.

This commit is contained in:
Luke Downing
2021-10-19 21:40:40 +01:00
parent de46ee0f64
commit eed3ed5513
4 changed files with 85 additions and 6 deletions

View File

@ -7,8 +7,10 @@ namespace Pest\Concerns;
use Closure;
use Pest\Support\ChainableClosure;
use Pest\Support\ExceptionTrace;
use Pest\Support\Reflection;
use Pest\TestSuite;
use PHPUnit\Framework\ExecutionOrderDependency;
use function sprintf;
use Throwable;
/**
@ -257,7 +259,7 @@ trait Testable
*/
public function toString(): string
{
return \sprintf(
return sprintf(
'%s::%s',
self::$__filename,
$this->__description
@ -283,9 +285,28 @@ trait Testable
*/
private function resolveTestArguments(array $arguments): array
{
return array_map(function ($data) {
return $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, 'factoryTest');
$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);
}
/**

View File

@ -204,4 +204,12 @@ final class Reflection
return $arguments;
}
/**
* @return mixed
*/
public static function getFunctionVariable(Closure $function, string $key)
{
return (new ReflectionFunction($function))->getStaticVariables()[$key] ?? null;
}
}