Skip to content

Commit d62b3ca

Browse files
Improve runtime tests for test proxies
1 parent 6e388aa commit d62b3ca

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

tests/_files/TestProxyFixture.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
class TestProxyFixture
11+
{
12+
public function returnString()
13+
{
14+
return 'result';
15+
}
16+
17+
public function returnTypedString(): string
18+
{
19+
return 'result';
20+
}
21+
22+
public function returnObject()
23+
{
24+
return new self;
25+
}
26+
27+
public function returnTypedObject(): self
28+
{
29+
return new self;
30+
}
31+
}
Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/*
33
* This file is part of PHPUnit.
44
*
@@ -8,34 +8,60 @@
88
* file that was distributed with this source code.
99
*/
1010

11+
use PHPUnit\Framework\MockObject\MockObject;
1112
use PHPUnit\Framework\TestCase;
1213

13-
class ProxyObjectTest extends TestCase
14+
final class ProxyObjectTest extends TestCase
1415
{
15-
public function testMockedMethodIsProxiedToOriginalMethod(): void
16+
public function testProxyingMethodWithUndeclaredScalarReturnTypeWorks(): void
1617
{
17-
$proxy = $this->getMockBuilder(Bar::class)
18-
->enableProxyingToOriginalMethods()
19-
->getMock();
18+
$proxy = $this->createTestProxy(TestProxyFixture::class);
2019

2120
$proxy->expects($this->once())
22-
->method('doSomethingElse');
21+
->method('returnString');
2322

24-
$foo = new Foo;
23+
\assert($proxy instanceof MockObject);
24+
\assert($proxy instanceof TestProxyFixture);
2525

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

29-
public function testMockedMethodWithReferenceIsProxiedToOriginalMethod(): void
29+
public function testProxyingMethodWithDeclaredScalarReturnTypeWorks(): void
3030
{
31-
$proxy = $this->getMockBuilder(MethodCallbackByReference::class)
32-
->enableProxyingToOriginalMethods()
33-
->getMock();
31+
$proxy = $this->createTestProxy(TestProxyFixture::class);
3432

35-
$a = $b = $c = 0;
33+
$proxy->expects($this->once())
34+
->method('returnTypedString');
35+
36+
\assert($proxy instanceof MockObject);
37+
\assert($proxy instanceof TestProxyFixture);
38+
39+
$this->assertSame('result', $proxy->returnTypedString());
40+
}
41+
42+
public function testProxyingMethodWithUndeclaredObjectReturnTypeWorks(): void
43+
{
44+
$proxy = $this->createTestProxy(TestProxyFixture::class);
45+
46+
$proxy->expects($this->once())
47+
->method('returnObject');
48+
49+
\assert($proxy instanceof MockObject);
50+
\assert($proxy instanceof TestProxyFixture);
51+
52+
$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnObject());
53+
}
54+
55+
public function testProxyingMethodWithDeclaredObjectReturnTypeWorks(): void
56+
{
57+
$proxy = $this->createTestProxy(TestProxyFixture::class);
58+
59+
$proxy->expects($this->once())
60+
->method('returnTypedObject');
3661

37-
$proxy->callback($a, $b, $c);
62+
\assert($proxy instanceof MockObject);
63+
\assert($proxy instanceof TestProxyFixture);
3864

39-
$this->assertEquals(1, $b);
65+
$this->assertInstanceOf(TestProxyFixture::class, $proxy->returnTypedObject());
4066
}
4167
}

0 commit comments

Comments
 (0)