From dd5a11a61f5e056aecb2e82294a2d7a8f691585b Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:28:46 +0200 Subject: [PATCH 01/10] updated TestCall.php to store multiple datasets Took 1 hour 59 minutes --- src/PendingObjects/TestCall.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index cd65ae76..b6547596 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -81,7 +81,7 @@ final class TestCall */ public function with($data): TestCall { - $this->testCaseFactory->dataset = $data; + $this->testCaseFactory->datasets[] = $data; return $this; } From 3b784060b8eaaf0db16b006ec632a3e516207ba7 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:29:29 +0200 Subject: [PATCH 02/10] updated TestCaseFactory.php to store multiple datasets Took 44 seconds --- src/Factories/TestCaseFactory.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index ddba1083..5285fc9e 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -62,9 +62,9 @@ final class TestCaseFactory /** * Holds the dataset, if any. * - * @var Closure|iterable|string|null + * @var array|string> */ - public $dataset; + public $datasets = []; /** * The FQN of the test case class. @@ -155,7 +155,7 @@ final class TestCaseFactory return $testCase; }; - $datasets = Datasets::resolve($this->description, $this->dataset); + $datasets = Datasets::resolve($this->description, $this->datasets); return array_map($createTest, array_keys($datasets), $datasets); } From 19a45c856ea7d59e076a623dca4a2c196e2330df Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:30:11 +0200 Subject: [PATCH 03/10] updates Dataset::resolve to generate a matrix of values from multiple datasets Took 42 seconds --- src/Datasets.php | 77 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 8eff980c..424e7c2e 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -51,37 +51,61 @@ final class Datasets /** * Resolves the current dataset to an array value. * - * @param Traversable|Closure|iterable|string|null $data + * @param array|string> $datasets * * @return array */ - public static function resolve(string $description, $data): array + public static function resolve(string $description, array $datasets): array { /* @phpstan-ignore-next-line */ - if (is_null($data) || empty($data)) { + if (empty($datasets)) { return [$description => []]; } - if (is_string($data)) { - $data = self::get($data); + $processedDatasets = []; + + foreach ($datasets as $index => $data) { + $processedDataset = []; + + if (is_string($data)) { + $datasets[$index] = self::get($data); + } + + if (is_callable($datasets[$index])) { + $datasets[$index] = call_user_func($datasets[$index]); + } + + if ($datasets[$index] instanceof Traversable) { + $datasets[$index] = iterator_to_array($datasets[$index]); + } + + foreach ($datasets[$index] as $key => $values) { + $values = is_array($values) ? $values : [$values]; + $processedDataset[] = [ + 'label' => self::getDataSetDescription($key, $values), + 'values' => $values, + ]; + } + + $processedDatasets[] = $processedDataset; } - if (is_callable($data)) { - $data = call_user_func($data); - } - - if ($data instanceof Traversable) { - $data = iterator_to_array($data); - } + $datasetCombinations = self::getDataSetsCombinations($processedDatasets); $dataSetDescriptions = []; - $dataSetValues = []; + $dataSetValues = []; - foreach ($data as $key => $values) { - $values = is_array($values) ? $values : [$values]; + foreach ($datasetCombinations as $datasets){ + $partialDescriptions = []; + $values = []; - $dataSetDescriptions[] = $description . self::getDataSetDescription($key, $values); - $dataSetValues[] = $values; + foreach ($datasets as $dataset_data){ + $partialDescriptions[] = $dataset_data['label']; + $values = array_merge($values, $dataset_data['values']); + } + + $dataSetDescriptions[] = $description." with ".implode(" / ", $partialDescriptions); + $dataSetValues[] = $values; } foreach (array_count_values($dataSetDescriptions) as $descriptionToCheck => $count) { @@ -103,6 +127,21 @@ final class Datasets return $namedData; } + private static function getDataSetsCombinations($combinations): array + { + $result = [[]]; + foreach ($combinations as $index => $values) { + $tmp = []; + foreach ($result as $resultItem) { + foreach ($values as $value) { + $tmp[] = array_merge($resultItem, [$index => $value]); + } + } + $result = $tmp; + } + return $result; + } + /** * @param int|string $key * @param array $data @@ -112,9 +151,9 @@ final class Datasets $exporter = new Exporter(); if (is_int($key)) { - return \sprintf(' with (%s)', $exporter->shortenedRecursiveExport($data)); + return \sprintf('(%s)', $exporter->shortenedRecursiveExport($data)); } - return \sprintf(' with data set "%s"', $key); + return \sprintf('data set "%s"', $key); } } From 60afbb2c2084fc5d1859c94dbb9ad97aa9807dc4 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:30:50 +0200 Subject: [PATCH 04/10] adds new test to check dataset matrix generation Took 39 seconds --- tests/Unit/Datasets.php | 54 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Datasets.php b/tests/Unit/Datasets.php index 39070038..85bcbbf1 100644 --- a/tests/Unit/Datasets.php +++ b/tests/Unit/Datasets.php @@ -4,8 +4,10 @@ use Pest\Datasets; it('show only the names of named datasets in their description', function () { $descriptions = array_keys(Datasets::resolve('test description', [ - 'one' => [1], - 'two' => [[2]], + [ + 'one' => [1], + 'two' => [[2]], + ], ])); expect($descriptions[0])->toBe('test description with data set "one"'); @@ -14,10 +16,54 @@ it('show only the names of named datasets in their description', function () { it('show the actual dataset of non-named datasets in their description', function () { $descriptions = array_keys(Datasets::resolve('test description', [ - [1], - [[2]], + [ + [1], + [[2]], + ], ])); expect($descriptions[0])->toBe('test description with (1)'); expect($descriptions[1])->toBe('test description with (array(2))'); }); + +$state = new stdClass(); +$state->combinations = [ + ['a', 'b', 'c', 1, 2, 'bar', 'foo', 'baz'], + ['a', 'b', 'c', 1, 2, 'zip', 'zap', 'zop'], + ['a', 'b', 'c', 3, 4, 'bar', 'foo', 'baz'], + ['a', 'b', 'c', 3, 4, 'zip', 'zap', 'zop'], + ['a', 'b', 'c', 5, 6, 'bar', 'foo', 'baz'], + ['a', 'b', 'c', 5, 6, 'zip', 'zap', 'zop'], + ['d', 'e', 'f', 1, 2, 'bar', 'foo', 'baz'], + ['d', 'e', 'f', 1, 2, 'zip', 'zap', 'zop'], + ['d', 'e', 'f', 3, 4, 'bar', 'foo', 'baz'], + ['d', 'e', 'f', 3, 4, 'zip', 'zap', 'zop'], + ['d', 'e', 'f', 5, 6, 'bar', 'foo', 'baz'], + ['d', 'e', 'f', 5, 6, 'zip', 'zap', 'zop'], + ['g', 'h', 'i', 1, 2, 'bar', 'foo', 'baz'], + ['g', 'h', 'i', 1, 2, 'zip', 'zap', 'zop'], + ['g', 'h', 'i', 3, 4, 'bar', 'foo', 'baz'], + ['g', 'h', 'i', 3, 4, 'zip', 'zap', 'zop'], + ['g', 'h', 'i', 5, 6, 'bar', 'foo', 'baz'], + ['g', 'h', 'i', 5, 6, 'zip', 'zap', 'zop'], +]; + +it('generates a matrix with given datasets', function ($a1, $a2, $a3, $b1, $b2, $c1, $c2, $c3) use ($state) { + $combinations = $state->combinations; + $set = $combinations[0]; + array_shift($combinations); + $state->combinations = $combinations; + + expect([$a1, $a2, $a3, $b1, $b2, $c1, $c2, $c3])->toMatchArray($set); +})->with([ + 'dataset_aa' => ['a1' => 'a', 'a2' => 'b', 'a3' => 'c'], + 'dataset_ab' => ['a1' => 'd', 'a2' => 'e', 'a3' => 'f'], + 'dataset_ac' => ['a1' => 'g', 'a2' => 'h', 'a3' => 'i'], +])->with([ + 'dataset_ba' => ['b1' => 1, 'b2' => 2], + 'dataset_bb' => ['b1' => 3, 'b2' => 4], + 'dataset_bc' => ['b1' => 5, 'b2' => 6], +])->with([ + ['c1' => 'bar', 'c2' => 'foo', 'c3' => 'baz'], + ['c1' => 'zip', 'c2' => 'zap', 'c3' => 'zop'], +]); From 294c41f0dce6ba2ec6fa3ed50ba90b8444df5cdf Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:33:43 +0200 Subject: [PATCH 05/10] lint fixes Took 3 minutes --- src/Datasets.php | 21 +++++++++++---------- tests/Unit/Datasets.php | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 424e7c2e..0d343e0c 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -51,7 +51,7 @@ final class Datasets /** * Resolves the current dataset to an array value. * - * @param array|string> $datasets + * @param array|string> $datasets * * @return array */ @@ -80,10 +80,10 @@ final class Datasets } foreach ($datasets[$index] as $key => $values) { - $values = is_array($values) ? $values : [$values]; + $values = is_array($values) ? $values : [$values]; $processedDataset[] = [ 'label' => self::getDataSetDescription($key, $values), - 'values' => $values, + 'values' => $values, ]; } @@ -93,19 +93,19 @@ final class Datasets $datasetCombinations = self::getDataSetsCombinations($processedDatasets); $dataSetDescriptions = []; - $dataSetValues = []; + $dataSetValues = []; - foreach ($datasetCombinations as $datasets){ + foreach ($datasetCombinations as $datasets) { $partialDescriptions = []; - $values = []; + $values = []; - foreach ($datasets as $dataset_data){ + foreach ($datasets as $dataset_data) { $partialDescriptions[] = $dataset_data['label']; - $values = array_merge($values, $dataset_data['values']); + $values = array_merge($values, $dataset_data['values']); } - $dataSetDescriptions[] = $description." with ".implode(" / ", $partialDescriptions); - $dataSetValues[] = $values; + $dataSetDescriptions[] = $description . ' with ' . implode(' / ', $partialDescriptions); + $dataSetValues[] = $values; } foreach (array_count_values($dataSetDescriptions) as $descriptionToCheck => $count) { @@ -139,6 +139,7 @@ final class Datasets } $result = $tmp; } + return $result; } diff --git a/tests/Unit/Datasets.php b/tests/Unit/Datasets.php index 85bcbbf1..dc3bd3c5 100644 --- a/tests/Unit/Datasets.php +++ b/tests/Unit/Datasets.php @@ -26,7 +26,7 @@ it('show the actual dataset of non-named datasets in their description', functio expect($descriptions[1])->toBe('test description with (array(2))'); }); -$state = new stdClass(); +$state = new stdClass(); $state->combinations = [ ['a', 'b', 'c', 1, 2, 'bar', 'foo', 'baz'], ['a', 'b', 'c', 1, 2, 'zip', 'zap', 'zop'], From 3117f11faeeda63dcd2fb26df921a9515c8c7af6 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:36:51 +0200 Subject: [PATCH 06/10] phpstan fixes Took 3 minutes --- src/Datasets.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 0d343e0c..196d08cd 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -95,11 +95,11 @@ final class Datasets $dataSetDescriptions = []; $dataSetValues = []; - foreach ($datasetCombinations as $datasets) { + foreach ($datasetCombinations as $datasetCombination) { $partialDescriptions = []; $values = []; - foreach ($datasets as $dataset_data) { + foreach ($datasetCombination as $dataset_data) { $partialDescriptions[] = $dataset_data['label']; $values = array_merge($values, $dataset_data['values']); } @@ -127,7 +127,12 @@ final class Datasets return $namedData; } - private static function getDataSetsCombinations($combinations): array + /** + * @param array $combinations + * + * @return array + */ + private static function getDataSetsCombinations(array $combinations): array { $result = [[]]; foreach ($combinations as $index => $values) { From 296e1c37e836fce09409229d9cb5b3f200e6b037 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 24 May 2021 23:43:53 +0200 Subject: [PATCH 07/10] updates snapshots Took 7 minutes --- tests/.snapshots/success.txt | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index d89b30e8..981a7ff8 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -170,6 +170,24 @@ PASS Tests\Unit\Datasets ✓ it show only the names of named datasets in their description ✓ it show the actual dataset of non-named datasets in their description + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_ba" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_ba" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bb" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bb" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bc" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bc" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_ba" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_ba" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bb" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bb" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bc" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bc" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_ba" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_ba" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bb" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bb" / ('zip', 'zap', 'zop') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bc" / ('bar', 'foo', 'baz') + ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bc" / ('zip', 'zap', 'zop') PASS Tests\Unit\Plugins\Version ✓ it outputs the version when --version is used @@ -220,5 +238,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 7 skipped, 120 passed + Tests: 7 skipped, 138 passed \ No newline at end of file From 838ac273ab48472085389d5139048f385ae5d17e Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Tue, 25 May 2021 23:56:46 +0200 Subject: [PATCH 08/10] writed tests with multiple datasets Took 1 hour 6 minutes --- src/PendingObjects/TestCall.php | 8 +-- tests/.snapshots/success.txt | 62 ++++++++++++++++------- tests/Features/Datasets.php | 86 +++++++++++++++++++++++++++++++ tests/Unit/Datasets.php | 90 +++++++++++++++++++-------------- 4 files changed, 185 insertions(+), 61 deletions(-) diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index b6547596..39486565 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -77,11 +77,13 @@ final class TestCall * Runs the current test multiple times with * each item of the given `iterable`. * - * @param \Closure|iterable|string $data + * @param array<\Closure|iterable|string> $data */ - public function with($data): TestCall + public function with(...$data): TestCall { - $this->testCaseFactory->datasets[] = $data; + foreach ($data as $dataset) { + $this->testCaseFactory->datasets[] = $dataset; + } return $this; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 981a7ff8..e62d3220 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -51,6 +51,45 @@ ✓ it creates unique test case names with ('Name 2', Pest\Plugin Object (), true) ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3 ✓ it creates unique test case names - count + ✓ lazy multiple datasets with (1) / (3) + ✓ lazy multiple datasets with (1) / (4) + ✓ lazy multiple datasets with (2) / (3) + ✓ lazy multiple datasets with (2) / (4) + ✓ lazy multiple datasets did the job right + ✓ eager multiple datasets with (1) / (3) + ✓ eager multiple datasets with (1) / (4) + ✓ eager multiple datasets with (2) / (3) + ✓ eager multiple datasets with (2) / (4) + ✓ eager multiple datasets did the job right + ✓ lazy registered multiple datasets with (1) / (1) + ✓ lazy registered multiple datasets with (1) / (2) + ✓ lazy registered multiple datasets with (2) / (1) + ✓ lazy registered multiple datasets with (2) / (2) + ✓ lazy registered multiple datasets did the job right + ✓ eager registered multiple datasets with (1) / (1) + ✓ eager registered multiple datasets with (1) / (2) + ✓ eager registered multiple datasets with (2) / (1) + ✓ eager registered multiple datasets with (2) / (2) + ✓ eager registered multiple datasets did the job right + ✓ eager wrapped registered multiple datasets with (1) / (1) + ✓ eager wrapped registered multiple datasets with (1) / (2) + ✓ eager wrapped registered multiple datasets with (2) / (1) + ✓ eager wrapped registered multiple datasets with (2) / (2) + ✓ eager wrapped registered multiple datasets did the job right + ✓ named multiple datasets with data set "one" / data set "three" + ✓ named multiple datasets with data set "one" / data set "four" + ✓ named multiple datasets with data set "two" / data set "three" + ✓ named multiple datasets with data set "two" / data set "four" + ✓ named multiple datasets did the job right + ✓ more than two datasets with (1) / (3) / (5) + ✓ more than two datasets with (1) / (3) / (6) + ✓ more than two datasets with (1) / (4) / (5) + ✓ more than two datasets with (1) / (4) / (6) + ✓ more than two datasets with (2) / (3) / (5) + ✓ more than two datasets with (2) / (3) / (6) + ✓ more than two datasets with (2) / (4) / (5) + ✓ more than two datasets with (2) / (4) / (6) + ✓ more than two datasets did the job right PASS Tests\Features\Exceptions ✓ it gives access the the underlying expectException @@ -170,24 +209,9 @@ PASS Tests\Unit\Datasets ✓ it show only the names of named datasets in their description ✓ it show the actual dataset of non-named datasets in their description - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_ba" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_ba" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bb" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bb" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bc" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_aa" / data set "dataset_bc" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_ba" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_ba" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bb" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bb" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bc" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ab" / data set "dataset_bc" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_ba" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_ba" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bb" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bb" / ('zip', 'zap', 'zop') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bc" / ('bar', 'foo', 'baz') - ✓ it generates a matrix with given datasets with data set "dataset_ac" / data set "dataset_bc" / ('zip', 'zap', 'zop') + ✓ it show only the names of multiple named datasets in their description + ✓ it show the actual dataset of multiple non-named datasets in their description + ✓ it show the correct description for mixed named and not-named datasets PASS Tests\Unit\Plugins\Version ✓ it outputs the version when --version is used @@ -238,5 +262,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 7 skipped, 138 passed + Tests: 7 skipped, 162 passed \ No newline at end of file diff --git a/tests/Features/Datasets.php b/tests/Features/Datasets.php index dabda849..b7aeb478 100644 --- a/tests/Features/Datasets.php +++ b/tests/Features/Datasets.php @@ -137,3 +137,89 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo it('creates unique test case names - count', function () use (&$counter) { expect($counter)->toBe(6); }); + +$datasets_a = [[1], [2]]; +$datasets_b = [[3], [4]]; + +test('lazy multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) { + $state->text .= $text_a . $text_b; + expect($datasets_a)->toContain([$text_a]); + expect($datasets_b)->toContain([$text_b]); +})->with($datasets_a, $datasets_b); + +test('lazy multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('12121212121213142324'); +}); + +$state->text = ''; + +test('eager multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) { + $state->text .= $text_a . $text_b; + expect($datasets_a)->toContain([$text_a]); + expect($datasets_b)->toContain([$text_b]); +})->with(function () use ($datasets_a) { + return $datasets_a; +})->with(function () use ($datasets_b) { + return $datasets_b; +}); + +test('eager multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('1212121212121314232413142324'); +}); + +test('lazy registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) { + $state->text .= $text_a . $text_b; + expect($datasets)->toContain([$text_a]); + expect($datasets)->toContain([$text_b]); +})->with('numbers.array')->with('numbers.array'); + +test('lazy registered multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('121212121212131423241314232411122122'); +}); + +test('eager registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) { + $state->text .= $text_a . $text_b; + expect($datasets)->toContain([$text_a]); + expect($datasets)->toContain([$text_b]); +})->with('numbers.array')->with('numbers.closure'); + +test('eager registered multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('12121212121213142324131423241112212211122122'); +}); + +test('eager wrapped registered multiple datasets', function ($text_a, $text_b) use ($state, $datasets) { + $state->text .= $text_a . $text_b; + expect($datasets)->toContain([$text_a]); + expect($datasets)->toContain([$text_b]); +})->with('numbers.closure.wrapped')->with('numbers.closure'); + +test('eager wrapped registered multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('1212121212121314232413142324111221221112212211122122'); +}); + +test('named multiple datasets', function ($text_a, $text_b) use ($state, $datasets_a, $datasets_b) { + $state->text .= $text_a . $text_b; + expect($datasets_a)->toContain([$text_a]); + expect($datasets_b)->toContain([$text_b]); +})->with([ + 'one' => [1], + 'two' => [2], +])->with([ + 'three' => [3], + 'four' => [4], +]); + +test('named multiple datasets did the job right', function () use ($state) { + expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324'); +}); + +test('more than two datasets', function ($text_a, $text_b, $text_c) use ($state, $datasets_a, $datasets_b) { + $state->text .= $text_a . $text_b . $text_c; + expect($datasets_a)->toContain([$text_a]); + expect($datasets_b)->toContain([$text_b]); + expect([5, 6])->toContain($text_c); +})->with($datasets_a, $datasets_b)->with([5, 6]); + +test('more than two datasets did the job right', function () use ($state) { + expect($state->text)->toBe('121212121212131423241314232411122122111221221112212213142324135136145146235236245246'); +}); diff --git a/tests/Unit/Datasets.php b/tests/Unit/Datasets.php index dc3bd3c5..08f82352 100644 --- a/tests/Unit/Datasets.php +++ b/tests/Unit/Datasets.php @@ -26,44 +26,56 @@ it('show the actual dataset of non-named datasets in their description', functio expect($descriptions[1])->toBe('test description with (array(2))'); }); -$state = new stdClass(); -$state->combinations = [ - ['a', 'b', 'c', 1, 2, 'bar', 'foo', 'baz'], - ['a', 'b', 'c', 1, 2, 'zip', 'zap', 'zop'], - ['a', 'b', 'c', 3, 4, 'bar', 'foo', 'baz'], - ['a', 'b', 'c', 3, 4, 'zip', 'zap', 'zop'], - ['a', 'b', 'c', 5, 6, 'bar', 'foo', 'baz'], - ['a', 'b', 'c', 5, 6, 'zip', 'zap', 'zop'], - ['d', 'e', 'f', 1, 2, 'bar', 'foo', 'baz'], - ['d', 'e', 'f', 1, 2, 'zip', 'zap', 'zop'], - ['d', 'e', 'f', 3, 4, 'bar', 'foo', 'baz'], - ['d', 'e', 'f', 3, 4, 'zip', 'zap', 'zop'], - ['d', 'e', 'f', 5, 6, 'bar', 'foo', 'baz'], - ['d', 'e', 'f', 5, 6, 'zip', 'zap', 'zop'], - ['g', 'h', 'i', 1, 2, 'bar', 'foo', 'baz'], - ['g', 'h', 'i', 1, 2, 'zip', 'zap', 'zop'], - ['g', 'h', 'i', 3, 4, 'bar', 'foo', 'baz'], - ['g', 'h', 'i', 3, 4, 'zip', 'zap', 'zop'], - ['g', 'h', 'i', 5, 6, 'bar', 'foo', 'baz'], - ['g', 'h', 'i', 5, 6, 'zip', 'zap', 'zop'], -]; +it('show only the names of multiple named datasets in their description', function () { + $descriptions = array_keys(Datasets::resolve('test description', [ + [ + 'one' => [1], + 'two' => [[2]], + ], + [ + 'three' => [3], + 'four' => [[4]], + ], + ])); -it('generates a matrix with given datasets', function ($a1, $a2, $a3, $b1, $b2, $c1, $c2, $c3) use ($state) { - $combinations = $state->combinations; - $set = $combinations[0]; - array_shift($combinations); - $state->combinations = $combinations; + expect($descriptions[0])->toBe('test description with data set "one" / data set "three"'); + expect($descriptions[1])->toBe('test description with data set "one" / data set "four"'); + expect($descriptions[2])->toBe('test description with data set "two" / data set "three"'); + expect($descriptions[3])->toBe('test description with data set "two" / data set "four"'); +}); - expect([$a1, $a2, $a3, $b1, $b2, $c1, $c2, $c3])->toMatchArray($set); -})->with([ - 'dataset_aa' => ['a1' => 'a', 'a2' => 'b', 'a3' => 'c'], - 'dataset_ab' => ['a1' => 'd', 'a2' => 'e', 'a3' => 'f'], - 'dataset_ac' => ['a1' => 'g', 'a2' => 'h', 'a3' => 'i'], -])->with([ - 'dataset_ba' => ['b1' => 1, 'b2' => 2], - 'dataset_bb' => ['b1' => 3, 'b2' => 4], - 'dataset_bc' => ['b1' => 5, 'b2' => 6], -])->with([ - ['c1' => 'bar', 'c2' => 'foo', 'c3' => 'baz'], - ['c1' => 'zip', 'c2' => 'zap', 'c3' => 'zop'], -]); +it('show the actual dataset of multiple non-named datasets in their description', function () { + $descriptions = array_keys(Datasets::resolve('test description', [ + [ + [1], + [[2]], + ], + [ + [3], + [[4]], + ], + ])); + + expect($descriptions[0])->toBe('test description with (1) / (3)'); + expect($descriptions[1])->toBe('test description with (1) / (array(4))'); + expect($descriptions[2])->toBe('test description with (array(2)) / (3)'); + expect($descriptions[3])->toBe('test description with (array(2)) / (array(4))'); +}); + +it('show the correct description for mixed named and not-named datasets', function () { + $descriptions = array_keys(Datasets::resolve('test description', [ + [ + 'one' => [1], + [[2]], + ], + [ + [3], + 'four' => [[4]], + ], + ])); + + expect($descriptions[0])->toBe('test description with data set "one" / (3)'); + expect($descriptions[1])->toBe('test description with data set "one" / data set "four"'); + expect($descriptions[2])->toBe('test description with (array(2)) / (3)'); + expect($descriptions[3])->toBe('test description with (array(2)) / data set "four"'); +}); From ea0be9e7a4e1001ef617dc273475f8b87fe9a653 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Thu, 27 May 2021 08:46:23 +0200 Subject: [PATCH 09/10] Refactored Datasets::resolve() to make it more readable --- src/Datasets.php | 68 ++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 196d08cd..89725107 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -62,35 +62,9 @@ final class Datasets return [$description => []]; } - $processedDatasets = []; + $datasets = self::processDatasets($datasets); - foreach ($datasets as $index => $data) { - $processedDataset = []; - - if (is_string($data)) { - $datasets[$index] = self::get($data); - } - - if (is_callable($datasets[$index])) { - $datasets[$index] = call_user_func($datasets[$index]); - } - - if ($datasets[$index] instanceof Traversable) { - $datasets[$index] = iterator_to_array($datasets[$index]); - } - - foreach ($datasets[$index] as $key => $values) { - $values = is_array($values) ? $values : [$values]; - $processedDataset[] = [ - 'label' => self::getDataSetDescription($key, $values), - 'values' => $values, - ]; - } - - $processedDatasets[] = $processedDataset; - } - - $datasetCombinations = self::getDataSetsCombinations($processedDatasets); + $datasetCombinations = self::getDataSetsCombinations($datasets); $dataSetDescriptions = []; $dataSetValues = []; @@ -127,6 +101,44 @@ final class Datasets return $namedData; } + /** + * @param array|string> $datasets + * + * @return array + */ + private static function processDatasets(array $datasets): array + { + $processedDatasets = []; + + foreach ($datasets as $index => $data) { + $processedDataset = []; + + if (is_string($data)) { + $datasets[$index] = self::get($data); + } + + if (is_callable($datasets[$index])) { + $datasets[$index] = call_user_func($datasets[$index]); + } + + if ($datasets[$index] instanceof Traversable) { + $datasets[$index] = iterator_to_array($datasets[$index]); + } + + foreach ($datasets[$index] as $key => $values) { + $values = is_array($values) ? $values : [$values]; + $processedDataset[] = [ + 'label' => self::getDataSetDescription($key, $values), + 'values' => $values, + ]; + } + + $processedDatasets[] = $processedDataset; + } + + return $processedDatasets; + } + /** * @param array $combinations * From d32a648af50f3f4aba150a7572c5bb086223f1a8 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Thu, 10 Jun 2021 09:03:32 +0200 Subject: [PATCH 10/10] updated test snapshots after merge from master --- tests/.snapshots/success.txt | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index d392dedc..eb3a3532 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -51,6 +51,45 @@ ✓ it creates unique test case names with ('Name 2', Pest\Plugin Object (), true) ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3 ✓ it creates unique test case names - count + ✓ lazy multiple datasets with (1) / (3) + ✓ lazy multiple datasets with (1) / (4) + ✓ lazy multiple datasets with (2) / (3) + ✓ lazy multiple datasets with (2) / (4) + ✓ lazy multiple datasets did the job right + ✓ eager multiple datasets with (1) / (3) + ✓ eager multiple datasets with (1) / (4) + ✓ eager multiple datasets with (2) / (3) + ✓ eager multiple datasets with (2) / (4) + ✓ eager multiple datasets did the job right + ✓ lazy registered multiple datasets with (1) / (1) + ✓ lazy registered multiple datasets with (1) / (2) + ✓ lazy registered multiple datasets with (2) / (1) + ✓ lazy registered multiple datasets with (2) / (2) + ✓ lazy registered multiple datasets did the job right + ✓ eager registered multiple datasets with (1) / (1) + ✓ eager registered multiple datasets with (1) / (2) + ✓ eager registered multiple datasets with (2) / (1) + ✓ eager registered multiple datasets with (2) / (2) + ✓ eager registered multiple datasets did the job right + ✓ eager wrapped registered multiple datasets with (1) / (1) + ✓ eager wrapped registered multiple datasets with (1) / (2) + ✓ eager wrapped registered multiple datasets with (2) / (1) + ✓ eager wrapped registered multiple datasets with (2) / (2) + ✓ eager wrapped registered multiple datasets did the job right + ✓ named multiple datasets with data set "one" / data set "three" + ✓ named multiple datasets with data set "one" / data set "four" + ✓ named multiple datasets with data set "two" / data set "three" + ✓ named multiple datasets with data set "two" / data set "four" + ✓ named multiple datasets did the job right + ✓ more than two datasets with (1) / (3) / (5) + ✓ more than two datasets with (1) / (3) / (6) + ✓ more than two datasets with (1) / (4) / (5) + ✓ more than two datasets with (1) / (4) / (6) + ✓ more than two datasets with (2) / (3) / (5) + ✓ more than two datasets with (2) / (3) / (6) + ✓ more than two datasets with (2) / (4) / (5) + ✓ more than two datasets with (2) / (4) / (6) + ✓ more than two datasets did the job right PASS Tests\Features\Exceptions ✓ it gives access the the underlying expectException @@ -174,6 +213,9 @@ PASS Tests\Unit\Datasets ✓ it show only the names of named datasets in their description ✓ it show the actual dataset of non-named datasets in their description + ✓ it show only the names of multiple named datasets in their description + ✓ it show the actual dataset of multiple non-named datasets in their description + ✓ it show the correct description for mixed named and not-named datasets PASS Tests\Unit\Plugins\Version ✓ it outputs the version when --version is used @@ -224,5 +266,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 7 skipped, 122 passed + Tests: 7 skipped, 164 passed \ No newline at end of file