diff --git a/src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php b/src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php deleted file mode 100644 index db8abd1eaa2..00000000000 --- a/src/Framework/MockObject/Generator/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\Generator; - -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 101a23d04be..5512ba561ec 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\CannotUseAddMethodsException; use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException; @@ -158,55 +157,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 6e68f4a12eb..125e95ec7c7 100644 --- a/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/Creation/MockBuilderTest.php @@ -21,19 +21,15 @@ use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations; use PHPUnit\Framework\Attributes\Medium; use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException; use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException; use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException; use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException; use PHPUnit\Framework\TestCase; -use PHPUnit\TestFixture\MockObject\AbstractClass; use PHPUnit\TestFixture\MockObject\ClassCallingMethodInConstructor; use PHPUnit\TestFixture\MockObject\ExtendableClass; use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration; -use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod; #[CoversClass(MockBuilder::class)] -#[CoversClass(CannotUseAddMethodsException::class)] #[CoversClass(DuplicateMethodException::class)] #[CoversClass(InvalidMethodNameException::class)] #[CoversClass(NameAlreadyInUseException::class)] @@ -65,78 +61,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(); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class')] - public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods(): void - { - $double = $this->getMockBuilder(AbstractClass::class) - ->getMockForAbstractClass(); - - $double->expects($this->once())->method('doSomethingElse')->willReturn(true); - - $this->assertTrue($double->doSomething()); - } - - #[IgnorePhpunitDeprecations] - #[TestDox('getMockForTrait() can be used to create a mock object for a trait')] - public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods(): void - { - $double = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class) - ->getMockForTrait(); - - $double->method('abstractMethod')->willReturn(true); - - $this->assertTrue($double->concreteMethod()); - } - #[IgnorePhpunitDeprecations] #[TestDox('onlyMethods() can be used to configure which methods should be doubled')] public function testCreatesPartialMockObjectForExtendableClass(): void