Skip to content

Commit

Permalink
Add tests for argument cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 24, 2023
1 parent 83a37cf commit b2d3e2a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/_files/mock-object/InterfaceWithMethodThatExpectsObject.php
@@ -0,0 +1,15 @@
<?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;

interface InterfaceWithMethodThatExpectsObject
{
public function doSomething(object $object): object;
}
26 changes: 26 additions & 0 deletions tests/unit/Framework/MockObject/TestDoubleTestCase.php
Expand Up @@ -20,8 +20,10 @@
use PHPUnit\TestFixture\MockObject\Enumeration;
use PHPUnit\TestFixture\MockObject\ExtendableClass;
use PHPUnit\TestFixture\MockObject\FinalClass;
use PHPUnit\TestFixture\MockObject\InterfaceWithMethodThatExpectsObject;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use PHPUnit\TestFixture\MockObject\ReadonlyClass;
use stdClass;

abstract class TestDoubleTestCase extends TestCase
{
Expand Down Expand Up @@ -125,6 +127,30 @@ final public function testConfiguredReturnValueMustBeCompatibleWithReturnTypeDec
$double->method('doSomething')->willReturn(null);
}

public function testObjectsPassedAsArgumentAreNotClonedByDefault(): void
{
$object = new stdClass;

$double = $this->createTestDouble(InterfaceWithMethodThatExpectsObject::class);

$double->method('doSomething')->willReturnArgument(0);

$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

0 comments on commit b2d3e2a

Please sign in to comment.