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

Callback constraint does not handle variadic arguments correctly when used for mock object expectations #5891

Closed
xopino opened this issue Jul 8, 2024 · 3 comments · Fixed by #5944
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11

Comments

@xopino
Copy link

xopino commented Jul 8, 2024

Q A
PHPUnit version 9.6.13
PHP version 8.2
Installation Method Composer

Summary

I need to assert the number of elements being passed as an argument on a mocked method for a mocked service.
The argument being passed is an spread operator, and I experience a weird behaviour when unpacking the array.
It always unpack the array as 1 element even the argument passed is an array with multiple elements, it just returns one.

Current behavior

Unpacking spread operator when trying to assert the expected arguments being passed to a mocked method returns always 1 element of the array being passed instead of the full array.

How to reproduce

//Class being mocked:

interface EventBusInterface
{
    public function publish(DomainEvent ...$events): void;
}

//Class being used:

$this->eventBus->publish(...$events);

//Test:

$eventBusMock = $this->createMock(EventBusInterface::class);
$eventBusMock
    ->expects($this->once())
    ->method('publish')
    ->with(self::callback(function (...$receivedEvents) : bool {

        $this->assertCount($expectedNumber, $receivedEvents); //Received events is always 1 element...
        return true;
    }));

Expected behavior

Inside the self::callback function, when counting the number of elements inside of the array passed as spread operator should return the actual number of elements.

For example:

$events = [$event1, $event2];
$this->eventBus->publish(...$events);
$eventBusMock = $this->createMock(EventBusInterface::class);
$eventBusMock
    ->expects($this->once())
    ->method('publish')
    ->with(self::callback(function (...$receivedEvents) : bool {

        $this->assertCount(2, $receivedEvents); //Should return true, as $receivedEvents should contain 2 events.
        return true;
    }));
@xopino xopino added the type/bug Something is broken label Jul 8, 2024
@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects version/9 Something affects PHPUnit 9 labels Jul 8, 2024
@sebastianbergmann
Copy link
Owner

Bugfix Support for PHPUnit 9 has ended. Does this issue also affect PHPUnit 10 and/or PHPUnit 11?

@sebastianbergmann
Copy link
Owner

No feedback, closing.

@kubawerlos
Copy link
Contributor

@sebastianbergmann this is still happening on 10.5 branch: #5944

@sebastianbergmann sebastianbergmann added version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11 and removed version/9 Something affects PHPUnit 9 labels Sep 9, 2024
@sebastianbergmann sebastianbergmann changed the title Weird behaviour when mocking spread operator as argument. Callback constraint does not handle variadic arguments correctly when used for mock object expectations Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants