Skip to content

Commit

Permalink
Add tests cloning of test double objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jul 13, 2023
1 parent 0308f56 commit 5325aaa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/_files/mock-object/ExtendableClassWithCloneMethod.php
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\MockObject;

use Exception;

class ExtendableClassWithCloneMethod
{
/**
* @throws Exception
*/
public function __clone(): void
{
throw new Exception(__METHOD__);
}

public function method(): bool
{
return true;
}
}
20 changes: 20 additions & 0 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Expand Up @@ -12,6 +12,7 @@
use Exception;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\MockObject\ExtendableClassWithCloneMethod;
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodThatExpectsObject;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use stdClass;
Expand Down Expand Up @@ -184,6 +185,25 @@ final public function testMethodCanBeConfiguredToThrowAnException(): void
$this->fail();
}

#[TestDox('Original __clone() method is not called by default when test double object is cloned')]
public function testOriginalCloneMethodIsNotCalledByDefaultWhenTestDoubleObjectIsCloned(): void
{
$double = clone $this->createTestDouble(ExtendableClassWithCloneMethod::class);

$this->assertFalse($double->method());
}

#[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-template RealInstanceType of object
*
Expand Down

0 comments on commit 5325aaa

Please sign in to comment.