From a5561182bbdcb99b2dc85e41164b489ab939a510 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 23 Oct 2020 13:42:25 +0200 Subject: [PATCH 01/22] build(.travis.yml) add more tested PHP versions --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 19f52fc..dbdd17c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,8 @@ php: - '7.0' - '7.1' - '7.2' + - '7.3' + - '7.4' - nightly script: From ba7b7aa6c5b93140dac68df17e9b8393beb9648f Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 1 Dec 2020 14:01:36 +0200 Subject: [PATCH 02/22] Files on Windows can have different line separators, especially under git. --- src/tad/FunctionMocker/Template/MethodCode.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tad/FunctionMocker/Template/MethodCode.php b/src/tad/FunctionMocker/Template/MethodCode.php index 79db7e5..1e9837d 100644 --- a/src/tad/FunctionMocker/Template/MethodCode.php +++ b/src/tad/FunctionMocker/Template/MethodCode.php @@ -77,11 +77,14 @@ protected function getMethodCode($methodName) $contents = $this->contents; } + $contents = str_replace( "\r\n", "\n", $contents ); + $contents = str_replace( "\r", "\n", $contents ); + $startLine = $method->getStartLine(); $endLine = $method->getEndLine(); $classAliases = []; - $lines = explode(PHP_EOL, $contents); + $lines = explode("\n", $contents); foreach ($lines as $line) { $frags = explode(' ', $line); if (!empty($frags) && $frags[0] == 'use') { From d6eb0c32a52eddf1f4b6a0ed49c0d8da3ad3f633 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 1 Dec 2020 14:10:41 +0200 Subject: [PATCH 03/22] Fix DIRECTORY_SEPARATOR in it_should_return_properly_filtered_paths_arrays test. --- tests/tad/FunctionMocker/UtilsTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/tad/FunctionMocker/UtilsTest.php b/tests/tad/FunctionMocker/UtilsTest.php index dd35918..08171a7 100644 --- a/tests/tad/FunctionMocker/UtilsTest.php +++ b/tests/tad/FunctionMocker/UtilsTest.php @@ -35,6 +35,9 @@ public function pathArrays() { * @dataProvider pathArrays */ public function it_should_return_properly_filtered_paths_arrays( $in, $out ) { + $in = str_replace( '/', DIRECTORY_SEPARATOR, $in ); + $out = str_replace( '/', DIRECTORY_SEPARATOR, $out ); + $this->assertEquals( $out, Utils::filterPathListFrom( $in, $this->rootDir ) ); } } From c1c1d918a4b6bfffe176a4fd8a724088deb0e6d5 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 2 Dec 2020 14:40:35 +0200 Subject: [PATCH 04/22] Replace '/' with DIRECTORY_SEPARATOR to make replace() work properly on Windows. Otherwise, dispatchDynamic() in patchwork cannot locate non-existing functions and replace() returns null instead of expected value. --- src/tad/FunctionMocker/FunctionMocker.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/tad/FunctionMocker/FunctionMocker.php b/src/tad/FunctionMocker/FunctionMocker.php index 6afb37d..2d5975d 100644 --- a/src/tad/FunctionMocker/FunctionMocker.php +++ b/src/tad/FunctionMocker/FunctionMocker.php @@ -435,6 +435,26 @@ public static function getPatchworkConfiguration($options = [], $destinationFold if (empty($options['cache-path'])) { $options['cache-path'] = $destinationFolder . DIRECTORY_SEPARATOR . 'cache'; } + + $options = self::makePathsUniform( $options ); + + return $options; + } + + /** + * Replace '/' in paths by DIRECTORY_SEPARATOR to work properly on Windows. + * + * @param array $options An array of options as those supported by Patchwork configuration. + * + * @return array + */ + private static function makePathsUniform( $options ) { + $pathArrays = [ 'whitelist', 'blacklist', 'cache-path' ]; + + foreach ( $pathArrays as $pathArray ) { + $options[$pathArray] = str_replace( '/', DIRECTORY_SEPARATOR, $options[$pathArray] ); + } + return $options; } } From dac927d17bed133977093c2399262601d428c45d Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 2 Dec 2020 15:17:49 +0200 Subject: [PATCH 05/22] Revert "build(.travis.yml) add more tested PHP versions" This reverts commit a5561182 --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index dbdd17c..19f52fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,6 @@ php: - '7.0' - '7.1' - '7.2' - - '7.3' - - '7.4' - nightly script: From 0383729be9af2f1dba5e553b29c899f10aba8932 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 2 Dec 2020 15:38:06 +0200 Subject: [PATCH 06/22] Run tests on php 7.3 and 7.4. We cannot run tests on 8.0 so far, as phpunit8+ require that "setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void". All tests should be edited accordingly. --- .travis.yml | 14 +++++++++----- phpunit.xml | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 19f52fc..a04d3eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,15 @@ php: - '7.0' - '7.1' - '7.2' - - nightly + - '7.3' + - '7.4' script: # test with PHPUnit ~5.7 - - composer require --dev phpunit/phpunit:~5.7 - - vendor/bin/phpunit - # test with PHPUnit ~6.0 - - if [[ ${TRAVIS_PHP_VERSION:0:1} != "5" ]]; then rm -rf vendor composer.lock && composer require --dev phpunit/phpunit:~6.0 && vendor/bin/phpunit; fi + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "5.6" || ${TRAVIS_PHP_VERSION:0:3} = "7.0" || ${TRAVIS_PHP_VERSION:0:3} = "7.1" ]]; then composer require --dev phpunit/phpunit:~5.7 && vendor/bin/phpunit; fi + # test with PHPUnit ~6.5 + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.2" ]]; then composer require --dev phpunit/phpunit:~6.5 && vendor/bin/phpunit; fi + # test with PHPUnit ~7.5 + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.3" || ${TRAVIS_PHP_VERSION:0:3} = "7.4" ]]; then composer require --dev phpunit/phpunit:~7.5 && vendor/bin/phpunit; fi + # test with PHPUnit ~9.0 + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "8.0" ]]; then composer require --dev phpunit/phpunit:~9.4 && vendor/bin/phpunit; fi diff --git a/phpunit.xml b/phpunit.xml index b978aec..b5019cc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,7 +8,7 @@ beStrictAboutTodoAnnotatedTests="true" colors="true" verbose="true"> - + tests From 59a724aeb7331689507150ed38886ac182f7fcb7 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 2 Dec 2020 22:13:43 +0200 Subject: [PATCH 07/22] Make code and tests running on php7.2 - 8.0. No backward compatibility with php 5.6 - 7.1. Should be tagged as the next version of function-mocker: 2.0.0. --- .gitignore | 1 + .travis.yml | 16 +++----- composer.json | 1 - phpunit.xml | 10 +---- src/shims.php | 20 ---------- .../Call/CallHandlerInterface.php | 6 +-- .../Call/Verifier/AbstractVerifier.php | 8 ++-- .../Verifier/InstanceMethodCallVerifier.php | 39 ++++++++++++------- src/tad/FunctionMocker/Forge/Step.php | 9 +++-- src/tad/FunctionMocker/FunctionMocker.php | 2 +- src/tad/FunctionMocker/MockWrapper.php | 33 +++++++++------- .../Replacers/InstanceForger.php | 13 ++++++- .../FunctionMocker/Template/ClassTemplate.php | 4 +- .../Template/Extender/SpyExtender.php | 2 +- .../Template/VerifyingClassTemplate.php | 6 +-- .../AbstractClassMockingTest.php | 4 +- .../tad/FunctionMocker/AliasedClassesTest.php | 4 +- .../FunctionMocker/AssertionWrappingTest.php | 4 +- tests/tad/FunctionMocker/BatchReplaceTest.php | 4 +- tests/tad/FunctionMocker/Forge/StepTest.php | 2 +- tests/tad/FunctionMocker/ForgeTest.php | 4 +- .../FunctionArgsConstraintsCheckTest.php | 4 +- .../FunctionReplacementInOrderTest.php | 4 +- .../FunctionReplacementTest.php | 4 +- .../FunctionMocker/GlobalReplacementTest.php | 4 +- .../tad/FunctionMocker/InstanceMethodTest.php | 8 ++-- .../FunctionMocker/InterfaceMockingTest.php | 4 +- .../PassAndCallOriginalTest.php | 4 +- .../PatchworkConfigurationTest.php | 12 ++++-- .../StaticMethodConstraintsCheckTest.php | 4 +- tests/tad/FunctionMocker/StaticMethodTest.php | 4 +- tests/tad/FunctionMocker/TraitMockingTest.php | 4 +- tests/tad/FunctionMocker/UtilsTest.php | 2 +- 33 files changed, 125 insertions(+), 125 deletions(-) delete mode 100644 src/shims.php diff --git a/.gitignore b/.gitignore index edccfa0..4471f33 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ composer.lock patchwork.json cache/* pw-cs-*.yml +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index a04d3eb..b6dfa60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,13 @@ language: php php: - - '5.6' - - '7.0' - - '7.1' - '7.2' - '7.3' - '7.4' + # - '8.0' is not available so far on Travis script: - # test with PHPUnit ~5.7 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "5.6" || ${TRAVIS_PHP_VERSION:0:3} = "7.0" || ${TRAVIS_PHP_VERSION:0:3} = "7.1" ]]; then composer require --dev phpunit/phpunit:~5.7 && vendor/bin/phpunit; fi - # test with PHPUnit ~6.5 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.2" ]]; then composer require --dev phpunit/phpunit:~6.5 && vendor/bin/phpunit; fi - # test with PHPUnit ~7.5 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.3" || ${TRAVIS_PHP_VERSION:0:3} = "7.4" ]]; then composer require --dev phpunit/phpunit:~7.5 && vendor/bin/phpunit; fi - # test with PHPUnit ~9.0 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "8.0" ]]; then composer require --dev phpunit/phpunit:~9.4 && vendor/bin/phpunit; fi + # test with PHPUnit ~8.5 + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.2" ]]; then composer require --dev phpunit/phpunit:~8.5 && vendor/bin/phpunit; fi + # test with PHPUnit ~9.4 + - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.3" || ${TRAVIS_PHP_VERSION:0:3} = "7.4" || ${TRAVIS_PHP_VERSION:0:3} = "8.0" ]]; then composer require --dev phpunit/phpunit:~9.4 && vendor/bin/phpunit; fi diff --git a/composer.json b/composer.json index 87cfef8..05a7701 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ "tad\\FunctionMocker": "src" }, "files": [ - "src/shims.php", "src/functions.php" ] } diff --git a/phpunit.xml b/phpunit.xml index b5019cc..556456a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,8 +1,8 @@ tests - - - - src - - diff --git a/src/shims.php b/src/shims.php deleted file mode 100644 index 2c8ded6..0000000 --- a/src/shims.php +++ /dev/null @@ -1,20 +0,0 @@ -=' ) ) ) { - return; -} - -/** - * Load PHPUnit 6.0+ Aliases - */ -$aliases = [ - 'PHPUnit\Framework\MockObject\MockObject' => 'PHPUnit_Framework_MockObject_MockObject', - 'PHPUnit\Framework\MockObject\Matcher\InvokedRecorder' => 'PHPUnit_Framework_MockObject_Matcher_InvokedRecorder', - 'PHPUnit\Framework\MockObject\Invocation\ObjectInvocation' => 'PHPUnit_Framework_MockObject_Invocation_Object', -]; - -foreach ( $aliases as $original => $alias ) { - if ( ! class_exists( $original ) || class_exists( $alias ) ) { - continue; - } - class_alias( $original, $alias ); -} diff --git a/src/tad/FunctionMocker/Call/CallHandlerInterface.php b/src/tad/FunctionMocker/Call/CallHandlerInterface.php index 36d43ce..e2309ff 100644 --- a/src/tad/FunctionMocker/Call/CallHandlerInterface.php +++ b/src/tad/FunctionMocker/Call/CallHandlerInterface.php @@ -3,18 +3,18 @@ namespace tad\FunctionMocker\Call; -use PHPUnit_Framework_MockObject_Matcher_InvokedRecorder; +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use tad\FunctionMocker\ReplacementRequest; interface CallHandlerInterface { /** - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder + * @param InvocationOrder $invokedRecorder * * @return mixed */ - public function setInvokedRecorder(PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder); + public function setInvokedRecorder(InvocationOrder $invokedRecorder); /** * @param ReplacementRequest $request diff --git a/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php b/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php index bb30c93..cf36d84 100644 --- a/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php +++ b/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php @@ -2,7 +2,7 @@ namespace tad\FunctionMocker\Call\Verifier; -use PHPUnit_Framework_MockObject_Matcher_InvokedRecorder; +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use tad\FunctionMocker\Call\CallHandlerInterface; use tad\FunctionMocker\MatchingStrategy\MatchingStrategyFactory; use tad\FunctionMocker\ReplacementRequest; @@ -20,7 +20,7 @@ abstract class AbstractVerifier implements VerifierInterface, CallHandlerInterfa protected $constraintClass; /** - * @var PHPUnit_Framework_MockObject_Matcher_InvokedRecorder + * @var InvocationOrder */ protected $invokedRecorder; @@ -70,11 +70,11 @@ public function wasCalledWithOnce(array $args) } /** - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder + * @param InvocationOrder $invokedRecorder * * @return mixed */ - public function setInvokedRecorder(PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder) + public function setInvokedRecorder(InvocationOrder $invokedRecorder) { $this->invokedRecorder = $invokedRecorder; } diff --git a/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php b/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php index 6743666..70a1489 100644 --- a/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php +++ b/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php @@ -3,6 +3,8 @@ namespace tad\FunctionMocker\Call\Verifier; +use PHPUnit\Framework\MockObject\Invocation; +use ReflectionClass; use tad\FunctionMocker\Call\Logger\LoggerInterface; use tad\FunctionMocker\ReturnValue; @@ -37,15 +39,14 @@ private function realWasCalledTimes( $times ) { /** * @param array $args - * * @param string $methodName * * @return array */ protected function getCallTimesWithArgs( $methodName, array $args = null ) { - $invocations = $this->invokedRecorder->getInvocations(); + $invocations = $this->getInvocations(); $callTimes = 0; - array_map( function ( \PHPUnit_Framework_MockObject_Invocation_Object $invocation ) use ( &$callTimes, $args, $methodName ) { + array_map( function ( Invocation $invocation ) use ( &$callTimes, $args, $methodName ) { if ( is_array( $args ) ) { $callTimes += $this->compareName( $invocation, $methodName ) && $this->compareArgs( $invocation, $args ); } else { @@ -57,29 +58,39 @@ protected function getCallTimesWithArgs( $methodName, array $args = null ) { } /** - * @param \PHPUnit_Framework_MockObject_Invocation_Object $invocation + * @return array + * @throws \ReflectionException + */ + private function getInvocations() { + $reflectionClass = new ReflectionClass( get_class( $this->invokedRecorder ) ); + $parentReflectionClass = $reflectionClass->getParentClass(); + + $invocationsProperty = $parentReflectionClass->getProperty('invocations'); + $invocationsProperty->setAccessible( true ); + + return $invocationsProperty->getValue( $this->invokedRecorder ); + } + + /** + * @param Invocation $invocation * @param $methodName * * @return bool */ - private function compareName( \PHPUnit_Framework_MockObject_Invocation_Object $invocation, $methodName ) { - $invokedMethodName = method_exists( $invocation, 'getMethodName' ) - ? $invocation->getMethodName() - : $invocation->methodName; + private function compareName( Invocation $invocation, $methodName ) { + $invokedMethodName = $invocation->getMethodName(); return $invokedMethodName === $methodName; } /** - * @param \PHPUnit_Framework_MockObject_Invocation_Object $invocation - * @param $args + * @param Invocation $invocation + * @param $args * * @return bool|mixed|void */ - private function compareArgs( \PHPUnit_Framework_MockObject_Invocation_Object $invocation, $args ) { - $parameters = method_exists( $invocation, 'getParameters' ) - ? $invocation->getParameters() - : $invocation->parameters; + private function compareArgs( Invocation $invocation, $args ) { + $parameters = $invocation->getParameters(); if ( count( $args ) > count( $parameters ) ) { return false; diff --git a/src/tad/FunctionMocker/Forge/Step.php b/src/tad/FunctionMocker/Forge/Step.php index cb654b7..acf05fd 100644 --- a/src/tad/FunctionMocker/Forge/Step.php +++ b/src/tad/FunctionMocker/Forge/Step.php @@ -2,6 +2,7 @@ namespace tad\FunctionMocker\Forge; +use PHPUnit\Framework\MockObject\MockObject; use tad\FunctionMocker\ReplacementRequest; use tad\FunctionMocker\Replacers\InstanceForger; use tad\FunctionMocker\ReturnValue; @@ -63,13 +64,15 @@ protected function setUpMockObject() } } - /** - * @return \PHPUnit_Framework_MockObject_MockObject - */ + /** + * @return MockObject + * @throws \ReflectionException + */ protected function getForgedMockObject() { $methods = array_merge(array_keys($this->methods), ['__construct']); $mockObject = $this->instanceForger->getPHPUnitMockObjectFor($this->class, $methods); + return $mockObject; } diff --git a/src/tad/FunctionMocker/FunctionMocker.php b/src/tad/FunctionMocker/FunctionMocker.php index 2d5975d..1cd3ac1 100644 --- a/src/tad/FunctionMocker/FunctionMocker.php +++ b/src/tad/FunctionMocker/FunctionMocker.php @@ -411,7 +411,7 @@ public static function writePatchworkConfig(array $options = null, $destinationF * * @return array */ - public static function getPatchworkConfiguration($options = [], $destinationFolder) { + public static function getPatchworkConfiguration($options = [], $destinationFolder = '') { $translatedFields = ['include' => 'whitelist', 'exclude' => 'blacklist']; foreach ($translatedFields as $from => $to) { diff --git a/src/tad/FunctionMocker/MockWrapper.php b/src/tad/FunctionMocker/MockWrapper.php index de4c851..074188e 100644 --- a/src/tad/FunctionMocker/MockWrapper.php +++ b/src/tad/FunctionMocker/MockWrapper.php @@ -2,6 +2,8 @@ namespace tad\FunctionMocker; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use tad\FunctionMocker\Template\ClassTemplate; use tad\FunctionMocker\Template\ClassTemplateInterface; use tad\FunctionMocker\Template\Extender\ExtenderInterface; @@ -40,30 +42,31 @@ public function getWrappedObject() } /** - * @param \PHPUnit_Framework_MockObject_MockObject|MockObject $mockObject - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder + * @param MockObject $mockObject + * @param InvocationOrder $invokedRecorder * @param ReplacementRequest $request + * * @return mixed */ - public function wrap(\PHPUnit_Framework_MockObject_MockObject $mockObject, \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder, ReplacementRequest $request) + public function wrap(MockObject $mockObject, InvocationOrder $invokedRecorder, ReplacementRequest $request) { $extender = new SpyExtender(); return $this->getWrappedInstance($mockObject, $extender, $invokedRecorder, $request); } - /** - * @param \PHPUnit_Framework_MockObject_MockObject $object - * @param $extender - * - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder - * @param ReplacementRequest $request - * - * @throws \Exception - * - * @return mixed - */ - protected function getWrappedInstance(\PHPUnit_Framework_MockObject_MockObject $object, ExtenderInterface $extender, \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder = null, ReplacementRequest $request = null) + /** + * @param MockObject $object + * @param ExtenderInterface $extender + * + * @param InvocationOrder $invokedRecorder + * @param ReplacementRequest $request + * + * @throws \Exception + * + * @return mixed + */ + protected function getWrappedInstance(MockObject $object, ExtenderInterface $extender, InvocationOrder $invokedRecorder = null, ReplacementRequest $request = null) { $mockClassName = get_class($object); $extendClassName = sprintf('%s_%s', uniqid('Extended_'), $mockClassName); diff --git a/src/tad/FunctionMocker/Replacers/InstanceForger.php b/src/tad/FunctionMocker/Replacers/InstanceForger.php index 1de6879..78a24d6 100644 --- a/src/tad/FunctionMocker/Replacers/InstanceForger.php +++ b/src/tad/FunctionMocker/Replacers/InstanceForger.php @@ -3,6 +3,8 @@ namespace tad\FunctionMocker\Replacers; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use tad\FunctionMocker\MockWrapper; use tad\FunctionMocker\ReplacementRequest; use tad\FunctionMocker\ReturnValue; @@ -15,7 +17,7 @@ class InstanceForger { /** - * @var \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder + * @var InvocationOrder */ protected $invokedRecorder; @@ -44,6 +46,13 @@ public function getMock(ReplacementRequest $request, ReturnValue $returnValue) return $wrapperInstance; } + /** + * @param $className + * @param array $methods + * + * @return MockObject|\PHPUnit_Framework_MockObject_MockObject + * @throws \ReflectionException + */ public function getPHPUnitMockObjectFor($className, array $methods) { $rc = new \ReflectionClass($className); @@ -77,7 +86,7 @@ public function getPHPUnitMockObjectFor($className, array $methods) } /** - * @param \PHPUnit_Framework_MockObject_MockObject $mockObject + * @param \PHPUnit\Framework\MockObject\MockObject $mockObject * @param $methodName * @param ReturnValue|null $returnValue */ diff --git a/src/tad/FunctionMocker/Template/ClassTemplate.php b/src/tad/FunctionMocker/Template/ClassTemplate.php index fe653ec..32d0145 100644 --- a/src/tad/FunctionMocker/Template/ClassTemplate.php +++ b/src/tad/FunctionMocker/Template/ClassTemplate.php @@ -22,11 +22,11 @@ public function __get_functionMocker_CallHandler(){ return \$this->__functionMocker_callHandler; } - public function __set_functionMocker_originalMockObject(\PHPUnit_Framework_MockObject_MockObject \$mockObject){ + public function __set_functionMocker_originalMockObject(\PHPUnit\Framework\MockObject\MockObject \$mockObject){ \$this->__functionMocker_originalMockObject = \$mockObject; } - public function __set_functionMocker_invokedRecorder(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder \$invokedRecorder){ + public function __set_functionMocker_invokedRecorder(\PHPUnit\Framework\MockObject\Rule\InvocationOrder \$invokedRecorder){ \$this->__functionMocker_invokedRecorder = \$invokedRecorder; } diff --git a/src/tad/FunctionMocker/Template/Extender/SpyExtender.php b/src/tad/FunctionMocker/Template/Extender/SpyExtender.php index 75b944a..3210918 100644 --- a/src/tad/FunctionMocker/Template/Extender/SpyExtender.php +++ b/src/tad/FunctionMocker/Template/Extender/SpyExtender.php @@ -13,7 +13,7 @@ public function getExtendedMethodCallsAndNames() { return array( 'wasCalledTimes($times)' => 'wasCalledTimes', - 'wasCalledWithTimes(array $args = array(), $times)' => 'wasCalledWithTimes', + 'wasCalledWithTimes(array $args = array(), $times = 0)' => 'wasCalledWithTimes', 'wasNotCalled()' => 'wasNotCalled', 'wasNotCalledWith(array $args = array())' => 'wasNotCalledWith', 'wasCalledWithOnce(array $args = array())' => 'wasCalledWithOnce', diff --git a/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php b/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php index 1c84ffb..8dfa246 100644 --- a/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php +++ b/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php @@ -24,11 +24,11 @@ public function __get_functionMocker_CallHandler(){ return \$this->__functionMocker_callHandler; } - public function __set_functionMocker_originalMockObject(\PHPUnit_Framework_MockObject_MockObject \$mockObject){ + public function __set_functionMocker_originalMockObject(\PHPUnit\Framework\MockObject\MockObject \$mockObject){ \$this->__functionMocker_originalMockObject = \$mockObject; } - public function __set_functionMocker_invokedRecorder(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder \$invokedRecorder){ + public function __set_functionMocker_invokedRecorder(\PHPUnit\Framework\MockObject\Rule\InvocationOrder \$invokedRecorder){ \$this->__functionMocker_invokedRecorder = \$invokedRecorder; } @@ -89,7 +89,7 @@ public function wasCalledTimes(\$times){ protected function wasCalledWithTimesTemplate() { return <<< CODESET - public function wasCalledWithTimes(array \$args = array(), \$times){ + public function wasCalledWithTimes(array \$args = array(), \$times = 0){ \$args = [ \$args, \$times, diff --git a/tests/tad/FunctionMocker/AbstractClassMockingTest.php b/tests/tad/FunctionMocker/AbstractClassMockingTest.php index c3bb3a9..53be8d1 100644 --- a/tests/tad/FunctionMocker/AbstractClassMockingTest.php +++ b/tests/tad/FunctionMocker/AbstractClassMockingTest.php @@ -9,13 +9,13 @@ class AbstractClassMockingTest extends \PHPUnit\Framework\TestCase protected $ns; - public function setUp() { + public function setUp(): void { Test::setUp(); $this->ns = __NAMESPACE__; } - public function tearDown() { + public function tearDown(): void { Test::tearDown(); } diff --git a/tests/tad/FunctionMocker/AliasedClassesTest.php b/tests/tad/FunctionMocker/AliasedClassesTest.php index ee1982a..cffa782 100644 --- a/tests/tad/FunctionMocker/AliasedClassesTest.php +++ b/tests/tad/FunctionMocker/AliasedClassesTest.php @@ -7,11 +7,11 @@ class AliasedClassesTest extends \PHPUnit\Framework\TestCase { - public function setUp() { + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() { + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/AssertionWrappingTest.php b/tests/tad/FunctionMocker/AssertionWrappingTest.php index ce5e301..5f2f93c 100644 --- a/tests/tad/FunctionMocker/AssertionWrappingTest.php +++ b/tests/tad/FunctionMocker/AssertionWrappingTest.php @@ -7,12 +7,12 @@ class AssertionWrappingTest extends \PHPUnit\Framework\TestCase { - public function setUp() + public function setUp(): void { Test::setUp(); } - public function tearDown() + public function tearDown(): void { Test::tearDown(); } diff --git a/tests/tad/FunctionMocker/BatchReplaceTest.php b/tests/tad/FunctionMocker/BatchReplaceTest.php index b5f150c..c1602ab 100644 --- a/tests/tad/FunctionMocker/BatchReplaceTest.php +++ b/tests/tad/FunctionMocker/BatchReplaceTest.php @@ -8,12 +8,12 @@ class BatchReplaceTest extends \PHPUnit\Framework\TestCase { - public function setUp() + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/Forge/StepTest.php b/tests/tad/FunctionMocker/Forge/StepTest.php index d5d2182..a60f258 100644 --- a/tests/tad/FunctionMocker/Forge/StepTest.php +++ b/tests/tad/FunctionMocker/Forge/StepTest.php @@ -8,7 +8,7 @@ class StepTest extends \PHPUnit\Framework\TestCase { protected $class; - public function setUp() + public function setUp(): void { $this->class = $class = __NAMESPACE__ . '\StepDummyClass'; } diff --git a/tests/tad/FunctionMocker/ForgeTest.php b/tests/tad/FunctionMocker/ForgeTest.php index 333de77..bde47f0 100644 --- a/tests/tad/FunctionMocker/ForgeTest.php +++ b/tests/tad/FunctionMocker/ForgeTest.php @@ -9,13 +9,13 @@ class ForgeTest extends \PHPUnit\Framework\TestCase { protected $class; - public function setUp() + public function setUp(): void { Sut::setUp(); $this->class = __NAMESPACE__ . '\ForgeClass'; } - public function tearDown() + public function tearDown(): void { Sut::tearDown(); } diff --git a/tests/tad/FunctionMocker/FunctionArgsConstraintsCheckTest.php b/tests/tad/FunctionMocker/FunctionArgsConstraintsCheckTest.php index ea7a285..4857593 100644 --- a/tests/tad/FunctionMocker/FunctionArgsConstraintsCheckTest.php +++ b/tests/tad/FunctionMocker/FunctionArgsConstraintsCheckTest.php @@ -7,12 +7,12 @@ class FunctionArgsConstraintsCheckTest extends TestCase { - public function setUp() + public function setUp(): void { Sut::setUp(); } - public function tearDown() + public function tearDown(): void { Sut::tearDown(); } diff --git a/tests/tad/FunctionMocker/FunctionReplacementInOrderTest.php b/tests/tad/FunctionMocker/FunctionReplacementInOrderTest.php index b2ee07c..6cd77af 100644 --- a/tests/tad/FunctionMocker/FunctionReplacementInOrderTest.php +++ b/tests/tad/FunctionMocker/FunctionReplacementInOrderTest.php @@ -7,11 +7,11 @@ class FunctionReplacementInOrderTest extends TestCase { - public function setUp() { + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() { + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/FunctionReplacementTest.php b/tests/tad/FunctionMocker/FunctionReplacementTest.php index 4f8f45e..cf440fe 100644 --- a/tests/tad/FunctionMocker/FunctionReplacementTest.php +++ b/tests/tad/FunctionMocker/FunctionReplacementTest.php @@ -9,12 +9,12 @@ class FunctionReplacementTest extends TestCase { - public function setUp() + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/GlobalReplacementTest.php b/tests/tad/FunctionMocker/GlobalReplacementTest.php index c0ea1f4..5a12bc1 100644 --- a/tests/tad/FunctionMocker/GlobalReplacementTest.php +++ b/tests/tad/FunctionMocker/GlobalReplacementTest.php @@ -8,12 +8,12 @@ class GlobalReplacementTest extends \PHPUnit\Framework\TestCase { - public function setUp() + public function setUp(): void { Test::setUp(); } - public function tearDown() + public function tearDown(): void { Test::tearDown(); } diff --git a/tests/tad/FunctionMocker/InstanceMethodTest.php b/tests/tad/FunctionMocker/InstanceMethodTest.php index fd68791..99cfe88 100644 --- a/tests/tad/FunctionMocker/InstanceMethodTest.php +++ b/tests/tad/FunctionMocker/InstanceMethodTest.php @@ -14,13 +14,13 @@ class InstanceMethodTest extends TestCase */ protected $testClass; - public function setUp() + public function setUp(): void { $this->testClass = __NAMESPACE__ . '\TestClass'; FunctionMocker::setUp(); } - public function tearDown() + public function tearDown(): void { FunctionMocker::tearDown(); } @@ -38,13 +38,13 @@ public function it_should_return_an_object_extending_the_replaced_one() /** * @test - * it should return an object implementing the PHPUnit_Framework_MockObject_MockObject interface when stubbing + * it should return an object implementing the PHPUnit\Framework\MockObject\MockObject interface when stubbing */ public function it_should_return_an_object_implementing_the_php_unit_framework_mock_object_mock_object_interface_when_stubbing() { $stub = FunctionMocker::replace($this->testClass . '::methodOne'); - $this->assertInstanceOf('\PHPUnit_Framework_MockObject_MockObject', $stub); + $this->assertInstanceOf('\PHPUnit\Framework\MockObject\MockObject', $stub); } /** diff --git a/tests/tad/FunctionMocker/InterfaceMockingTest.php b/tests/tad/FunctionMocker/InterfaceMockingTest.php index ef7d7bc..5574e84 100644 --- a/tests/tad/FunctionMocker/InterfaceMockingTest.php +++ b/tests/tad/FunctionMocker/InterfaceMockingTest.php @@ -17,13 +17,13 @@ class InstanceMockingTest extends \PHPUnit\Framework\TestCase protected $ns; - public function setUp() { + public function setUp(): void { Test::setUp(); $this->ns = __NAMESPACE__; } - public function tearDown() { + public function tearDown(): void { Test::tearDown(); } diff --git a/tests/tad/FunctionMocker/PassAndCallOriginalTest.php b/tests/tad/FunctionMocker/PassAndCallOriginalTest.php index 732b489..5c6c14e 100644 --- a/tests/tad/FunctionMocker/PassAndCallOriginalTest.php +++ b/tests/tad/FunctionMocker/PassAndCallOriginalTest.php @@ -14,11 +14,11 @@ class PassAndCallOriginalTest extends \PHPUnit\Framework\TestCase { - public function setUp() { + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() { + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php index c7a8a54..1f3d931 100644 --- a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php +++ b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php @@ -4,17 +4,19 @@ use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; +use PHPUnit_Runner_Version; use tad\FunctionMocker\FunctionMocker; class PatchworkConfigurationTest extends TestCase { protected $toRemove = []; - protected function setUp() { + protected function setUp(): void { $this->removeFiles(); } - protected function tearDown() { + protected function tearDown(): void { $this->removeFiles(); } @@ -82,7 +84,11 @@ public function should_rewrite_configuration_file_if_changed() { $this->assertTrue(FunctionMocker::writePatchworkConfig($newConfigContents, __DIR__)); $this->assertFileExists($configFile); - $this->assertFileNotExists($previousChecksumFile); + if ( version_compare( substr( Version::id(), 0, 3 ), '8.5', '=') ) { + $this->assertFileNotExists($previousChecksumFile); + } else { + $this->assertFileDoesNotExist($previousChecksumFile); + } $newChecksum = md5(json_encode(FunctionMocker::getPatchworkConfiguration($newConfigContents, __DIR__))); $newChecksumFile = __DIR__ . "/pw-cs-{$newChecksum}.yml"; $this->assertFileExists($newChecksumFile); diff --git a/tests/tad/FunctionMocker/StaticMethodConstraintsCheckTest.php b/tests/tad/FunctionMocker/StaticMethodConstraintsCheckTest.php index 61ba0de..60d5177 100644 --- a/tests/tad/FunctionMocker/StaticMethodConstraintsCheckTest.php +++ b/tests/tad/FunctionMocker/StaticMethodConstraintsCheckTest.php @@ -7,12 +7,12 @@ class StaticMethodConstraintsCheckTest extends TestCase { - public function setUp() + public function setUp(): void { Sut::setUp(); } - public function tearDown() + public function tearDown(): void { Sut::tearDown(); } diff --git a/tests/tad/FunctionMocker/StaticMethodTest.php b/tests/tad/FunctionMocker/StaticMethodTest.php index cebcfaf..a91e288 100644 --- a/tests/tad/FunctionMocker/StaticMethodTest.php +++ b/tests/tad/FunctionMocker/StaticMethodTest.php @@ -8,11 +8,11 @@ class StaticMethodTest extends TestCase { - public function setUp() { + public function setUp(): void { FunctionMocker::setUp(); } - public function tearDown() { + public function tearDown(): void { FunctionMocker::tearDown(); } diff --git a/tests/tad/FunctionMocker/TraitMockingTest.php b/tests/tad/FunctionMocker/TraitMockingTest.php index ef81c5c..368056b 100644 --- a/tests/tad/FunctionMocker/TraitMockingTest.php +++ b/tests/tad/FunctionMocker/TraitMockingTest.php @@ -37,12 +37,12 @@ class TraitMockingTest extends \PHPUnit\Framework\TestCase */ protected $sutClass; - public function setUp() { + public function setUp(): void { $this->sutClass = __NAMESPACE__ . '\TestTrait'; Test::setUp(); } - public function tearDown() { + public function tearDown(): void { Test::tearDown(); } diff --git a/tests/tad/FunctionMocker/UtilsTest.php b/tests/tad/FunctionMocker/UtilsTest.php index 08171a7..0f87e45 100644 --- a/tests/tad/FunctionMocker/UtilsTest.php +++ b/tests/tad/FunctionMocker/UtilsTest.php @@ -15,7 +15,7 @@ class UtilsTest extends \PHPUnit\Framework\TestCase { private $rootDir; - public function setUp() { + public function setUp(): void { $this->rootDir = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); } From 4778bee46909d0da3fc42f76f645d8a241d0e112 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Thu, 3 Dec 2020 11:34:40 +0200 Subject: [PATCH 08/22] We do not need to require different phpunit versions in travis.yml. "phpunit/phpunit": "8.5 - 9.4" in composer.json makes it automatically. On php 7.2: phpunit 8.5 On php 7.3 - 8.0: phpunit 9.4 --- .travis.yml | 6 ++---- composer.json | 7 ++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6dfa60..b60dfb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,5 @@ php: # - '8.0' is not available so far on Travis script: - # test with PHPUnit ~8.5 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.2" ]]; then composer require --dev phpunit/phpunit:~8.5 && vendor/bin/phpunit; fi - # test with PHPUnit ~9.4 - - if [[ ${TRAVIS_PHP_VERSION:0:3} = "7.3" || ${TRAVIS_PHP_VERSION:0:3} = "7.4" || ${TRAVIS_PHP_VERSION:0:3} = "8.0" ]]; then composer require --dev phpunit/phpunit:~9.4 && vendor/bin/phpunit; fi + - composer install + - vendor/bin/phpunit \ No newline at end of file diff --git a/composer.json b/composer.json index 05a7701..6984f41 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,11 @@ ], "minimum-stability": "stable", "require": { - "php": ">=5.6.0", - "phpunit/phpunit": ">=5.7", + "php": ">=7.2", + "phpunit/phpunit": "8.5 - 9.4", "antecedent/patchwork": "^2.0", "lucatume/args": "^1.0" }, - "require-dev": { - "phpunit/phpunit": "^5.7" - }, "autoload": { "psr-0": { "tad\\FunctionMocker": "src" From 2c72b0e88914f54274333045643ba756ac77fcbd Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Thu, 3 Dec 2020 13:08:36 +0200 Subject: [PATCH 09/22] Use github actions. --- .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ .travis.yml | 11 ----------- 2 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..18e471d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,27 @@ +name: CI + +on: [push] + +jobs: + run: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + php-version: ['7.2', '7.3', '7.4', '8.0'] + name: PHP ${{ matrix.php-version }} on ${{ matrix.os }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + + - name: Install dependencies + run: composer install + + - name: PHPUnit tests + run: "vendor/bin/phpunit" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b60dfb4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: php - -php: - - '7.2' - - '7.3' - - '7.4' - # - '8.0' is not available so far on Travis - -script: - - composer install - - vendor/bin/phpunit \ No newline at end of file From 1b34401b8a71d225b7aca296fd56a99a1e44b47a Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Thu, 3 Dec 2020 14:47:19 +0200 Subject: [PATCH 10/22] Run Github actions on pull request. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18e471d..c012134 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI -on: [push] +on: [push, pull_request] jobs: run: From baed5c3168d4e52ac82adb5b025f6ce835392aa0 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Fri, 4 Dec 2020 12:15:37 +0200 Subject: [PATCH 11/22] Allow to use phpunit 9.5. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6984f41..618fa11 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "minimum-stability": "stable", "require": { "php": ">=7.2", - "phpunit/phpunit": "8.5 - 9.4", + "phpunit/phpunit": "8.5 - 9.5", "antecedent/patchwork": "^2.0", "lucatume/args": "^1.0" }, From f57cbb29c6abf66c382314f9808b50335af3abbc Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 21:21:29 +0200 Subject: [PATCH 12/22] Make tests running on PHP 7.1. --- bin/update-tests | 79 +++++++++++++++++++ composer.json | 13 ++- .../Call/CallHandlerInterface.php | 4 +- .../Call/Verifier/AbstractVerifier.php | 6 +- src/tad/FunctionMocker/MockWrapper.php | 28 +++---- .../FunctionMocker/Template/ClassTemplate.php | 4 +- .../Template/VerifyingClassTemplate.php | 4 +- 7 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 bin/update-tests diff --git a/bin/update-tests b/bin/update-tests new file mode 100644 index 0000000..4bb8700 --- /dev/null +++ b/bin/update-tests @@ -0,0 +1,79 @@ +#!/bin/bash +# This file updates tests according to the phpunit library used for current php version, or php version in 1st argument. +# Usage: +# update-tests - to update tests according to the phpunit library used for current php version. +# update-tests x.x - to update tests according to the phpunit library used for specific php version x.x, where x.x = 5.6|7.0|7.1|7.2|7.3|7.4|8.0. + +# Directory with phpunit tests. +TEST_DIR="tests" + +if grep -q Microsoft /proc/version; then + DEV_MODE=$(cmd.exe /c echo %COMPOSER_DEV_MODE% | sed -nr 's/\r//p') +else + DEV_MODE=$COMPOSER_DEV_MODE +fi + +if [[ $1 == '' && $DEV_MODE != '1' ]]; then + echo "Script works with composer in dev mode only." + exit 0 +fi + +if [[ $1 == '' ]]; then + PHP_VERSION=$(php -v | sed -nr "s/PHP ([^.]*?\.[^.]*?)\..*/\1/p") +else + if [[ $1 == 'revert' ]]; then + # Restore test files to the current branch version. + git checkout -- $TEST_DIR + echo "Tests reverted to the initial state." + exit 0 + fi + PHP_VERSION=$1 +fi + +echo "PHP_VERSION: $PHP_VERSION" + +VERSION_FILE="vendor/phpunit/phpunit/src/Runner/Version.php" +CURRENT_PHP_UNIT='' + +RESULT=$(test -f $VERSION_FILE && sed -nr "s/.*new Version.+'(.+\..+)\..*'.*/\1/p" $VERSION_FILE) + +if [[ $? == 0 ]]; then + CURRENT_PHP_UNIT=$RESULT + echo "CURRENT_PHP_UNIT: $CURRENT_PHP_UNIT" +else + echo "CURRENT_PHP_UNIT: Not found." +fi + +if [[ $PHP_VERSION == '5.6' ]]; then + PHP_UNIT='5.7' +elif [[ $PHP_VERSION == '7.0' ]]; then + PHP_UNIT='6.5' +elif [[ $PHP_VERSION == '7.1' ]]; then + PHP_UNIT='7.5' +elif [[ $PHP_VERSION == '7.2' ]]; then + PHP_UNIT='8.5' +elif [[ $PHP_VERSION == '7.3' || $PHP_VERSION == '7.4' || $PHP_VERSION == '8.0' ]]; then + PHP_UNIT='9.5' +fi + +if [[ $PHP_UNIT == '' ]]; then + echo "Wrong PHP version: $PHP_VERSION" + exit 1 +fi + +if [[ $1 == '' && $CURRENT_PHP_UNIT == "$PHP_UNIT" ]]; then + # Do nothing if current version of phpunit is the same as required. Important on CI. + # Anytime force update available specifying the first argument like 'update-phpunit 7.0' + echo "Nothing to do with phpunit." + exit 0 +fi + +# Restore test files to the current branch version. +git checkout -- $TEST_DIR + +if [[ $PHP_UNIT == '5.7' || $PHP_UNIT == '6.5' || $PHP_UNIT == '7.5' ]]; then + echo "Preparing tests for phpunit-$PHP_UNIT" +# find $TEST_DIR -type f -exec sed -i "s/: void / /g" {} \; +fi + +exit 0 diff --git a/composer.json b/composer.json index 618fa11..a5c9eac 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,7 @@ ], "minimum-stability": "stable", "require": { - "php": ">=7.2", - "phpunit/phpunit": "8.5 - 9.5", + "phpunit/phpunit": "5.7 - 9.5", "antecedent/patchwork": "^2.0", "lucatume/args": "^1.0" }, @@ -22,5 +21,15 @@ "files": [ "src/functions.php" ] + }, + "bin": [ + "bin/update-tests" + ], + "scripts": { + "pre-update-cmd": "update-tests", + "update-tests": "update-tests", + "revert-tests": "update-tests revert", + "phpcs": "phpcs --colors --standard=phpcs.xml", + "unit": "phpunit" } } diff --git a/src/tad/FunctionMocker/Call/CallHandlerInterface.php b/src/tad/FunctionMocker/Call/CallHandlerInterface.php index e2309ff..385c0f8 100644 --- a/src/tad/FunctionMocker/Call/CallHandlerInterface.php +++ b/src/tad/FunctionMocker/Call/CallHandlerInterface.php @@ -10,11 +10,11 @@ interface CallHandlerInterface { /** - * @param InvocationOrder $invokedRecorder + * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder * * @return mixed */ - public function setInvokedRecorder(InvocationOrder $invokedRecorder); + public function setInvokedRecorder($invokedRecorder); /** * @param ReplacementRequest $request diff --git a/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php b/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php index cf36d84..57640d7 100644 --- a/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php +++ b/src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php @@ -20,7 +20,7 @@ abstract class AbstractVerifier implements VerifierInterface, CallHandlerInterfa protected $constraintClass; /** - * @var InvocationOrder + * @var \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder */ protected $invokedRecorder; @@ -70,11 +70,11 @@ public function wasCalledWithOnce(array $args) } /** - * @param InvocationOrder $invokedRecorder + * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder * * @return mixed */ - public function setInvokedRecorder(InvocationOrder $invokedRecorder) + public function setInvokedRecorder($invokedRecorder) { $this->invokedRecorder = $invokedRecorder; } diff --git a/src/tad/FunctionMocker/MockWrapper.php b/src/tad/FunctionMocker/MockWrapper.php index 074188e..3bf1133 100644 --- a/src/tad/FunctionMocker/MockWrapper.php +++ b/src/tad/FunctionMocker/MockWrapper.php @@ -41,14 +41,14 @@ public function getWrappedObject() return $this->wrappedObject; } - /** - * @param MockObject $mockObject - * @param InvocationOrder $invokedRecorder - * @param ReplacementRequest $request - * - * @return mixed - */ - public function wrap(MockObject $mockObject, InvocationOrder $invokedRecorder, ReplacementRequest $request) + /** + * @param \PHPUnit_Framework_MockObject_MockObject|MockObject $mockObject + * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder + * @param ReplacementRequest $request + * + * @return mixed + */ + public function wrap($mockObject, $invokedRecorder, ReplacementRequest $request) { $extender = new SpyExtender(); @@ -56,17 +56,17 @@ public function wrap(MockObject $mockObject, InvocationOrder $invokedRecorder, R } /** - * @param MockObject $object - * @param ExtenderInterface $extender + * @param \PHPUnit_Framework_MockObject_MockObject|MockObject $object + * @param ExtenderInterface $extender * - * @param InvocationOrder $invokedRecorder - * @param ReplacementRequest $request + * @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder + * @param ReplacementRequest $request * + * @return mixed * @throws \Exception * - * @return mixed */ - protected function getWrappedInstance(MockObject $object, ExtenderInterface $extender, InvocationOrder $invokedRecorder = null, ReplacementRequest $request = null) + protected function getWrappedInstance($object, ExtenderInterface $extender, $invokedRecorder = null, ReplacementRequest $request = null) { $mockClassName = get_class($object); $extendClassName = sprintf('%s_%s', uniqid('Extended_'), $mockClassName); diff --git a/src/tad/FunctionMocker/Template/ClassTemplate.php b/src/tad/FunctionMocker/Template/ClassTemplate.php index 32d0145..a0f7b98 100644 --- a/src/tad/FunctionMocker/Template/ClassTemplate.php +++ b/src/tad/FunctionMocker/Template/ClassTemplate.php @@ -22,11 +22,11 @@ public function __get_functionMocker_CallHandler(){ return \$this->__functionMocker_callHandler; } - public function __set_functionMocker_originalMockObject(\PHPUnit\Framework\MockObject\MockObject \$mockObject){ + public function __set_functionMocker_originalMockObject(\$mockObject){ \$this->__functionMocker_originalMockObject = \$mockObject; } - public function __set_functionMocker_invokedRecorder(\PHPUnit\Framework\MockObject\Rule\InvocationOrder \$invokedRecorder){ + public function __set_functionMocker_invokedRecorder(\$invokedRecorder){ \$this->__functionMocker_invokedRecorder = \$invokedRecorder; } diff --git a/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php b/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php index 8dfa246..98ecc57 100644 --- a/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php +++ b/src/tad/FunctionMocker/Template/VerifyingClassTemplate.php @@ -24,11 +24,11 @@ public function __get_functionMocker_CallHandler(){ return \$this->__functionMocker_callHandler; } - public function __set_functionMocker_originalMockObject(\PHPUnit\Framework\MockObject\MockObject \$mockObject){ + public function __set_functionMocker_originalMockObject(\$mockObject){ \$this->__functionMocker_originalMockObject = \$mockObject; } - public function __set_functionMocker_invokedRecorder(\PHPUnit\Framework\MockObject\Rule\InvocationOrder \$invokedRecorder){ + public function __set_functionMocker_invokedRecorder(\$invokedRecorder){ \$this->__functionMocker_invokedRecorder = \$invokedRecorder; } From e3b40b0d79c8e9fcd3252c2cc4451ac163c6c47c Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 21:31:55 +0200 Subject: [PATCH 13/22] Make tests running on PHP 7.0. --- bin/update-tests | 2 +- tests/tad/FunctionMocker/PatchworkConfigurationTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/update-tests b/bin/update-tests index 4bb8700..d1f7145 100644 --- a/bin/update-tests +++ b/bin/update-tests @@ -73,7 +73,7 @@ git checkout -- $TEST_DIR if [[ $PHP_UNIT == '5.7' || $PHP_UNIT == '6.5' || $PHP_UNIT == '7.5' ]]; then echo "Preparing tests for phpunit-$PHP_UNIT" -# find $TEST_DIR -type f -exec sed -i "s/: void / /g" {} \; + find $TEST_DIR -type f -exec sed -i "s/: void//g" {} \; fi exit 0 diff --git a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php index 1f3d931..7dc280a 100644 --- a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php +++ b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php @@ -84,10 +84,10 @@ public function should_rewrite_configuration_file_if_changed() { $this->assertTrue(FunctionMocker::writePatchworkConfig($newConfigContents, __DIR__)); $this->assertFileExists($configFile); - if ( version_compare( substr( Version::id(), 0, 3 ), '8.5', '=') ) { - $this->assertFileNotExists($previousChecksumFile); - } else { + if ( version_compare( substr( Version::id(), 0, 3 ), '8.5', '>=') ) { $this->assertFileDoesNotExist($previousChecksumFile); + } else { + $this->assertFileNotExists($previousChecksumFile); } $newChecksum = md5(json_encode(FunctionMocker::getPatchworkConfiguration($newConfigContents, __DIR__))); $newChecksumFile = __DIR__ . "/pw-cs-{$newChecksum}.yml"; From f866af8169c5ccc49b60a4afc3a905a56104e170 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 21:36:05 +0200 Subject: [PATCH 14/22] Make tests running on PHP 7.0 - 8.0. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c012134..f1ebc2c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - php-version: ['7.2', '7.3', '7.4', '8.0'] + php-version: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] name: PHP ${{ matrix.php-version }} on ${{ matrix.os }} steps: From 9dce5d1da50410523641ca8cb087de2a4a712f19 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 21:42:16 +0200 Subject: [PATCH 15/22] Fix deprecated assertFileNotExists(). --- tests/tad/FunctionMocker/PatchworkConfigurationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php index 7dc280a..1378c4b 100644 --- a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php +++ b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php @@ -84,7 +84,7 @@ public function should_rewrite_configuration_file_if_changed() { $this->assertTrue(FunctionMocker::writePatchworkConfig($newConfigContents, __DIR__)); $this->assertFileExists($configFile); - if ( version_compare( substr( Version::id(), 0, 3 ), '8.5', '>=') ) { + if ( version_compare( substr( Version::id(), 0, 1 ), '9', '>=') ) { $this->assertFileDoesNotExist($previousChecksumFile); } else { $this->assertFileNotExists($previousChecksumFile); From 3e034a595607081753d852ba3a1dd8ba3a0372ea Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 22:25:01 +0200 Subject: [PATCH 16/22] Fix call verifier. --- .../Verifier/InstanceMethodCallVerifier.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php b/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php index 70a1489..701ef42 100644 --- a/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php +++ b/src/tad/FunctionMocker/Call/Verifier/InstanceMethodCallVerifier.php @@ -46,7 +46,7 @@ private function realWasCalledTimes( $times ) { protected function getCallTimesWithArgs( $methodName, array $args = null ) { $invocations = $this->getInvocations(); $callTimes = 0; - array_map( function ( Invocation $invocation ) use ( &$callTimes, $args, $methodName ) { + array_map( function ( $invocation ) use ( &$callTimes, $args, $methodName ) { if ( is_array( $args ) ) { $callTimes += $this->compareName( $invocation, $methodName ) && $this->compareArgs( $invocation, $args ); } else { @@ -72,25 +72,29 @@ private function getInvocations() { } /** - * @param Invocation $invocation + * @param \PHPUnit_Framework_MockObject_Invocation_Object|Invocation $invocation * @param $methodName * * @return bool */ - private function compareName( Invocation $invocation, $methodName ) { - $invokedMethodName = $invocation->getMethodName(); + private function compareName( $invocation, $methodName ) { + $invokedMethodName = method_exists( $invocation, 'getMethodName' ) + ? $invocation->getMethodName() + : $invocation->methodName; return $invokedMethodName === $methodName; } /** - * @param Invocation $invocation + * @param \PHPUnit_Framework_MockObject_Invocation_Object|Invocation $invocation * @param $args * * @return bool|mixed|void */ - private function compareArgs( Invocation $invocation, $args ) { - $parameters = $invocation->getParameters(); + private function compareArgs( $invocation, $args ) { + $parameters = method_exists( $invocation, 'getParameters' ) + ? $invocation->getParameters() + : $invocation->parameters; if ( count( $args ) > count( $parameters ) ) { return false; From 79b4921262c29f285b66ecbf2952417a0b0c7396 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Tue, 26 Jan 2021 22:34:23 +0200 Subject: [PATCH 17/22] Fix instance method test. --- .../tad/FunctionMocker/InstanceMethodTest.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/tad/FunctionMocker/InstanceMethodTest.php b/tests/tad/FunctionMocker/InstanceMethodTest.php index 99cfe88..9b0cf7a 100644 --- a/tests/tad/FunctionMocker/InstanceMethodTest.php +++ b/tests/tad/FunctionMocker/InstanceMethodTest.php @@ -36,16 +36,23 @@ public function it_should_return_an_object_extending_the_replaced_one() $this->assertInstanceOf($this->testClass, $stub); } - /** - * @test - * it should return an object implementing the PHPUnit\Framework\MockObject\MockObject interface when stubbing - */ - public function it_should_return_an_object_implementing_the_php_unit_framework_mock_object_mock_object_interface_when_stubbing() - { - $stub = FunctionMocker::replace($this->testClass . '::methodOne'); - - $this->assertInstanceOf('\PHPUnit\Framework\MockObject\MockObject', $stub); - } + /** + * @test + * it should return an object implementing the + * PHPUnit_Framework_MockObject_MockObject or + * PHPUnit\Framework\MockObject\MockObject + * interface when stubbing + */ + public function it_should_return_an_object_implementing_the_php_unit_framework_mock_object_mock_object_interface_when_stubbing() + { + $stub = FunctionMocker::replace($this->testClass . '::methodOne'); + + if (version_compare(PHP_VERSION, '7.0', '<' )) { + $this->assertInstanceOf('\PHPUnit_Framework_MockObject_MockObject', $stub); + } else { + $this->assertInstanceOf('\PHPUnit\Framework\MockObject\MockObject', $stub); + } + } /** * @test From ebc5e06ff0fce5843345f6de5e13f3354db353b2 Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 27 Jan 2021 13:25:21 +0200 Subject: [PATCH 18/22] Make sure we use a proper version of the phpunit in tests. --- tests/tad/FunctionMocker/InstanceMethodTest.php | 5 ++++- tests/tad/FunctionMocker/PatchworkConfigurationTest.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/tad/FunctionMocker/InstanceMethodTest.php b/tests/tad/FunctionMocker/InstanceMethodTest.php index 9b0cf7a..fa59599 100644 --- a/tests/tad/FunctionMocker/InstanceMethodTest.php +++ b/tests/tad/FunctionMocker/InstanceMethodTest.php @@ -47,7 +47,10 @@ public function it_should_return_an_object_implementing_the_php_unit_framework_m { $stub = FunctionMocker::replace($this->testClass . '::methodOne'); - if (version_compare(PHP_VERSION, '7.0', '<' )) { + if ( + class_exists(\PHPUnit_Runner_Version::class ) && + version_compare( substr( \PHPUnit_Runner_Version::id(), 0, 1 ), '5', '=') + ) { $this->assertInstanceOf('\PHPUnit_Framework_MockObject_MockObject', $stub); } else { $this->assertInstanceOf('\PHPUnit\Framework\MockObject\MockObject', $stub); diff --git a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php index 1378c4b..386c0a9 100644 --- a/tests/tad/FunctionMocker/PatchworkConfigurationTest.php +++ b/tests/tad/FunctionMocker/PatchworkConfigurationTest.php @@ -84,7 +84,10 @@ public function should_rewrite_configuration_file_if_changed() { $this->assertTrue(FunctionMocker::writePatchworkConfig($newConfigContents, __DIR__)); $this->assertFileExists($configFile); - if ( version_compare( substr( Version::id(), 0, 1 ), '9', '>=') ) { + if ( + class_exists( Version::class ) && + version_compare( substr( Version::id(), 0, 1 ), '9', '>=') + ) { $this->assertFileDoesNotExist($previousChecksumFile); } else { $this->assertFileNotExists($previousChecksumFile); From a8e10109e94f6d959d62675c0f7cda33a3b2ef3c Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 27 Jan 2021 13:27:34 +0200 Subject: [PATCH 19/22] Fix revert-test composer command. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a5c9eac..cf652f0 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "scripts": { "pre-update-cmd": "update-tests", "update-tests": "update-tests", - "revert-tests": "update-tests revert", + "revert-tests": "composer update-tests revert", "phpcs": "phpcs --colors --standard=phpcs.xml", "unit": "phpunit" } From b3ea9d4802937b4f027c6ab6f31a10bd074b01be Mon Sep 17 00:00:00 2001 From: KAGG Design Date: Wed, 27 Jan 2021 13:47:06 +0200 Subject: [PATCH 20/22] Add PHP 5.6 on CI. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1ebc2c..0c312bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - php-version: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] name: PHP ${{ matrix.php-version }} on ${{ matrix.os }} steps: From 266de9af8ad589c9b903f60e8df3967cc32f9b12 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 19 Apr 2021 13:54:57 +0200 Subject: [PATCH 21/22] doc(changelog.md) add changelog entry --- changelog.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 5f9e664..805344f 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. This projec ## [Unreleased][unreleased] +## [1.4.0] +### Added +- PHP 8 compatibility and test coverage (thanks @kagg-design) + +### Fixed +- Windows compatibility issues (thanks @kagg-design) + ## [1.3.9] ### Added - the `FunctionMocker::replaceInOrder` method to return values from a set from a replaced function or static method, thanks @wppunk, fixes #11 @@ -75,7 +82,9 @@ All notable changes to this project will be documented in this file. This projec ### Fixed - issue where verifying the same instance replacement would result in double instance creations -[unreleased]: https://github.com/lucatume/function-mocker/compare/1.3.8...HEAD +[unreleased]: https://github.com/lucatume/function-mocker/compare/1.4.0...HEAD +[1.4.0]: https://github.com/lucatume/function-mocker/compare/1.3.9...1.4.0 +[1.3.9]: https://github.com/lucatume/function-mocker/compare/1.3.8...1.3.9 [1.3.8]: https://github.com/lucatume/function-mocker/compare/1.3.7...1.3.8 [1.3.7]: https://github.com/lucatume/function-mocker/compare/1.3.6...1.3.7 [1.3.6]: https://github.com/lucatume/function-mocker/compare/1.3.5...1.3.6 From 1f36a4005ed5d44b4bf417db05605716db3450b3 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 19 Apr 2021 13:57:12 +0200 Subject: [PATCH 22/22] doc(README.md) update build status badge to GH CI --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e7c904b..32768d4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ *A [Patchwork](http://antecedent.github.io/patchwork/) powered function mocker.* -[![Build Status](https://travis-ci.org/lucatume/function-mocker.svg?branch=master)](https://travis-ci.org/lucatume/function-mocker) +![Build Status](https://github.com/lucatume/function-mocker/actions/workflows/ci.yml/badge.svg) ## Show me the code This can be written in a [PHPUnit](http://phpunit.de/) test suite