Skip to content

Commit

Permalink
Allow PHP 8 and PHPUnit 9 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Oct 20, 2020
1 parent 9c5bf68 commit 2bd63d7
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 29 deletions.
60 changes: 38 additions & 22 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
sudo: false
dist: xenial
os: linux

language: php

git:
depth: 1
depth: 10

cache:
directories:
- $HOME/.composer

language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4snapshot

env:
global:
- DEFAULT_COMPOSER_FLAGS="--no-interaction --no-progress"
- DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
- COMPOSER_FLAGS=""

stages:
- Fast Test
- Static code analysis
- Test

Expand All @@ -31,20 +25,13 @@ before_install:
- phpenv config-rm xdebug.ini || return 0

# Composer: boost installation
- composer global show -ND 2>&1 | grep "hirak/prestissimo" || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo

install:
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
- composer info -D | sort

script:
- vendor/bin/phpunit
- composer global show hirak/prestissimo -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo

jobs:
include:
-
stage: Static code analysis
php: 7.3
php: 7.4
install:
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS
- travis_retry composer update -d dev-tools $DEFAULT_COMPOSER_FLAGS
Expand All @@ -55,3 +42,32 @@ jobs:
- dev-tools/vendor/bin/composer-require-checker check composer.json --config-file=.composer-require-checker.json || travis_terminate 1
- dev-tools/vendor/bin/phpmd src,tests text phpmd.xml || travis_terminate 1
- dev-tools/vendor/bin/php-cs-fixer fix --diff --dry-run -v || travis_terminate 1

- &STANDARD_TEST_JOB
stage: Test
php: 7.0
install:
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
- composer info -D | sort
script:
- vendor/bin/phpunit
-
<<: *STANDARD_TEST_JOB
php: 5.6
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
-
<<: *STANDARD_TEST_JOB
php: 7.1
-
<<: *STANDARD_TEST_JOB
php: 7.2
-
<<: *STANDARD_TEST_JOB
php: 7.3
-
<<: *STANDARD_TEST_JOB
stage: Fast Test
php: 7.4
-
<<: *STANDARD_TEST_JOB
php: nightly
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
}
],
"require": {
"php": "^5.5 || ^7.0",
"phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0 || ^8.0",
"phpunitgoodpractices/polyfill": "^1.1"
"php": "^5.5 || ^7.0 || ^8.0",
"phpunit/phpunit": "^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.0 || ^9.0",
"phpunitgoodpractices/polyfill": "^1.4"
},
"require-dev": {
"johnkary/phpunit-speedtrap": "^1.1 || ^2.0 || ^3.0",
Expand Down
4 changes: 3 additions & 1 deletion src/Constraint/IsIdenticalString.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class_alias(IsIdenticalStringForV5::class, IsIdenticalString::class);
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '8.0.0') < 0) {
class_alias(IsIdenticalStringForV7::class, IsIdenticalString::class);
} else {
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '9.0.0') < 0) {
class_alias(IsIdenticalStringForV8::class, IsIdenticalString::class);
} else {
class_alias(IsIdenticalStringForV9::class, IsIdenticalString::class);
}
85 changes: 85 additions & 0 deletions src/Constraint/IsIdenticalStringForV9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of PHP CS Fixer / PHPUnit Constraint IsIdenticalString.
*
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\PhpunitConstraintIsIdenticalString\Constraint;

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

/**
* @author Kuba Werłos <werlos@gmail.com>
*
* @internal
*/
final class IsIdenticalStringForV9 extends Constraint
{
/**
* @var mixed
*/
private $value;

/**
* @var IsIdentical
*/
private $isIdentical;

/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
$this->isIdentical = new IsIdentical($this->value);
}

public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
{
try {
return $this->isIdentical->evaluate($other, $description, $returnResult);
} catch (ExpectationFailedException $exception) {
$message = $exception->getMessage();

$additionalFailureDescription = $this->additionalFailureDescription($other);

if ($additionalFailureDescription) {
$message .= "\n".$additionalFailureDescription;
}

throw new ExpectationFailedException(
$message,
$exception->getComparisonFailure(),
$exception
);
}
}

public function toString(): string
{
return $this->isIdentical->toString();
}

protected function additionalFailureDescription($other): string
{
if (
$other === $this->value
|| preg_replace('/(\r\n|\n\r|\r)/', "\n", $other) !== preg_replace('/(\r\n|\n\r|\r)/', "\n", $this->value)
) {
return '';
}

return ' #Warning: Strings contain different line endings! Debug using remapping ["\r" => "R", "\n" => "N", "\t" => "T"]:'
."\n"
.' -'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $other)
."\n"
.' +'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $this->value);
}
}
12 changes: 9 additions & 3 deletions tests/Constraint/IsIdenticalStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public function testSameStringsConstraintFail()
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegExp(
'#^Failed asserting that two strings are identical\.[\n] \#Warning\: Strings contain different line endings\! Debug using remapping \["\\\\r" => "R", "\\\\n" => "N", "\\\\t" => "T"\]\:\n \-N\n \+RN$#'
);
if (\is_callable([$this, 'expectExceptionMessageMatches'])) {
$this->expectExceptionMessageMatches(
'#^Failed asserting that two strings are identical\.[\n] \#Warning\: Strings contain different line endings\! Debug using remapping \["\\\\r" => "R", "\\\\n" => "N", "\\\\t" => "T"\]\:\n \-N\n \+RN$#'
);
} else {
$this->expectExceptionMessageRegExp(
'#^Failed asserting that two strings are identical\.[\n] \#Warning\: Strings contain different line endings\! Debug using remapping \["\\\\r" => "R", "\\\\n" => "N", "\\\\t" => "T"\]\:\n \-N\n \+RN$#'
);
}

$constraint = new IsIdenticalString("\r\n");
$constraint->evaluate("\n");
Expand Down

0 comments on commit 2bd63d7

Please sign in to comment.