diff --git a/ChangeLog-12.0.md b/ChangeLog-12.0.md index f0011b5f166..afe23a0ada4 100644 --- a/ChangeLog-12.0.md +++ b/ChangeLog-12.0.md @@ -13,6 +13,7 @@ All notable changes of the PHPUnit 12.0 release series are documented in this fi * [#5250](https://github.com/sebastianbergmann/phpunit/issues/5250): `TestCase::getObjectForTrait()` * [#5313](https://github.com/sebastianbergmann/phpunit/issues/5313): `MockBuilder::getMockForTrait()` * [#5314](https://github.com/sebastianbergmann/phpunit/issues/5314): `MockBuilder::getMockForAbstractClass()` +* [#5321](https://github.com/sebastianbergmann/phpunit/issues/5321): `MockBuilder::addMethods()` * [#5978](https://github.com/sebastianbergmann/phpunit/issues/5978): Support for PHP 8.2 [12.0.0]: https://github.com/sebastianbergmann/phpunit/compare/11.5...main diff --git a/DEPRECATIONS.md b/DEPRECATIONS.md index bb4b4451fe8..8e3306c66da 100644 --- a/DEPRECATIONS.md +++ b/DEPRECATIONS.md @@ -26,7 +26,6 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co | [#5309](https://github.com/sebastianbergmann/phpunit/issues/5309) | `MockBuilder::enableAutoload()` | 10.1.0 | | | [#5315](https://github.com/sebastianbergmann/phpunit/issues/5315) | `MockBuilder::disableArgumentCloning()` | 10.1.0 | | | [#5315](https://github.com/sebastianbergmann/phpunit/issues/5315) | `MockBuilder::enableArgumentCloning()` | 10.1.0 | | -| [#5320](https://github.com/sebastianbergmann/phpunit/issues/5320) | `MockBuilder::addMethods()` | 10.1.0 | | | [#5415](https://github.com/sebastianbergmann/phpunit/issues/5415) | Support for doubling interfaces (or classes) that have a method named `method` | 11.0.0 | | | [#5423](https://github.com/sebastianbergmann/phpunit/issues/5423) | `TestCase::onConsecutiveCalls()` | 10.3.0 | Use `$double->willReturn()` instead of `$double->will($this->onConsecutiveCalls())` | | [#5423](https://github.com/sebastianbergmann/phpunit/issues/5423) | `TestCase::returnArgument()` | 10.3.0 | Use `$double->willReturnArgument()` instead of `$double->will($this->returnArgument())` | diff --git a/src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php b/src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php deleted file mode 100644 index 9fc5078870c..00000000000 --- a/src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\MockObject\Generator; - -use function sprintf; - -/** - * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit - * - * @internal This class is not covered by the backward compatibility promise for PHPUnit - */ -final class CannotUseAddMethodsException extends \PHPUnit\Framework\Exception implements Exception -{ - public function __construct(string $type, string $methodName) - { - parent::__construct( - sprintf( - 'Trying to configure method "%s" with addMethods(), but it exists in class "%s". Use onlyMethods() for methods that exist in the class', - $methodName, - $type, - ), - ); - } -} diff --git a/src/Framework/MockObject/MockBuilder.php b/src/Framework/MockObject/MockBuilder.php index b79ad878c06..8daa17dd370 100644 --- a/src/Framework/MockObject/MockBuilder.php +++ b/src/Framework/MockObject/MockBuilder.php @@ -15,7 +15,6 @@ use function debug_backtrace; use PHPUnit\Event\Facade as EventFacade; use PHPUnit\Framework\InvalidArgumentException; -use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException; use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException; use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException; use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException; @@ -164,57 +163,6 @@ public function onlyMethods(array $methods): self return $this; } - /** - * Specifies methods that don't exist in the class which you want to mock. - * - * @param list $methods - * - * @throws CannotUseAddMethodsException - * @throws ReflectionException - * @throws RuntimeException - * - * @return $this - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/5320 - */ - public function addMethods(array $methods): self - { - EventFacade::emitter()->testTriggeredPhpunitDeprecation( - $this->testCase->valueObjectForEvents(), - 'MockBuilder::addMethods() is deprecated and will be removed in PHPUnit 12 without replacement.', - ); - - if (empty($methods)) { - $this->emptyMethodsArray = true; - - return $this; - } - - try { - $reflector = new ReflectionClass($this->type); - - // @codeCoverageIgnoreStart - /** @phpstan-ignore catch.neverThrown */ - } catch (\ReflectionException $e) { - throw new ReflectionException( - $e->getMessage(), - $e->getCode(), - $e, - ); - // @codeCoverageIgnoreEnd - } - - foreach ($methods as $method) { - if ($reflector->hasMethod($method)) { - throw new CannotUseAddMethodsException($this->type, $method); - } - } - - $this->methods = array_merge($this->methods, $methods); - - return $this; - } - /** * Specifies the arguments for the constructor. * diff --git a/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php b/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php index 046e2519c46..7ca327b2a45 100644 --- a/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php @@ -63,54 +63,6 @@ public function testCannotCreateMockObjectWithSpecifiedClassNameWhenClassWithTha ->getMock(); } - #[IgnorePhpunitDeprecations] - #[TestDox('addMethods() can be used to configure an additional method for the mock object class when the original class does not have a method of the same name')] - public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(): void - { - $double = $this->getMockBuilder(ExtendableClass::class) - ->addMethods(['additionalMethod']) - ->getMock(); - - $value = 'value'; - - $double->method('additionalMethod')->willReturn($value); - - $this->assertSame($value, $double->additionalMethod()); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('addMethods() cannot be used to configure an additional method for the mock object class when the original class has a method of the same name')] - public function testCannotCreateMockObjectForExtendableClassAddingMethodToItThatItAlreadyHas(): void - { - $this->expectException(CannotUseAddMethodsException::class); - - $this->getMockBuilder(ExtendableClass::class) - ->addMethods(['doSomething']) - ->getMock(); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('addMethods() cannot be used to configure an additional method for the mock object class multiple times using the same name')] - public function testCannotCreateMockObjectForExtendableClassAddingMultipleMethodsWithSameNameToIt(): void - { - $this->expectException(DuplicateMethodException::class); - - $this->getMockBuilder(ExtendableClass::class) - ->addMethods(['additionalMethod', 'additionalMethod']) - ->getMock(); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('addMethods() cannot be used to configure an additional method for the mock object class with invalid name')] - public function testCannotCreateMockObjectForExtendableClassAddingMethodToItWithInvalidName(): void - { - $this->expectException(InvalidMethodNameException::class); - - $this->getMockBuilder(ExtendableClass::class) - ->addMethods(['1234']) - ->getMock(); - } - #[TestDox('onlyMethods() can be used to configure which methods should be doubled')] public function testCreatesPartialMockObjectForExtendableClass(): void {