diff --git a/composer.json b/composer.json index bee8f8b7..1c6e5591 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "scripts": { "lint": "php-cs-fixer fix -v", "test:lint": "php-cs-fixer fix -v --dry-run", - "test:types": "phpstan analyse --ansi --memory-limit=0", + "test:types": "phpstan analyse --ansi", "test:unit": "php bin/pest --colors=always --exclude-group=integration", "test:integration": "php bin/pest --colors=always --group=integration", "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always", 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/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');