Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Created May 22, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/b68a45adf93aa9e2c94c80de301ac17d to your computer and use it in GitHub Desktop.
Save ziadoz/b68a45adf93aa9e2c94c80de301ac17d to your computer and use it in GitHub Desktop.
PHPUnit Mocking Closures
<?php
// Closure can't be mocked because it's a final class.
// Mock another class instead and add __invoke to it.
$mockClosure = $this->getMockBuilder(\stdClass::class)
->addMethods(['__invoke'])
->getMock();
$mockClosure->expects($this->exactly(1))
->method('__invoke')
->willReturn('Hello, World!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment