From 7195d437a549ca34c3c50f1a76cbf315d2915a6b Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Wed, 11 Dec 2024 09:53:20 +0100 Subject: [PATCH] Move tests that test functionality of mock objects created using the Mock Builder API from TestDoubleTestCase (which contains common test cases for test stubs and mock objects) to MockObjectTest --- .../Framework/MockObject/MockObjectTest.php | 50 +++++++++++++++++++ .../MockObject/TestDoubleTestCase.php | 46 ----------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/tests/unit/Framework/MockObject/MockObjectTest.php b/tests/unit/Framework/MockObject/MockObjectTest.php index 8031e993fe0..55f4f60862d 100644 --- a/tests/unit/Framework/MockObject/MockObjectTest.php +++ b/tests/unit/Framework/MockObject/MockObjectTest.php @@ -10,6 +10,7 @@ namespace PHPUnit\Framework\MockObject; use function call_user_func_array; +use Exception; use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Medium; @@ -17,10 +18,13 @@ use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; use PHPUnit\TestFixture\MockObject\AnInterface; +use PHPUnit\TestFixture\MockObject\ExtendableClassWithCloneMethod; use PHPUnit\TestFixture\MockObject\InterfaceWithImplicitProtocol; +use PHPUnit\TestFixture\MockObject\InterfaceWithMethodThatExpectsObject; use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration; use PHPUnit\TestFixture\MockObject\MethodWIthVariadicVariables; use ReflectionProperty; +use stdClass; #[Group('test-doubles')] #[Group('test-doubles/mock-object')] @@ -449,6 +453,52 @@ public function testExpectationsAreClonedWhenTestDoubleIsCloned(): void $this->assertSame(2, $clone->doSomethingElse(0)); } + #[TestDox('__toString() method returns empty string when return value generation is disabled and no return value is configured')] + public function testToStringMethodReturnsEmptyStringWhenReturnValueGenerationIsDisabledAndNoReturnValueIsConfigured(): void + { + $double = $this->getMockBuilder(InterfaceWithReturnTypeDeclaration::class) + ->disableAutoReturnValueGeneration() + ->getMock(); + + $this->assertSame('', $double->__toString()); + } + + public function testMethodDoesNotReturnValueWhenReturnValueGenerationIsDisabledAndNoReturnValueIsConfigured(): void + { + $double = $this->getMockBuilder(InterfaceWithReturnTypeDeclaration::class) + ->disableAutoReturnValueGeneration() + ->getMock(); + + $this->expectException(ReturnValueNotConfiguredException::class); + $this->expectExceptionMessage('No return value is configured for ' . InterfaceWithReturnTypeDeclaration::class . '::doSomething() and return value generation is disabled'); + + $double->doSomething(); + } + + public function testCloningOfObjectsPassedAsArgumentCanBeEnabled(): void + { + $object = new stdClass; + + $double = $this->getMockBuilder(InterfaceWithMethodThatExpectsObject::class) + ->enableArgumentCloning() + ->getMock(); + + $double->method('doSomething')->willReturnArgument(0); + + $this->assertNotSame($object, $double->doSomething($object)); + } + + #[TestDox('Original __clone() method can optionally be called when test double object is cloned')] + public function testOriginalCloneMethodCanOptionallyBeCalledWhenTestDoubleObjectIsCloned(): void + { + $double = $this->getMockBuilder(ExtendableClassWithCloneMethod::class)->enableOriginalClone()->getMock(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage(ExtendableClassWithCloneMethod::class . '::__clone'); + + clone $double; + } + /** * @psalm-param class-string $type */ diff --git a/tests/unit/Framework/MockObject/TestDoubleTestCase.php b/tests/unit/Framework/MockObject/TestDoubleTestCase.php index c883bd53cc7..177f296ae81 100644 --- a/tests/unit/Framework/MockObject/TestDoubleTestCase.php +++ b/tests/unit/Framework/MockObject/TestDoubleTestCase.php @@ -35,28 +35,6 @@ final public function testMethodReturnsGeneratedValueWhenReturnValueGenerationIs $this->assertFalse($double->doSomething()); } - #[TestDox('__toString() method returns empty string when return value generation is disabled and no return value is configured')] - final public function testToStringMethodReturnsEmptyStringWhenReturnValueGenerationIsDisabledAndNoReturnValueIsConfigured(): void - { - $double = $this->getMockBuilder(InterfaceWithReturnTypeDeclaration::class) - ->disableAutoReturnValueGeneration() - ->getMock(); - - $this->assertSame('', $double->__toString()); - } - - final public function testMethodDoesNotReturnValueWhenReturnValueGenerationIsDisabledAndNoReturnValueIsConfigured(): void - { - $double = $this->getMockBuilder(InterfaceWithReturnTypeDeclaration::class) - ->disableAutoReturnValueGeneration() - ->getMock(); - - $this->expectException(ReturnValueNotConfiguredException::class); - $this->expectExceptionMessage('No return value is configured for ' . InterfaceWithReturnTypeDeclaration::class . '::doSomething() and return value generation is disabled'); - - $double->doSomething(); - } - final public function testMethodReturnsConfiguredValueWhenReturnValueIsConfigured(): void { $double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class); @@ -86,19 +64,6 @@ public function testObjectsPassedAsArgumentAreNotClonedByDefault(): void $this->assertSame($object, $double->doSomething($object)); } - public function testCloningOfObjectsPassedAsArgumentCanBeEnabled(): void - { - $object = new stdClass; - - $double = $this->getMockBuilder(InterfaceWithMethodThatExpectsObject::class) - ->enableArgumentCloning() - ->getMock(); - - $double->method('doSomething')->willReturnArgument(0); - - $this->assertNotSame($object, $double->doSomething($object)); - } - final public function testMethodCanBeConfiguredToReturnOneOfItsArguments(): void { $double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class); @@ -258,17 +223,6 @@ final public function testOriginalCloneMethodIsNotCalledByDefaultWhenTestDoubleO $this->assertFalse($double->doSomething()); } - #[TestDox('Original __clone() method can optionally be called when test double object is cloned')] - final public function testOriginalCloneMethodCanOptionallyBeCalledWhenTestDoubleObjectIsCloned(): void - { - $double = $this->getMockBuilder(ExtendableClassWithCloneMethod::class)->enableOriginalClone()->getMock(); - - $this->expectException(Exception::class); - $this->expectExceptionMessage(ExtendableClassWithCloneMethod::class . '::__clone'); - - clone $double; - } - public function testMethodNameCanOnlyBeConfiguredOnce(): void { $double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);