Skip to content

Added support for PHPUnit 6.5 and 7.0 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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)
{
$remover = function () {
MockFunctionGenerator::removeDefaultArguments($this->parameters);
};

$remover->bindTo($invocation, Invocation\StaticInvocation::class)();
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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