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 closure mocks #5759

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Framework/MockObject/Runtime/Stub/ClosureMock.php
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)
Copy link
Contributor Author

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:

* @method InvocationMocker method($constraint)

*/
class ClosureMock
{
public function __invoke(): mixed

Check warning on line 25 in src/Framework/MockObject/Runtime/Stub/ClosureMock.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/MockObject/Runtime/Stub/ClosureMock.php#L25

Added line #L25 was not covered by tests
{
return null;

Check warning on line 27 in src/Framework/MockObject/Runtime/Stub/ClosureMock.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/MockObject/Runtime/Stub/ClosureMock.php#L27

Added line #L27 was not covered by tests
}

/**
* @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

View workflow job for this annotation

GitHub Actions / Type Checker

UndefinedMethod

src/Framework/MockObject/Runtime/Stub/ClosureMock.php:43:23: UndefinedMethod: Method PHPUnit\Framework\MockObject\Stub\ClosureMock::method does not exist (see https://psalm.dev/022)
}
}
12 changes: 12 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
use PHPUnit\Framework\MockObject\Rule\InvokedAtMostCount as InvokedAtMostCountMatcher;
use PHPUnit\Framework\MockObject\Rule\InvokedCount as InvokedCountMatcher;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\MockObject\Stub\ClosureMock;
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls as ConsecutiveCallsStub;
use PHPUnit\Framework\MockObject\Stub\Exception as ExceptionStub;
use PHPUnit\Framework\MockObject\Stub\ReturnArgument as ReturnArgumentStub;
Expand Down Expand Up @@ -1387,6 +1388,17 @@ final protected function createPartialMock(string $originalClassName, array $met
return $partialMock;
}

/**
* Creates mock of a closure.
*
* @throws InvalidArgumentException
* @throws MockObjectException
*/
final protected function createClosureMock(): ClosureMock|MockObject
{
return $this->createPartialMock(ClosureMock::class, ['__invoke']);
}

/**
* Creates a test proxy for the specified class.
*
Expand Down
86 changes: 86 additions & 0 deletions tests/unit/Framework/MockObject/Creation/CreateClosureMockTest.php
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, []);
}
}