diff --git a/ChangeLog-10.0.md b/ChangeLog-10.0.md index 03d992ba3a4..23ab265c6b2 100644 --- a/ChangeLog-10.0.md +++ b/ChangeLog-10.0.md @@ -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 `` elements for PHPT tests * The `forceCoversAnnotation` attribute of the `` element of PHPUnit's XML configuration file has been renamed to `requireCoverageMetadata` * The `beStrictAboutCoversAnnotation` attribute of the `` 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 diff --git a/src/Framework/MockObject/MockBuilder.php b/src/Framework/MockObject/MockBuilder.php index dbd907f3969..ae26c96b258 100644 --- a/src/Framework/MockObject/MockBuilder.php +++ b/src/Framework/MockObject/MockBuilder.php @@ -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); + } } } diff --git a/tests/unit/Framework/MockObject/MockBuilderTest.php b/tests/unit/Framework/MockObject/MockBuilderTest.php index 10ef2a11d80..865f1038fc9 100644 --- a/tests/unit/Framework/MockObject/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/MockBuilderTest.php @@ -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();