1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/equatable/blob/master/LICENSE MIT |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace F500\Equatable\PHPUnit; |
8
|
|
|
|
9
|
|
|
use F500\Equatable\PHPUnit\Constraint\EquatableCollectionContains; |
10
|
|
|
use F500\Equatable\PHPUnit\Constraint\IsEqual; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
14
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait EquatableAssertionCapabilities |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param mixed $expected |
20
|
|
|
* @param mixed $actual |
21
|
|
|
* @param string $message |
22
|
|
|
* @param float $delta |
23
|
|
|
* @param int $maxDepth |
24
|
|
|
* @param bool $canonicalize |
25
|
|
|
* @param bool $ignoreCase |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public static function assertEquals( |
|
|
|
|
28
|
|
|
$expected, |
29
|
|
|
$actual, |
30
|
|
|
$message = '', |
31
|
|
|
$delta = 0.0, |
32
|
|
|
$maxDepth = 10, |
33
|
|
|
$canonicalize = false, |
34
|
|
|
$ignoreCase = false |
35
|
|
|
) { |
36
|
|
|
$constraint = self::equalTo($expected, $delta, $maxDepth, $canonicalize, $ignoreCase); |
37
|
|
|
|
38
|
|
|
\PHPUnit_Framework_Assert::assertThat($actual, $constraint, $message); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $expected |
43
|
|
|
* @param mixed $actual |
44
|
|
|
* @param string $message |
45
|
|
|
* @param float $delta |
46
|
|
|
* @param int $maxDepth |
47
|
|
|
* @param bool $canonicalize |
48
|
|
|
* @param bool $ignoreCase |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public static function assertNotEquals( |
|
|
|
|
51
|
|
|
$expected, |
52
|
|
|
$actual, |
53
|
|
|
$message = '', |
54
|
|
|
$delta = 0.0, |
55
|
|
|
$maxDepth = 10, |
56
|
|
|
$canonicalize = false, |
57
|
|
|
$ignoreCase = false |
58
|
|
|
) { |
59
|
|
|
$constraint = new \PHPUnit_Framework_Constraint_Not( |
60
|
|
|
self::equalTo($expected, $delta, $maxDepth, $canonicalize, $ignoreCase) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
\PHPUnit_Framework_Assert::assertThat($actual, $constraint, $message); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $needle |
68
|
|
|
* @param mixed $haystack |
69
|
|
|
* @param string $message |
70
|
|
|
* @param bool $ignoreCase |
71
|
|
|
* @param bool $checkForObjectIdentity |
72
|
|
|
* @param bool $checkForNonObjectIdentity |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public static function assertContains( |
|
|
|
|
75
|
|
|
$needle, |
76
|
|
|
$haystack, |
77
|
|
|
$message = '', |
78
|
|
|
$ignoreCase = false, |
79
|
|
|
$checkForObjectIdentity = true, |
80
|
|
|
$checkForNonObjectIdentity = false |
81
|
|
|
) { |
82
|
|
|
if (is_array($haystack) || is_object($haystack) && $haystack instanceof \Traversable) { |
83
|
|
|
$constraint = self::contains($needle, $checkForObjectIdentity, $checkForNonObjectIdentity); |
84
|
|
|
} elseif (is_string($haystack)) { |
85
|
|
|
if (!is_string($needle)) { |
86
|
|
|
throw \PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$constraint = new \PHPUnit_Framework_Constraint_StringContains($needle, $ignoreCase); |
90
|
|
|
} else { |
91
|
|
|
throw \PHPUnit_Util_InvalidArgumentHelper::factory(2, 'array, traversable or string'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
\PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param mixed $needle |
99
|
|
|
* @param mixed $haystack |
100
|
|
|
* @param string $message |
101
|
|
|
* @param bool $ignoreCase |
102
|
|
|
* @param bool $checkForObjectIdentity |
103
|
|
|
* @param bool $checkForNonObjectIdentity |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public static function assertNotContains( |
|
|
|
|
106
|
|
|
$needle, |
107
|
|
|
$haystack, |
108
|
|
|
$message = '', |
109
|
|
|
$ignoreCase = false, |
110
|
|
|
$checkForObjectIdentity = true, |
111
|
|
|
$checkForNonObjectIdentity = false |
112
|
|
|
) { |
113
|
|
|
if (is_array($haystack) || is_object($haystack) && $haystack instanceof \Traversable) { |
114
|
|
|
$constraint = new \PHPUnit_Framework_Constraint_Not( |
115
|
|
|
self::contains($needle, $checkForObjectIdentity, $checkForNonObjectIdentity) |
116
|
|
|
); |
117
|
|
|
} elseif (is_string($haystack)) { |
118
|
|
|
if (!is_string($needle)) { |
119
|
|
|
throw \PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$constraint = new \PHPUnit_Framework_Constraint_Not( |
123
|
|
|
new \PHPUnit_Framework_Constraint_StringContains($needle, $ignoreCase) |
124
|
|
|
); |
125
|
|
|
} else { |
126
|
|
|
throw \PHPUnit_Util_InvalidArgumentHelper::factory(2, 'array, traversable or string'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
\PHPUnit_Framework_Assert::assertThat($haystack, $constraint, $message); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $value |
134
|
|
|
* @param float $delta |
135
|
|
|
* @param int $maxDepth |
136
|
|
|
* @param bool $canonicalize |
137
|
|
|
* @param bool $ignoreCase |
138
|
|
|
* |
139
|
|
|
* @return IsEqual |
140
|
|
|
*/ |
141
|
|
|
public static function equalTo($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
142
|
|
|
{ |
143
|
|
|
return new IsEqual( |
144
|
|
|
$value, |
145
|
|
|
$delta, |
146
|
|
|
$maxDepth, |
147
|
|
|
$canonicalize, |
148
|
|
|
$ignoreCase |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $attributeName |
154
|
|
|
* @param mixed $value |
155
|
|
|
* @param float $delta |
156
|
|
|
* @param int $maxDepth |
157
|
|
|
* @param bool $canonicalize |
158
|
|
|
* @param bool $ignoreCase |
159
|
|
|
* |
160
|
|
|
* @return \PHPUnit_Framework_Constraint_Attribute |
161
|
|
|
*/ |
162
|
|
|
public static function attributeEqualTo( |
163
|
|
|
$attributeName, |
164
|
|
|
$value, |
165
|
|
|
$delta = 0.0, |
166
|
|
|
$maxDepth = 10, |
167
|
|
|
$canonicalize = false, |
168
|
|
|
$ignoreCase = false |
169
|
|
|
) { |
170
|
|
|
return \PHPUnit_Framework_Assert::attribute( |
171
|
|
|
self::equalTo($value, $delta, $maxDepth, $canonicalize, $ignoreCase), |
172
|
|
|
$attributeName |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param mixed $value |
178
|
|
|
* @param bool $checkForObjectIdentity |
179
|
|
|
* @param bool $checkForNonObjectIdentity |
180
|
|
|
* |
181
|
|
|
* @return EquatableCollectionContains |
182
|
|
|
*/ |
183
|
|
|
public static function contains($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
184
|
|
|
{ |
185
|
|
|
return new EquatableCollectionContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.