|
| 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