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 6e388aa commit d62b3ca
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
31 changes: 31 additions & 0 deletions tests/_files/TestProxyFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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.
*/
class TestProxyFixture
{
public function returnString()
{
return 'result';
}

public function returnTypedString(): string
{
return 'result';
}

public function returnObject()
{
return new self;
}

public function returnTypedObject(): self
{
return new self;
}
}
58 changes: 42 additions & 16 deletions tests/unit/Framework/MockObject/ProxyObjectTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
Expand All @@ -8,34 +8,60 @@
* file that was distributed with this source code.
*/

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ProxyObjectTest extends TestCase
final class ProxyObjectTest extends TestCase
{
public function testMockedMethodIsProxiedToOriginalMethod(): void
public function testProxyingMethodWithUndeclaredScalarReturnTypeWorks(): void
{
$proxy = $this->getMockBuilder(Bar::class)
->enableProxyingToOriginalMethods()
->getMock();
$proxy = $this->createTestProxy(TestProxyFixture::class);

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

$foo = new Foo;
\assert($proxy instanceof MockObject);
\assert($proxy instanceof TestProxyFixture);

$this->assertEquals('result', $foo->doSomething($proxy));
$this->assertSame('result', $proxy->returnString());
}

public function testMockedMethodWithReferenceIsProxiedToOriginalMethod(): void
public function testProxyingMethodWithDeclaredScalarReturnTypeWorks(): void
{
$proxy = $this->getMockBuilder(MethodCallbackByReference::class)
->enableProxyingToOriginalMethods()
->getMock();
$proxy = $this->createTestProxy(TestProxyFixture::class);

$a = $b = $c = 0;
$proxy->expects($this->once())
->method('returnTypedString');

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

$this->assertSame('result', $proxy->returnTypedString());
}

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

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

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

$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnObject());
}

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

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

$proxy->callback($a, $b, $c);
\assert($proxy instanceof MockObject);
\assert($proxy instanceof TestProxyFixture);

$this->assertEquals(1, $b);
$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnTypedObject());
}
}

0 comments on commit d62b3ca

Please sign in to comment.