Skip to content

Commit

Permalink
Merge branch 'feature/phpunit-8.1-support'
Browse files Browse the repository at this point in the history
Close #35
Fix #34
  • Loading branch information
michalbundyra committed Apr 6, 2019
2 parents c3cee2c + f53e5e5 commit d49b8c5
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
- PHPUNIT_VERSION=~7.4.0
- PHPUNIT_VERSION=~7.5.0
- PHPUNIT_VERSION=~8.0.0
- PHPUNIT_VERSION=~8.1.0

php:
- 7.3
Expand All @@ -34,10 +35,14 @@ matrix:
exclude:
- php: 7.1
env: PHPUNIT_VERSION=dev-master
- php: 7.1
env: PHPUNIT_VERSION=~8.1.0
- php: 7.1
env: PHPUNIT_VERSION=~8.0.0
- php: 7
env: PHPUNIT_VERSION=dev-master
- php: 7
env: PHPUNIT_VERSION=~8.1.0
- php: 7
env: PHPUNIT_VERSION=~8.0.0
- php: 7
Expand Down
17 changes: 17 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class_alias(
);
}

if (! class_exists(\PHPUnit\Framework\MockObject\InvocationMocker::class)) {
class_alias(
\PHPUnit_Framework_MockObject_InvocationMocker::class,
\PHPUnit\Framework\MockObject\InvocationMocker::class
);
}

if (! class_exists(\PHPUnit\Framework\BaseTestListener::class)) {
include __DIR__ . '/compatibility/BaseTestListener.php';
class_alias(
Expand All @@ -54,3 +61,13 @@ class_alias(
phpmock\phpunit\MockDisabler::class
);
}

if (class_exists(\PHPUnit\Runner\Version::class)
&& version_compare(\PHPUnit\Runner\Version::id(), '8.1.0') >= 0
) {
class_alias(\phpmock\phpunit\DefaultArgumentRemoverReturnTypes::class, \phpmock\phpunit\DefaultArgumentRemover::class);
class_alias(\phpmock\phpunit\MockObjectProxyReturnTypes::class, \phpmock\phpunit\MockObjectProxy::class);
} else {
class_alias(\phpmock\phpunit\DefaultArgumentRemoverNoReturnTypes::class, \phpmock\phpunit\DefaultArgumentRemover::class);
class_alias(\phpmock\phpunit\MockObjectProxyNoReturnTypes::class, \phpmock\phpunit\MockObjectProxy::class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class DefaultArgumentRemover implements InvocationInterface
class DefaultArgumentRemoverNoReturnTypes implements InvocationInterface
{

/**
Expand Down
75 changes: 75 additions & 0 deletions classes/DefaultArgumentRemoverReturnTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace phpmock\phpunit;

use phpmock\generator\MockFunctionGenerator;
use PHPUnit\Framework\MockObject\Invocation;
use PHPUnit\Framework\MockObject\Matcher\Invocation as InvocationInterface;

/**
* Removes default arguments from the invocation.
*
* @author Markus Malkusch <[email protected]>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class DefaultArgumentRemoverReturnTypes implements InvocationInterface
{

/**
* @SuppressWarnings(PHPMD)
*/
public function invoked(Invocation $invocation)
{
}

/**
* @SuppressWarnings(PHPMD)
*/
public function matches(Invocation $invocation) : bool
{
if ($invocation instanceof Invocation\StaticInvocation) {
$this->removeDefaultArguments($invocation);
} else {
MockFunctionGenerator::removeDefaultArguments($invocation->parameters);
}

return false;
}

public function verify() : void
{
}

/**
* This method is not defined in the interface, but used in
* PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers().
*
* @return boolean
* @see \PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers()
*/
public function hasMatchers()
{
return false;
}

public function toString() : string
{
return __CLASS__;
}

/**
* Remove default arguments from StaticInvocation or its children (hack)
*
* @SuppressWarnings(PHPMD)
*/
private function removeDefaultArguments(Invocation\StaticInvocation $invocation)
{
$remover = function () {
MockFunctionGenerator::removeDefaultArguments($this->parameters);
};

$remover->bindTo($invocation, Invocation\StaticInvocation::class)();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class MockObjectProxy implements MockObject
class MockObjectProxyNoReturnTypes implements MockObject
{

/**
Expand Down
94 changes: 94 additions & 0 deletions classes/MockObjectProxyReturnTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace phpmock\phpunit;

use PHPUnit\Framework\MockObject\Builder\InvocationMocker as BuilderInvocationMocker;
use PHPUnit\Framework\MockObject\InvocationMocker;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\MockObject;
use phpmock\integration\MockDelegateFunctionBuilder;

/**
* Proxy for PHPUnit's PHPUnit_Framework_MockObject_MockObject.
*
* @author Markus Malkusch <[email protected]>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class MockObjectProxyReturnTypes implements MockObject
{

/**
* @var MockObject $mockObject The mock object.
*/
private $mockObject;

/**
* Inject the subject.
*
* @param MockObject $mockObject The subject.
*/
public function __construct(MockObject $mockObject)
{
$this->mockObject = $mockObject;
}

/**
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_getInvocationMocker() : InvocationMocker
{
// @codingStandardsIgnoreEnd
return $this->mockObject->__phpunit_getInvocationMocker();
}

/**
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_setOriginalObject($originalObject) : void
{
// @codingStandardsIgnoreEnd
$this->mockObject->__phpunit_setOriginalObject($originalObject);
}

/**
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_verify(bool $unsetInvocationMocker = true) : void
{
// @codingStandardsIgnoreEnd
$this->mockObject->__phpunit_verify($unsetInvocationMocker);
}

public function expects(Invocation $matcher) : BuilderInvocationMocker
{
return $this->mockObject->expects($matcher)->method(MockDelegateFunctionBuilder::METHOD);
}

/**
* This method is not part of the contract but was found in
* PHPUnit's mocked_class.tpl.dist.
*
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_hasMatchers() : bool
{
// @codingStandardsIgnoreEnd
return $this->mockObject->__phpunit_hasMatchers();
}

/**
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_setReturnValueGeneration(bool $returnValueGeneration) : void
{
// @codingStandardsIgnoreEnd
$this->mockObject->__phpunit_setReturnValueGeneration($returnValueGeneration);
}
}
23 changes: 15 additions & 8 deletions tests/MockObjectProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public function testExpects()
public function testHasMatcher()
{
$prophecy = $this->prophesize(MockObject::class);
$prophecy->__phpunit_hasMatchers()->willReturn("foo");
$prophecy->__phpunit_hasMatchers()->willReturn(true);
$mock = $prophecy->reveal();

$proxy = new MockObjectProxy($mock);

$result = $proxy->__phpunit_hasMatchers();
$this->assertEquals("foo", $result);
$this->assertTrue($result);
}

/**
Expand All @@ -84,16 +84,23 @@ public function testHasMatcher()
* @test
* @dataProvider provideTestProxiedMethods
*/
public function testProxiedMethods($method, array $arguments = [], $expected = "foo")
public function testProxiedMethods($method, array $arguments = [], $expected = null)
{
$prophecy = $this->prophesize(MockObject::class);
call_user_func_array([$prophecy, $method], $arguments)->willReturn($expected);
if ($expected) {
call_user_func_array([$prophecy, $method], $arguments)->willReturn($expected)->shouldBeCalledTimes(1);
} else {
call_user_func_array([$prophecy, $method], $arguments)->shouldBeCalledTimes(1);
}
$mock = $prophecy->reveal();

$proxy = new MockObjectProxy($mock);

$result = call_user_func_array([$proxy, $method], $arguments);
$this->assertEquals($expected, $result);

if ($expected) {
$this->assertSame($expected, $result);
}
}

/**
Expand All @@ -104,9 +111,9 @@ public function testProxiedMethods($method, array $arguments = [], $expected = "
public function provideTestProxiedMethods()
{
return [
["__phpunit_getInvocationMocker"],
["__phpunit_setOriginalObject", ["bar"]],
["__phpunit_verify", [true]],
['__phpunit_getInvocationMocker', [], new \PHPUnit\Framework\MockObject\InvocationMocker([], true)],
['__phpunit_setOriginalObject', ['bar']],
['__phpunit_verify', [true]],
];
}
}

0 comments on commit d49b8c5

Please sign in to comment.