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

  1. 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.
  2. Remove the unused parameter set(s), or fix the data/path so the code actually makes the remaining calls.
  3. 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.
  4. Ensure expects() matches the real count (exactly(1) instead of any()) so leftover sets cannot hide.
  5. 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

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


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/1aa2076f80420c52. Report an issue: GitHub.