Skip to content

Commit

Permalink
Added support for PHPUnit 6.5 and 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Mar 22, 2018
1 parent b42fc41 commit 15f769a
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 40 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Expand Up @@ -13,15 +13,22 @@ env:
- PHPUNIT_VERSION=~6.2.0
- PHPUNIT_VERSION=~6.3.0
- PHPUNIT_VERSION=~6.4.0
- PHPUNIT_VERSION=~6.5.0
- PHPUNIT_VERSION=~7.0.0

php:
- 7.2
- 7.1
- 7.0
- 7
- hhvm

matrix:
fast_finish: true
exclude:
- php: 7
env: PHPUNIT_VERSION=dev-master
- php: 7
env: PHPUNIT_VERSION=~7.0.0
allow_failures:
- php: hhvm
- env: PHPUNIT_VERSION=dev-master
Expand Down
42 changes: 42 additions & 0 deletions autoload.php
@@ -0,0 +1,42 @@
<?php

if (! interface_exists(\PHPUnit\Framework\MockObject\Matcher\Invocation::class)) {
class_alias(
\PHPUnit_Framework_MockObject_Matcher_Invocation::class,
\PHPUnit\Framework\MockObject\Matcher\Invocation::class
);
}

if (! interface_exists(\PHPUnit\Framework\MockObject\Invocation::class)) {
class_alias(
\PHPUnit_Framework_MockObject_Invocation::class,
\PHPUnit\Framework\MockObject\Invocation::class
);
}

if (! interface_exists(\PHPUnit\Framework\MockObject\MockObject::class)) {
class_alias(
\PHPUnit_Framework_MockObject_MockObject::class,
\PHPUnit\Framework\MockObject\MockObject::class
);
}

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

if (! class_exists(\PHPUnit\Framework\BaseTestListener::class)) {
include __DIR__ . '/compatibility/BaseTestListener.php';
class_alias(
phpmock\phpunit\MockDisablerPHPUnit7::class,
phpmock\phpunit\MockDisabler::class
);
} else {
class_alias(
phpmock\phpunit\MockDisablerPHPUnit6::class,
phpmock\phpunit\MockDisabler::class
);
}
31 changes: 26 additions & 5 deletions classes/DefaultArgumentRemover.php
Expand Up @@ -3,6 +3,8 @@
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.
Expand All @@ -12,22 +14,27 @@
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class DefaultArgumentRemover implements \PHPUnit_Framework_MockObject_Matcher_Invocation
class DefaultArgumentRemover implements InvocationInterface
{

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

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

return false;
}

Expand All @@ -47,8 +54,22 @@ public function hasMatchers()
return false;
}

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

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

$remover->bindTo($invocation, Invocation\StaticInvocation::class)();
}
}
Expand Up @@ -16,7 +16,7 @@
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class MockDisabler extends BaseTestListener
class MockDisablerPHPUnit6 extends BaseTestListener
{

/**
Expand Down
51 changes: 51 additions & 0 deletions classes/MockDisablerPHPUnit7.php
@@ -0,0 +1,51 @@
<?php

namespace phpmock\phpunit;

use phpmock\Deactivatable;
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;

/**
* Test listener for PHPUnit integration.
*
* This class disables mock functions after a test was run.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
class MockDisablerPHPUnit7 extends BaseTestListener
{

/**
* @var Deactivatable The function mocks.
*/
private $deactivatable;

/**
* Sets the function mocks.
*
* @param Deactivatable $deactivatable The function mocks.
*/
public function __construct(Deactivatable $deactivatable)
{
$this->deactivatable = $deactivatable;
}

/**
* Disables the function mocks.
*
* @param Test $test The test.
* @param int $time The test duration.
*
* @see Mock::disable()
*/
public function endTest(Test $test, float $time) : void
{
parent::endTest($test, $time);

$this->deactivatable->disable();
}
}
5 changes: 3 additions & 2 deletions classes/MockObjectProxy.php
Expand Up @@ -2,7 +2,8 @@

namespace phpmock\phpunit;

use PHPUnit_Framework_MockObject_MockObject as MockObject;
use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit\Framework\MockObject\MockObject;
use phpmock\integration\MockDelegateFunctionBuilder;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public function __phpunit_verify()
return $this->mockObject->__phpunit_verify();
}

public function expects(\PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
public function expects(Invocation $matcher)
{
return $this->mockObject->expects($matcher)->method(MockDelegateFunctionBuilder::METHOD);
}
Expand Down
27 changes: 2 additions & 25 deletions classes/PHPMock.php
Expand Up @@ -5,6 +5,7 @@
use phpmock\integration\MockDelegateFunctionBuilder;
use phpmock\MockBuilder;
use phpmock\Deactivatable;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Adds building a function mock functionality into \PHPUnit\Framework\TestCase.
Expand Down Expand Up @@ -35,30 +36,6 @@
*/
trait PHPMock
{

/**
* Returns a builder object to create mock objects using a fluent interface.
*
* This method exists in \PHPUnit\Framework\TestCase.
*
* @param string $className Name of the class to mock.
* @return \PHPUnit_Framework_MockObject_MockBuilder
* @see \PHPUnit\Framework\TestCase::getMockBuilder()
* @internal
*/
abstract protected function getMockBuilder($className);

/**
* Returns the test result.
*
* This method exists in \PHPUnit\Framework\TestCase.
*
* @return \PHPUnit\Framework\TestResult The test result.
* @see \PHPUnit\Framework\TestCase::getTestResultObject()
* @internal
*/
abstract protected function getTestResultObject();

/**
* Returns the enabled function mock.
*
Expand All @@ -67,7 +44,7 @@ abstract protected function getTestResultObject();
* @param string $namespace The function namespace.
* @param string $name The function name.
*
* @return \PHPUnit_Framework_MockObject_MockObject The PHPUnit mock.
* @return MockObject The PHPUnit mock.
*/
public function getFunctionMock($namespace, $name)
{
Expand Down
13 changes: 13 additions & 0 deletions compatibility/BaseTestListener.php
@@ -0,0 +1,13 @@
<?php

namespace PHPUnit\Framework;

/**
* Compatibility class to work with PHPUnit 7
*
* @internal
*/
abstract class BaseTestListener implements TestListener
{
use TestListenerDefaultImplementation;
}
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -14,11 +14,12 @@
}
],
"autoload": {
"files": ["autoload.php"],
"psr-4": {"phpmock\\phpunit\\": "classes/"}
},
"require": {
"php": ">=7",
"phpunit/phpunit": "^6 <6.5",
"phpunit/phpunit": "^6 || ^7",
"php-mock/php-mock-integration": "^2"
},
"archive": {
Expand Down
12 changes: 7 additions & 5 deletions tests/MockObjectProxyTest.php
Expand Up @@ -2,9 +2,11 @@

namespace phpmock\phpunit;

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

/**
* Tests MockObjectProxyTest.
Expand All @@ -25,13 +27,13 @@ class MockObjectProxyTest extends TestCase
*/
public function testExpects()
{
$matcher = $this->getMockBuilder(\PHPUnit_Framework_MockObject_Matcher_Invocation::class)->getMock();
$matcher = $this->getMockBuilder(Invocation::class)->getMock();

$invocationMocker = $this->getMockBuilder(InvocationMocker::class)->disableOriginalConstructor()->getMock();
$invocationMocker->expects($this->once())->method("method")
->with(MockDelegateFunctionBuilder::METHOD)->willReturn($invocationMocker);

$prophecy = $this->prophesize(\PHPUnit_Framework_MockObject_MockObject::class);
$prophecy = $this->prophesize(MockObject::class);
$prophecy->expects($matcher)->willReturn($invocationMocker);
$mock = $prophecy->reveal();

Expand All @@ -53,7 +55,7 @@ public function testExpects()
*/
public function testHasMatcher()
{
$prophecy = $this->prophesize(\PHPUnit_Framework_MockObject_MockObject::class);
$prophecy = $this->prophesize(MockObject::class);
$prophecy->__phpunit_hasMatchers()->willReturn("foo");
$mock = $prophecy->reveal();

Expand All @@ -74,7 +76,7 @@ public function testHasMatcher()
*/
public function testProxiedMethods($method, array $arguments = [], $expected = "foo")
{
$prophecy = $this->prophesize(\PHPUnit_Framework_MockObject_MockObject::class);
$prophecy = $this->prophesize(MockObject::class);
call_user_func_array([$prophecy, $method], $arguments)->willReturn($expected);
$mock = $prophecy->reveal();

Expand Down

0 comments on commit 15f769a

Please sign in to comment.