diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index d8137311..72bf02bb 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -273,7 +273,19 @@ trait Testable */ public function __test() { - return $this->__callClosure($this->__test, func_get_args()); + return $this->__callClosure($this->__test, $this->resolveTestArguments(func_get_args())); + } + + /** + * Resolve the passed arguments. Any Closures will be bound to the testcase and resolved. + * + * @throws Throwable + */ + private function resolveTestArguments(array $arguments): array + { + return array_map(function ($data) { + return $data instanceof Closure ? $this->__callClosure($data, []) : $data; + }, $arguments); } /** diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9790bb20..55b9eaab 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -96,6 +96,11 @@ ✓ more than two datasets with (2) / (4) / (5) ✓ more than two datasets with (2) / (4) / (6) ✓ 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 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 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 PASS Tests\Features\Exceptions ✓ it gives access the the underlying expectException @@ -614,5 +619,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 390 passed + Tests: 4 incompleted, 9 skipped, 395 passed \ No newline at end of file diff --git a/tests/Datasets/Bound.php b/tests/Datasets/Bound.php new file mode 100644 index 00000000..e442a658 --- /dev/null +++ b/tests/Datasets/Bound.php @@ -0,0 +1,11 @@ +foo = 'bar'; +}); + it('throws exception if dataset does not exist', function () { $this->expectException(DatasetDoesNotExist::class); $this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`."); @@ -223,3 +227,17 @@ test('more than two datasets', function ($text_a, $text_b, $text_c) use ($state, test('more than two datasets did the job right', function () use ($state) { expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324135136145146235236245246'); }); + +it('can resolve a dataset after the test case is available', function ($result) { + expect($result)->toBe('bar'); +})->with([ + function () { return $this->foo; }, +]); + +it('can resolve a dataset after the test case is available with shared yield sets', function ($result) { + expect($result)->toBeInt()->toBeLessThan(3); +})->with('bound.closure'); + +it('can resolve a dataset after the test case is available with shared array sets', function ($result) { + expect($result)->toBeInt()->toBeLessThan(3); +})->with('bound.array');