Skip to content

Commit

Permalink
Merge pull request #975 from Katalam/repeat
Browse files Browse the repository at this point in the history
[2.x] Sharing `repeat` iteration as `dataset` variable
  • Loading branch information
nunomaduro authored Jan 25, 2024
2 parents 4e31973 + 67e452e commit 815ae3c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ private function __resolveTestArguments(array $arguments): array
$method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name());

if ($method->repetitions > 1) {
array_shift($arguments);
// 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;
}

$underlyingTest = Reflection::getFunctionVariable($this->__test, 'closure');
Expand Down
2 changes: 1 addition & 1 deletion src/Factories/TestCaseMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getClosure(TestCase $concrete): Closure
*/
public function receivesArguments(): bool
{
return $this->datasets !== [] || $this->depends !== [];
return $this->datasets !== [] || $this->depends !== [] || $this->repetitions > 1;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Features/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,30 @@
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', 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']);

0 comments on commit 815ae3c

Please sign in to comment.