1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/equatable/blob/master/LICENSE MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace F500\Equatable\PHPUnit\Constraint; |
10
|
|
|
|
11
|
|
|
use F500\Equatable\Equatable; |
12
|
|
|
use PHPUnit\Framework\Constraint\IsEqual as BaseIsEqual; |
13
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
14
|
|
|
use SebastianBergmann\Comparator\ComparisonFailure; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
18
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class IsEqual extends BaseIsEqual |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var mixed |
24
|
|
|
*/ |
25
|
21 |
|
private $value; |
|
|
|
|
26
|
|
|
|
27
|
21 |
|
/** |
28
|
3 |
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function __construct($value, float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false) |
|
|
|
|
31
|
18 |
|
{ |
32
|
9 |
|
parent::__construct($value, $delta, $maxDepth, $canonicalize, $ignoreCase); |
33
|
9 |
|
|
34
|
9 |
|
$this->value = $value; |
35
|
9 |
|
} |
36
|
9 |
|
|
37
|
9 |
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function evaluate($other, $description = '', $returnResult = false) |
41
|
9 |
|
{ |
42
|
6 |
|
if ($this->value === $other) { |
43
|
6 |
|
return true; |
44
|
6 |
|
} |
45
|
6 |
|
|
46
|
6 |
|
if ($this->value instanceof Equatable) { |
47
|
6 |
|
return $this->handleResult( |
48
|
|
|
$this->value, |
49
|
|
|
$other, |
50
|
|
|
$this->value->equals($other), |
51
|
3 |
|
$description, |
52
|
|
|
$returnResult |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($other instanceof Equatable) { |
57
|
|
|
return $this->handleResult( |
58
|
|
|
$this->value, |
59
|
|
|
$other, |
60
|
|
|
$other->equals($this->value), |
61
|
|
|
$description, |
62
|
|
|
$returnResult |
63
|
15 |
|
); |
64
|
|
|
} |
65
|
15 |
|
|
66
|
3 |
|
return parent::evaluate($other, $description, $returnResult); |
67
|
|
|
} |
68
|
|
|
|
69
|
12 |
|
/** |
70
|
6 |
|
* @param mixed $expected |
71
|
|
|
* @param mixed $actual |
72
|
|
|
* @param bool $result |
73
|
6 |
|
* @param string $description |
74
|
6 |
|
* @param bool $returnResult |
75
|
6 |
|
* |
76
|
6 |
|
* @return bool |
77
|
6 |
|
*/ |
78
|
6 |
|
private function handleResult($expected, $actual, $result, $description, $returnResult) |
79
|
6 |
|
{ |
80
|
|
|
if ($result) { |
81
|
|
|
return true; |
82
|
6 |
|
} |
83
|
6 |
|
|
84
|
6 |
|
if ($returnResult) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$comparisonFailure = new ComparisonFailure( |
89
|
|
|
$expected, |
90
|
|
|
$actual, |
91
|
|
|
$this->exporter->export($expected), |
92
|
|
|
$this->exporter->export($actual), |
93
|
|
|
false, |
94
|
|
|
'Failed asserting that two equatable objects are equal.' |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
throw new ExpectationFailedException( |
98
|
|
|
trim($description . "\n" . $comparisonFailure->getMessage()), |
99
|
|
|
$comparisonFailure |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|