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

Add support for stubbing and mocking closures #3536

Closed
HeahDude opened this issue Feb 20, 2019 · 5 comments
Closed

Add support for stubbing and mocking closures #3536

HeahDude opened this issue Feb 20, 2019 · 5 comments
Labels
feature/test-doubles Test Stubs and Mock Objects type/enhancement A new idea that should be implemented

Comments

@HeahDude
Copy link

HeahDude commented Feb 20, 2019

Q A
PHPUnit version 8.1
PHP version 7.x
Installation Method Composer / PHAR

Would you accept a PR to support mocking closure, to ease checking that one is called?

I.e : https://3v4l.org/Yaq5A

public function testClosureIsCalled()
{
    $callback = function () {};

    $this->assertClosureWillBeCalled($callback);

    SomeBusiness:caller($callback);
}
@stale stale bot added the stale label Apr 21, 2019
Repository owner deleted a comment from stale bot Apr 22, 2019
@stale stale bot removed the stale label Apr 22, 2019
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-doubles Test Stubs and Mock Objects labels Apr 22, 2019
@sebastianbergmann sebastianbergmann changed the title [Feature request] Mocking closure Add support for stubbing and mocking closures Apr 22, 2019
@sebastianbergmann
Copy link
Owner

I cannot promise acceptance of such a pull request but would, of course, consider it.

@morozov
Copy link
Contributor

morozov commented May 17, 2019

This is already possible by mocking __invoke() on stdClass:

public function testClosureIsCalled() : void
{
    $closure = $this->createPartialMock(stdClass::class, ['__invoke']);

    $closure->expects(
        $this->exactly(2)
    )->method('__invoke');

    $closure();
    $closure();
}

Unlike this workaround, the proposed API is not really flexible, however, it'd be nice if the workaround was formalized as a feature.

@veewee
Copy link

veewee commented Oct 1, 2020

@morozov
The provided solution results in:

createPartialMock() called with method(s) __invoke that do not exist in stdClass. This will not be allowed in future versions of PHPUnit.

An API for this might be nice indeed!

@ChrisHSandN
Copy link

After some experimentation there still seems to be a method which works:

$mock = $this
  ->getMockBuilder(\stdclass::class)
  ->addMethods(['__invoke'])
  ->getMock();

$mock
  ->expects(self::once())
  ->method('__invoke'));

(Tested on PHPUnit 9 / PHP 7.3)

@mnapoli
Copy link
Contributor

mnapoli commented Mar 20, 2024

Following up on this: #5759

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/enhancement A new idea that should be implemented
Projects
None yet
Development

No branches or pull requests

6 participants