Skip to content

Commit

Permalink
Merge branch '10.5' into 11.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 11, 2024
2 parents a6586b3 + 7195d43 commit 04bd064
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 60 deletions.
65 changes: 65 additions & 0 deletions tests/unit/Framework/MockObject/MockObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
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\IgnorePhpunitDeprecations;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\Attributes\RequiresMethod;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\MockObject\Runtime\PropertyHook;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\MockObject\AnInterface;
use PHPUnit\TestFixture\MockObject\ExtendableClassWithCloneMethod;
use PHPUnit\TestFixture\MockObject\ExtendableClassWithPropertyWithSetHook;
use PHPUnit\TestFixture\MockObject\ExtendableReadonlyClassWithCloneMethod;
use PHPUnit\TestFixture\MockObject\InterfaceWithImplicitProtocol;
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodThatExpectsObject;
use PHPUnit\TestFixture\MockObject\InterfaceWithPropertyWithSetHook;
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 @@ -484,6 +490,65 @@ public function testExpectationCanBeConfiguredForSetHookForPropertyOfExtendableC
$double->property = 'value';
}

#[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();
}

#[IgnorePhpunitDeprecations]
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;
}

#[TestDox('Original __clone() method can optionally be called when test double object is cloned (readonly class)')]
#[RequiresPhp('^8.3')]
public function testOriginalCloneMethodCanOptionallyBeCalledWhenTestDoubleObjectOfReadonlyClassIsCloned(): void
{
$double = $this->getMockBuilder(ExtendableReadonlyClassWithCloneMethod::class)->enableOriginalClone()->getMock();

$this->expectException(Exception::class);
$this->expectExceptionMessage(ExtendableReadonlyClassWithCloneMethod::class . '::__clone');

clone $double;
}

/**
* @param class-string $type
*/
Expand Down
60 changes: 0 additions & 60 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace PHPUnit\Framework\MockObject;

use Exception;
use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\Ticket;
Expand Down Expand Up @@ -43,28 +42,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 @@ -94,20 +71,6 @@ public function testObjectsPassedAsArgumentAreNotClonedByDefault(): void
$this->assertSame($object, $double->doSomething($object));
}

#[IgnorePhpunitDeprecations]
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 @@ -267,17 +230,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;
}

#[TestDox('Original __clone() method is not called by default when test double object is cloned (readonly class)')]
#[RequiresPhp('^8.3')]
final public function testOriginalCloneMethodIsNotCalledByDefaultWhenTestDoubleObjectOfReadonlyClassIsCloned(): void
Expand All @@ -287,18 +239,6 @@ final public function testOriginalCloneMethodIsNotCalledByDefaultWhenTestDoubleO
$this->assertFalse($double->doSomething());
}

#[TestDox('Original __clone() method can optionally be called when test double object is cloned (readonly class)')]
#[RequiresPhp('^8.3')]
final public function testOriginalCloneMethodCanOptionallyBeCalledWhenTestDoubleObjectOfReadonlyClassIsCloned(): void
{
$double = $this->getMockBuilder(ExtendableReadonlyClassWithCloneMethod::class)->enableOriginalClone()->getMock();

$this->expectException(Exception::class);
$this->expectExceptionMessage(ExtendableReadonlyClassWithCloneMethod::class . '::__clone');

clone $double;
}

public function testMethodNameCanOnlyBeConfiguredOnce(): void
{
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);
Expand Down

0 comments on commit 04bd064

Please sign in to comment.