This commit is contained in:
Fabio Ivona
2023-03-13 17:38:52 +01:00
parent 88e576c3a3
commit 1bd9c9e60d
2 changed files with 59 additions and 16 deletions

View File

@ -228,17 +228,30 @@ trait Testable
$this->__description = self::$__latestDescription = $this->dataName() ? $method->description.' with '.$this->dataName() : $method->description; $this->__description = self::$__latestDescription = $this->dataName() ? $method->description.' with '.$this->dataName() : $method->description;
if (count($arguments) !== 1) {
return $arguments;
}
if (! $arguments[0] instanceof Closure) {
return $arguments;
}
$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure'); $underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
$testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest)); $testParameterTypes = array_values(Reflection::getFunctionArguments($underlyingTest));
if (count($arguments) !== 1) {
foreach ($arguments as $argumentIndex => $argumentValue) {
if (!$argumentValue instanceof Closure) {
continue;
}
if (in_array($testParameterTypes[$argumentIndex], [\Closure::class, 'callable'])) {
continue;
}
$arguments[$argumentIndex] = $this->__callClosure($argumentValue, []);
}
return $arguments;
}
if (!$arguments[0] instanceof Closure) {
return $arguments;
}
if (in_array($testParameterTypes[0], [\Closure::class, 'callable'])) { if (in_array($testParameterTypes[0], [\Closure::class, 'callable'])) {
return $arguments; return $arguments;
} }
@ -247,7 +260,7 @@ trait Testable
if (count($testParameterTypes) === 1) { if (count($testParameterTypes) === 1) {
return [$boundDatasetResult]; return [$boundDatasetResult];
} }
if (! is_array($boundDatasetResult)) { if (!is_array($boundDatasetResult)) {
return [$boundDatasetResult]; return [$boundDatasetResult];
} }

View File

@ -262,9 +262,34 @@ it('can resolve a dataset after the test case is available', function ($result)
function () { function () {
return $this->foo; return $this->foo;
}, },
[function () { [
function () {
return $this->foo;
},
],
]);
it('can resolve a dataset after the test case is available with multiple datasets', function ($result, $result2) {
expect($result)->toBe('bar');
})->with([
function () {
return $this->foo; return $this->foo;
}], },
[
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) {
@ -279,18 +304,23 @@ it('resolves a potential bound dataset logically', function ($foo, $bar) {
expect($foo)->toBe('foo'); expect($foo)->toBe('foo');
expect($bar())->toBe('bar'); expect($bar())->toBe('bar');
})->with([ })->with([
['foo', function () { [
return 'bar'; 'foo',
}], // This should be passed as a closure because we've passed multiple arguments 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) { it('resolves a potential bound dataset logically even when the closure comes first', function ($foo, $bar) {
expect($foo())->toBe('foo'); expect($foo())->toBe('foo');
expect($bar)->toBe('bar'); expect($bar)->toBe('bar');
})->with([ })->with([
[function () { [
return 'foo'; function () {
}, 'bar'], // This should be passed as a closure because we've passed multiple arguments 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) { it('will not resolve a closure if it is type hinted as a closure', function (Closure $data) {