Skip to content

Commit

Permalink
Move tests that test functionality of mock objects created using the …
Browse files Browse the repository at this point in the history
…Mock Builder API from TestDoubleTestCase (which contains common test cases for test stubs and mock objects) to MockObjectTest
  • Loading branch information
sebastianbergmann committed Dec 11, 2024
1 parent a770e0d commit 7195d43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
50 changes: 50 additions & 0 deletions tests/unit/Framework/MockObject/MockObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
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;
use PHPUnit\Framework\Attributes\TestDox;
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')]
Expand Down Expand Up @@ -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
*/
Expand Down
46 changes: 0 additions & 46 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7195d43

Please sign in to comment.