Skip to content

Commit

Permalink
sebastianbergmann#4852 Allow use addMethods on unknown type mock in M…
Browse files Browse the repository at this point in the history
…ockBuilder
  • Loading branch information
alamirault committed Jan 4, 2022
1 parent d9655c8 commit b090f40
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
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

0 comments on commit b090f40

Please sign in to comment.