Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Sharing repeat iteration as dataset variable #975

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,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 @@ -116,7 +116,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']);
Loading