| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PHPUnit.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PHPUnit\Framework\Constraint;
|
|
|
11 |
|
|
|
12 |
use function get_class;
|
|
|
13 |
use function is_array;
|
|
|
14 |
use function is_object;
|
|
|
15 |
use function is_string;
|
|
|
16 |
use function sprintf;
|
|
|
17 |
use PHPUnit\Framework\ExpectationFailedException;
|
|
|
18 |
use SebastianBergmann\Comparator\ComparisonFailure;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
|
|
|
22 |
*/
|
|
|
23 |
final class IsIdentical extends Constraint
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* @var mixed
|
|
|
27 |
*/
|
|
|
28 |
private $value;
|
|
|
29 |
|
|
|
30 |
public function __construct($value)
|
|
|
31 |
{
|
|
|
32 |
$this->value = $value;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Evaluates the constraint for parameter $other.
|
|
|
37 |
*
|
|
|
38 |
* If $returnResult is set to false (the default), an exception is thrown
|
|
|
39 |
* in case of a failure. null is returned otherwise.
|
|
|
40 |
*
|
|
|
41 |
* If $returnResult is true, the result of the evaluation is returned as
|
|
|
42 |
* a boolean value instead: true in case of success, false in case of a
|
|
|
43 |
* failure.
|
|
|
44 |
*
|
|
|
45 |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
46 |
* @throws ExpectationFailedException
|
|
|
47 |
*/
|
|
|
48 |
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
|
|
|
49 |
{
|
|
|
50 |
$success = $this->value === $other;
|
|
|
51 |
|
|
|
52 |
if ($returnResult) {
|
|
|
53 |
return $success;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if (!$success) {
|
|
|
57 |
$f = null;
|
|
|
58 |
|
|
|
59 |
// if both values are strings, make sure a diff is generated
|
|
|
60 |
if (is_string($this->value) && is_string($other)) {
|
|
|
61 |
$f = new ComparisonFailure(
|
|
|
62 |
$this->value,
|
|
|
63 |
$other,
|
|
|
64 |
sprintf("'%s'", $this->value),
|
|
|
65 |
sprintf("'%s'", $other)
|
|
|
66 |
);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// if both values are array, make sure a diff is generated
|
|
|
70 |
if (is_array($this->value) && is_array($other)) {
|
|
|
71 |
$f = new ComparisonFailure(
|
|
|
72 |
$this->value,
|
|
|
73 |
$other,
|
|
|
74 |
$this->exporter()->export($this->value),
|
|
|
75 |
$this->exporter()->export($other)
|
|
|
76 |
);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$this->fail($other, $description, $f);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return null;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Returns a string representation of the constraint.
|
|
|
87 |
*
|
|
|
88 |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
89 |
*/
|
|
|
90 |
public function toString(): string
|
|
|
91 |
{
|
|
|
92 |
if (is_object($this->value)) {
|
|
|
93 |
return 'is identical to an object of class "' .
|
|
|
94 |
get_class($this->value) . '"';
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return 'is identical to ' . $this->exporter()->export($this->value);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Returns the description of the failure.
|
|
|
102 |
*
|
|
|
103 |
* The beginning of failure messages is "Failed asserting that" in most
|
|
|
104 |
* cases. This method should return the second part of that sentence.
|
|
|
105 |
*
|
|
|
106 |
* @param mixed $other evaluated value or object
|
|
|
107 |
*
|
|
|
108 |
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
109 |
*/
|
|
|
110 |
protected function failureDescription($other): string
|
|
|
111 |
{
|
|
|
112 |
if (is_object($this->value) && is_object($other)) {
|
|
|
113 |
return 'two variables reference the same object';
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (is_string($this->value) && is_string($other)) {
|
|
|
117 |
return 'two strings are identical';
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
if (is_array($this->value) && is_array($other)) {
|
|
|
121 |
return 'two arrays are identical';
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return parent::failureDescription($other);
|
|
|
125 |
}
|
|
|
126 |
}
|