From d8fae6d689db529ce66e916333253391d0446b39 Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 28 Jul 2021 10:39:39 +0100 Subject: [PATCH] Datasets can now access the test case and are executed after the setup method has run. --- composer.json | 2 +- src/Concerns/Testable.php | 14 +++++++++++++- tests/Datasets/Bound.php | 11 +++++++++++ tests/Features/Datasets.php | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/Datasets/Bound.php 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');