Closure mock #257

mnapoli opened this Issue Oct 2, 2015 · 3 comments


None yet

4 participants

mnapoli commented Oct 2, 2015

It would be great to be able to create a mock that:

  • behaves like a closure
  • allows to spy if/how it is called


$mock = $this->getMockCallback();
    ->with(1, 2)

The mock would mock that behavior while allowing to spy calls:

$callback = function ($a, $b) {
    return $a + $b;

I might accept a pull request that implements this but I won't implement it myself.


It is nearly possible with the current API.

class ClosureMockTest extends PHPUnit_Framework_TestCase {

  public function testClosureMock() {
    $mock = $this->getMockCallback();
      ->method('__invoke') // method matcher
      ->with(1, 2)

    $mock(3, 4);

  private function getMockCallback() {
    $mock = $this
    return $mock;

You still need the method matcher (it is required). I see two possible solutions:

  1. Return a mock with a modified expects() that adds the method matcher.
  2. Use a pseudo class name like callable and some special handling.
enumag commented Aug 30, 2016 edited

Just found out that this actually is possible with the current API:

$mock = $this->createPartialMock(\stdClass::class, ['__invoke']);
      ->with(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment