Skip to content

Commit

Permalink
Improve runtime tests for test proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 19, 2019
1 parent c051ac3 commit 898243a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
24 changes: 24 additions & 0 deletions tests/_files/FinalClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

final class FinalClass
{
private $value;

public function __construct($value)
{
$this->value = $value;
}

public function value()
{
return $this->value;
}
}
24 changes: 21 additions & 3 deletions tests/_files/TestProxyFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@ public function returnTypedString(): string

public function returnObject()
{
return new self;
$result = new stdClass;

$result->foo = 'bar';

return $result;
}

public function returnTypedObject(): stdClass
{
$result = new stdClass;

$result->foo = 'bar';

return $result;
}

public function returnObjectOfFinalClass()
{
return new FinalClass('value');
}

public function returnTypedObject(): self
public function returnTypedObjectOfFinalClass(): FinalClass
{
return new self;
return new FinalClass('value');
}
}
29 changes: 21 additions & 8 deletions tests/unit/Framework/MockObject/ProxyObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

final class ProxyObjectTest extends TestCase
{
public function testProxyingMethodWithUndeclaredScalarReturnTypeWorks(): void
public function testProxyingWorksForMethodThatReturnsUndeclaredScalarValue(): void
{
$proxy = $this->createTestProxy(TestProxyFixture::class);

Expand All @@ -26,7 +26,7 @@ public function testProxyingMethodWithUndeclaredScalarReturnTypeWorks(): void
$this->assertSame('result', $proxy->returnString());
}

public function testProxyingMethodWithDeclaredScalarReturnTypeWorks(): void
public function testProxyingWorksForMethodThatReturnsDeclaredScalarValue(): void
{
$proxy = $this->createTestProxy(TestProxyFixture::class);

Expand All @@ -39,29 +39,42 @@ public function testProxyingMethodWithDeclaredScalarReturnTypeWorks(): void
$this->assertSame('result', $proxy->returnTypedString());
}

public function testProxyingMethodWithUndeclaredObjectReturnTypeWorks(): void
public function testProxyingWorksForMethodThatReturnsUndeclaredObject(): void
{
$proxy = $this->createTestProxy(TestProxyFixture::class);

$proxy->expects($this->once())
->method('returnObject');
->method('returnObject');

\assert($proxy instanceof MockObject);
\assert($proxy instanceof TestProxyFixture);

$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnObject());
$this->assertSame('bar', $proxy->returnObject()->foo);
}

public function testProxyingMethodWithDeclaredObjectReturnTypeWorks(): void
public function testProxyingWorksForMethodThatReturnsDeclaredObject(): void
{
$proxy = $this->createTestProxy(TestProxyFixture::class);

$proxy->expects($this->once())
->method('returnTypedObject');
->method('returnTypedObject');

\assert($proxy instanceof MockObject);
\assert($proxy instanceof TestProxyFixture);

$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnTypedObject());
$this->assertSame('bar', $proxy->returnTypedObject()->foo);
}

public function testProxyingWorksForMethodThatReturnsUndeclaredObjectOfFinalClass(): void
{
$proxy = $this->createTestProxy(TestProxyFixture::class);

$proxy->expects($this->once())
->method('returnObjectOfFinalClass');

\assert($proxy instanceof MockObject);
\assert($proxy instanceof TestProxyFixture);

$this->assertSame('value', $proxy->returnObjectOfFinalClass()->value());
}
}

0 comments on commit 898243a

Please sign in to comment.