-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mocking closures is doable today by mocking __invoke on an invokable class. However this is troublesome, as it requires writing an invokable class, and mocking a class method. This new helper makes things easier via a new `$this->createClosureMock()` method.
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\MockObject\Stub; | ||
|
||
use PHPUnit\Framework\InvalidArgumentException; | ||
use PHPUnit\Framework\MockObject\Builder\InvocationMocker; | ||
use PHPUnit\Framework\MockObject\MethodCannotBeConfiguredException; | ||
use PHPUnit\Framework\MockObject\MethodNameAlreadyConfiguredException; | ||
use PHPUnit\Framework\MockObject\Rule\InvocationOrder; | ||
|
||
/** | ||
* @mixin \PHPUnit\Framework\MockObject\MockObject | ||
* | ||
* @method InvocationMocker method($constraint) | ||
*/ | ||
class ClosureMock | ||
{ | ||
public function __invoke(): mixed | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
* @throws MethodCannotBeConfiguredException | ||
* @throws MethodNameAlreadyConfiguredException | ||
*/ | ||
public function expectsClosure(InvocationOrder $invocationRule): InvocationMocker | ||
{ | ||
return $this->expects($invocationRule) | ||
->method('__invoke'); | ||
} | ||
|
||
public function closure(): InvocationMocker | ||
{ | ||
return $this->method('__invoke'); | ||
Check failure on line 43 in src/Framework/MockObject/Runtime/Stub/ClosureMock.php GitHub Actions / Type CheckerUndefinedMethod
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/unit/Framework/MockObject/Creation/CreateClosureMockTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\MockObject; | ||
|
||
use function call_user_func_array; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\Attributes\Medium; | ||
use PHPUnit\Framework\Attributes\TestDox; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\MockObject\Stub\ClosureMock; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionProperty; | ||
|
||
#[Group('test-doubles')] | ||
#[Group('test-doubles/creation')] | ||
#[Group('test-doubles/mock-object')] | ||
#[Medium] | ||
#[TestDox('createClosureMock()')] | ||
final class CreateClosureMockTest extends TestCase | ||
{ | ||
public function testCreateClosureMock(): void | ||
{ | ||
$mock = $this->createClosureMock(); | ||
|
||
$this->assertInstanceOf(ClosureMock::class, $mock); | ||
$this->assertInstanceOf(Stub::class, $mock); | ||
} | ||
|
||
public function testCreateClosureMockWithReturnValue(): void | ||
{ | ||
$mock = $this->createClosureMock(); | ||
|
||
$mock->closure()->willReturn(123); | ||
|
||
$this->assertSame(123, $mock()); | ||
} | ||
|
||
public function testCreateClosureMockWithExpectation(): void | ||
{ | ||
$mock = $this->createClosureMock(); | ||
|
||
$mock->expectsClosure($this->once()) | ||
->willReturn(123); | ||
|
||
$this->assertSame(123, $mock()); | ||
} | ||
|
||
public function testClosureMockAppliesExpects(): void | ||
{ | ||
$mock = $this->createClosureMock(); | ||
|
||
$mock->expectsClosure($this->once()); | ||
|
||
$this->assertThatMockObjectExpectationFails( | ||
"Expectation failed for method name is \"__invoke\" when invoked 1 time.\nMethod was expected to be called 1 time, actually called 0 times.\n", | ||
$mock, | ||
); | ||
} | ||
|
||
private function assertThatMockObjectExpectationFails(string $expectationFailureMessage, MockObject $mock, string $methodName = '__phpunit_verify', array $arguments = []): void | ||
{ | ||
try { | ||
call_user_func_array([$mock, $methodName], $arguments); | ||
} catch (ExpectationFailedException|MatchBuilderNotFoundException $e) { | ||
$this->assertSame($expectationFailureMessage, $e->getMessage()); | ||
|
||
return; | ||
} finally { | ||
$this->resetMockObjects(); | ||
} | ||
|
||
$this->fail(); | ||
} | ||
|
||
private function resetMockObjects(): void | ||
{ | ||
(new ReflectionProperty(TestCase::class, 'mockObjects'))->setValue($this, []); | ||
} | ||
} |