Datasets can now access the test case and are executed after the setup method has run.

This commit is contained in:
luke
2021-07-28 10:39:39 +01:00
parent 252f9a0e46
commit d8fae6d689
4 changed files with 43 additions and 2 deletions

View File

@ -57,7 +57,7 @@
"scripts": { "scripts": {
"lint": "php-cs-fixer fix -v", "lint": "php-cs-fixer fix -v",
"test:lint": "php-cs-fixer fix -v --dry-run", "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:unit": "php bin/pest --colors=always --exclude-group=integration",
"test:integration": "php bin/pest --colors=always --group=integration", "test:integration": "php bin/pest --colors=always --group=integration",
"update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always", "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always",

View File

@ -273,7 +273,19 @@ trait Testable
*/ */
public function __test() 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);
} }
/** /**

11
tests/Datasets/Bound.php Normal file
View File

@ -0,0 +1,11 @@
<?php
dataset('bound.closure', function () {
yield function () { return 1; };
yield function () { return 2; };
});
dataset('bound.array', [
function () { return 1; },
function () { return 2; },
]);

View File

@ -5,6 +5,10 @@ use Pest\Exceptions\DatasetAlreadyExist;
use Pest\Exceptions\DatasetDoesNotExist; use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Plugin; use Pest\Plugin;
beforeEach(function () {
$this->foo = 'bar';
});
it('throws exception if dataset does not exist', function () { it('throws exception if dataset does not exist', function () {
$this->expectException(DatasetDoesNotExist::class); $this->expectException(DatasetDoesNotExist::class);
$this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`."); $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) { test('more than two datasets did the job right', function () use ($state) {
expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324135136145146235236245246'); 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');