1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Entity\Property\PHPUnitComparator; |
4
|
|
|
|
5
|
|
|
use SebastianBergmann\Comparator\Comparator; |
6
|
|
|
use SebastianBergmann\Comparator\ComparisonFailure; |
7
|
|
|
|
8
|
|
|
class StrictScalarComparator extends Comparator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param mixed $expected |
12
|
|
|
* @param mixed $actual |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
public function accepts($expected, $actual) |
16
|
|
|
{ |
17
|
|
|
return ((is_scalar($expected) xor null === $expected) && |
18
|
|
|
(is_scalar($actual) xor null === $actual)) |
19
|
|
|
// allow comparison between strings and objects featuring __toString() |
20
|
|
|
|| (is_string($expected) && is_object($actual) && method_exists($actual, '__toString')) |
21
|
|
|
|| (is_object($expected) && method_exists($expected, '__toString') && is_string($actual)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param mixed $expected |
26
|
|
|
* @param mixed $actual |
27
|
|
|
* @param float $delta |
28
|
|
|
* @param bool|false $canonicalize |
29
|
|
|
* @param bool|false $ignoreCase |
30
|
|
|
* @throws ComparisonFailure |
31
|
|
|
*/ |
32
|
|
|
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) |
33
|
|
|
{ |
34
|
|
|
$expectedToCompare = $expected; |
35
|
|
|
$actualToCompare = $actual; |
36
|
|
|
|
37
|
|
|
// always compare as strings to avoid strange behaviour |
38
|
|
|
// otherwise 0 == 'Foobar' |
39
|
|
|
if (is_string($expected) || is_string($actual)) { |
40
|
|
|
$expectedToCompare = (string) $expectedToCompare; |
41
|
|
|
$actualToCompare = (string) $actualToCompare; |
42
|
|
|
|
43
|
|
|
if ($ignoreCase) { |
44
|
|
|
$expectedToCompare = strtolower($expectedToCompare); |
45
|
|
|
$actualToCompare = strtolower($actualToCompare); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (is_null($expectedToCompare) && !is_bool($actual) || is_null($actual) && !is_bool($expectedToCompare)) { |
50
|
|
|
if ($expected !== $actual) { |
51
|
|
|
$this->throwComparisonFailureException($expected, $actual); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($expectedToCompare != $actualToCompare) { |
56
|
|
|
if (is_string($expected) && is_string($actual)) { |
57
|
|
|
throw new ComparisonFailure( |
58
|
|
|
$expected, |
59
|
|
|
$actual, |
60
|
|
|
$this->exporter->export($expected), |
61
|
|
|
$this->exporter->export($actual), |
62
|
|
|
false, |
63
|
|
|
'Failed asserting that two strings are equal.' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->throwComparisonFailureException($expected, $actual); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $expected |
73
|
|
|
* @param $actual |
74
|
|
|
*/ |
75
|
|
|
private function throwComparisonFailureException($expected, $actual) |
76
|
|
|
{ |
77
|
|
|
throw new ComparisonFailure( |
78
|
|
|
$expected, |
79
|
|
|
$actual, |
80
|
|
|
$this->exporter->export($expected), |
81
|
|
|
$this->exporter->export($actual), |
82
|
|
|
false, |
83
|
|
|
sprintf( |
84
|
|
|
'Failed asserting that %s matches expected %s.', |
85
|
|
|
$this->exporter->export($actual), |
86
|
|
|
$this->exporter->export($expected) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|