diff --git a/src/Repositories/DatasetsRepository.php b/src/Repositories/DatasetsRepository.php index 47f710ef..3b00b7b4 100644 --- a/src/Repositories/DatasetsRepository.php +++ b/src/Repositories/DatasetsRepository.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest\Repositories; use Closure; +use Generator; use Pest\Exceptions\DatasetAlreadyExists; use Pest\Exceptions\DatasetDoesNotExist; use Pest\Exceptions\ShouldNotHappen; @@ -155,7 +156,10 @@ final class DatasetsRepository } if ($datasets[$index] instanceof Traversable) { - $datasets[$index] = iterator_to_array($datasets[$index], false); + $preserveKeysForArrayIterator = $datasets[$index] instanceof Generator + && is_string($datasets[$index]->key()); + + $datasets[$index] = iterator_to_array($datasets[$index], $preserveKeysForArrayIterator); } foreach ($datasets[$index] as $key => $values) { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 4df70c77..9ee929b7 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -107,6 +107,8 @@ ✓ eager registered wrapped datasets with Generator functions with (3) ✓ eager registered wrapped datasets with Generator functions with (4) ✓ eager registered wrapped datasets with Generator functions did the job right + ✓ eager registered wrapped datasets with Generator functions display description with data set "taylor" + ✓ eager registered wrapped datasets with Generator functions display description with data set "james" ✓ it can resolve a dataset after the test case is available with (Closure Object (...)) #1 ✓ it can resolve a dataset after the test case is available with (Closure Object (...)) #2 ✓ it can resolve a dataset after the test case is available with shared yield sets with (Closure Object (...)) #1 @@ -897,4 +899,4 @@ PASS Tests\Visual\Version ✓ visual snapshot of help command output - Tests: 4 incomplete, 2 todos, 18 skipped, 622 passed (1509 assertions) \ No newline at end of file + diff --git a/tests/Features/DatasetsTests.php b/tests/Features/DatasetsTests.php index 1698824b..aeb0ba82 100644 --- a/tests/Features/DatasetsTests.php +++ b/tests/Features/DatasetsTests.php @@ -249,6 +249,13 @@ test('eager registered wrapped datasets with Generator functions did the job rig expect($wrapped_generator_state->text)->toBe('1234'); }); +test('eager registered wrapped datasets with Generator functions display description', function ($wrapped_generator_state_with_description) { + expect($wrapped_generator_state_with_description)->not->toBeEmpty(); +})->with(function () { + yield 'taylor' => 'taylor@laravel.com'; + yield 'james' => 'james@laravel.com'; +}); + it('can resolve a dataset after the test case is available', function ($result) { expect($result)->toBe('bar'); })->with([