Skip to content

Commit

Permalink
Merge pull request #18 from lucatume/wrap-kagg-design-php8
Browse files Browse the repository at this point in the history
Wrap PR #17
  • Loading branch information
lucatume authored Apr 19, 2021
2 parents 925d699 + 1f36a40 commit 6410a3f
Show file tree
Hide file tree
Showing 38 changed files with 299 additions and 144 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on: [push, pull_request]

jobs:
run:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
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:
- 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"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ composer.lock
patchwork.json
cache/*
pw-cs-*.yml
.phpunit.result.cache
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions bin/update-tests
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=5.6.0",
"phpunit/phpunit": ">=5.7",
"phpunit/phpunit": "5.7 - 9.5",
"antecedent/patchwork": "^2.0",
"lucatume/args": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-0": {
"tad\\FunctionMocker": "src"
},
"files": [
"src/shims.php",
"src/functions.php"
]
},
"bin": [
"bin/update-tests"
],
"scripts": {
"pre-update-cmd": "update-tests",
"update-tests": "update-tests",
"revert-tests": "composer update-tests revert",
"phpcs": "phpcs --colors --standard=phpcs.xml",
"unit": "phpunit"
}
}
12 changes: 3 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="tests/_bootstrap.php"
forceCoversAnnotation="true"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuite>
<testsuite name="function-mocker">
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
20 changes: 0 additions & 20 deletions src/shims.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/tad/FunctionMocker/Call/CallHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder
*
* @return mixed
*/
public function setInvokedRecorder(PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder);
public function setInvokedRecorder($invokedRecorder);

/**
* @param ReplacementRequest $request
Expand Down
8 changes: 4 additions & 4 deletions src/tad/FunctionMocker/Call/Verifier/AbstractVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +20,7 @@ abstract class AbstractVerifier implements VerifierInterface, CallHandlerInterfa
protected $constraintClass;

/**
* @var PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
* @var \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder
*/
protected $invokedRecorder;

Expand Down Expand Up @@ -70,11 +70,11 @@ public function wasCalledWithOnce(array $args)
}

/**
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedRecorder|InvocationOrder $invokedRecorder
*
* @return mixed
*/
public function setInvokedRecorder(PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $invokedRecorder)
public function setInvokedRecorder($invokedRecorder)
{
$this->invokedRecorder = $invokedRecorder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) use ( &$callTimes, $args, $methodName ) {
if ( is_array( $args ) ) {
$callTimes += $this->compareName( $invocation, $methodName ) && $this->compareArgs( $invocation, $args );
} else {
Expand All @@ -57,12 +58,26 @@ 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 \PHPUnit_Framework_MockObject_Invocation_Object|Invocation $invocation
* @param $methodName
*
* @return bool
*/
private function compareName( \PHPUnit_Framework_MockObject_Invocation_Object $invocation, $methodName ) {
private function compareName( $invocation, $methodName ) {
$invokedMethodName = method_exists( $invocation, 'getMethodName' )
? $invocation->getMethodName()
: $invocation->methodName;
Expand All @@ -71,12 +86,12 @@ private function compareName( \PHPUnit_Framework_MockObject_Invocation_Object $i
}

/**
* @param \PHPUnit_Framework_MockObject_Invocation_Object $invocation
* @param $args
* @param \PHPUnit_Framework_MockObject_Invocation_Object|Invocation $invocation
* @param $args
*
* @return bool|mixed|void
*/
private function compareArgs( \PHPUnit_Framework_MockObject_Invocation_Object $invocation, $args ) {
private function compareArgs( $invocation, $args ) {
$parameters = method_exists( $invocation, 'getParameters' )
? $invocation->getParameters()
: $invocation->parameters;
Expand Down
9 changes: 6 additions & 3 deletions src/tad/FunctionMocker/Forge/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit 6410a3f

Please sign in to comment.