sebastianbergmann/phpunit · error · ExpectationFailedException
Too many parameter sets given, %d out of %d expected paramet
Error message
Too many parameter sets given, %d out of %d expected parameter set%s %s been called.
What it means
Despite its wording, this PHPUnit error fires when fewer calls arrived than configured: withParameterSetsInOrder(...) was given more parameter sets than the code under test consumed. OrderedParameterSets::verify() runs at mock-verification time; if parameter sets are still left on the stack (count($this->stack) > 0), it reports how many sets were actually used versus how many were configured. 'Too many parameter sets given' means the test over-specified the expected interaction, not that the code called too often.
Source
Thrown at src/Framework/MockObject/Runtime/Rule/OrderedParameterSets.php:84
$parameters = array_shift($this->stack);
$this->applied[] = $parameters;
$parameters->apply($invocation);
}
/**
* Checks if the invocation $invocation matches the current rules. If it
* does the rule will get the invoked() method called which should check
* if an expectation is met.
*
* @throws ExpectationFailedException
*/
public function verify(): void
{
if (count($this->applied) !== $this->numberOfConfiguredParameterSets &&
count($this->stack) > 0) {
throw new ExpectationFailedException(
sprintf(
'Too many parameter sets given, %d out of %d expected parameter set%s %s been called.',
count($this->applied),
$this->numberOfConfiguredParameterSets,
$this->numberOfConfiguredParameterSets !== 1 ? 's' : '',
count($this->applied) !== 1 ? 'have' : 'has',
),
);
}
foreach ($this->applied as $parameters) {
$parameters->verify();
}
}
}
View on GitHub (pinned to f123cdb2a2)
Solutions
- Compare the two counts in the message: the first is how many sets were consumed, the second how many you configured — trust the consumed count.
- Remove the unused parameter set(s), or fix the data/path so the code actually makes the remaining calls.
- If some calls are conditional, split the expectation into separate matchers (one withParameterSetsInOrder per scenario) or use withParameterSetsInPartialOrder() with pinned sets for the calls that must happen at a given position.
- Ensure expects() matches the real count (exactly(1) instead of any()) so leftover sets cannot hide.
- If the skipped call reveals a real bug (missing second save), fix the production code path.
Example fix
// before
$repo->expects($this->any())->method('save')
->withParameterSetsInOrder([$user], [$audit]);
$service->register($user); // only saves $user, audit write is skipped
// after: configure only the calls that actually happen
$repo->expects($this->once())->method('save')
->withParameterSetsInOrder([$user]); Defensive patterns
Strategy: validation
Prevention
- Read the message as 'fewer calls than sets': keep sets and expects() count in sync.
- Use expects(exactly(n)) with n sets so a shortfall always fails loudly and identifiably.
- Delete parameter sets when you delete a call from the SUT; stale sets are the main cause.
- Keep each test to one scenario — conditional second calls belong in a separate test with their own matcher.
When it happens
Trigger: $mock->expects($this->any())->method('save')->withParameterSetsInOrder([$dtoA], [$dtoB]) while the code calls save() only once. The error appears after the test body, when PHPUnit verifies the mock: 1 out of 2 expected parameter sets has been called.
Common situations: An early return or exception in the SUT skipping later calls; tests written against an older flow that used to iterate twice; one branch of a conditional not exercised by the fixture; copy-pasted parameter sets left over from another test; fixtures shrinking so the loop body runs fewer times.
Related errors
- Expected invocation at least %d time%s but it occurred %d ti
- Expected invocation at least once but it never occurred.
- Expected invocation at most %d time%s but it occurred %d tim
- Method was expected to be called %d time%s, actually called
- Not enough parameter sets configured, only %d parameter sets
AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23).
Data as JSON: /api/errors/1aa2076f80420c52.
Report an issue: GitHub.