PHPackages                             eboreum/phpunit-with-consecutive-alternative - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. eboreum/phpunit-with-consecutive-alternative

ActiveLibrary[Testing &amp; Quality](/categories/testing)

eboreum/phpunit-with-consecutive-alternative
============================================

Do you miss the old "withConsecutive" method in PHPUnit? This library fixes that problem for you.

1.0.0(1y ago)02.1k↓50%1MITPHPPHP &gt;=8.2CI passing

Since Feb 14Pushed 1y ago2 watchersCompare

[ Source](https://github.com/eboreum/phpunit-with-consecutive-alternative)[ Packagist](https://packagist.org/packages/eboreum/phpunit-with-consecutive-alternative)[ RSS](/packages/eboreum-phpunit-with-consecutive-alternative/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (1)

A sensible alternative to PHPUnit's now removed "withConsecutive" method
========================================================================

[](#a-sensible-alternative-to-phpunits-now-removed-withconsecutive-method)

[![license](https://camo.githubusercontent.com/4726e8d3db87336ff90482817d2db7991d41beae87d3a62605d0bfb2e2b38135/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65626f7265756d2f706870756e69742d776974682d636f6e73656375746976652d616c7465726e61746976653f6c6162656c3d6c6963656e7365)](https://camo.githubusercontent.com/4726e8d3db87336ff90482817d2db7991d41beae87d3a62605d0bfb2e2b38135/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65626f7265756d2f706870756e69742d776974682d636f6e73656375746976652d616c7465726e61746976653f6c6162656c3d6c6963656e7365)[![build](https://github.com/eboreum/phpunit-with-consecutive-alternative/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/eboreum/phpunit-with-consecutive-alternative/actions/workflows/build.yml/badge.svg?branch=main)[![Code Coverage](https://camo.githubusercontent.com/a8ce86e05d81acbc205613f43d6a79ca6ac9b88476904df6642078658470365a/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6b61666f736f2f36356661666331636536333632393264363661343264636534623462373531612f7261772f746573742d636f7665726167655f5f6d61696e2e6a736f6e)](https://github.com/eboreum/phpunit-with-consecutive-alternative/actions)[![PHPStan Level](https://camo.githubusercontent.com/0ccd5f7725bd889ed40de3f5baff47b9210bca5a5ec5359be1bc9cf247f67b7a/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f676973742e67697468756275736572636f6e74656e742e636f6d2f6b61666f736f2f36356661666331636536333632393264363661343264636534623462373531612f7261772f7068707374616e2d6c6576656c5f5f6d61696e2e6a736f6e)](https://github.com/eboreum/phpunit-with-consecutive-alternative/actions)

Do you miss the old "[withConsecutive](https://docs.phpunit.de/en/8.5/test-doubles.html?highlight=withconsecutive#test-doubles-mock-objects-examples-with-consecutive-php)" method in PHPUnit? This library solves that problem for you.

Installation
============

[](#installation)

Via [Composer](https://getcomposer.org/) ():

```
composer install eboreum/phpunit-with-consecutive-alternative

```

Via GitHub:

```
git clone git@github.com:eboreum/phpunit-with-consecutive-alternative.git

```

Using this library
==================

[](#using-this-library)

Within a test method — i.e. a method inside a child of `\PHPUnit\Framework\TestCase` — simply do the following:

```
use Eboreum\PhpunitWithConsecutiveAlternative\MethodCallExpectation;
use Eboreum\PhpunitWithConsecutiveAlternative\WillHandleConsecutiveCalls;

...

$object = $this->createMock(DateTime::class);

$willHandleConsecutiveCalls = new WillHandleConsecutiveCalls();
$willHandleConsecutiveCalls->expectConsecutiveCalls(
    $object,
    'setISODate',
    new MethodCallExpectation($object, 1999, 42),
    new MethodCallExpectation($object, 2000, 43, 2),
);

...

$this->assertSame($object, $object->setISODate(1999, 42));
$this->assertSame($object, $object->setISODate(2000, 43, 2));
```

Make it easy for yourself
=========================

[](#make-it-easy-for-yourself)

Make your own (abstract) test case class and simply add a proxy method doing the above.

Example:

```
use Eboreum\PhpunitWithConsecutiveAlternative\MethodCallExpectation;
use Eboreum\PhpunitWithConsecutiveAlternative\WillHandleConsecutiveCalls;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

abstract class AbstractUnitTestCase extends TestCase
{
    /**
     * @param non-empty-string $methodName
     */
    final public static function expectConsecutiveCalls(
        MockObject $object,
        string $methodName,
        MethodCallExpectation ...$methodCallExpectations,
    ): void {
        (new WillHandleConsecutiveCalls())->expectConsecutiveCalls($object, $methodName, ...$methodCallExpectations);
    }
}
```

⚠️ **Notice:** We do ***not*** hook into `\PHPUnit\Framework\MockObject\Builder\InvocationMocker` and so — contrary to the original `withConsecutive` — we place the above method on `\PHPUnit\Framework\TestCase` instead.

- In simpler terms: You call a method on `\PHPUnit\Framework\TestCase`, with the mock as an argument, rather than calling a method on the mock itself.
- This change is largely because PHPUnit utilizes the "final" keyword a lot on classes and does not support decorators, making extending (an instance of) InvocationMocker nigh-impossible (unless we do evil class-override things).

Why was "withConsecutive" removed?
==================================

[](#why-was-withconsecutive-removed)

You can see some of the reasoning and discussion about it here: [sebastianbergmann/phpunit#4026](https://github.com/sebastianbergmann/phpunit/issues/4026)

The main reason [@sebastianbergmann](https://github.com/sebastianbergmann) decided to remove `withConsecutive` (and `at`, by the way) was with the argument: Don't mock what you don't own. Sadly, the resource he links to in [sebastianbergmann/phpunit#4026](https://github.com/sebastianbergmann/phpunit/issues/4026) — being  — is now a 404 page. Sebastian is a fantastic person and I and many others greatly appreciate his work over the years. Truly! However, I for one **disagree** with the "Don't mock what you don't own" sentiment. If something is part of the public API — third-part or not — one should be able to and allowed to mock it. Period.

No convenient alternative has been provided in PHPUnit itself.

A core problem — fixed
======================

[](#a-core-problem--fixed)

A core problem was the disjointed connection between arguments and their corresponding return values, as they would be provided in separate methods, i.e.:

- `withConsecutive`
- `willReturnOnConsecutiveCalls` or `willReturn`

In order to make this connection abundantly clear, the value-object class `\Eboreum\PhpunitWithConsecutiveAlternative\MethodCallExpectation` has been implemented. Within it is stored a return value (required) and 0 or more arguments.

**Notice:** You may indeed use callbacks (i.e. `\PHPUnit\Framework\TestCase->callback(...)`) for the arguments instead of the actual values.

License &amp; Disclaimer
========================

[](#license--disclaimer)

See [`LICENSE`](LICENSE) file. Basically: Use this library at your own risk.

Contributing
============

[](#contributing)

We prefer that you create a ticket and or a pull request at , and have a discussion about a feature or bug here.

Credits
=======

[](#credits)

Authors
-------

[](#authors)

- **Kasper Søfren** (kafoso)
    E-mail: [](mailto:soefritz@gmail.com)
    Homepage: [](https://github.com/kafoso)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance44

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

458d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/83704139?v=4)[Eboreum](/maintainers/eboreum)[@eboreum](https://github.com/eboreum)

---

Top Contributors

[![kafoso](https://avatars.githubusercontent.com/u/3169691?v=4)](https://github.com/kafoso "kafoso (18 commits)")

---

Tags

phpunitwithconsecutive

###  Code Quality

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/eboreum-phpunit-with-consecutive-alternative/health.svg)

```
[![Health](https://phpackages.com/badges/eboreum-phpunit-with-consecutive-alternative/health.svg)](https://phpackages.com/packages/eboreum-phpunit-with-consecutive-alternative)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[johnkary/phpunit-speedtrap

Find and report on slow tests in your PHPUnit test suite

78337.2M122](/packages/johnkary-phpunit-speedtrap)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69617.9M510](/packages/spatie-phpunit-snapshot-assertions)[phpspec/prophecy-phpunit

Integrating the Prophecy mocking library in PHPUnit test cases

19454.9M1.4k](/packages/phpspec-prophecy-phpunit)[yoast/phpunit-polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

18438.5M841](/packages/yoast-phpunit-polyfills)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
