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; |
10
|
|
|
|
11
|
|
|
use F500\Equatable\PHPUnit\Constraint\EquatableCollectionContains; |
12
|
|
|
use F500\Equatable\PHPUnit\Constraint\IsEqual; |
13
|
|
|
use PHPUnit\Framework\Constraint\IsEqual as PHPUnitIsEqual; |
14
|
|
|
use PHPUnit\Framework\Constraint\TraversableContains as PHPUnitTraversableContains; |
15
|
|
|
use PHPUnit\Framework\Assert; |
16
|
|
|
use PHPUnit\Framework\Constraint\Attribute; |
17
|
|
|
use PHPUnit\Framework\Constraint\LogicalNot; |
18
|
|
|
use PHPUnit\Framework\Constraint\StringContains; |
19
|
|
|
use PHPUnit\Util\InvalidArgumentHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
23
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait EquatableAssertionCapabilities |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param mixed $expected |
29
|
|
|
* @param mixed $actual |
30
|
|
|
* @param string $message |
31
|
|
|
* @param float $delta |
32
|
|
|
* @param int $maxDepth |
33
|
|
|
* @param bool $canonicalize |
34
|
|
|
* @param bool $ignoreCase |
35
|
|
|
*/ |
36
|
|
|
public static function assertEquals( |
37
|
|
|
$expected, |
38
|
|
|
$actual, |
39
|
|
|
string $message = '', |
40
|
|
|
float $delta = 0.0, |
41
|
|
|
int $maxDepth = 10, |
42
|
|
|
bool $canonicalize = false, |
43
|
|
|
bool $ignoreCase = false |
44
|
|
|
): void { |
45
|
|
|
$constraint = self::equalTo($expected, $delta, $maxDepth, $canonicalize, $ignoreCase); |
46
|
|
|
|
47
|
|
|
Assert::assertThat($actual, $constraint, $message); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $expected |
52
|
|
|
* @param mixed $actual |
53
|
|
|
* @param string $message |
54
|
|
|
* @param float $delta |
55
|
|
|
* @param int $maxDepth |
56
|
|
|
* @param bool $canonicalize |
57
|
|
|
* @param bool $ignoreCase |
58
|
|
|
*/ |
59
|
|
|
public static function assertNotEquals( |
60
|
|
|
$expected, |
61
|
|
|
$actual, |
62
|
|
|
string $message = '', |
63
|
|
|
$delta = 0.0, |
64
|
|
|
$maxDepth = 10, |
65
|
|
|
$canonicalize = false, |
66
|
|
|
$ignoreCase = false |
67
|
|
|
): void { |
68
|
|
|
$constraint = new LogicalNot( |
69
|
|
|
self::equalTo($expected, $delta, $maxDepth, $canonicalize, $ignoreCase) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
Assert::assertThat($actual, $constraint, $message); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $needle |
77
|
|
|
* @param mixed $haystack |
78
|
|
|
* @param string $message |
79
|
|
|
* @param bool $ignoreCase |
80
|
|
|
* @param bool $checkForObjectIdentity |
81
|
|
|
* @param bool $checkForNonObjectIdentity |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public static function assertContains( |
|
|
|
|
84
|
|
|
$needle, |
85
|
|
|
$haystack, |
86
|
|
|
string $message = '', |
87
|
|
|
bool $ignoreCase = false, |
88
|
|
|
bool $checkForObjectIdentity = true, |
89
|
|
|
bool $checkForNonObjectIdentity = false |
90
|
|
|
): void { |
91
|
|
|
if (is_array($haystack) || is_object($haystack) && $haystack instanceof \Traversable) { |
92
|
|
|
$constraint = self::contains($needle, $checkForObjectIdentity, $checkForNonObjectIdentity); |
93
|
|
|
} elseif (is_string($haystack)) { |
94
|
|
|
if (!is_string($needle)) { |
95
|
|
|
throw InvalidArgumentHelper::factory(1, 'string'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$constraint = new StringContains($needle, $ignoreCase); |
99
|
|
|
} else { |
100
|
|
|
throw InvalidArgumentHelper::factory(2, 'array, traversable or string'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
Assert::assertThat($haystack, $constraint, $message); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $needle |
108
|
|
|
* @param mixed $haystack |
109
|
|
|
* @param string $message |
110
|
|
|
* @param bool $ignoreCase |
111
|
|
|
* @param bool $checkForObjectIdentity |
112
|
|
|
* @param bool $checkForNonObjectIdentity |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public static function assertNotContains( |
|
|
|
|
115
|
|
|
$needle, |
116
|
|
|
$haystack, |
117
|
|
|
string $message = '', |
118
|
|
|
bool $ignoreCase = false, |
119
|
|
|
bool $checkForObjectIdentity = true, |
120
|
|
|
bool $checkForNonObjectIdentity = false |
121
|
|
|
): void { |
122
|
|
|
if (is_array($haystack) || is_object($haystack) && $haystack instanceof \Traversable) { |
123
|
|
|
$constraint = new LogicalNot( |
124
|
|
|
self::contains($needle, $checkForObjectIdentity, $checkForNonObjectIdentity) |
125
|
|
|
); |
126
|
|
|
} elseif (is_string($haystack)) { |
127
|
|
|
if (!is_string($needle)) { |
128
|
|
|
throw InvalidArgumentHelper::factory(1, 'string'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$constraint = new LogicalNot( |
132
|
|
|
new StringContains($needle, $ignoreCase) |
133
|
|
|
); |
134
|
|
|
} else { |
135
|
|
|
throw InvalidArgumentHelper::factory(2, 'array, traversable or string'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
Assert::assertThat($haystack, $constraint, $message); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param mixed $value |
143
|
|
|
* @param float $delta |
144
|
|
|
* @param int $maxDepth |
145
|
|
|
* @param bool $canonicalize |
146
|
|
|
* @param bool $ignoreCase |
147
|
|
|
* |
148
|
|
|
* @return PHPUnitIsEqual |
149
|
|
|
*/ |
150
|
|
|
public static function equalTo( |
151
|
|
|
$value, |
152
|
|
|
float $delta = 0.0, |
153
|
|
|
int $maxDepth = 10, |
154
|
|
|
bool $canonicalize = false, |
155
|
|
|
bool $ignoreCase = false |
156
|
|
|
): PHPUnitIsEqual { |
157
|
|
|
return new IsEqual( |
158
|
|
|
$value, |
159
|
|
|
$delta, |
160
|
|
|
$maxDepth, |
161
|
|
|
$canonicalize, |
162
|
|
|
$ignoreCase |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $attributeName |
168
|
|
|
* @param mixed $value |
169
|
|
|
* @param float $delta |
170
|
|
|
* @param int $maxDepth |
171
|
|
|
* @param bool $canonicalize |
172
|
|
|
* @param bool $ignoreCase |
173
|
|
|
* |
174
|
|
|
* @return Attribute |
175
|
|
|
*/ |
176
|
|
|
public static function attributeEqualTo( |
177
|
|
|
string $attributeName, |
178
|
|
|
$value, |
179
|
|
|
float $delta = 0.0, |
180
|
|
|
int $maxDepth = 10, |
181
|
|
|
bool $canonicalize = false, |
182
|
|
|
bool $ignoreCase = false |
183
|
|
|
): Attribute { |
184
|
|
|
return Assert::attribute( |
185
|
|
|
self::equalTo($value, $delta, $maxDepth, $canonicalize, $ignoreCase), |
186
|
|
|
$attributeName |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param mixed $value |
192
|
|
|
* @param bool $checkForObjectIdentity |
193
|
|
|
* @param bool $checkForNonObjectIdentity |
194
|
|
|
* |
195
|
|
|
* @return PHPUnitTraversableContains |
196
|
|
|
*/ |
197
|
|
|
public static function contains( |
198
|
|
|
$value, |
199
|
|
|
bool $checkForObjectIdentity = true, |
200
|
|
|
bool $checkForNonObjectIdentity = false |
201
|
|
|
): PHPUnitTraversableContains { |
202
|
|
|
return new EquatableCollectionContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.