Skip to content

Commit 3cae983

Browse files
kubawerloskeradus
authored andcommitted
Allow PHPUnit 8 (#3)
1 parent 32fceb4 commit 3cae983

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.php_cs
22
/.php_cs.cache
3+
/.phpunit.result.cache
34
/composer.lock
45
/vendor/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^5.5 || ^7.0",
14-
"phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0",
14+
"phpunit/phpunit": "^5.7.23 || ^6.4.3 || ^7.0 || ^8.0",
1515
"phpunitgoodpractices/polyfill": "^1.1"
1616
},
1717
"conflict": {

src/Constraint/IsIdenticalString.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0') < 0) {
1515
class_alias(IsIdenticalStringForV5::class, IsIdenticalString::class);
16-
} else {
16+
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '8.0.0') < 0) {
1717
class_alias(IsIdenticalStringForV7::class, IsIdenticalString::class);
18+
} else {
19+
class_alias(IsIdenticalStringForV8::class, IsIdenticalString::class);
1820
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer / PHPUnit Constraint IsIdenticalString.
5+
*
6+
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PhpCsFixer\PhpunitConstraintIsIdenticalString\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use PHPUnit\Framework\Constraint\IsIdentical;
16+
use PHPUnit\Framework\ExpectationFailedException;
17+
18+
/**
19+
* @author Kuba Werłos <werlos@gmail.com>
20+
*
21+
* @internal
22+
*/
23+
final class IsIdenticalStringForV8 extends Constraint
24+
{
25+
/**
26+
* @var mixed
27+
*/
28+
private $value;
29+
30+
/**
31+
* @var IsIdentical
32+
*/
33+
private $isIdentical;
34+
35+
/**
36+
* @param mixed $value
37+
*/
38+
public function __construct($value)
39+
{
40+
$this->value = $value;
41+
$this->isIdentical = new IsIdentical($this->value);
42+
}
43+
44+
public function evaluate($other, string $description = '', bool $returnResult = false)
45+
{
46+
try {
47+
return $this->isIdentical->evaluate($other, $description, $returnResult);
48+
} catch (ExpectationFailedException $exception) {
49+
$message = $exception->getMessage();
50+
51+
$additionalFailureDescription = $this->additionalFailureDescription($other);
52+
53+
if ($additionalFailureDescription) {
54+
$message .= "\n".$additionalFailureDescription;
55+
}
56+
57+
throw new ExpectationFailedException(
58+
$message,
59+
$exception->getComparisonFailure(),
60+
$exception
61+
);
62+
}
63+
}
64+
65+
public function toString(): string
66+
{
67+
return $this->isIdentical->toString();
68+
}
69+
70+
protected function additionalFailureDescription($other): string
71+
{
72+
if (
73+
$other === $this->value
74+
|| preg_replace('/(\r\n|\n\r|\r)/', "\n", $other) !== preg_replace('/(\r\n|\n\r|\r)/', "\n", $this->value)
75+
) {
76+
return '';
77+
}
78+
79+
return ' #Warning: Strings contain different line endings! Debug using remapping ["\r" => "R", "\n" => "N", "\t" => "T"]:'
80+
."\n"
81+
.' -'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $other)
82+
."\n"
83+
.' +'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $this->value);
84+
}
85+
}

0 commit comments

Comments
 (0)