Skip to content

Commit d49b8c5

Browse files
committed
Merge branch 'feature/phpunit-8.1-support'
Close php-mock#35 Fix php-mock#34
2 parents c3cee2c + f53e5e5 commit d49b8c5

7 files changed

+208
-10
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env:
2121
- PHPUNIT_VERSION=~7.4.0
2222
- PHPUNIT_VERSION=~7.5.0
2323
- PHPUNIT_VERSION=~8.0.0
24+
- PHPUNIT_VERSION=~8.1.0
2425

2526
php:
2627
- 7.3
@@ -34,10 +35,14 @@ matrix:
3435
exclude:
3536
- php: 7.1
3637
env: PHPUNIT_VERSION=dev-master
38+
- php: 7.1
39+
env: PHPUNIT_VERSION=~8.1.0
3740
- php: 7.1
3841
env: PHPUNIT_VERSION=~8.0.0
3942
- php: 7
4043
env: PHPUNIT_VERSION=dev-master
44+
- php: 7
45+
env: PHPUNIT_VERSION=~8.1.0
4146
- php: 7
4247
env: PHPUNIT_VERSION=~8.0.0
4348
- php: 7

autoload.php

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class_alias(
4242
);
4343
}
4444

45+
if (! class_exists(\PHPUnit\Framework\MockObject\InvocationMocker::class)) {
46+
class_alias(
47+
\PHPUnit_Framework_MockObject_InvocationMocker::class,
48+
\PHPUnit\Framework\MockObject\InvocationMocker::class
49+
);
50+
}
51+
4552
if (! class_exists(\PHPUnit\Framework\BaseTestListener::class)) {
4653
include __DIR__ . '/compatibility/BaseTestListener.php';
4754
class_alias(
@@ -54,3 +61,13 @@ class_alias(
5461
phpmock\phpunit\MockDisabler::class
5562
);
5663
}
64+
65+
if (class_exists(\PHPUnit\Runner\Version::class)
66+
&& version_compare(\PHPUnit\Runner\Version::id(), '8.1.0') >= 0
67+
) {
68+
class_alias(\phpmock\phpunit\DefaultArgumentRemoverReturnTypes::class, \phpmock\phpunit\DefaultArgumentRemover::class);
69+
class_alias(\phpmock\phpunit\MockObjectProxyReturnTypes::class, \phpmock\phpunit\MockObjectProxy::class);
70+
} else {
71+
class_alias(\phpmock\phpunit\DefaultArgumentRemoverNoReturnTypes::class, \phpmock\phpunit\DefaultArgumentRemover::class);
72+
class_alias(\phpmock\phpunit\MockObjectProxyNoReturnTypes::class, \phpmock\phpunit\MockObjectProxy::class);
73+
}

classes/DefaultArgumentRemover.php renamed to classes/DefaultArgumentRemoverNoReturnTypes.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @license http://www.wtfpl.net/txt/copying/ WTFPL
1515
* @internal
1616
*/
17-
class DefaultArgumentRemover implements InvocationInterface
17+
class DefaultArgumentRemoverNoReturnTypes implements InvocationInterface
1818
{
1919

2020
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace phpmock\phpunit;
4+
5+
use phpmock\generator\MockFunctionGenerator;
6+
use PHPUnit\Framework\MockObject\Invocation;
7+
use PHPUnit\Framework\MockObject\Matcher\Invocation as InvocationInterface;
8+
9+
/**
10+
* Removes default arguments from the invocation.
11+
*
12+
* @author Markus Malkusch <markus@malkusch.de>
13+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
14+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
15+
* @internal
16+
*/
17+
class DefaultArgumentRemoverReturnTypes implements InvocationInterface
18+
{
19+
20+
/**
21+
* @SuppressWarnings(PHPMD)
22+
*/
23+
public function invoked(Invocation $invocation)
24+
{
25+
}
26+
27+
/**
28+
* @SuppressWarnings(PHPMD)
29+
*/
30+
public function matches(Invocation $invocation) : bool
31+
{
32+
if ($invocation instanceof Invocation\StaticInvocation) {
33+
$this->removeDefaultArguments($invocation);
34+
} else {
35+
MockFunctionGenerator::removeDefaultArguments($invocation->parameters);
36+
}
37+
38+
return false;
39+
}
40+
41+
public function verify() : void
42+
{
43+
}
44+
45+
/**
46+
* This method is not defined in the interface, but used in
47+
* PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers().
48+
*
49+
* @return boolean
50+
* @see \PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers()
51+
*/
52+
public function hasMatchers()
53+
{
54+
return false;
55+
}
56+
57+
public function toString() : string
58+
{
59+
return __CLASS__;
60+
}
61+
62+
/**
63+
* Remove default arguments from StaticInvocation or its children (hack)
64+
*
65+
* @SuppressWarnings(PHPMD)
66+
*/
67+
private function removeDefaultArguments(Invocation\StaticInvocation $invocation)
68+
{
69+
$remover = function () {
70+
MockFunctionGenerator::removeDefaultArguments($this->parameters);
71+
};
72+
73+
$remover->bindTo($invocation, Invocation\StaticInvocation::class)();
74+
}
75+
}

classes/MockObjectProxy.php renamed to classes/MockObjectProxyNoReturnTypes.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @license http://www.wtfpl.net/txt/copying/ WTFPL
1515
* @internal
1616
*/
17-
class MockObjectProxy implements MockObject
17+
class MockObjectProxyNoReturnTypes implements MockObject
1818
{
1919

2020
/**
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace phpmock\phpunit;
4+
5+
use PHPUnit\Framework\MockObject\Builder\InvocationMocker as BuilderInvocationMocker;
6+
use PHPUnit\Framework\MockObject\InvocationMocker;
7+
use PHPUnit\Framework\MockObject\Matcher\Invocation;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use phpmock\integration\MockDelegateFunctionBuilder;
10+
11+
/**
12+
* Proxy for PHPUnit's PHPUnit_Framework_MockObject_MockObject.
13+
*
14+
* @author Markus Malkusch <markus@malkusch.de>
15+
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
16+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
17+
* @internal
18+
*/
19+
class MockObjectProxyReturnTypes implements MockObject
20+
{
21+
22+
/**
23+
* @var MockObject $mockObject The mock object.
24+
*/
25+
private $mockObject;
26+
27+
/**
28+
* Inject the subject.
29+
*
30+
* @param MockObject $mockObject The subject.
31+
*/
32+
public function __construct(MockObject $mockObject)
33+
{
34+
$this->mockObject = $mockObject;
35+
}
36+
37+
/**
38+
* @SuppressWarnings(PHPMD)
39+
*/
40+
// @codingStandardsIgnoreStart
41+
public function __phpunit_getInvocationMocker() : InvocationMocker
42+
{
43+
// @codingStandardsIgnoreEnd
44+
return $this->mockObject->__phpunit_getInvocationMocker();
45+
}
46+
47+
/**
48+
* @SuppressWarnings(PHPMD)
49+
*/
50+
// @codingStandardsIgnoreStart
51+
public function __phpunit_setOriginalObject($originalObject) : void
52+
{
53+
// @codingStandardsIgnoreEnd
54+
$this->mockObject->__phpunit_setOriginalObject($originalObject);
55+
}
56+
57+
/**
58+
* @SuppressWarnings(PHPMD)
59+
*/
60+
// @codingStandardsIgnoreStart
61+
public function __phpunit_verify(bool $unsetInvocationMocker = true) : void
62+
{
63+
// @codingStandardsIgnoreEnd
64+
$this->mockObject->__phpunit_verify($unsetInvocationMocker);
65+
}
66+
67+
public function expects(Invocation $matcher) : BuilderInvocationMocker
68+
{
69+
return $this->mockObject->expects($matcher)->method(MockDelegateFunctionBuilder::METHOD);
70+
}
71+
72+
/**
73+
* This method is not part of the contract but was found in
74+
* PHPUnit's mocked_class.tpl.dist.
75+
*
76+
* @SuppressWarnings(PHPMD)
77+
*/
78+
// @codingStandardsIgnoreStart
79+
public function __phpunit_hasMatchers() : bool
80+
{
81+
// @codingStandardsIgnoreEnd
82+
return $this->mockObject->__phpunit_hasMatchers();
83+
}
84+
85+
/**
86+
* @SuppressWarnings(PHPMD)
87+
*/
88+
// @codingStandardsIgnoreStart
89+
public function __phpunit_setReturnValueGeneration(bool $returnValueGeneration) : void
90+
{
91+
// @codingStandardsIgnoreEnd
92+
$this->mockObject->__phpunit_setReturnValueGeneration($returnValueGeneration);
93+
}
94+
}

tests/MockObjectProxyTest.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public function testExpects()
6666
public function testHasMatcher()
6767
{
6868
$prophecy = $this->prophesize(MockObject::class);
69-
$prophecy->__phpunit_hasMatchers()->willReturn("foo");
69+
$prophecy->__phpunit_hasMatchers()->willReturn(true);
7070
$mock = $prophecy->reveal();
7171

7272
$proxy = new MockObjectProxy($mock);
7373

7474
$result = $proxy->__phpunit_hasMatchers();
75-
$this->assertEquals("foo", $result);
75+
$this->assertTrue($result);
7676
}
7777

7878
/**
@@ -84,16 +84,23 @@ public function testHasMatcher()
8484
* @test
8585
* @dataProvider provideTestProxiedMethods
8686
*/
87-
public function testProxiedMethods($method, array $arguments = [], $expected = "foo")
87+
public function testProxiedMethods($method, array $arguments = [], $expected = null)
8888
{
8989
$prophecy = $this->prophesize(MockObject::class);
90-
call_user_func_array([$prophecy, $method], $arguments)->willReturn($expected);
90+
if ($expected) {
91+
call_user_func_array([$prophecy, $method], $arguments)->willReturn($expected)->shouldBeCalledTimes(1);
92+
} else {
93+
call_user_func_array([$prophecy, $method], $arguments)->shouldBeCalledTimes(1);
94+
}
9195
$mock = $prophecy->reveal();
9296

9397
$proxy = new MockObjectProxy($mock);
9498

9599
$result = call_user_func_array([$proxy, $method], $arguments);
96-
$this->assertEquals($expected, $result);
100+
101+
if ($expected) {
102+
$this->assertSame($expected, $result);
103+
}
97104
}
98105

99106
/**
@@ -104,9 +111,9 @@ public function testProxiedMethods($method, array $arguments = [], $expected = "
104111
public function provideTestProxiedMethods()
105112
{
106113
return [
107-
["__phpunit_getInvocationMocker"],
108-
["__phpunit_setOriginalObject", ["bar"]],
109-
["__phpunit_verify", [true]],
114+
['__phpunit_getInvocationMocker', [], new \PHPUnit\Framework\MockObject\InvocationMocker([], true)],
115+
['__phpunit_setOriginalObject', ['bar']],
116+
['__phpunit_verify', [true]],
110117
];
111118
}
112119
}

0 commit comments

Comments
 (0)