Skip to content
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

PHPUnit 7 Compatibility #263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require-dev": {
"ext-soap": "*",
"doctrine/common": "2.3.*",
"phpunit/phpunit": "^6.0",
"phpunit/phpunit": "^7.0",
"hamcrest/hamcrest-php": "1.1.*",
"codeclimate/php-test-reporter": "dev-master"
},
Expand Down
9 changes: 8 additions & 1 deletion src/Phake.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Phake
const CLIENT_DEFAULT = 'DEFAULT';
const CLIENT_PHPUNIT = 'PHPUNIT';
const CLIENT_PHPUNIT6 = 'PHPUNIT6';
const CLIENT_PHPUNIT7 = 'PHPUNIT7';

/**
* Returns a new mock object based on the given class name.
Expand Down Expand Up @@ -571,7 +572,11 @@ public static function getClient()
if (class_exists('PHPUnit_Framework_TestCase')) {
return self::$client = new Phake_Client_PHPUnit();
} else if (class_exists('PHPUnit\Framework\TestCase')) {
return self::$client = new Phake_Client_PHPUnit6();
if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '>=')) {
return self::$client = new Phake_Client_PHPUnit7();
} else {
return self::$client = new Phake_Client_PHPUnit6();
}
}
return self::$client = new Phake_Client_Default();
} else {
Expand All @@ -594,6 +599,8 @@ public static function setClient($client)
self::$client = new Phake_Client_PHPUnit();
} elseif ($client == self::CLIENT_PHPUNIT6) {
self::$client = new Phake_Client_PHPUnit6();
} elseif ($client == self::CLIENT_PHPUNIT7) {
self::$client = new Phake_Client_PHPUnit7();
}
else {
self::$client = new Phake_Client_Default();
Expand Down
72 changes: 72 additions & 0 deletions src/Phake/Client/PHPUnit7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* Phake - Mocking Framework
*
* Copyright (c) 2010, Mike Lively <mike.lively@sellingsource.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Lively nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Testing
* @package Phake
* @author Mike Lively <m@digitalsandwich.com>
* @copyright 2010 Mike Lively <m@digitalsandwich.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.digitalsandwich.com/
*/

use PHPUnit\Framework\Assert;

/**
* The client adapter used for PHPUnit.
*
* This adapter allows PHPUnit to report failed verify() calls as test failures instead of errors. It also counts
* verify() calls as assertions.
*/
class Phake_Client_PHPUnit7 implements Phake_Client_IClient
{
public function processVerifierResult(\Phake_CallRecorder_VerifierResult $result)
{
Assert::assertThat($result, $this->getConstraint());

return $result->getMatchedCalls();
}

public function processObjectFreeze()
{
Assert::assertThat(true, Assert::isTrue());
}

private function getConstraint()
{
return new Phake_PHPUnit_VerifierResultConstraintV7();
}
}
6 changes: 5 additions & 1 deletion src/Phake/Matchers/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public function createMatcher($argument, Phake_Matchers_IChainableArgumentMatche
} elseif ($argument instanceof PHPUnit_Framework_Constraint) {
$return = new Phake_Matchers_PHPUnitConstraintAdapter($argument);
} elseif ($argument instanceof \PHPUnit\Framework\Constraint\Constraint) {
$return = new Phake_Matchers_PHPUnit6ConstraintAdapter($argument);
if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '>=')) {
$return = new Phake_Matchers_PHPUnit7ConstraintAdapter($argument);
} else {
$return = new Phake_Matchers_PHPUnit6ConstraintAdapter($argument);
}
} elseif ($argument instanceof Hamcrest\Matcher) {
$return = new Phake_Matchers_HamcrestMatcherAdapter($argument);
} elseif ($argument instanceof Phake_Matchers_IArgumentMatcher) {
Expand Down
95 changes: 95 additions & 0 deletions src/Phake/Matchers/PHPUnit7ConstraintAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* Phake - Mocking Framework
*
* Copyright (c) 2010-2012, Mike Lively <m@digitalsandwich.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Lively nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Testing
* @package Phake
* @author Mike Lively <m@digitalsandwich.com>
* @copyright 2010 Mike Lively <m@digitalsandwich.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.digitalsandwich.com/
*/

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;

/**
* An adapter class allowing PHPUnit constraints to be treated as though they were Phake argument
* matchers.
*/
class Phake_Matchers_PHPUnit7ConstraintAdapter extends Phake_Matchers_SingleArgumentMatcher
{
/**
* @var \PHPUnit\Framework\Constraint\Constraint
*/
private $constraint;

/**
* @param Constraint $constraint
*/
public function __construct(Constraint $constraint)
{
$this->constraint = $constraint;
}

/**
* Executes the matcher on a given argument value.
*
* Forwards the call to PHPUnit's evaluate() method.
*
* @param mixed $argument
* @throws Phake_Exception_MethodMatcherException
*/
protected function matches(&$argument)
{
try {
$this->constraint->evaluate($argument, '');
} catch (ExpectationFailedException $e) {
$failure = $e->getComparisonFailure();
if ($failure instanceof \SebastianBergmann\Comparator\ComparisonFailure) {
$failure = $failure->getDiff();
} else {
$failure = '';
}
throw new Phake_Exception_MethodMatcherException($e->getMessage() . "\n" . $failure, $e);
}
}

public function __toString()
{
return $this->constraint->toString();
}
}
75 changes: 75 additions & 0 deletions src/Phake/PHPUnit/VerifierResultConstraintV7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* Phake - Mocking Framework
*
* Copyright (c) 2010, Mike Lively <mike.lively@sellingsource.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Lively nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Testing
* @package Phake
* @author Mike Lively <m@digitalsandwich.com>
* @copyright 2010 Mike Lively <m@digitalsandwich.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.digitalsandwich.com/
*/

use PHPUnit\Framework\Constraint\Constraint;

/**
* A PHPUnit constraint that wraps a phake verifier to allow assertions on expected calls.
*/
class Phake_PHPUnit_VerifierResultConstraintV7 extends Constraint
{
protected function matches($other): bool
{
if (!$other instanceof Phake_CallRecorder_VerifierResult) {
throw new InvalidArgumentException("You must pass an instance of Phake_CallRecorder_VerifierResult");
}
return $other->getVerified();
}

public function toString(): string
{
return 'is called';
}

protected function failureDescription($other): string
{
if (!$other instanceof Phake_CallRecorder_VerifierResult) {
throw new InvalidArgumentException("You must pass an instance of Phake_CallRecorder_VerifierResult");
}

return $other->getFailureDescription();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Phake_ClassGenerator_InvocationHandler_FrozenObjectCheckTest extends TestC

public function setUp()
{
Phake::setClient(Phake::CLIENT_PHPUNIT6);
Phake::setClient(Phake::CLIENT_PHPUNIT7);
Phake::initAnnotations($this);
$this->handler = new Phake_ClassGenerator_InvocationHandler_FrozenObjectCheck($this->mockInfo);
}
Expand Down