diff --git a/src/Datasets.php b/src/Datasets.php index 19ed1359..ca43a31c 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest; use Closure; +use Generator; use Pest\Exceptions\DatasetAlreadyExist; use Pest\Exceptions\DatasetDoesNotExist; use SebastianBergmann\Exporter\Exporter; @@ -122,7 +123,10 @@ final class Datasets } 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 c1095e96..0f8280ec 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -101,6 +101,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 (...)) ✓ 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 @@ -727,5 +729,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 485 passed + Tests: 4 incompleted, 9 skipped, 487 passed \ No newline at end of file diff --git a/tests/Features/Datasets.php b/tests/Features/Datasets.php index 83b47277..dcc7db3a 100644 --- a/tests/Features/Datasets.php +++ b/tests/Features/Datasets.php @@ -247,6 +247,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([