From b17c1e8ff68273e986f7ebabb29d9b852fc6847c Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Wed, 7 Feb 2024 15:41:18 +0100 Subject: [PATCH] Closes #5321 --- .../CannotUseAddMethodsException.php | 29 ---------- src/Framework/MockObject/MockBuilder.php | 50 ----------------- .../MockObject/Creation/MockBuilderTest.php | 53 ------------------- 3 files changed, 132 deletions(-) delete mode 100644 src/Framework/MockObject/Exception/CannotUseAddMethodsException.php diff --git a/src/Framework/MockObject/Exception/CannotUseAddMethodsException.php b/src/Framework/MockObject/Exception/CannotUseAddMethodsException.php deleted file mode 100644 index 848746b523a..00000000000 --- a/src/Framework/MockObject/Exception/CannotUseAddMethodsException.php +++ /dev/null @@ -1,29 +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; - -use function sprintf; - -/** - * @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 451f27512a4..db09366f08c 100644 --- a/src/Framework/MockObject/MockBuilder.php +++ b/src/Framework/MockObject/MockBuilder.php @@ -13,7 +13,6 @@ use function assert; use function debug_backtrace; use PHPUnit\Event\Facade as EventFacade; -use PHPUnit\Framework\Exception; use PHPUnit\Framework\InvalidArgumentException; use PHPUnit\Framework\MockObject\Generator\ClassAlreadyExistsException; use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException; @@ -157,55 +156,6 @@ public function onlyMethods(array $methods): self return $this; } - /** - * Specifies methods that don't exist in the class which you want to mock. - * - * @psalm-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 - } 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 a4dd724074b..a024126ce4f 100644 --- a/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php @@ -17,13 +17,10 @@ use PHPUnit\Framework\Attributes\Medium; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; -use PHPUnit\TestFixture\MockObject\AbstractClass; use PHPUnit\TestFixture\MockObject\ExtendableClass; use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration; -use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod; #[CoversClass(MockBuilder::class)] -#[CoversClass(CannotUseAddMethodsException::class)] #[Group('test-doubles')] #[Group('test-doubles/creation')] #[Group('test-doubles/mock-object')] @@ -42,56 +39,6 @@ public function testCanCreateMockObjectWithSpecifiedClassName(): void $this->assertSame($className, $double::class); } - #[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 testCannotCreateMockObjectForExtendableClassAddingMethodsToItThatItAlreadyHas(): void - { - $this->expectException(CannotUseAddMethodsException::class); - - $this->getMockBuilder(ExtendableClass::class) - ->addMethods(['doSomething']) - ->getMock(); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class')] - public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods(): void - { - $mock = $this->getMockBuilder(AbstractClass::class) - ->getMockForAbstractClass(); - - $mock->expects($this->once())->method('doSomethingElse')->willReturn(true); - - $this->assertTrue($mock->doSomething()); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('getMockForTrait() can be used to create a mock object for a trait')] - public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods(): void - { - $mock = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class) - ->getMockForTrait(); - - $mock->method('abstractMethod')->willReturn(true); - - $this->assertTrue($mock->concreteMethod()); - } - #[IgnorePhpunitDeprecations] #[TestDox('onlyMethods() can be used to configure which methods should be doubled')] public function testCreatesPartialMockObjectForExtendableClass(): void