Skip to content

! change: Refactor nested WasCalled assertions#29

Merged
remipelhate merged 5 commits into
mainfrom
change/refactor-was-called-assertion-wrappers
Jun 5, 2026
Merged

! change: Refactor nested WasCalled assertions#29
remipelhate merged 5 commits into
mainfrom
change/refactor-was-called-assertion-wrappers

Conversation

@remipelhate

@remipelhate remipelhate commented Jun 5, 2026

Copy link
Copy Markdown
Member

Warning

This PR contains a breaking change. See upgrade guide for more details.

Context

In an attempt not to overload the WasCalled constraint with more methods, this PR aims to simplify its API by abstracting the withSame() method onto a standalone assertion Craftzing\TestBench\PHPUnit\Constraint\Callables\Assertions\WithSameArguments. This allows us to add more WasCalled assertions like Craftzing\TestBench\PHPUnit\Constraint\Callables\Assertions\WithEqualArguments (as introduced in this PR.

Failure output improvements

Previously, whenever a WasCalled assertion failed, PHPUnit would export the full object in the failure description. E.g.:

There were 4 failures:

1) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalled
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable Object #165 (
    'invocations' => Array &0 [],
    'return' => null,
) was called.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:69

2) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledWithExpectedArguments
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable Object #2431 (
    'invocations' => Array &0 [
        0 => Craftzing\TestBench\PHPUnit\Doubles\CallableInvocation Object #2533 (
            'arguments' => Array &1 [
                0 => 'last',
                1 => 'first',
            ],
        ),
    ],
    'return' => null,
) was called with given arguments.

* Failed asserting that two strings are identical.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:92

3) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledExpectedTimesWithExpectedArguments with data set "Too few times" (Craftzing\TestBench\PHPUnit\DataProviders\QuantableConstraint Object (...))
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable Object #2254 (
    'invocations' => Array &0 [
        0 => Craftzing\TestBench\PHPUnit\Doubles\CallableInvocation Object #2263 (
            'arguments' => Array &1 [
                0 => 'first',
                1 => 'last',
            ],
        ),
        1 => Craftzing\TestBench\PHPUnit\Doubles\CallableInvocation Object #2278 (
            'arguments' => Array &2 [
                0 => 'first',
                1 => 'last',
            ],
        ),
    ],
    'return' => null,
) was called 1 time(s) with given arguments.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:145

4) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledExpectedTimesWithExpectedArguments with data set "Too many times" (Craftzing\TestBench\PHPUnit\DataProviders\QuantableConstraint Object (...))
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable Object #2543 (
    'invocations' => Array &0 [
        0 => Craftzing\TestBench\PHPUnit\Doubles\CallableInvocation Object #2257 (
            'arguments' => Array &1 [
                0 => 'first',
                1 => 'last',
            ],
        ),
        1 => Craftzing\TestBench\PHPUnit\Doubles\CallableInvocation Object #2431 (
            'arguments' => Array &2 [
                0 => 'first',
                1 => 'last',
            ],
        ),
    ],
    'return' => null,
) was called 3 time(s) with given arguments.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:145

Even with really simply arguments like short strings, this output was already very verbose. And it only gets worse when passing objects as arguments.

Therefore, this PR proposes to simplify the object representation to:

  • only include the FQCN and object ID
  • include the total amount of invocations (when asserting that a callable was called a specific amount of times)
There were 4 failures:

1) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalled
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable#165 was called.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:69

2) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledWithExpectedArguments
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable#2431 was called with given arguments.

* Failed asserting that two strings are identical.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:92

3) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledExpectedTimesWithExpectedArguments with data set "Too few times" (Craftzing\TestBench\PHPUnit\DataProviders\QuantableConstraint Object (...))
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable#2254 (with 2 total invocations) was called 1 time(s) with given arguments.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:145

4) Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalledTest::itFailsWhenNotCalledExpectedTimesWithExpectedArguments with data set "Too many times" (Craftzing\TestBench\PHPUnit\DataProviders\QuantableConstraint Object (...))
Failed asserting that Craftzing\TestBench\PHPUnit\Doubles\SpyCallable#2274 (with 2 total invocations) was called 3 time(s) with given arguments.

/code/src/PHPUnit/Constraint/Callables/WasCalledTest.php:145

Upgrade guide

Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalled::withSame() is no longer available. You can replace it by providing the new Craftzing\TestBench\PHPUnit\Constraint\Callables\Assertions\WithSameArguments assertion as first argument to WasCalled:

use Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalled;
use Craftzing\TestBench\PHPUnit\Constraint\Callables\Assertions\WithSameArguments;

$this->assertThat($callable, new WasCalles(new WithSameArguments(...$arguments)));

You may now also use loose comparison:

use Craftzing\TestBench\PHPUnit\Constraint\Callables\WasCalled;
use Craftzing\TestBench\PHPUnit\Constraint\Callables\Assertions\WithEqualArguments;

$this->assertThat($callable, new WasCalles(new WithEqualArguments(...$arguments)));

@remipelhate remipelhate self-assigned this Jun 5, 2026
@remipelhate remipelhate requested a review from fowbi June 5, 2026 10:27
@remipelhate remipelhate changed the title ! change: Refactor WasCalled assertion wrappers ! change: Refactor nested WasCalled assertions Jun 5, 2026
@remipelhate remipelhate merged commit 2ebf0f7 into main Jun 5, 2026
5 checks passed
@remipelhate remipelhate deleted the change/refactor-was-called-assertion-wrappers branch June 5, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant