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

Wrap PR #17 #18

Merged
merged 22 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a556118
build(.travis.yml) add more tested PHP versions
lucatume Oct 23, 2020
ba7b7aa
Files on Windows can have different line separators, especially under…
kagg-design Dec 1, 2020
d6eb0c3
Fix DIRECTORY_SEPARATOR in it_should_return_properly_filtered_paths_a…
kagg-design Dec 1, 2020
c1c1d91
Replace '/' with DIRECTORY_SEPARATOR to make replace() work properly …
kagg-design Dec 2, 2020
dac927d
Revert "build(.travis.yml) add more tested PHP versions"
kagg-design Dec 2, 2020
0383729
Run tests on php 7.3 and 7.4.
kagg-design Dec 2, 2020
59a724a
Make code and tests running on php7.2 - 8.0.
kagg-design Dec 2, 2020
4778bee
We do not need to require different phpunit versions in travis.yml.
kagg-design Dec 3, 2020
2c72b0e
Use github actions.
kagg-design Dec 3, 2020
1b34401
Run Github actions on pull request.
kagg-design Dec 3, 2020
baed5c3
Allow to use phpunit 9.5.
kagg-design Dec 4, 2020
f57cbb2
Make tests running on PHP 7.1.
kagg-design Jan 26, 2021
e3b40b0
Make tests running on PHP 7.0.
kagg-design Jan 26, 2021
f866af8
Make tests running on PHP 7.0 - 8.0.
kagg-design Jan 26, 2021
9dce5d1
Fix deprecated assertFileNotExists().
kagg-design Jan 26, 2021
3e034a5
Fix call verifier.
kagg-design Jan 26, 2021
79b4921
Fix instance method test.
kagg-design Jan 26, 2021
ebc5e06
Make sure we use a proper version of the phpunit in tests.
kagg-design Jan 27, 2021
a8e1010
Fix revert-test composer command.
kagg-design Jan 27, 2021
b3ea9d4
Add PHP 5.6 on CI.
kagg-design Jan 27, 2021
266de9a
doc(changelog.md) add changelog entry
lucatume Apr 19, 2021
1f36a40
doc(README.md) update build status badge to GH CI
lucatume Apr 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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