Skip to content

Commit

Permalink
feat: Support for PHPUnit 10 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra authored Mar 6, 2023
1 parent b9ba2db commit 97b3278
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 27 deletions.
71 changes: 69 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
strategy:
matrix:
php-version:
- '8.2'
- '8.1'
- '8.0'
- '7.4'
Expand All @@ -21,6 +22,8 @@ jobs:
- '7.1'
- '7.0'
phpunit-version:
- '10.0.0'
- '9.6.0'
- '9.5.0'
- '9.4.0'
- '9.3.0'
Expand All @@ -46,6 +49,52 @@ jobs:
- '6.0.0'

exclude:
# PHP 8.2 Exclusions
- php-version: '8.2'
phpunit-version: '9.4.0'
- php-version: '8.2'
phpunit-version: '9.3.0'
- php-version: '8.2'
phpunit-version: '9.2.0'
- php-version: '8.2'
phpunit-version: '9.1.0'
- php-version: '8.2'
phpunit-version: '9.0.0'
- php-version: '8.2'
phpunit-version: '8.4.0'
- php-version: '8.2'
phpunit-version: '8.3.0'
- php-version: '8.2'
phpunit-version: '8.2.0'
- php-version: '8.2'
phpunit-version: '8.1.0'
- php-version: '8.2'
phpunit-version: '8.0.0'
- php-version: '8.2'
phpunit-version: '7.5.0'
- php-version: '8.2'
phpunit-version: '7.4.0'
- php-version: '8.2'
phpunit-version: '7.3.0'
- php-version: '8.2'
phpunit-version: '7.2.0'
- php-version: '8.2'
phpunit-version: '7.1.0'
- php-version: '8.2'
phpunit-version: '7.0.0'
- php-version: '8.2'
phpunit-version: '6.5.0'
- php-version: '8.2'
phpunit-version: '6.4.0'
- php-version: '8.2'
phpunit-version: '6.3.0'
- php-version: '8.2'
phpunit-version: '6.2.0'
- php-version: '8.2'
phpunit-version: '6.1.0'
- php-version: '8.2'
phpunit-version: '6.0.0'

# PHP 8.1 Exclusions
- php-version: '8.1'
phpunit-version: '9.4.0'
Expand Down Expand Up @@ -93,6 +142,8 @@ jobs:
phpunit-version: '6.0.0'

# PHP 8.0 Exclusions
- php-version: '8.0'
phpunit-version: '10.0.0'
- php-version: '8.0'
phpunit-version: '9.2.0'
- php-version: '8.0'
Expand Down Expand Up @@ -135,6 +186,8 @@ jobs:
phpunit-version: '6.0.0'

# PHP 7.4 Exclusions
- php-version: '7.4'
phpunit-version: '10.0.0'
- php-version: '7.4'
phpunit-version: '8.1.0'
- php-version: '7.4'
Expand All @@ -161,8 +214,16 @@ jobs:
phpunit-version: '6.1.0'
- php-version: '7.4'
phpunit-version: '6.0.0'

# PHP 7.3 Exclusions
- php-version: '7.3'
phpunit-version: '10.0.0'

# PHP 7.2 Exclusions
- php-version: '7.2'
phpunit-version: '10.0.0'
- php-version: '7.2'
phpunit-version: '9.6.0'
- php-version: '7.2'
phpunit-version: '9.5.0'
- php-version: '7.2'
Expand All @@ -177,6 +238,10 @@ jobs:
phpunit-version: '9.0.0'

# PHP 7.1 Exclusions
- php-version: '7.1'
phpunit-version: '10.0.0'
- php-version: '7.1'
phpunit-version: '9.6.0'
- php-version: '7.1'
phpunit-version: '9.5.0'
- php-version: '7.1'
Expand All @@ -203,6 +268,10 @@ jobs:
phpunit-version: '8.0.0'

# PHP 7.0 Exclusions
- php-version: '7.0'
phpunit-version: '10.0.0'
- php-version: '7.0'
phpunit-version: '9.6.0'
- php-version: '7.0'
phpunit-version: '9.5.0'
- php-version: '7.0'
Expand Down Expand Up @@ -244,8 +313,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand Down
20 changes: 17 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ class_alias(
);
}

if (! class_exists(\PHPUnit\Framework\BaseTestListener::class)) {
$hasVersion = class_exists(\PHPUnit\Runner\Version::class);

if ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '10.0.0') >= 0
) {
class_alias(
phpmock\phpunit\MockDisablerPHPUnit10::class,
phpmock\phpunit\MockDisabler::class
);
} elseif (! class_exists(\PHPUnit\Framework\BaseTestListener::class)) {
include __DIR__ . '/compatibility/BaseTestListener.php';
class_alias(
phpmock\phpunit\MockDisablerPHPUnit7::class,
Expand All @@ -80,12 +89,17 @@ class_alias(
);
}

if (class_exists(\PHPUnit\Runner\Version::class)
if ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '10.0.0') >= 0
) {
class_alias(\phpmock\phpunit\DefaultArgumentRemoverReturnTypes100::class, \phpmock\phpunit\DefaultArgumentRemover::class);
class_alias(\phpmock\phpunit\MockObjectProxyReturnTypes100::class, \phpmock\phpunit\MockObjectProxy::class);
} elseif ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '8.4.0') >= 0
) {
class_alias(\phpmock\phpunit\DefaultArgumentRemoverReturnTypes84::class, \phpmock\phpunit\DefaultArgumentRemover::class);
class_alias(\phpmock\phpunit\MockObjectProxyReturnTypes84::class, \phpmock\phpunit\MockObjectProxy::class);
} elseif (class_exists(\PHPUnit\Runner\Version::class)
} elseif ($hasVersion
&& version_compare(\PHPUnit\Runner\Version::id(), '8.1.0') >= 0
) {
class_alias(\phpmock\phpunit\DefaultArgumentRemoverReturnTypes::class, \phpmock\phpunit\DefaultArgumentRemover::class);
Expand Down
81 changes: 81 additions & 0 deletions classes/DefaultArgumentRemoverReturnTypes100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace phpmock\phpunit;

use phpmock\generator\MockFunctionGenerator;
use PHPUnit\Framework\MockObject\Invocation;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;

/**
* 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 DefaultArgumentRemoverReturnTypes100 extends InvocationOrder
{
/**
* @SuppressWarnings(PHPMD)
*/
public function invokedDo(Invocation $invocation): void
{
}

/**
* @SuppressWarnings(PHPMD)
*/
public function matches(Invocation $invocation) : bool
{
$iClass = class_exists(Invocation::class);

if ($iClass
|| $invocation instanceof Invocation\StaticInvocation
) {
$this->removeDefaultArguments(
$invocation,
$iClass ? Invocation::class : Invocation\StaticInvocation::class
);
} 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 $invocation, string $class)
{
$remover = function () {
MockFunctionGenerator::removeDefaultArguments($this->parameters);
};

$remover->bindTo($invocation, $class)();
}
}
48 changes: 48 additions & 0 deletions classes/MockDisablerPHPUnit10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace phpmock\phpunit;

use phpmock\Deactivatable;
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\FinishedSubscriber;

/**
* 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 MockDisablerPHPUnit10 implements FinishedSubscriber
{
/**
* @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;
}

/**
* @SuppressWarnings(PHPMD)
*/
public function notify(Finished $event) : void
{
$this->deactivatable->disable();
}

public function endTest(): void
{
$this->deactivatable->disable();
}
}
92 changes: 92 additions & 0 deletions classes/MockObjectProxyReturnTypes100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace phpmock\phpunit;

use PHPUnit\Framework\MockObject\Builder\InvocationMocker as BuilderInvocationMocker;
use PHPUnit\Framework\MockObject\InvocationHandler;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
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 MockObjectProxyReturnTypes100 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_getInvocationHandler(): InvocationHandler
{
return $this->mockObject->__phpunit_getInvocationHandler();
}

/**
* @SuppressWarnings(PHPMD)
*/
// @codingStandardsIgnoreStart
public function __phpunit_setOriginalObject(object $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(InvocationOrder $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);
}
}
Loading

0 comments on commit 97b3278

Please sign in to comment.