From 3ee5c29a005582a4faaee8b08f2547f18755c579 Mon Sep 17 00:00:00 2001 From: Katalam Date: Thu, 5 Oct 2023 23:07:03 +0200 Subject: [PATCH 1/3] feat: add repeat iteration as function argument if no extra dataset is provided --- src/Concerns/Testable.php | 2 +- src/Factories/TestCaseMethodFactory.php | 2 +- tests/Features/Repeat.php | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index e8d00588..9a618560 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -262,7 +262,7 @@ trait Testable { $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); - if ($method->repetitions > 1) { + if ($method->repetitions > 1 && $method->datasets !== []) { array_shift($arguments); } diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index 4c42a557..edfa7e48 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -116,7 +116,7 @@ final class TestCaseMethodFactory */ public function receivesArguments(): bool { - return $this->datasets !== [] || $this->depends !== []; + return $this->datasets !== [] || $this->depends !== [] || $this->repetitions > 1; } /** diff --git a/tests/Features/Repeat.php b/tests/Features/Repeat.php index e0651d33..614c666b 100644 --- a/tests/Features/Repeat.php +++ b/tests/Features/Repeat.php @@ -16,3 +16,7 @@ test('multiple times with multiple dataset', function (int $numberA, int $number expect([1, 2, 3])->toContain($numberA) ->and([4, 5, 6])->toContain($numberB); })->repeat(times: 7)->with(['a' => 1, 'b' => 2, 'c' => 3], [4, 5, 6]); + +test('multiple times with iterator as argument', function (int $iteration) { + expect($iteration)->toBeGreaterThan(0); +})->repeat(times: 8); From ecff90da1c773023e37de57d26c6ec71626d970d Mon Sep 17 00:00:00 2001 From: Katalam Date: Fri, 6 Oct 2023 15:07:48 +0200 Subject: [PATCH 2/3] fix: add repeat iteration as the last argument when combined with dataset --- src/Concerns/Testable.php | 5 +++-- tests/Features/Repeat.php | 29 ++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 9a618560..27db6631 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -262,8 +262,9 @@ trait Testable { $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); - if ($method->repetitions > 1 && $method->datasets !== []) { - array_shift($arguments); + if ($method->repetitions > 1) { + $firstArgument = array_shift($arguments); + $arguments[] = $firstArgument; } $underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure'); diff --git a/tests/Features/Repeat.php b/tests/Features/Repeat.php index 614c666b..811c1185 100644 --- a/tests/Features/Repeat.php +++ b/tests/Features/Repeat.php @@ -17,6 +17,29 @@ test('multiple times with multiple dataset', function (int $numberA, int $number ->and([4, 5, 6])->toContain($numberB); })->repeat(times: 7)->with(['a' => 1, 'b' => 2, 'c' => 3], [4, 5, 6]); -test('multiple times with iterator as argument', function (int $iteration) { - expect($iteration)->toBeGreaterThan(0); -})->repeat(times: 8); +test('multiple times with iterator', function (int $iteration) { + expect($iteration) + ->toBeNumeric() + ->toBeGreaterThan(0); +})->repeat(times: 2); + +test('multiple times with repeat iterator with single dataset', function (string $letter, int $iteration) { + expect($letter) + ->toBeString() + ->toBeIn(['a', 'b', 'c']) + ->and($iteration) + ->toBeNumeric() + ->toBeGreaterThan(0); +})->repeat(times: 2)->with(['a', 'b', 'c']); + +test('multiple times with repeat iterator with multiple dataset', function (string $letterA, string $letterB, int $iteration) { + expect($letterA) + ->toBeString() + ->toBeIn(['a', 'b', 'c']) + ->and($letterB) + ->toBeString() + ->toBeIn(['d', 'e', 'f']) + ->and($iteration) + ->toBeNumeric() + ->toBeGreaterThan(0); +})->repeat(times: 2)->with(['a', 'b', 'c'], ['d', 'e', 'f']); From 67e452e9ed28013617bceb83786f1c2232137e95 Mon Sep 17 00:00:00 2001 From: Katalam Date: Fri, 6 Oct 2023 15:10:02 +0200 Subject: [PATCH 3/3] chore: add docs --- src/Concerns/Testable.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Concerns/Testable.php b/src/Concerns/Testable.php index 27db6631..e9775e85 100644 --- a/src/Concerns/Testable.php +++ b/src/Concerns/Testable.php @@ -263,6 +263,10 @@ trait Testable $method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name()); if ($method->repetitions > 1) { + // If the test is repeated, the first argument is the iteration number + // we need to move it to the end of the arguments list + // so that the datasets are the first n arguments + // and the iteration number is the last argument $firstArgument = array_shift($arguments); $arguments[] = $firstArgument; }