-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 closure mocks #5759
Open
mnapoli
wants to merge
1
commit into
sebastianbergmann:main
Choose a base branch
from
mnapoli:closure-mocks
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add closure mocks #5759
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 <sebastian@phpunit.de> | ||
* | ||
* 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 <sebastian@phpunit.de> | ||
* | ||
* 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, []); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why Psalm does not recognize this annotation. PhpStorm does, and I used the same annotation as in
MockObject
:phpunit/src/Framework/MockObject/Runtime/Interface/MockObject.php
Line 16 in 437af97