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

#4852 Allow use addMethods on unknown type mock in MockBuilder #4854

Closed
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
1 change: 1 addition & 0 deletions ChangeLog-10.0.md
Expand Up @@ -36,6 +36,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
* The JUnit XML logfile now has both `name` and `file` attributes on `<testcase>` elements for PHPT tests
* The `forceCoversAnnotation` attribute of the `<phpunit>` element of PHPUnit's XML configuration file has been renamed to `requireCoverageMetadata`
* The `beStrictAboutCoversAnnotation` attribute of the `<phpunit>` element of PHPUnit's XML configuration file has been renamed to `beStrictAboutCoverageMetadata`
* [#4852](https://github.com/sebastianbergmann/phpunit/issues/4852): Allow using `addMethods` when `allowMockingUnknownTypes` is true in MockBuilder

### Removed

Expand Down
30 changes: 16 additions & 14 deletions src/Framework/MockObject/MockBuilder.php
Expand Up @@ -212,21 +212,23 @@ public function addMethods(array $methods): self
return $this;
}

try {
$reflector = new ReflectionClass($this->type);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
if (!$this->allowMockingUnknownTypes) {
try {
$reflector = new ReflectionClass($this->type);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd

foreach ($methods as $method) {
if ($reflector->hasMethod($method)) {
throw new CannotUseAddMethodsException($this->type, $method);
foreach ($methods as $method) {
if ($reflector->hasMethod($method)) {
throw new CannotUseAddMethodsException($this->type, $method);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Expand Up @@ -123,6 +123,27 @@ public function testAbleToUseOnlyMethodsAfterAddMethods(): void
$this->assertNull($mock->mockableMethod());
}

public function testAddMethodsWithNonExistingClassName(): void
{
$this->expectException(\PHPUnit\Framework\MockObject\ReflectionException::class);
$this->expectErrorMessage('Class "FooBar" does not exist');

$this->getMockBuilder('FooBar')
->disallowMockingUnknownTypes()
->addMethods(['mockableMethod'])
->getMock();
}

public function testAddMethodsWithNonExistingClassNameAndAllowMockingUnknownTypes(): void
{
$mock = $this->getMockBuilder('FooBar')
->allowMockingUnknownTypes()
->addMethods(['mockableMethod'])
->getMock();

$this->assertNull($mock->mockableMethod());
}

public function testByDefaultDoesNotPassArgumentsToTheConstructor(): void
{
$mock = $this->getMockBuilder(Mockable::class)->getMock();
Expand Down