Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PHPUnit_Framework_Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PHPUnit_Framework_Assert, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class PHPUnit_Framework_Assert |
||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private static $count = 0; |
||
22 | |||
23 | /** |
||
24 | * Asserts that an array has a specified key. |
||
25 | * |
||
26 | * @param mixed $key |
||
27 | * @param array|ArrayAccess $array |
||
28 | * @param string $message |
||
29 | * |
||
30 | * @since Method available since Release 3.0.0 |
||
31 | */ |
||
32 | public static function assertArrayHasKey($key, $array, $message = '') |
||
33 | { |
||
34 | if (!(is_integer($key) || is_string($key))) { |
||
35 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
36 | 1, |
||
37 | 'integer or string' |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | if (!(is_array($array) || $array instanceof ArrayAccess)) { |
||
42 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
43 | 2, |
||
44 | 'array or ArrayAccess' |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key); |
||
49 | |||
50 | self::assertThat($array, $constraint, $message); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Asserts that an array has a specified subset. |
||
55 | * |
||
56 | * @param array|ArrayAccess $subset |
||
57 | * @param array|ArrayAccess $array |
||
58 | * @param bool $strict Check for object identity |
||
59 | * @param string $message |
||
60 | * |
||
61 | * @since Method available since Release 4.4.0 |
||
62 | */ |
||
63 | public static function assertArraySubset($subset, $array, $strict = false, $message = '') |
||
64 | { |
||
65 | if (!is_array($subset)) { |
||
66 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
67 | 1, |
||
68 | 'array or ArrayAccess' |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | if (!is_array($array)) { |
||
73 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
74 | 2, |
||
75 | 'array or ArrayAccess' |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | $constraint = new PHPUnit_Framework_Constraint_ArraySubset($subset, $strict); |
||
80 | |||
81 | self::assertThat($array, $constraint, $message); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Asserts that an array does not have a specified key. |
||
86 | * |
||
87 | * @param mixed $key |
||
88 | * @param array|ArrayAccess $array |
||
89 | * @param string $message |
||
90 | * |
||
91 | * @since Method available since Release 3.0.0 |
||
92 | */ |
||
93 | public static function assertArrayNotHasKey($key, $array, $message = '') |
||
94 | { |
||
95 | if (!(is_integer($key) || is_string($key))) { |
||
96 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
97 | 1, |
||
98 | 'integer or string' |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | if (!(is_array($array) || $array instanceof ArrayAccess)) { |
||
103 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
104 | 2, |
||
105 | 'array or ArrayAccess' |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
110 | new PHPUnit_Framework_Constraint_ArrayHasKey($key) |
||
111 | ); |
||
112 | |||
113 | self::assertThat($array, $constraint, $message); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Asserts that a haystack contains a needle. |
||
118 | * |
||
119 | * @param mixed $needle |
||
120 | * @param mixed $haystack |
||
121 | * @param string $message |
||
122 | * @param bool $ignoreCase |
||
123 | * @param bool $checkForObjectIdentity |
||
124 | * @param bool $checkForNonObjectIdentity |
||
125 | * |
||
126 | * @since Method available since Release 2.1.0 |
||
127 | */ |
||
128 | public static function assertContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
||
129 | { |
||
130 | if (is_array($haystack) || |
||
131 | is_object($haystack) && $haystack instanceof Traversable) { |
||
132 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains( |
||
133 | $needle, |
||
134 | $checkForObjectIdentity, |
||
135 | $checkForNonObjectIdentity |
||
136 | ); |
||
137 | } elseif (is_string($haystack)) { |
||
138 | if (!is_string($needle)) { |
||
139 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
140 | 1, |
||
141 | 'string' |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $constraint = new PHPUnit_Framework_Constraint_StringContains( |
||
146 | $needle, |
||
147 | $ignoreCase |
||
148 | ); |
||
149 | } else { |
||
150 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
151 | 2, |
||
152 | 'array, traversable or string' |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | self::assertThat($haystack, $constraint, $message); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Asserts that a haystack that is stored in a static attribute of a class |
||
161 | * or an attribute of an object contains a needle. |
||
162 | * |
||
163 | * @param mixed $needle |
||
164 | * @param string $haystackAttributeName |
||
165 | * @param mixed $haystackClassOrObject |
||
166 | * @param string $message |
||
167 | * @param bool $ignoreCase |
||
168 | * @param bool $checkForObjectIdentity |
||
169 | * @param bool $checkForNonObjectIdentity |
||
170 | * |
||
171 | * @since Method available since Release 3.0.0 |
||
172 | */ |
||
173 | public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
||
174 | { |
||
175 | self::assertContains( |
||
176 | $needle, |
||
177 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
178 | $message, |
||
179 | $ignoreCase, |
||
180 | $checkForObjectIdentity, |
||
181 | $checkForNonObjectIdentity |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Asserts that a haystack does not contain a needle. |
||
187 | * |
||
188 | * @param mixed $needle |
||
189 | * @param mixed $haystack |
||
190 | * @param string $message |
||
191 | * @param bool $ignoreCase |
||
192 | * @param bool $checkForObjectIdentity |
||
193 | * @param bool $checkForNonObjectIdentity |
||
194 | * |
||
195 | * @since Method available since Release 2.1.0 |
||
196 | */ |
||
197 | public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
||
198 | { |
||
199 | if (is_array($haystack) || |
||
200 | is_object($haystack) && $haystack instanceof Traversable) { |
||
201 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
202 | new PHPUnit_Framework_Constraint_TraversableContains( |
||
203 | $needle, |
||
204 | $checkForObjectIdentity, |
||
205 | $checkForNonObjectIdentity |
||
206 | ) |
||
207 | ); |
||
208 | } elseif (is_string($haystack)) { |
||
209 | if (!is_string($needle)) { |
||
210 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
211 | 1, |
||
212 | 'string' |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
217 | new PHPUnit_Framework_Constraint_StringContains( |
||
218 | $needle, |
||
219 | $ignoreCase |
||
220 | ) |
||
221 | ); |
||
222 | } else { |
||
223 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
224 | 2, |
||
225 | 'array, traversable or string' |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | self::assertThat($haystack, $constraint, $message); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Asserts that a haystack that is stored in a static attribute of a class |
||
234 | * or an attribute of an object does not contain a needle. |
||
235 | * |
||
236 | * @param mixed $needle |
||
237 | * @param string $haystackAttributeName |
||
238 | * @param mixed $haystackClassOrObject |
||
239 | * @param string $message |
||
240 | * @param bool $ignoreCase |
||
241 | * @param bool $checkForObjectIdentity |
||
242 | * @param bool $checkForNonObjectIdentity |
||
243 | * |
||
244 | * @since Method available since Release 3.0.0 |
||
245 | */ |
||
246 | public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
||
247 | { |
||
248 | self::assertNotContains( |
||
249 | $needle, |
||
250 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
251 | $message, |
||
252 | $ignoreCase, |
||
253 | $checkForObjectIdentity, |
||
254 | $checkForNonObjectIdentity |
||
255 | ); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Asserts that a haystack contains only values of a given type. |
||
260 | * |
||
261 | * @param string $type |
||
262 | * @param mixed $haystack |
||
263 | * @param bool $isNativeType |
||
264 | * @param string $message |
||
265 | * |
||
266 | * @since Method available since Release 3.1.4 |
||
267 | */ |
||
268 | public static function assertContainsOnly($type, $haystack, $isNativeType = null, $message = '') |
||
269 | { |
||
270 | if (!(is_array($haystack) || |
||
271 | is_object($haystack) && $haystack instanceof Traversable)) { |
||
272 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
273 | 2, |
||
274 | 'array or traversable' |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | if ($isNativeType == null) { |
||
279 | $isNativeType = PHPUnit_Util_Type::isType($type); |
||
280 | } |
||
281 | |||
282 | self::assertThat( |
||
283 | $haystack, |
||
284 | new PHPUnit_Framework_Constraint_TraversableContainsOnly( |
||
285 | $type, |
||
286 | $isNativeType |
||
287 | ), |
||
288 | $message |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Asserts that a haystack contains only instances of a given classname |
||
294 | * |
||
295 | * @param string $classname |
||
296 | * @param array|Traversable $haystack |
||
297 | * @param string $message |
||
298 | */ |
||
299 | public static function assertContainsOnlyInstancesOf($classname, $haystack, $message = '') |
||
300 | { |
||
301 | if (!(is_array($haystack) || |
||
302 | is_object($haystack) && $haystack instanceof Traversable)) { |
||
303 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
304 | 2, |
||
305 | 'array or traversable' |
||
306 | ); |
||
307 | } |
||
308 | |||
309 | self::assertThat( |
||
310 | $haystack, |
||
311 | new PHPUnit_Framework_Constraint_TraversableContainsOnly( |
||
312 | $classname, |
||
313 | false |
||
314 | ), |
||
315 | $message |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Asserts that a haystack that is stored in a static attribute of a class |
||
321 | * or an attribute of an object contains only values of a given type. |
||
322 | * |
||
323 | * @param string $type |
||
324 | * @param string $haystackAttributeName |
||
325 | * @param mixed $haystackClassOrObject |
||
326 | * @param bool $isNativeType |
||
327 | * @param string $message |
||
328 | * |
||
329 | * @since Method available since Release 3.1.4 |
||
330 | */ |
||
331 | public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '') |
||
332 | { |
||
333 | self::assertContainsOnly( |
||
334 | $type, |
||
335 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
336 | $isNativeType, |
||
337 | $message |
||
338 | ); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Asserts that a haystack does not contain only values of a given type. |
||
343 | * |
||
344 | * @param string $type |
||
345 | * @param mixed $haystack |
||
346 | * @param bool $isNativeType |
||
347 | * @param string $message |
||
348 | * |
||
349 | * @since Method available since Release 3.1.4 |
||
350 | */ |
||
351 | public static function assertNotContainsOnly($type, $haystack, $isNativeType = null, $message = '') |
||
352 | { |
||
353 | if (!(is_array($haystack) || |
||
354 | is_object($haystack) && $haystack instanceof Traversable)) { |
||
355 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
356 | 2, |
||
357 | 'array or traversable' |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | if ($isNativeType == null) { |
||
362 | $isNativeType = PHPUnit_Util_Type::isType($type); |
||
363 | } |
||
364 | |||
365 | self::assertThat( |
||
366 | $haystack, |
||
367 | new PHPUnit_Framework_Constraint_Not( |
||
368 | new PHPUnit_Framework_Constraint_TraversableContainsOnly( |
||
369 | $type, |
||
370 | $isNativeType |
||
371 | ) |
||
372 | ), |
||
373 | $message |
||
374 | ); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Asserts that a haystack that is stored in a static attribute of a class |
||
379 | * or an attribute of an object does not contain only values of a given |
||
380 | * type. |
||
381 | * |
||
382 | * @param string $type |
||
383 | * @param string $haystackAttributeName |
||
384 | * @param mixed $haystackClassOrObject |
||
385 | * @param bool $isNativeType |
||
386 | * @param string $message |
||
387 | * |
||
388 | * @since Method available since Release 3.1.4 |
||
389 | */ |
||
390 | public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '') |
||
391 | { |
||
392 | self::assertNotContainsOnly( |
||
393 | $type, |
||
394 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
395 | $isNativeType, |
||
396 | $message |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Asserts the number of elements of an array, Countable or Traversable. |
||
402 | * |
||
403 | * @param int $expectedCount |
||
404 | * @param mixed $haystack |
||
405 | * @param string $message |
||
406 | */ |
||
407 | public static function assertCount($expectedCount, $haystack, $message = '') |
||
408 | { |
||
409 | if (!is_int($expectedCount)) { |
||
410 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer'); |
||
411 | } |
||
412 | |||
413 | if (!$haystack instanceof Countable && |
||
414 | !$haystack instanceof Traversable && |
||
415 | !is_array($haystack)) { |
||
416 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable'); |
||
417 | } |
||
418 | |||
419 | self::assertThat( |
||
420 | $haystack, |
||
421 | new PHPUnit_Framework_Constraint_Count($expectedCount), |
||
422 | $message |
||
423 | ); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Asserts the number of elements of an array, Countable or Traversable |
||
428 | * that is stored in an attribute. |
||
429 | * |
||
430 | * @param int $expectedCount |
||
431 | * @param string $haystackAttributeName |
||
432 | * @param mixed $haystackClassOrObject |
||
433 | * @param string $message |
||
434 | * |
||
435 | * @since Method available since Release 3.6.0 |
||
436 | */ |
||
437 | public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '') |
||
438 | { |
||
439 | self::assertCount( |
||
440 | $expectedCount, |
||
441 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
442 | $message |
||
443 | ); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Asserts the number of elements of an array, Countable or Traversable. |
||
448 | * |
||
449 | * @param int $expectedCount |
||
450 | * @param mixed $haystack |
||
451 | * @param string $message |
||
452 | */ |
||
453 | public static function assertNotCount($expectedCount, $haystack, $message = '') |
||
454 | { |
||
455 | if (!is_int($expectedCount)) { |
||
456 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer'); |
||
457 | } |
||
458 | |||
459 | if (!$haystack instanceof Countable && |
||
460 | !$haystack instanceof Traversable && |
||
461 | !is_array($haystack)) { |
||
462 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable'); |
||
463 | } |
||
464 | |||
465 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
466 | new PHPUnit_Framework_Constraint_Count($expectedCount) |
||
467 | ); |
||
468 | |||
469 | self::assertThat($haystack, $constraint, $message); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Asserts the number of elements of an array, Countable or Traversable |
||
474 | * that is stored in an attribute. |
||
475 | * |
||
476 | * @param int $expectedCount |
||
477 | * @param string $haystackAttributeName |
||
478 | * @param mixed $haystackClassOrObject |
||
479 | * @param string $message |
||
480 | * |
||
481 | * @since Method available since Release 3.6.0 |
||
482 | */ |
||
483 | public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '') |
||
484 | { |
||
485 | self::assertNotCount( |
||
486 | $expectedCount, |
||
487 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
488 | $message |
||
489 | ); |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Asserts that two variables are equal. |
||
494 | * |
||
495 | * @param mixed $expected |
||
496 | * @param mixed $actual |
||
497 | * @param string $message |
||
498 | * @param float $delta |
||
499 | * @param int $maxDepth |
||
500 | * @param bool $canonicalize |
||
501 | * @param bool $ignoreCase |
||
502 | */ |
||
503 | public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
504 | { |
||
505 | $constraint = new PHPUnit_Framework_Constraint_IsEqual( |
||
506 | $expected, |
||
507 | $delta, |
||
508 | $maxDepth, |
||
509 | $canonicalize, |
||
510 | $ignoreCase |
||
511 | ); |
||
512 | |||
513 | self::assertThat($actual, $constraint, $message); |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Asserts that a variable is equal to an attribute of an object. |
||
518 | * |
||
519 | * @param mixed $expected |
||
520 | * @param string $actualAttributeName |
||
521 | * @param string $actualClassOrObject |
||
522 | * @param string $message |
||
523 | * @param float $delta |
||
524 | * @param int $maxDepth |
||
525 | * @param bool $canonicalize |
||
526 | * @param bool $ignoreCase |
||
527 | */ |
||
528 | public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
529 | { |
||
530 | self::assertEquals( |
||
531 | $expected, |
||
532 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
533 | $message, |
||
534 | $delta, |
||
535 | $maxDepth, |
||
536 | $canonicalize, |
||
537 | $ignoreCase |
||
538 | ); |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Asserts that two variables are not equal. |
||
543 | * |
||
544 | * @param mixed $expected |
||
545 | * @param mixed $actual |
||
546 | * @param string $message |
||
547 | * @param float $delta |
||
548 | * @param int $maxDepth |
||
549 | * @param bool $canonicalize |
||
550 | * @param bool $ignoreCase |
||
551 | * |
||
552 | * @since Method available since Release 2.3.0 |
||
553 | */ |
||
554 | public static function assertNotEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
555 | { |
||
556 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
557 | new PHPUnit_Framework_Constraint_IsEqual( |
||
558 | $expected, |
||
559 | $delta, |
||
560 | $maxDepth, |
||
561 | $canonicalize, |
||
562 | $ignoreCase |
||
563 | ) |
||
564 | ); |
||
565 | |||
566 | self::assertThat($actual, $constraint, $message); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Asserts that a variable is not equal to an attribute of an object. |
||
571 | * |
||
572 | * @param mixed $expected |
||
573 | * @param string $actualAttributeName |
||
574 | * @param string $actualClassOrObject |
||
575 | * @param string $message |
||
576 | * @param float $delta |
||
577 | * @param int $maxDepth |
||
578 | * @param bool $canonicalize |
||
579 | * @param bool $ignoreCase |
||
580 | */ |
||
581 | public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
582 | { |
||
583 | self::assertNotEquals( |
||
584 | $expected, |
||
585 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
586 | $message, |
||
587 | $delta, |
||
588 | $maxDepth, |
||
589 | $canonicalize, |
||
590 | $ignoreCase |
||
591 | ); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Asserts that a variable is empty. |
||
596 | * |
||
597 | * @param mixed $actual |
||
598 | * @param string $message |
||
599 | * |
||
600 | * @throws PHPUnit_Framework_AssertionFailedError |
||
601 | */ |
||
602 | public static function assertEmpty($actual, $message = '') |
||
603 | { |
||
604 | self::assertThat($actual, self::isEmpty(), $message); |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Asserts that a static attribute of a class or an attribute of an object |
||
609 | * is empty. |
||
610 | * |
||
611 | * @param string $haystackAttributeName |
||
612 | * @param mixed $haystackClassOrObject |
||
613 | * @param string $message |
||
614 | * |
||
615 | * @since Method available since Release 3.5.0 |
||
616 | */ |
||
617 | public static function assertAttributeEmpty($haystackAttributeName, $haystackClassOrObject, $message = '') |
||
618 | { |
||
619 | self::assertEmpty( |
||
620 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
621 | $message |
||
622 | ); |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Asserts that a variable is not empty. |
||
627 | * |
||
628 | * @param mixed $actual |
||
629 | * @param string $message |
||
630 | * |
||
631 | * @throws PHPUnit_Framework_AssertionFailedError |
||
632 | */ |
||
633 | public static function assertNotEmpty($actual, $message = '') |
||
634 | { |
||
635 | self::assertThat($actual, self::logicalNot(self::isEmpty()), $message); |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * Asserts that a static attribute of a class or an attribute of an object |
||
640 | * is not empty. |
||
641 | * |
||
642 | * @param string $haystackAttributeName |
||
643 | * @param mixed $haystackClassOrObject |
||
644 | * @param string $message |
||
645 | * |
||
646 | * @since Method available since Release 3.5.0 |
||
647 | */ |
||
648 | public static function assertAttributeNotEmpty($haystackAttributeName, $haystackClassOrObject, $message = '') |
||
649 | { |
||
650 | self::assertNotEmpty( |
||
651 | self::readAttribute($haystackClassOrObject, $haystackAttributeName), |
||
652 | $message |
||
653 | ); |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Asserts that a value is greater than another value. |
||
658 | * |
||
659 | * @param mixed $expected |
||
660 | * @param mixed $actual |
||
661 | * @param string $message |
||
662 | * |
||
663 | * @since Method available since Release 3.1.0 |
||
664 | */ |
||
665 | public static function assertGreaterThan($expected, $actual, $message = '') |
||
666 | { |
||
667 | self::assertThat($actual, self::greaterThan($expected), $message); |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * Asserts that an attribute is greater than another value. |
||
672 | * |
||
673 | * @param mixed $expected |
||
674 | * @param string $actualAttributeName |
||
675 | * @param string $actualClassOrObject |
||
676 | * @param string $message |
||
677 | * |
||
678 | * @since Method available since Release 3.1.0 |
||
679 | */ |
||
680 | public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
681 | { |
||
682 | self::assertGreaterThan( |
||
683 | $expected, |
||
684 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
685 | $message |
||
686 | ); |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * Asserts that a value is greater than or equal to another value. |
||
691 | * |
||
692 | * @param mixed $expected |
||
693 | * @param mixed $actual |
||
694 | * @param string $message |
||
695 | * |
||
696 | * @since Method available since Release 3.1.0 |
||
697 | */ |
||
698 | public static function assertGreaterThanOrEqual($expected, $actual, $message = '') |
||
699 | { |
||
700 | self::assertThat( |
||
701 | $actual, |
||
702 | self::greaterThanOrEqual($expected), |
||
703 | $message |
||
704 | ); |
||
705 | } |
||
706 | |||
707 | /** |
||
708 | * Asserts that an attribute is greater than or equal to another value. |
||
709 | * |
||
710 | * @param mixed $expected |
||
711 | * @param string $actualAttributeName |
||
712 | * @param string $actualClassOrObject |
||
713 | * @param string $message |
||
714 | * |
||
715 | * @since Method available since Release 3.1.0 |
||
716 | */ |
||
717 | public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
718 | { |
||
719 | self::assertGreaterThanOrEqual( |
||
720 | $expected, |
||
721 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
722 | $message |
||
723 | ); |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Asserts that a value is smaller than another value. |
||
728 | * |
||
729 | * @param mixed $expected |
||
730 | * @param mixed $actual |
||
731 | * @param string $message |
||
732 | * |
||
733 | * @since Method available since Release 3.1.0 |
||
734 | */ |
||
735 | public static function assertLessThan($expected, $actual, $message = '') |
||
736 | { |
||
737 | self::assertThat($actual, self::lessThan($expected), $message); |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * Asserts that an attribute is smaller than another value. |
||
742 | * |
||
743 | * @param mixed $expected |
||
744 | * @param string $actualAttributeName |
||
745 | * @param string $actualClassOrObject |
||
746 | * @param string $message |
||
747 | * |
||
748 | * @since Method available since Release 3.1.0 |
||
749 | */ |
||
750 | public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
751 | { |
||
752 | self::assertLessThan( |
||
753 | $expected, |
||
754 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
755 | $message |
||
756 | ); |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Asserts that a value is smaller than or equal to another value. |
||
761 | * |
||
762 | * @param mixed $expected |
||
763 | * @param mixed $actual |
||
764 | * @param string $message |
||
765 | * |
||
766 | * @since Method available since Release 3.1.0 |
||
767 | */ |
||
768 | public static function assertLessThanOrEqual($expected, $actual, $message = '') |
||
769 | { |
||
770 | self::assertThat($actual, self::lessThanOrEqual($expected), $message); |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * Asserts that an attribute is smaller than or equal to another value. |
||
775 | * |
||
776 | * @param mixed $expected |
||
777 | * @param string $actualAttributeName |
||
778 | * @param string $actualClassOrObject |
||
779 | * @param string $message |
||
780 | * |
||
781 | * @since Method available since Release 3.1.0 |
||
782 | */ |
||
783 | public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
784 | { |
||
785 | self::assertLessThanOrEqual( |
||
786 | $expected, |
||
787 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
788 | $message |
||
789 | ); |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * Asserts that the contents of one file is equal to the contents of another |
||
794 | * file. |
||
795 | * |
||
796 | * @param string $expected |
||
797 | * @param string $actual |
||
798 | * @param string $message |
||
799 | * @param bool $canonicalize |
||
800 | * @param bool $ignoreCase |
||
801 | * |
||
802 | * @since Method available since Release 3.2.14 |
||
803 | */ |
||
804 | public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false) |
||
805 | { |
||
806 | self::assertFileExists($expected, $message); |
||
807 | self::assertFileExists($actual, $message); |
||
808 | |||
809 | self::assertEquals( |
||
810 | file_get_contents($expected), |
||
811 | file_get_contents($actual), |
||
812 | $message, |
||
813 | 0, |
||
814 | 10, |
||
815 | $canonicalize, |
||
816 | $ignoreCase |
||
817 | ); |
||
818 | } |
||
819 | |||
820 | /** |
||
821 | * Asserts that the contents of one file is not equal to the contents of |
||
822 | * another file. |
||
823 | * |
||
824 | * @param string $expected |
||
825 | * @param string $actual |
||
826 | * @param string $message |
||
827 | * @param bool $canonicalize |
||
828 | * @param bool $ignoreCase |
||
829 | * |
||
830 | * @since Method available since Release 3.2.14 |
||
831 | */ |
||
832 | public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false) |
||
833 | { |
||
834 | self::assertFileExists($expected, $message); |
||
835 | self::assertFileExists($actual, $message); |
||
836 | |||
837 | self::assertNotEquals( |
||
838 | file_get_contents($expected), |
||
839 | file_get_contents($actual), |
||
840 | $message, |
||
841 | 0, |
||
842 | 10, |
||
843 | $canonicalize, |
||
844 | $ignoreCase |
||
845 | ); |
||
846 | } |
||
847 | |||
848 | /** |
||
849 | * Asserts that the contents of a string is equal |
||
850 | * to the contents of a file. |
||
851 | * |
||
852 | * @param string $expectedFile |
||
853 | * @param string $actualString |
||
854 | * @param string $message |
||
855 | * @param bool $canonicalize |
||
856 | * @param bool $ignoreCase |
||
857 | * |
||
858 | * @since Method available since Release 3.3.0 |
||
859 | */ |
||
860 | public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false) |
||
861 | { |
||
862 | self::assertFileExists($expectedFile, $message); |
||
863 | |||
864 | self::assertEquals( |
||
865 | file_get_contents($expectedFile), |
||
866 | $actualString, |
||
867 | $message, |
||
868 | 0, |
||
869 | 10, |
||
870 | $canonicalize, |
||
871 | $ignoreCase |
||
872 | ); |
||
873 | } |
||
874 | |||
875 | /** |
||
876 | * Asserts that the contents of a string is not equal |
||
877 | * to the contents of a file. |
||
878 | * |
||
879 | * @param string $expectedFile |
||
880 | * @param string $actualString |
||
881 | * @param string $message |
||
882 | * @param bool $canonicalize |
||
883 | * @param bool $ignoreCase |
||
884 | * |
||
885 | * @since Method available since Release 3.3.0 |
||
886 | */ |
||
887 | public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false) |
||
888 | { |
||
889 | self::assertFileExists($expectedFile, $message); |
||
890 | |||
891 | self::assertNotEquals( |
||
892 | file_get_contents($expectedFile), |
||
893 | $actualString, |
||
894 | $message, |
||
895 | 0, |
||
896 | 10, |
||
897 | $canonicalize, |
||
898 | $ignoreCase |
||
899 | ); |
||
900 | } |
||
901 | |||
902 | /** |
||
903 | * Asserts that a file exists. |
||
904 | * |
||
905 | * @param string $filename |
||
906 | * @param string $message |
||
907 | * |
||
908 | * @since Method available since Release 3.0.0 |
||
909 | */ |
||
910 | public static function assertFileExists($filename, $message = '') |
||
911 | { |
||
912 | if (!is_string($filename)) { |
||
913 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
914 | } |
||
915 | |||
916 | $constraint = new PHPUnit_Framework_Constraint_FileExists; |
||
917 | |||
918 | self::assertThat($filename, $constraint, $message); |
||
919 | } |
||
920 | |||
921 | /** |
||
922 | * Asserts that a file does not exist. |
||
923 | * |
||
924 | * @param string $filename |
||
925 | * @param string $message |
||
926 | * |
||
927 | * @since Method available since Release 3.0.0 |
||
928 | */ |
||
929 | public static function assertFileNotExists($filename, $message = '') |
||
930 | { |
||
931 | if (!is_string($filename)) { |
||
932 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
933 | } |
||
934 | |||
935 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
936 | new PHPUnit_Framework_Constraint_FileExists |
||
937 | ); |
||
938 | |||
939 | self::assertThat($filename, $constraint, $message); |
||
940 | } |
||
941 | |||
942 | /** |
||
943 | * Asserts that a condition is true. |
||
944 | * |
||
945 | * @param bool $condition |
||
946 | * @param string $message |
||
947 | * |
||
948 | * @throws PHPUnit_Framework_AssertionFailedError |
||
949 | */ |
||
950 | public static function assertTrue($condition, $message = '') |
||
951 | { |
||
952 | self::assertThat($condition, self::isTrue(), $message); |
||
953 | } |
||
954 | |||
955 | /** |
||
956 | * Asserts that a condition is not true. |
||
957 | * |
||
958 | * @param bool $condition |
||
959 | * @param string $message |
||
960 | * |
||
961 | * @throws PHPUnit_Framework_AssertionFailedError |
||
962 | */ |
||
963 | public static function assertNotTrue($condition, $message = '') |
||
964 | { |
||
965 | self::assertThat($condition, self::logicalNot(self::isTrue()), $message); |
||
966 | } |
||
967 | |||
968 | /** |
||
969 | * Asserts that a condition is false. |
||
970 | * |
||
971 | * @param bool $condition |
||
972 | * @param string $message |
||
973 | * |
||
974 | * @throws PHPUnit_Framework_AssertionFailedError |
||
975 | */ |
||
976 | public static function assertFalse($condition, $message = '') |
||
977 | { |
||
978 | self::assertThat($condition, self::isFalse(), $message); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * Asserts that a condition is not false. |
||
983 | * |
||
984 | * @param bool $condition |
||
985 | * @param string $message |
||
986 | * |
||
987 | * @throws PHPUnit_Framework_AssertionFailedError |
||
988 | */ |
||
989 | public static function assertNotFalse($condition, $message = '') |
||
990 | { |
||
991 | self::assertThat($condition, self::logicalNot(self::isFalse()), $message); |
||
992 | } |
||
993 | |||
994 | /** |
||
995 | * Asserts that a variable is not null. |
||
996 | * |
||
997 | * @param mixed $actual |
||
998 | * @param string $message |
||
999 | */ |
||
1000 | public static function assertNotNull($actual, $message = '') |
||
1001 | { |
||
1002 | self::assertThat($actual, self::logicalNot(self::isNull()), $message); |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * Asserts that a variable is null. |
||
1007 | * |
||
1008 | * @param mixed $actual |
||
1009 | * @param string $message |
||
1010 | */ |
||
1011 | public static function assertNull($actual, $message = '') |
||
1012 | { |
||
1013 | self::assertThat($actual, self::isNull(), $message); |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * Asserts that a class has a specified attribute. |
||
1018 | * |
||
1019 | * @param string $attributeName |
||
1020 | * @param string $className |
||
1021 | * @param string $message |
||
1022 | * |
||
1023 | * @since Method available since Release 3.1.0 |
||
1024 | */ |
||
1025 | public static function assertClassHasAttribute($attributeName, $className, $message = '') |
||
1026 | { |
||
1027 | if (!is_string($attributeName)) { |
||
1028 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1029 | } |
||
1030 | |||
1031 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1032 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1033 | } |
||
1034 | |||
1035 | if (!is_string($className) || !class_exists($className)) { |
||
1036 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className); |
||
1037 | } |
||
1038 | |||
1039 | $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute( |
||
1040 | $attributeName |
||
1041 | ); |
||
1042 | |||
1043 | self::assertThat($className, $constraint, $message); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * Asserts that a class does not have a specified attribute. |
||
1048 | * |
||
1049 | * @param string $attributeName |
||
1050 | * @param string $className |
||
1051 | * @param string $message |
||
1052 | * |
||
1053 | * @since Method available since Release 3.1.0 |
||
1054 | */ |
||
1055 | public static function assertClassNotHasAttribute($attributeName, $className, $message = '') |
||
1056 | { |
||
1057 | if (!is_string($attributeName)) { |
||
1058 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1059 | } |
||
1060 | |||
1061 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1062 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1063 | } |
||
1064 | |||
1065 | if (!is_string($className) || !class_exists($className)) { |
||
1066 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className); |
||
1067 | } |
||
1068 | |||
1069 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1070 | new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName) |
||
1071 | ); |
||
1072 | |||
1073 | self::assertThat($className, $constraint, $message); |
||
1074 | } |
||
1075 | |||
1076 | /** |
||
1077 | * Asserts that a class has a specified static attribute. |
||
1078 | * |
||
1079 | * @param string $attributeName |
||
1080 | * @param string $className |
||
1081 | * @param string $message |
||
1082 | * |
||
1083 | * @since Method available since Release 3.1.0 |
||
1084 | */ |
||
1085 | public static function assertClassHasStaticAttribute($attributeName, $className, $message = '') |
||
1086 | { |
||
1087 | if (!is_string($attributeName)) { |
||
1088 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1089 | } |
||
1090 | |||
1091 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1092 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1093 | } |
||
1094 | |||
1095 | if (!is_string($className) || !class_exists($className)) { |
||
1096 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className); |
||
1097 | } |
||
1098 | |||
1099 | $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute( |
||
1100 | $attributeName |
||
1101 | ); |
||
1102 | |||
1103 | self::assertThat($className, $constraint, $message); |
||
1104 | } |
||
1105 | |||
1106 | /** |
||
1107 | * Asserts that a class does not have a specified static attribute. |
||
1108 | * |
||
1109 | * @param string $attributeName |
||
1110 | * @param string $className |
||
1111 | * @param string $message |
||
1112 | * |
||
1113 | * @since Method available since Release 3.1.0 |
||
1114 | */ |
||
1115 | public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '') |
||
1116 | { |
||
1117 | if (!is_string($attributeName)) { |
||
1118 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1119 | } |
||
1120 | |||
1121 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1122 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1123 | } |
||
1124 | |||
1125 | if (!is_string($className) || !class_exists($className)) { |
||
1126 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className); |
||
1127 | } |
||
1128 | |||
1129 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1130 | new PHPUnit_Framework_Constraint_ClassHasStaticAttribute( |
||
1131 | $attributeName |
||
1132 | ) |
||
1133 | ); |
||
1134 | |||
1135 | self::assertThat($className, $constraint, $message); |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * Asserts that an object has a specified attribute. |
||
1140 | * |
||
1141 | * @param string $attributeName |
||
1142 | * @param object $object |
||
1143 | * @param string $message |
||
1144 | * |
||
1145 | * @since Method available since Release 3.0.0 |
||
1146 | */ |
||
1147 | public static function assertObjectHasAttribute($attributeName, $object, $message = '') |
||
1148 | { |
||
1149 | if (!is_string($attributeName)) { |
||
1150 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1151 | } |
||
1152 | |||
1153 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1154 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1155 | } |
||
1156 | |||
1157 | if (!is_object($object)) { |
||
1158 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object'); |
||
1159 | } |
||
1160 | |||
1161 | $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute( |
||
1162 | $attributeName |
||
1163 | ); |
||
1164 | |||
1165 | self::assertThat($object, $constraint, $message); |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * Asserts that an object does not have a specified attribute. |
||
1170 | * |
||
1171 | * @param string $attributeName |
||
1172 | * @param object $object |
||
1173 | * @param string $message |
||
1174 | * |
||
1175 | * @since Method available since Release 3.0.0 |
||
1176 | */ |
||
1177 | public static function assertObjectNotHasAttribute($attributeName, $object, $message = '') |
||
1178 | { |
||
1179 | if (!is_string($attributeName)) { |
||
1180 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1181 | } |
||
1182 | |||
1183 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
1184 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name'); |
||
1185 | } |
||
1186 | |||
1187 | if (!is_object($object)) { |
||
1188 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object'); |
||
1189 | } |
||
1190 | |||
1191 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1192 | new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName) |
||
1193 | ); |
||
1194 | |||
1195 | self::assertThat($object, $constraint, $message); |
||
1196 | } |
||
1197 | |||
1198 | /** |
||
1199 | * Asserts that two variables have the same type and value. |
||
1200 | * Used on objects, it asserts that two variables reference |
||
1201 | * the same object. |
||
1202 | * |
||
1203 | * @param mixed $expected |
||
1204 | * @param mixed $actual |
||
1205 | * @param string $message |
||
1206 | */ |
||
1207 | public static function assertSame($expected, $actual, $message = '') |
||
1208 | { |
||
1209 | if (is_bool($expected) && is_bool($actual)) { |
||
1210 | self::assertEquals($expected, $actual, $message); |
||
1211 | } else { |
||
1212 | $constraint = new PHPUnit_Framework_Constraint_IsIdentical( |
||
1213 | $expected |
||
1214 | ); |
||
1215 | |||
1216 | self::assertThat($actual, $constraint, $message); |
||
1217 | } |
||
1218 | } |
||
1219 | |||
1220 | /** |
||
1221 | * Asserts that a variable and an attribute of an object have the same type |
||
1222 | * and value. |
||
1223 | * |
||
1224 | * @param mixed $expected |
||
1225 | * @param string $actualAttributeName |
||
1226 | * @param object $actualClassOrObject |
||
1227 | * @param string $message |
||
1228 | */ |
||
1229 | public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
1230 | { |
||
1231 | self::assertSame( |
||
1232 | $expected, |
||
1233 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
1234 | $message |
||
1235 | ); |
||
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * Asserts that two variables do not have the same type and value. |
||
1240 | * Used on objects, it asserts that two variables do not reference |
||
1241 | * the same object. |
||
1242 | * |
||
1243 | * @param mixed $expected |
||
1244 | * @param mixed $actual |
||
1245 | * @param string $message |
||
1246 | */ |
||
1247 | public static function assertNotSame($expected, $actual, $message = '') |
||
1248 | { |
||
1249 | if (is_bool($expected) && is_bool($actual)) { |
||
1250 | self::assertNotEquals($expected, $actual, $message); |
||
1251 | } else { |
||
1252 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1253 | new PHPUnit_Framework_Constraint_IsIdentical($expected) |
||
1254 | ); |
||
1255 | |||
1256 | self::assertThat($actual, $constraint, $message); |
||
1257 | } |
||
1258 | } |
||
1259 | |||
1260 | /** |
||
1261 | * Asserts that a variable and an attribute of an object do not have the |
||
1262 | * same type and value. |
||
1263 | * |
||
1264 | * @param mixed $expected |
||
1265 | * @param string $actualAttributeName |
||
1266 | * @param object $actualClassOrObject |
||
1267 | * @param string $message |
||
1268 | */ |
||
1269 | public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '') |
||
1270 | { |
||
1271 | self::assertNotSame( |
||
1272 | $expected, |
||
1273 | self::readAttribute($actualClassOrObject, $actualAttributeName), |
||
1274 | $message |
||
1275 | ); |
||
1276 | } |
||
1277 | |||
1278 | /** |
||
1279 | * Asserts that a variable is of a given type. |
||
1280 | * |
||
1281 | * @param string $expected |
||
1282 | * @param mixed $actual |
||
1283 | * @param string $message |
||
1284 | * |
||
1285 | * @since Method available since Release 3.5.0 |
||
1286 | */ |
||
1287 | public static function assertInstanceOf($expected, $actual, $message = '') |
||
1288 | { |
||
1289 | if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) { |
||
1290 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name'); |
||
1291 | } |
||
1292 | |||
1293 | $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf( |
||
1294 | $expected |
||
1295 | ); |
||
1296 | |||
1297 | self::assertThat($actual, $constraint, $message); |
||
1298 | } |
||
1299 | |||
1300 | /** |
||
1301 | * Asserts that an attribute is of a given type. |
||
1302 | * |
||
1303 | * @param string $expected |
||
1304 | * @param string $attributeName |
||
1305 | * @param mixed $classOrObject |
||
1306 | * @param string $message |
||
1307 | * |
||
1308 | * @since Method available since Release 3.5.0 |
||
1309 | */ |
||
1310 | public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '') |
||
1311 | { |
||
1312 | self::assertInstanceOf( |
||
1313 | $expected, |
||
1314 | self::readAttribute($classOrObject, $attributeName), |
||
1315 | $message |
||
1316 | ); |
||
1317 | } |
||
1318 | |||
1319 | /** |
||
1320 | * Asserts that a variable is not of a given type. |
||
1321 | * |
||
1322 | * @param string $expected |
||
1323 | * @param mixed $actual |
||
1324 | * @param string $message |
||
1325 | * |
||
1326 | * @since Method available since Release 3.5.0 |
||
1327 | */ |
||
1328 | public static function assertNotInstanceOf($expected, $actual, $message = '') |
||
1329 | { |
||
1330 | if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) { |
||
1331 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name'); |
||
1332 | } |
||
1333 | |||
1334 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1335 | new PHPUnit_Framework_Constraint_IsInstanceOf($expected) |
||
1336 | ); |
||
1337 | |||
1338 | self::assertThat($actual, $constraint, $message); |
||
1339 | } |
||
1340 | |||
1341 | /** |
||
1342 | * Asserts that an attribute is of a given type. |
||
1343 | * |
||
1344 | * @param string $expected |
||
1345 | * @param string $attributeName |
||
1346 | * @param mixed $classOrObject |
||
1347 | * @param string $message |
||
1348 | * |
||
1349 | * @since Method available since Release 3.5.0 |
||
1350 | */ |
||
1351 | public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '') |
||
1352 | { |
||
1353 | self::assertNotInstanceOf( |
||
1354 | $expected, |
||
1355 | self::readAttribute($classOrObject, $attributeName), |
||
1356 | $message |
||
1357 | ); |
||
1358 | } |
||
1359 | |||
1360 | /** |
||
1361 | * Asserts that a variable is of a given type. |
||
1362 | * |
||
1363 | * @param string $expected |
||
1364 | * @param mixed $actual |
||
1365 | * @param string $message |
||
1366 | * |
||
1367 | * @since Method available since Release 3.5.0 |
||
1368 | */ |
||
1369 | public static function assertInternalType($expected, $actual, $message = '') |
||
1370 | { |
||
1371 | if (!is_string($expected)) { |
||
1372 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1373 | } |
||
1374 | |||
1375 | $constraint = new PHPUnit_Framework_Constraint_IsType( |
||
1376 | $expected |
||
1377 | ); |
||
1378 | |||
1379 | self::assertThat($actual, $constraint, $message); |
||
1380 | } |
||
1381 | |||
1382 | /** |
||
1383 | * Asserts that an attribute is of a given type. |
||
1384 | * |
||
1385 | * @param string $expected |
||
1386 | * @param string $attributeName |
||
1387 | * @param mixed $classOrObject |
||
1388 | * @param string $message |
||
1389 | * |
||
1390 | * @since Method available since Release 3.5.0 |
||
1391 | */ |
||
1392 | public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '') |
||
1393 | { |
||
1394 | self::assertInternalType( |
||
1395 | $expected, |
||
1396 | self::readAttribute($classOrObject, $attributeName), |
||
1397 | $message |
||
1398 | ); |
||
1399 | } |
||
1400 | |||
1401 | /** |
||
1402 | * Asserts that a variable is not of a given type. |
||
1403 | * |
||
1404 | * @param string $expected |
||
1405 | * @param mixed $actual |
||
1406 | * @param string $message |
||
1407 | * |
||
1408 | * @since Method available since Release 3.5.0 |
||
1409 | */ |
||
1410 | public static function assertNotInternalType($expected, $actual, $message = '') |
||
1411 | { |
||
1412 | if (!is_string($expected)) { |
||
1413 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1414 | } |
||
1415 | |||
1416 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1417 | new PHPUnit_Framework_Constraint_IsType($expected) |
||
1418 | ); |
||
1419 | |||
1420 | self::assertThat($actual, $constraint, $message); |
||
1421 | } |
||
1422 | |||
1423 | /** |
||
1424 | * Asserts that an attribute is of a given type. |
||
1425 | * |
||
1426 | * @param string $expected |
||
1427 | * @param string $attributeName |
||
1428 | * @param mixed $classOrObject |
||
1429 | * @param string $message |
||
1430 | * |
||
1431 | * @since Method available since Release 3.5.0 |
||
1432 | */ |
||
1433 | public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '') |
||
1434 | { |
||
1435 | self::assertNotInternalType( |
||
1436 | $expected, |
||
1437 | self::readAttribute($classOrObject, $attributeName), |
||
1438 | $message |
||
1439 | ); |
||
1440 | } |
||
1441 | |||
1442 | /** |
||
1443 | * Asserts that a string matches a given regular expression. |
||
1444 | * |
||
1445 | * @param string $pattern |
||
1446 | * @param string $string |
||
1447 | * @param string $message |
||
1448 | */ |
||
1449 | public static function assertRegExp($pattern, $string, $message = '') |
||
1450 | { |
||
1451 | if (!is_string($pattern)) { |
||
1452 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1453 | } |
||
1454 | |||
1455 | if (!is_string($string)) { |
||
1456 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1457 | } |
||
1458 | |||
1459 | $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern); |
||
1460 | |||
1461 | self::assertThat($string, $constraint, $message); |
||
1462 | } |
||
1463 | |||
1464 | /** |
||
1465 | * Asserts that a string does not match a given regular expression. |
||
1466 | * |
||
1467 | * @param string $pattern |
||
1468 | * @param string $string |
||
1469 | * @param string $message |
||
1470 | * |
||
1471 | * @since Method available since Release 2.1.0 |
||
1472 | */ |
||
1473 | public static function assertNotRegExp($pattern, $string, $message = '') |
||
1474 | { |
||
1475 | if (!is_string($pattern)) { |
||
1476 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1477 | } |
||
1478 | |||
1479 | if (!is_string($string)) { |
||
1480 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1481 | } |
||
1482 | |||
1483 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1484 | new PHPUnit_Framework_Constraint_PCREMatch($pattern) |
||
1485 | ); |
||
1486 | |||
1487 | self::assertThat($string, $constraint, $message); |
||
1488 | } |
||
1489 | |||
1490 | /** |
||
1491 | * Assert that the size of two arrays (or `Countable` or `Traversable` objects) |
||
1492 | * is the same. |
||
1493 | * |
||
1494 | * @param array|Countable|Traversable $expected |
||
1495 | * @param array|Countable|Traversable $actual |
||
1496 | * @param string $message |
||
1497 | */ |
||
1498 | public static function assertSameSize($expected, $actual, $message = '') |
||
1499 | { |
||
1500 | if (!$expected instanceof Countable && |
||
1501 | !$expected instanceof Traversable && |
||
1502 | !is_array($expected)) { |
||
1503 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable'); |
||
1504 | } |
||
1505 | |||
1506 | if (!$actual instanceof Countable && |
||
1507 | !$actual instanceof Traversable && |
||
1508 | !is_array($actual)) { |
||
1509 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable'); |
||
1510 | } |
||
1511 | |||
1512 | self::assertThat( |
||
1513 | $actual, |
||
1514 | new PHPUnit_Framework_Constraint_SameSize($expected), |
||
1515 | $message |
||
1516 | ); |
||
1517 | } |
||
1518 | |||
1519 | /** |
||
1520 | * Assert that the size of two arrays (or `Countable` or `Traversable` objects) |
||
1521 | * is not the same. |
||
1522 | * |
||
1523 | * @param array|Countable|Traversable $expected |
||
1524 | * @param array|Countable|Traversable $actual |
||
1525 | * @param string $message |
||
1526 | */ |
||
1527 | public static function assertNotSameSize($expected, $actual, $message = '') |
||
1528 | { |
||
1529 | if (!$expected instanceof Countable && |
||
1530 | !$expected instanceof Traversable && |
||
1531 | !is_array($expected)) { |
||
1532 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable'); |
||
1533 | } |
||
1534 | |||
1535 | if (!$actual instanceof Countable && |
||
1536 | !$actual instanceof Traversable && |
||
1537 | !is_array($actual)) { |
||
1538 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable'); |
||
1539 | } |
||
1540 | |||
1541 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1542 | new PHPUnit_Framework_Constraint_SameSize($expected) |
||
1543 | ); |
||
1544 | |||
1545 | self::assertThat($actual, $constraint, $message); |
||
1546 | } |
||
1547 | |||
1548 | /** |
||
1549 | * Asserts that a string matches a given format string. |
||
1550 | * |
||
1551 | * @param string $format |
||
1552 | * @param string $string |
||
1553 | * @param string $message |
||
1554 | * |
||
1555 | * @since Method available since Release 3.5.0 |
||
1556 | */ |
||
1557 | public static function assertStringMatchesFormat($format, $string, $message = '') |
||
1558 | { |
||
1559 | if (!is_string($format)) { |
||
1560 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1561 | } |
||
1562 | |||
1563 | if (!is_string($string)) { |
||
1564 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1565 | } |
||
1566 | |||
1567 | $constraint = new PHPUnit_Framework_Constraint_StringMatches($format); |
||
1568 | |||
1569 | self::assertThat($string, $constraint, $message); |
||
1570 | } |
||
1571 | |||
1572 | /** |
||
1573 | * Asserts that a string does not match a given format string. |
||
1574 | * |
||
1575 | * @param string $format |
||
1576 | * @param string $string |
||
1577 | * @param string $message |
||
1578 | * |
||
1579 | * @since Method available since Release 3.5.0 |
||
1580 | */ |
||
1581 | public static function assertStringNotMatchesFormat($format, $string, $message = '') |
||
1582 | { |
||
1583 | if (!is_string($format)) { |
||
1584 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1585 | } |
||
1586 | |||
1587 | if (!is_string($string)) { |
||
1588 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1589 | } |
||
1590 | |||
1591 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1592 | new PHPUnit_Framework_Constraint_StringMatches($format) |
||
1593 | ); |
||
1594 | |||
1595 | self::assertThat($string, $constraint, $message); |
||
1596 | } |
||
1597 | |||
1598 | /** |
||
1599 | * Asserts that a string matches a given format file. |
||
1600 | * |
||
1601 | * @param string $formatFile |
||
1602 | * @param string $string |
||
1603 | * @param string $message |
||
1604 | * |
||
1605 | * @since Method available since Release 3.5.0 |
||
1606 | */ |
||
1607 | public static function assertStringMatchesFormatFile($formatFile, $string, $message = '') |
||
1608 | { |
||
1609 | self::assertFileExists($formatFile, $message); |
||
1610 | |||
1611 | if (!is_string($string)) { |
||
1612 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1613 | } |
||
1614 | |||
1615 | $constraint = new PHPUnit_Framework_Constraint_StringMatches( |
||
1616 | file_get_contents($formatFile) |
||
1617 | ); |
||
1618 | |||
1619 | self::assertThat($string, $constraint, $message); |
||
1620 | } |
||
1621 | |||
1622 | /** |
||
1623 | * Asserts that a string does not match a given format string. |
||
1624 | * |
||
1625 | * @param string $formatFile |
||
1626 | * @param string $string |
||
1627 | * @param string $message |
||
1628 | * |
||
1629 | * @since Method available since Release 3.5.0 |
||
1630 | */ |
||
1631 | public static function assertStringNotMatchesFormatFile($formatFile, $string, $message = '') |
||
1632 | { |
||
1633 | self::assertFileExists($formatFile, $message); |
||
1634 | |||
1635 | if (!is_string($string)) { |
||
1636 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1637 | } |
||
1638 | |||
1639 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1640 | new PHPUnit_Framework_Constraint_StringMatches( |
||
1641 | file_get_contents($formatFile) |
||
1642 | ) |
||
1643 | ); |
||
1644 | |||
1645 | self::assertThat($string, $constraint, $message); |
||
1646 | } |
||
1647 | |||
1648 | /** |
||
1649 | * Asserts that a string starts with a given prefix. |
||
1650 | * |
||
1651 | * @param string $prefix |
||
1652 | * @param string $string |
||
1653 | * @param string $message |
||
1654 | * |
||
1655 | * @since Method available since Release 3.4.0 |
||
1656 | */ |
||
1657 | public static function assertStringStartsWith($prefix, $string, $message = '') |
||
1658 | { |
||
1659 | if (!is_string($prefix)) { |
||
1660 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1661 | } |
||
1662 | |||
1663 | if (!is_string($string)) { |
||
1664 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1665 | } |
||
1666 | |||
1667 | $constraint = new PHPUnit_Framework_Constraint_StringStartsWith( |
||
1668 | $prefix |
||
1669 | ); |
||
1670 | |||
1671 | self::assertThat($string, $constraint, $message); |
||
1672 | } |
||
1673 | |||
1674 | /** |
||
1675 | * Asserts that a string starts not with a given prefix. |
||
1676 | * |
||
1677 | * @param string $prefix |
||
1678 | * @param string $string |
||
1679 | * @param string $message |
||
1680 | * |
||
1681 | * @since Method available since Release 3.4.0 |
||
1682 | */ |
||
1683 | public static function assertStringStartsNotWith($prefix, $string, $message = '') |
||
1684 | { |
||
1685 | if (!is_string($prefix)) { |
||
1686 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1687 | } |
||
1688 | |||
1689 | if (!is_string($string)) { |
||
1690 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1691 | } |
||
1692 | |||
1693 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1694 | new PHPUnit_Framework_Constraint_StringStartsWith($prefix) |
||
1695 | ); |
||
1696 | |||
1697 | self::assertThat($string, $constraint, $message); |
||
1698 | } |
||
1699 | |||
1700 | /** |
||
1701 | * Asserts that a string ends with a given suffix. |
||
1702 | * |
||
1703 | * @param string $suffix |
||
1704 | * @param string $string |
||
1705 | * @param string $message |
||
1706 | * |
||
1707 | * @since Method available since Release 3.4.0 |
||
1708 | */ |
||
1709 | public static function assertStringEndsWith($suffix, $string, $message = '') |
||
1710 | { |
||
1711 | if (!is_string($suffix)) { |
||
1712 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1713 | } |
||
1714 | |||
1715 | if (!is_string($string)) { |
||
1716 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1717 | } |
||
1718 | |||
1719 | $constraint = new PHPUnit_Framework_Constraint_StringEndsWith($suffix); |
||
1720 | |||
1721 | self::assertThat($string, $constraint, $message); |
||
1722 | } |
||
1723 | |||
1724 | /** |
||
1725 | * Asserts that a string ends not with a given suffix. |
||
1726 | * |
||
1727 | * @param string $suffix |
||
1728 | * @param string $string |
||
1729 | * @param string $message |
||
1730 | * |
||
1731 | * @since Method available since Release 3.4.0 |
||
1732 | */ |
||
1733 | public static function assertStringEndsNotWith($suffix, $string, $message = '') |
||
1734 | { |
||
1735 | if (!is_string($suffix)) { |
||
1736 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
1737 | } |
||
1738 | |||
1739 | if (!is_string($string)) { |
||
1740 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
1741 | } |
||
1742 | |||
1743 | $constraint = new PHPUnit_Framework_Constraint_Not( |
||
1744 | new PHPUnit_Framework_Constraint_StringEndsWith($suffix) |
||
1745 | ); |
||
1746 | |||
1747 | self::assertThat($string, $constraint, $message); |
||
1748 | } |
||
1749 | |||
1750 | /** |
||
1751 | * Asserts that two XML files are equal. |
||
1752 | * |
||
1753 | * @param string $expectedFile |
||
1754 | * @param string $actualFile |
||
1755 | * @param string $message |
||
1756 | * |
||
1757 | * @since Method available since Release 3.1.0 |
||
1758 | */ |
||
1759 | public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '') |
||
1760 | { |
||
1761 | $expected = PHPUnit_Util_XML::loadFile($expectedFile); |
||
1762 | $actual = PHPUnit_Util_XML::loadFile($actualFile); |
||
1763 | |||
1764 | self::assertEquals($expected, $actual, $message); |
||
1765 | } |
||
1766 | |||
1767 | /** |
||
1768 | * Asserts that two XML files are not equal. |
||
1769 | * |
||
1770 | * @param string $expectedFile |
||
1771 | * @param string $actualFile |
||
1772 | * @param string $message |
||
1773 | * |
||
1774 | * @since Method available since Release 3.1.0 |
||
1775 | */ |
||
1776 | public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '') |
||
1777 | { |
||
1778 | $expected = PHPUnit_Util_XML::loadFile($expectedFile); |
||
1779 | $actual = PHPUnit_Util_XML::loadFile($actualFile); |
||
1780 | |||
1781 | self::assertNotEquals($expected, $actual, $message); |
||
1782 | } |
||
1783 | |||
1784 | /** |
||
1785 | * Asserts that two XML documents are equal. |
||
1786 | * |
||
1787 | * @param string $expectedFile |
||
1788 | * @param string $actualXml |
||
1789 | * @param string $message |
||
1790 | * |
||
1791 | * @since Method available since Release 3.3.0 |
||
1792 | */ |
||
1793 | public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '') |
||
1794 | { |
||
1795 | $expected = PHPUnit_Util_XML::loadFile($expectedFile); |
||
1796 | $actual = PHPUnit_Util_XML::load($actualXml); |
||
1797 | |||
1798 | self::assertEquals($expected, $actual, $message); |
||
1799 | } |
||
1800 | |||
1801 | /** |
||
1802 | * Asserts that two XML documents are not equal. |
||
1803 | * |
||
1804 | * @param string $expectedFile |
||
1805 | * @param string $actualXml |
||
1806 | * @param string $message |
||
1807 | * |
||
1808 | * @since Method available since Release 3.3.0 |
||
1809 | */ |
||
1810 | public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '') |
||
1811 | { |
||
1812 | $expected = PHPUnit_Util_XML::loadFile($expectedFile); |
||
1813 | $actual = PHPUnit_Util_XML::load($actualXml); |
||
1814 | |||
1815 | self::assertNotEquals($expected, $actual, $message); |
||
1816 | } |
||
1817 | |||
1818 | /** |
||
1819 | * Asserts that two XML documents are equal. |
||
1820 | * |
||
1821 | * @param string $expectedXml |
||
1822 | * @param string $actualXml |
||
1823 | * @param string $message |
||
1824 | * |
||
1825 | * @since Method available since Release 3.1.0 |
||
1826 | */ |
||
1827 | public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '') |
||
1828 | { |
||
1829 | $expected = PHPUnit_Util_XML::load($expectedXml); |
||
1830 | $actual = PHPUnit_Util_XML::load($actualXml); |
||
1831 | |||
1832 | self::assertEquals($expected, $actual, $message); |
||
1833 | } |
||
1834 | |||
1835 | /** |
||
1836 | * Asserts that two XML documents are not equal. |
||
1837 | * |
||
1838 | * @param string $expectedXml |
||
1839 | * @param string $actualXml |
||
1840 | * @param string $message |
||
1841 | * |
||
1842 | * @since Method available since Release 3.1.0 |
||
1843 | */ |
||
1844 | public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '') |
||
1845 | { |
||
1846 | $expected = PHPUnit_Util_XML::load($expectedXml); |
||
1847 | $actual = PHPUnit_Util_XML::load($actualXml); |
||
1848 | |||
1849 | self::assertNotEquals($expected, $actual, $message); |
||
1850 | } |
||
1851 | |||
1852 | /** |
||
1853 | * Asserts that a hierarchy of DOMElements matches. |
||
1854 | * |
||
1855 | * @param DOMElement $expectedElement |
||
1856 | * @param DOMElement $actualElement |
||
1857 | * @param bool $checkAttributes |
||
1858 | * @param string $message |
||
1859 | * |
||
1860 | * @since Method available since Release 3.3.0 |
||
1861 | */ |
||
1862 | public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, $checkAttributes = false, $message = '') |
||
1863 | { |
||
1864 | $tmp = new DOMDocument; |
||
1865 | $expectedElement = $tmp->importNode($expectedElement, true); |
||
1866 | |||
1867 | $tmp = new DOMDocument; |
||
1868 | $actualElement = $tmp->importNode($actualElement, true); |
||
1869 | |||
1870 | unset($tmp); |
||
1871 | |||
1872 | self::assertEquals( |
||
1873 | $expectedElement->tagName, |
||
1874 | $actualElement->tagName, |
||
1875 | $message |
||
1876 | ); |
||
1877 | |||
1878 | if ($checkAttributes) { |
||
1879 | self::assertEquals( |
||
1880 | $expectedElement->attributes->length, |
||
1881 | $actualElement->attributes->length, |
||
1882 | sprintf( |
||
1883 | '%s%sNumber of attributes on node "%s" does not match', |
||
1884 | $message, |
||
1885 | !empty($message) ? "\n" : '', |
||
1886 | $expectedElement->tagName |
||
1887 | ) |
||
1888 | ); |
||
1889 | |||
1890 | for ($i = 0; $i < $expectedElement->attributes->length; $i++) { |
||
1891 | $expectedAttribute = $expectedElement->attributes->item($i); |
||
1892 | $actualAttribute = $actualElement->attributes->getNamedItem( |
||
1893 | $expectedAttribute->name |
||
1894 | ); |
||
1895 | |||
1896 | if (!$actualAttribute) { |
||
1897 | self::fail( |
||
1898 | sprintf( |
||
1899 | '%s%sCould not find attribute "%s" on node "%s"', |
||
1900 | $message, |
||
1901 | !empty($message) ? "\n" : '', |
||
1902 | $expectedAttribute->name, |
||
1903 | $expectedElement->tagName |
||
1904 | ) |
||
1905 | ); |
||
1906 | } |
||
1907 | } |
||
1908 | } |
||
1909 | |||
1910 | PHPUnit_Util_XML::removeCharacterDataNodes($expectedElement); |
||
1911 | PHPUnit_Util_XML::removeCharacterDataNodes($actualElement); |
||
1912 | |||
1913 | self::assertEquals( |
||
1914 | $expectedElement->childNodes->length, |
||
1915 | $actualElement->childNodes->length, |
||
1916 | sprintf( |
||
1917 | '%s%sNumber of child nodes of "%s" differs', |
||
1918 | $message, |
||
1919 | !empty($message) ? "\n" : '', |
||
1920 | $expectedElement->tagName |
||
1921 | ) |
||
1922 | ); |
||
1923 | |||
1924 | for ($i = 0; $i < $expectedElement->childNodes->length; $i++) { |
||
1925 | self::assertEqualXMLStructure( |
||
1926 | $expectedElement->childNodes->item($i), |
||
1927 | $actualElement->childNodes->item($i), |
||
1928 | $checkAttributes, |
||
1929 | $message |
||
1930 | ); |
||
1931 | } |
||
1932 | } |
||
1933 | |||
1934 | /** |
||
1935 | * Assert the presence, absence, or count of elements in a document matching |
||
1936 | * the CSS $selector, regardless of the contents of those elements. |
||
1937 | * |
||
1938 | * The first argument, $selector, is the CSS selector used to match |
||
1939 | * the elements in the $actual document. |
||
1940 | * |
||
1941 | * The second argument, $count, can be either boolean or numeric. |
||
1942 | * When boolean, it asserts for presence of elements matching the selector |
||
1943 | * (true) or absence of elements (false). |
||
1944 | * When numeric, it asserts the count of elements. |
||
1945 | * |
||
1946 | * assertSelectCount("#binder", true, $xml); // any? |
||
1947 | * assertSelectCount(".binder", 3, $xml); // exactly 3? |
||
1948 | * |
||
1949 | * @param array $selector |
||
1950 | * @param int|bool|array $count |
||
1951 | * @param mixed $actual |
||
1952 | * @param string $message |
||
1953 | * @param bool $isHtml |
||
1954 | * |
||
1955 | * @since Method available since Release 3.3.0 |
||
1956 | * @deprecated |
||
1957 | * @codeCoverageIgnore |
||
1958 | */ |
||
1959 | public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = true) |
||
1960 | { |
||
1961 | trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED); |
||
1962 | |||
1963 | self::assertSelectEquals( |
||
1964 | $selector, |
||
1965 | true, |
||
1966 | $count, |
||
1967 | $actual, |
||
1968 | $message, |
||
1969 | $isHtml |
||
1970 | ); |
||
1971 | } |
||
1972 | |||
1973 | /** |
||
1974 | * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any? |
||
1975 | * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // 3? |
||
1976 | * |
||
1977 | * @param array $selector |
||
1978 | * @param string $pattern |
||
1979 | * @param int|bool|array $count |
||
1980 | * @param mixed $actual |
||
1981 | * @param string $message |
||
1982 | * @param bool $isHtml |
||
1983 | * |
||
1984 | * @since Method available since Release 3.3.0 |
||
1985 | * @deprecated |
||
1986 | * @codeCoverageIgnore |
||
1987 | */ |
||
1988 | public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = true) |
||
1989 | { |
||
1990 | trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED); |
||
1991 | |||
1992 | self::assertSelectEquals( |
||
1993 | $selector, |
||
1994 | "regexp:$pattern", |
||
1995 | $count, |
||
1996 | $actual, |
||
1997 | $message, |
||
1998 | $isHtml |
||
1999 | ); |
||
2000 | } |
||
2001 | |||
2002 | /** |
||
2003 | * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any? |
||
2004 | * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none? |
||
2005 | * |
||
2006 | * @param array $selector |
||
2007 | * @param string $content |
||
2008 | * @param int|bool|array $count |
||
2009 | * @param mixed $actual |
||
2010 | * @param string $message |
||
2011 | * @param bool $isHtml |
||
2012 | * |
||
2013 | * @since Method available since Release 3.3.0 |
||
2014 | * @deprecated |
||
2015 | * @codeCoverageIgnore |
||
2016 | */ |
||
2017 | public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = true) |
||
2018 | { |
||
2019 | trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED); |
||
2020 | |||
2021 | $tags = PHPUnit_Util_XML::cssSelect( |
||
2022 | $selector, |
||
2023 | $content, |
||
2024 | $actual, |
||
2025 | $isHtml |
||
2026 | ); |
||
2027 | |||
2028 | // assert specific number of elements |
||
2029 | if (is_numeric($count)) { |
||
2030 | $counted = $tags ? count($tags) : 0; |
||
2031 | self::assertEquals($count, $counted, $message); |
||
2032 | } // assert any elements exist if true, assert no elements exist if false |
||
2033 | elseif (is_bool($count)) { |
||
2034 | $any = count($tags) > 0 && $tags[0] instanceof DOMNode; |
||
2035 | |||
2036 | if ($count) { |
||
2037 | self::assertTrue($any, $message); |
||
2038 | } else { |
||
2039 | self::assertFalse($any, $message); |
||
2040 | } |
||
2041 | } // check for range number of elements |
||
2042 | elseif (is_array($count) && |
||
2043 | (isset($count['>']) || isset($count['<']) || |
||
2044 | isset($count['>=']) || isset($count['<=']))) { |
||
2045 | $counted = $tags ? count($tags) : 0; |
||
2046 | |||
2047 | if (isset($count['>'])) { |
||
2048 | self::assertTrue($counted > $count['>'], $message); |
||
2049 | } |
||
2050 | |||
2051 | if (isset($count['>='])) { |
||
2052 | self::assertTrue($counted >= $count['>='], $message); |
||
2053 | } |
||
2054 | |||
2055 | if (isset($count['<'])) { |
||
2056 | self::assertTrue($counted < $count['<'], $message); |
||
2057 | } |
||
2058 | |||
2059 | if (isset($count['<='])) { |
||
2060 | self::assertTrue($counted <= $count['<='], $message); |
||
2061 | } |
||
2062 | } else { |
||
2063 | throw new PHPUnit_Framework_Exception; |
||
2064 | } |
||
2065 | } |
||
2066 | |||
2067 | /** |
||
2068 | * Evaluate an HTML or XML string and assert its structure and/or contents. |
||
2069 | * |
||
2070 | * The first argument ($matcher) is an associative array that specifies the |
||
2071 | * match criteria for the assertion: |
||
2072 | * |
||
2073 | * - `id` : the node with the given id attribute must match the |
||
2074 | * corresponding value. |
||
2075 | * - `tag` : the node type must match the corresponding value. |
||
2076 | * - `attributes` : a hash. The node's attributes must match the |
||
2077 | * corresponding values in the hash. |
||
2078 | * - `content` : The text content must match the given value. |
||
2079 | * - `parent` : a hash. The node's parent must match the |
||
2080 | * corresponding hash. |
||
2081 | * - `child` : a hash. At least one of the node's immediate children |
||
2082 | * must meet the criteria described by the hash. |
||
2083 | * - `ancestor` : a hash. At least one of the node's ancestors must |
||
2084 | * meet the criteria described by the hash. |
||
2085 | * - `descendant` : a hash. At least one of the node's descendants must |
||
2086 | * meet the criteria described by the hash. |
||
2087 | * - `children` : a hash, for counting children of a node. |
||
2088 | * Accepts the keys: |
||
2089 | * - `count` : a number which must equal the number of children |
||
2090 | * that match |
||
2091 | * - `less_than` : the number of matching children must be greater |
||
2092 | * than this number |
||
2093 | * - `greater_than` : the number of matching children must be less than |
||
2094 | * this number |
||
2095 | * - `only` : another hash consisting of the keys to use to match |
||
2096 | * on the children, and only matching children will be |
||
2097 | * counted |
||
2098 | * |
||
2099 | * <code> |
||
2100 | * // Matcher that asserts that there is an element with an id="my_id". |
||
2101 | * $matcher = array('id' => 'my_id'); |
||
2102 | * |
||
2103 | * // Matcher that asserts that there is a "span" tag. |
||
2104 | * $matcher = array('tag' => 'span'); |
||
2105 | * |
||
2106 | * // Matcher that asserts that there is a "span" tag with the content |
||
2107 | * // "Hello World". |
||
2108 | * $matcher = array('tag' => 'span', 'content' => 'Hello World'); |
||
2109 | * |
||
2110 | * // Matcher that asserts that there is a "span" tag with content matching |
||
2111 | * // the regular expression pattern. |
||
2112 | * $matcher = array('tag' => 'span', 'content' => 'regexp:/Try P(HP|ython)/'); |
||
2113 | * |
||
2114 | * // Matcher that asserts that there is a "span" with an "list" class |
||
2115 | * // attribute. |
||
2116 | * $matcher = array( |
||
2117 | * 'tag' => 'span', |
||
2118 | * 'attributes' => array('class' => 'list') |
||
2119 | * ); |
||
2120 | * |
||
2121 | * // Matcher that asserts that there is a "span" inside of a "div". |
||
2122 | * $matcher = array( |
||
2123 | * 'tag' => 'span', |
||
2124 | * 'parent' => array('tag' => 'div') |
||
2125 | * ); |
||
2126 | * |
||
2127 | * // Matcher that asserts that there is a "span" somewhere inside a |
||
2128 | * // "table". |
||
2129 | * $matcher = array( |
||
2130 | * 'tag' => 'span', |
||
2131 | * 'ancestor' => array('tag' => 'table') |
||
2132 | * ); |
||
2133 | * |
||
2134 | * // Matcher that asserts that there is a "span" with at least one "em" |
||
2135 | * // child. |
||
2136 | * $matcher = array( |
||
2137 | * 'tag' => 'span', |
||
2138 | * 'child' => array('tag' => 'em') |
||
2139 | * ); |
||
2140 | * |
||
2141 | * // Matcher that asserts that there is a "span" containing a (possibly |
||
2142 | * // nested) "strong" tag. |
||
2143 | * $matcher = array( |
||
2144 | * 'tag' => 'span', |
||
2145 | * 'descendant' => array('tag' => 'strong') |
||
2146 | * ); |
||
2147 | * |
||
2148 | * // Matcher that asserts that there is a "span" containing 5-10 "em" tags |
||
2149 | * // as immediate children. |
||
2150 | * $matcher = array( |
||
2151 | * 'tag' => 'span', |
||
2152 | * 'children' => array( |
||
2153 | * 'less_than' => 11, |
||
2154 | * 'greater_than' => 4, |
||
2155 | * 'only' => array('tag' => 'em') |
||
2156 | * ) |
||
2157 | * ); |
||
2158 | * |
||
2159 | * // Matcher that asserts that there is a "div", with an "ul" ancestor and |
||
2160 | * // a "li" parent (with class="enum"), and containing a "span" descendant |
||
2161 | * // that contains an element with id="my_test" and the text "Hello World". |
||
2162 | * $matcher = array( |
||
2163 | * 'tag' => 'div', |
||
2164 | * 'ancestor' => array('tag' => 'ul'), |
||
2165 | * 'parent' => array( |
||
2166 | * 'tag' => 'li', |
||
2167 | * 'attributes' => array('class' => 'enum') |
||
2168 | * ), |
||
2169 | * 'descendant' => array( |
||
2170 | * 'tag' => 'span', |
||
2171 | * 'child' => array( |
||
2172 | * 'id' => 'my_test', |
||
2173 | * 'content' => 'Hello World' |
||
2174 | * ) |
||
2175 | * ) |
||
2176 | * ); |
||
2177 | * |
||
2178 | * // Use assertTag() to apply a $matcher to a piece of $html. |
||
2179 | * $this->assertTag($matcher, $html); |
||
2180 | * |
||
2181 | * // Use assertTag() to apply a $matcher to a piece of $xml. |
||
2182 | * $this->assertTag($matcher, $xml, '', false); |
||
2183 | * </code> |
||
2184 | * |
||
2185 | * The second argument ($actual) is a string containing either HTML or |
||
2186 | * XML text to be tested. |
||
2187 | * |
||
2188 | * The third argument ($message) is an optional message that will be |
||
2189 | * used if the assertion fails. |
||
2190 | * |
||
2191 | * The fourth argument ($html) is an optional flag specifying whether |
||
2192 | * to load the $actual string into a DOMDocument using the HTML or |
||
2193 | * XML load strategy. It is true by default, which assumes the HTML |
||
2194 | * load strategy. In many cases, this will be acceptable for XML as well. |
||
2195 | * |
||
2196 | * @param array $matcher |
||
2197 | * @param string $actual |
||
2198 | * @param string $message |
||
2199 | * @param bool $isHtml |
||
2200 | * |
||
2201 | * @since Method available since Release 3.3.0 |
||
2202 | * @deprecated |
||
2203 | * @codeCoverageIgnore |
||
2204 | */ |
||
2205 | public static function assertTag($matcher, $actual, $message = '', $isHtml = true) |
||
2206 | { |
||
2207 | trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED); |
||
2208 | |||
2209 | $dom = PHPUnit_Util_XML::load($actual, $isHtml); |
||
2210 | $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml); |
||
2211 | $matched = count($tags) > 0 && $tags[0] instanceof DOMNode; |
||
2212 | |||
2213 | self::assertTrue($matched, $message); |
||
2214 | } |
||
2215 | |||
2216 | /** |
||
2217 | * This assertion is the exact opposite of assertTag(). |
||
2218 | * |
||
2219 | * Rather than asserting that $matcher results in a match, it asserts that |
||
2220 | * $matcher does not match. |
||
2221 | * |
||
2222 | * @param array $matcher |
||
2223 | * @param string $actual |
||
2224 | * @param string $message |
||
2225 | * @param bool $isHtml |
||
2226 | * |
||
2227 | * @since Method available since Release 3.3.0 |
||
2228 | * @deprecated |
||
2229 | * @codeCoverageIgnore |
||
2230 | */ |
||
2231 | public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true) |
||
2232 | { |
||
2233 | trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED); |
||
2234 | |||
2235 | $dom = PHPUnit_Util_XML::load($actual, $isHtml); |
||
2236 | $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml); |
||
2237 | $matched = count($tags) > 0 && $tags[0] instanceof DOMNode; |
||
2238 | |||
2239 | self::assertFalse($matched, $message); |
||
2240 | } |
||
2241 | |||
2242 | /** |
||
2243 | * Evaluates a PHPUnit_Framework_Constraint matcher object. |
||
2244 | * |
||
2245 | * @param mixed $value |
||
2246 | * @param PHPUnit_Framework_Constraint $constraint |
||
2247 | * @param string $message |
||
2248 | * |
||
2249 | * @since Method available since Release 3.0.0 |
||
2250 | */ |
||
2251 | public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '') |
||
2252 | { |
||
2253 | self::$count += count($constraint); |
||
2254 | |||
2255 | $constraint->evaluate($value, $message); |
||
2256 | } |
||
2257 | |||
2258 | /** |
||
2259 | * Asserts that a string is a valid JSON string. |
||
2260 | * |
||
2261 | * @param string $actualJson |
||
2262 | * @param string $message |
||
2263 | * |
||
2264 | * @since Method available since Release 3.7.20 |
||
2265 | */ |
||
2266 | public static function assertJson($actualJson, $message = '') |
||
2267 | { |
||
2268 | if (!is_string($actualJson)) { |
||
2269 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
2270 | } |
||
2271 | |||
2272 | self::assertThat($actualJson, self::isJson(), $message); |
||
2273 | } |
||
2274 | |||
2275 | /** |
||
2276 | * Asserts that two given JSON encoded objects or arrays are equal. |
||
2277 | * |
||
2278 | * @param string $expectedJson |
||
2279 | * @param string $actualJson |
||
2280 | * @param string $message |
||
2281 | */ |
||
2282 | public static function assertJsonStringEqualsJsonString($expectedJson, $actualJson, $message = '') |
||
2283 | { |
||
2284 | self::assertJson($expectedJson, $message); |
||
2285 | self::assertJson($actualJson, $message); |
||
2286 | |||
2287 | $expected = json_decode($expectedJson); |
||
2288 | $actual = json_decode($actualJson); |
||
2289 | |||
2290 | self::assertEquals($expected, $actual, $message); |
||
2291 | } |
||
2292 | |||
2293 | /** |
||
2294 | * Asserts that two given JSON encoded objects or arrays are not equal. |
||
2295 | * |
||
2296 | * @param string $expectedJson |
||
2297 | * @param string $actualJson |
||
2298 | * @param string $message |
||
2299 | */ |
||
2300 | public static function assertJsonStringNotEqualsJsonString($expectedJson, $actualJson, $message = '') |
||
2301 | { |
||
2302 | self::assertJson($expectedJson, $message); |
||
2303 | self::assertJson($actualJson, $message); |
||
2304 | |||
2305 | $expected = json_decode($expectedJson); |
||
2306 | $actual = json_decode($actualJson); |
||
2307 | |||
2308 | self::assertNotEquals($expected, $actual, $message); |
||
2309 | } |
||
2310 | |||
2311 | /** |
||
2312 | * Asserts that the generated JSON encoded object and the content of the given file are equal. |
||
2313 | * |
||
2314 | * @param string $expectedFile |
||
2315 | * @param string $actualJson |
||
2316 | * @param string $message |
||
2317 | */ |
||
2318 | public static function assertJsonStringEqualsJsonFile($expectedFile, $actualJson, $message = '') |
||
2319 | { |
||
2320 | self::assertFileExists($expectedFile, $message); |
||
2321 | $expectedJson = file_get_contents($expectedFile); |
||
2322 | |||
2323 | self::assertJson($expectedJson, $message); |
||
2324 | self::assertJson($actualJson, $message); |
||
2325 | |||
2326 | // call constraint |
||
2327 | $constraint = new PHPUnit_Framework_Constraint_JsonMatches( |
||
2328 | $expectedJson |
||
2329 | ); |
||
2330 | |||
2331 | self::assertThat($actualJson, $constraint, $message); |
||
2332 | } |
||
2333 | |||
2334 | /** |
||
2335 | * Asserts that the generated JSON encoded object and the content of the given file are not equal. |
||
2336 | * |
||
2337 | * @param string $expectedFile |
||
2338 | * @param string $actualJson |
||
2339 | * @param string $message |
||
2340 | */ |
||
2341 | public static function assertJsonStringNotEqualsJsonFile($expectedFile, $actualJson, $message = '') |
||
2342 | { |
||
2343 | self::assertFileExists($expectedFile, $message); |
||
2344 | $expectedJson = file_get_contents($expectedFile); |
||
2345 | |||
2346 | self::assertJson($expectedJson, $message); |
||
2347 | self::assertJson($actualJson, $message); |
||
2348 | |||
2349 | // call constraint |
||
2350 | $constraint = new PHPUnit_Framework_Constraint_JsonMatches( |
||
2351 | $expectedJson |
||
2352 | ); |
||
2353 | |||
2354 | self::assertThat($actualJson, new PHPUnit_Framework_Constraint_Not($constraint), $message); |
||
2355 | } |
||
2356 | |||
2357 | /** |
||
2358 | * Asserts that two JSON files are not equal. |
||
2359 | * |
||
2360 | * @param string $expectedFile |
||
2361 | * @param string $actualFile |
||
2362 | * @param string $message |
||
2363 | */ |
||
2364 | public static function assertJsonFileNotEqualsJsonFile($expectedFile, $actualFile, $message = '') |
||
2365 | { |
||
2366 | self::assertFileExists($expectedFile, $message); |
||
2367 | self::assertFileExists($actualFile, $message); |
||
2368 | |||
2369 | $actualJson = file_get_contents($actualFile); |
||
2370 | $expectedJson = file_get_contents($expectedFile); |
||
2371 | |||
2372 | self::assertJson($expectedJson, $message); |
||
2373 | self::assertJson($actualJson, $message); |
||
2374 | |||
2375 | // call constraint |
||
2376 | $constraintExpected = new PHPUnit_Framework_Constraint_JsonMatches( |
||
2377 | $expectedJson |
||
2378 | ); |
||
2379 | |||
2380 | $constraintActual = new PHPUnit_Framework_Constraint_JsonMatches($actualJson); |
||
2381 | |||
2382 | self::assertThat($expectedJson, new PHPUnit_Framework_Constraint_Not($constraintActual), $message); |
||
2383 | self::assertThat($actualJson, new PHPUnit_Framework_Constraint_Not($constraintExpected), $message); |
||
2384 | } |
||
2385 | |||
2386 | /** |
||
2387 | * Asserts that two JSON files are equal. |
||
2388 | * |
||
2389 | * @param string $expectedFile |
||
2390 | * @param string $actualFile |
||
2391 | * @param string $message |
||
2392 | */ |
||
2393 | public static function assertJsonFileEqualsJsonFile($expectedFile, $actualFile, $message = '') |
||
2394 | { |
||
2395 | self::assertFileExists($expectedFile, $message); |
||
2396 | self::assertFileExists($actualFile, $message); |
||
2397 | |||
2398 | $actualJson = file_get_contents($actualFile); |
||
2399 | $expectedJson = file_get_contents($expectedFile); |
||
2400 | |||
2401 | self::assertJson($expectedJson, $message); |
||
2402 | self::assertJson($actualJson, $message); |
||
2403 | |||
2404 | // call constraint |
||
2405 | $constraintExpected = new PHPUnit_Framework_Constraint_JsonMatches( |
||
2406 | $expectedJson |
||
2407 | ); |
||
2408 | |||
2409 | $constraintActual = new PHPUnit_Framework_Constraint_JsonMatches($actualJson); |
||
2410 | |||
2411 | self::assertThat($expectedJson, $constraintActual, $message); |
||
2412 | self::assertThat($actualJson, $constraintExpected, $message); |
||
2413 | } |
||
2414 | |||
2415 | /** |
||
2416 | * Returns a PHPUnit_Framework_Constraint_And matcher object. |
||
2417 | * |
||
2418 | * @return PHPUnit_Framework_Constraint_And |
||
2419 | * |
||
2420 | * @since Method available since Release 3.0.0 |
||
2421 | */ |
||
2422 | public static function logicalAnd() |
||
2423 | { |
||
2424 | $constraints = func_get_args(); |
||
2425 | |||
2426 | $constraint = new PHPUnit_Framework_Constraint_And; |
||
2427 | $constraint->setConstraints($constraints); |
||
2428 | |||
2429 | return $constraint; |
||
2430 | } |
||
2431 | |||
2432 | /** |
||
2433 | * Returns a PHPUnit_Framework_Constraint_Or matcher object. |
||
2434 | * |
||
2435 | * @return PHPUnit_Framework_Constraint_Or |
||
2436 | * |
||
2437 | * @since Method available since Release 3.0.0 |
||
2438 | */ |
||
2439 | public static function logicalOr() |
||
2440 | { |
||
2441 | $constraints = func_get_args(); |
||
2442 | |||
2443 | $constraint = new PHPUnit_Framework_Constraint_Or; |
||
2444 | $constraint->setConstraints($constraints); |
||
2445 | |||
2446 | return $constraint; |
||
2447 | } |
||
2448 | |||
2449 | /** |
||
2450 | * Returns a PHPUnit_Framework_Constraint_Not matcher object. |
||
2451 | * |
||
2452 | * @param PHPUnit_Framework_Constraint $constraint |
||
2453 | * |
||
2454 | * @return PHPUnit_Framework_Constraint_Not |
||
2455 | * |
||
2456 | * @since Method available since Release 3.0.0 |
||
2457 | */ |
||
2458 | public static function logicalNot(PHPUnit_Framework_Constraint $constraint) |
||
2459 | { |
||
2460 | return new PHPUnit_Framework_Constraint_Not($constraint); |
||
2461 | } |
||
2462 | |||
2463 | /** |
||
2464 | * Returns a PHPUnit_Framework_Constraint_Xor matcher object. |
||
2465 | * |
||
2466 | * @return PHPUnit_Framework_Constraint_Xor |
||
2467 | * |
||
2468 | * @since Method available since Release 3.0.0 |
||
2469 | */ |
||
2470 | public static function logicalXor() |
||
2471 | { |
||
2472 | $constraints = func_get_args(); |
||
2473 | |||
2474 | $constraint = new PHPUnit_Framework_Constraint_Xor; |
||
2475 | $constraint->setConstraints($constraints); |
||
2476 | |||
2477 | return $constraint; |
||
2478 | } |
||
2479 | |||
2480 | /** |
||
2481 | * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object. |
||
2482 | * |
||
2483 | * @return PHPUnit_Framework_Constraint_IsAnything |
||
2484 | * |
||
2485 | * @since Method available since Release 3.0.0 |
||
2486 | */ |
||
2487 | public static function anything() |
||
2488 | { |
||
2489 | return new PHPUnit_Framework_Constraint_IsAnything; |
||
2490 | } |
||
2491 | |||
2492 | /** |
||
2493 | * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object. |
||
2494 | * |
||
2495 | * @return PHPUnit_Framework_Constraint_IsTrue |
||
2496 | * |
||
2497 | * @since Method available since Release 3.3.0 |
||
2498 | */ |
||
2499 | public static function isTrue() |
||
2500 | { |
||
2501 | return new PHPUnit_Framework_Constraint_IsTrue; |
||
2502 | } |
||
2503 | |||
2504 | /** |
||
2505 | * Returns a PHPUnit_Framework_Constraint_Callback matcher object. |
||
2506 | * |
||
2507 | * @param callable $callback |
||
2508 | * |
||
2509 | * @return PHPUnit_Framework_Constraint_Callback |
||
2510 | */ |
||
2511 | public static function callback($callback) |
||
2512 | { |
||
2513 | return new PHPUnit_Framework_Constraint_Callback($callback); |
||
2514 | } |
||
2515 | |||
2516 | /** |
||
2517 | * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object. |
||
2518 | * |
||
2519 | * @return PHPUnit_Framework_Constraint_IsFalse |
||
2520 | * |
||
2521 | * @since Method available since Release 3.3.0 |
||
2522 | */ |
||
2523 | public static function isFalse() |
||
2524 | { |
||
2525 | return new PHPUnit_Framework_Constraint_IsFalse; |
||
2526 | } |
||
2527 | |||
2528 | /** |
||
2529 | * Returns a PHPUnit_Framework_Constraint_IsJson matcher object. |
||
2530 | * |
||
2531 | * @return PHPUnit_Framework_Constraint_IsJson |
||
2532 | * |
||
2533 | * @since Method available since Release 3.7.20 |
||
2534 | */ |
||
2535 | public static function isJson() |
||
2536 | { |
||
2537 | return new PHPUnit_Framework_Constraint_IsJson; |
||
2538 | } |
||
2539 | |||
2540 | /** |
||
2541 | * Returns a PHPUnit_Framework_Constraint_IsNull matcher object. |
||
2542 | * |
||
2543 | * @return PHPUnit_Framework_Constraint_IsNull |
||
2544 | * |
||
2545 | * @since Method available since Release 3.3.0 |
||
2546 | */ |
||
2547 | public static function isNull() |
||
2548 | { |
||
2549 | return new PHPUnit_Framework_Constraint_IsNull; |
||
2550 | } |
||
2551 | |||
2552 | /** |
||
2553 | * Returns a PHPUnit_Framework_Constraint_Attribute matcher object. |
||
2554 | * |
||
2555 | * @param PHPUnit_Framework_Constraint $constraint |
||
2556 | * @param string $attributeName |
||
2557 | * |
||
2558 | * @return PHPUnit_Framework_Constraint_Attribute |
||
2559 | * |
||
2560 | * @since Method available since Release 3.1.0 |
||
2561 | */ |
||
2562 | public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName) |
||
2563 | { |
||
2564 | return new PHPUnit_Framework_Constraint_Attribute( |
||
2565 | $constraint, |
||
2566 | $attributeName |
||
2567 | ); |
||
2568 | } |
||
2569 | |||
2570 | /** |
||
2571 | * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher |
||
2572 | * object. |
||
2573 | * |
||
2574 | * @param mixed $value |
||
2575 | * @param bool $checkForObjectIdentity |
||
2576 | * @param bool $checkForNonObjectIdentity |
||
2577 | * |
||
2578 | * @return PHPUnit_Framework_Constraint_TraversableContains |
||
2579 | * |
||
2580 | * @since Method available since Release 3.0.0 |
||
2581 | */ |
||
2582 | public static function contains($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) |
||
2583 | { |
||
2584 | return new PHPUnit_Framework_Constraint_TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity); |
||
2585 | } |
||
2586 | |||
2587 | /** |
||
2588 | * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher |
||
2589 | * object. |
||
2590 | * |
||
2591 | * @param string $type |
||
2592 | * |
||
2593 | * @return PHPUnit_Framework_Constraint_TraversableContainsOnly |
||
2594 | * |
||
2595 | * @since Method available since Release 3.1.4 |
||
2596 | */ |
||
2597 | public static function containsOnly($type) |
||
2598 | { |
||
2599 | return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type); |
||
2600 | } |
||
2601 | |||
2602 | /** |
||
2603 | * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher |
||
2604 | * object. |
||
2605 | * |
||
2606 | * @param string $classname |
||
2607 | * |
||
2608 | * @return PHPUnit_Framework_Constraint_TraversableContainsOnly |
||
2609 | */ |
||
2610 | public static function containsOnlyInstancesOf($classname) |
||
2611 | { |
||
2612 | return new PHPUnit_Framework_Constraint_TraversableContainsOnly($classname, false); |
||
2613 | } |
||
2614 | |||
2615 | /** |
||
2616 | * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object. |
||
2617 | * |
||
2618 | * @param mixed $key |
||
2619 | * |
||
2620 | * @return PHPUnit_Framework_Constraint_ArrayHasKey |
||
2621 | * |
||
2622 | * @since Method available since Release 3.0.0 |
||
2623 | */ |
||
2624 | public static function arrayHasKey($key) |
||
2625 | { |
||
2626 | return new PHPUnit_Framework_Constraint_ArrayHasKey($key); |
||
2627 | } |
||
2628 | |||
2629 | /** |
||
2630 | * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object. |
||
2631 | * |
||
2632 | * @param mixed $value |
||
2633 | * @param float $delta |
||
2634 | * @param int $maxDepth |
||
2635 | * @param bool $canonicalize |
||
2636 | * @param bool $ignoreCase |
||
2637 | * |
||
2638 | * @return PHPUnit_Framework_Constraint_IsEqual |
||
2639 | * |
||
2640 | * @since Method available since Release 3.0.0 |
||
2641 | */ |
||
2642 | public static function equalTo($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
2643 | { |
||
2644 | return new PHPUnit_Framework_Constraint_IsEqual( |
||
2645 | $value, |
||
2646 | $delta, |
||
2647 | $maxDepth, |
||
2648 | $canonicalize, |
||
2649 | $ignoreCase |
||
2650 | ); |
||
2651 | } |
||
2652 | |||
2653 | /** |
||
2654 | * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object |
||
2655 | * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher |
||
2656 | * object. |
||
2657 | * |
||
2658 | * @param string $attributeName |
||
2659 | * @param mixed $value |
||
2660 | * @param float $delta |
||
2661 | * @param int $maxDepth |
||
2662 | * @param bool $canonicalize |
||
2663 | * @param bool $ignoreCase |
||
2664 | * |
||
2665 | * @return PHPUnit_Framework_Constraint_Attribute |
||
2666 | * |
||
2667 | * @since Method available since Release 3.1.0 |
||
2668 | */ |
||
2669 | public static function attributeEqualTo($attributeName, $value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) |
||
2670 | { |
||
2671 | return self::attribute( |
||
2672 | self::equalTo( |
||
2673 | $value, |
||
2674 | $delta, |
||
2675 | $maxDepth, |
||
2676 | $canonicalize, |
||
2677 | $ignoreCase |
||
2678 | ), |
||
2679 | $attributeName |
||
2680 | ); |
||
2681 | } |
||
2682 | |||
2683 | /** |
||
2684 | * Returns a PHPUnit_Framework_Constraint_IsEmpty matcher object. |
||
2685 | * |
||
2686 | * @return PHPUnit_Framework_Constraint_IsEmpty |
||
2687 | * |
||
2688 | * @since Method available since Release 3.5.0 |
||
2689 | */ |
||
2690 | public static function isEmpty() |
||
2691 | { |
||
2692 | return new PHPUnit_Framework_Constraint_IsEmpty; |
||
2693 | } |
||
2694 | |||
2695 | /** |
||
2696 | * Returns a PHPUnit_Framework_Constraint_FileExists matcher object. |
||
2697 | * |
||
2698 | * @return PHPUnit_Framework_Constraint_FileExists |
||
2699 | * |
||
2700 | * @since Method available since Release 3.0.0 |
||
2701 | */ |
||
2702 | public static function fileExists() |
||
2703 | { |
||
2704 | return new PHPUnit_Framework_Constraint_FileExists; |
||
2705 | } |
||
2706 | |||
2707 | /** |
||
2708 | * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object. |
||
2709 | * |
||
2710 | * @param mixed $value |
||
2711 | * |
||
2712 | * @return PHPUnit_Framework_Constraint_GreaterThan |
||
2713 | * |
||
2714 | * @since Method available since Release 3.0.0 |
||
2715 | */ |
||
2716 | public static function greaterThan($value) |
||
2717 | { |
||
2718 | return new PHPUnit_Framework_Constraint_GreaterThan($value); |
||
2719 | } |
||
2720 | |||
2721 | /** |
||
2722 | * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps |
||
2723 | * a PHPUnit_Framework_Constraint_IsEqual and a |
||
2724 | * PHPUnit_Framework_Constraint_GreaterThan matcher object. |
||
2725 | * |
||
2726 | * @param mixed $value |
||
2727 | * |
||
2728 | * @return PHPUnit_Framework_Constraint_Or |
||
2729 | * |
||
2730 | * @since Method available since Release 3.1.0 |
||
2731 | */ |
||
2732 | public static function greaterThanOrEqual($value) |
||
2733 | { |
||
2734 | return self::logicalOr( |
||
2735 | new PHPUnit_Framework_Constraint_IsEqual($value), |
||
2736 | new PHPUnit_Framework_Constraint_GreaterThan($value) |
||
2737 | ); |
||
2738 | } |
||
2739 | |||
2740 | /** |
||
2741 | * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object. |
||
2742 | * |
||
2743 | * @param string $attributeName |
||
2744 | * |
||
2745 | * @return PHPUnit_Framework_Constraint_ClassHasAttribute |
||
2746 | * |
||
2747 | * @since Method available since Release 3.1.0 |
||
2748 | */ |
||
2749 | public static function classHasAttribute($attributeName) |
||
2750 | { |
||
2751 | return new PHPUnit_Framework_Constraint_ClassHasAttribute( |
||
2752 | $attributeName |
||
2753 | ); |
||
2754 | } |
||
2755 | |||
2756 | /** |
||
2757 | * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher |
||
2758 | * object. |
||
2759 | * |
||
2760 | * @param string $attributeName |
||
2761 | * |
||
2762 | * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute |
||
2763 | * |
||
2764 | * @since Method available since Release 3.1.0 |
||
2765 | */ |
||
2766 | public static function classHasStaticAttribute($attributeName) |
||
2767 | { |
||
2768 | return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute( |
||
2769 | $attributeName |
||
2770 | ); |
||
2771 | } |
||
2772 | |||
2773 | /** |
||
2774 | * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object. |
||
2775 | * |
||
2776 | * @param string $attributeName |
||
2777 | * |
||
2778 | * @return PHPUnit_Framework_Constraint_ObjectHasAttribute |
||
2779 | * |
||
2780 | * @since Method available since Release 3.0.0 |
||
2781 | */ |
||
2782 | public static function objectHasAttribute($attributeName) |
||
2783 | { |
||
2784 | return new PHPUnit_Framework_Constraint_ObjectHasAttribute( |
||
2785 | $attributeName |
||
2786 | ); |
||
2787 | } |
||
2788 | |||
2789 | /** |
||
2790 | * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object. |
||
2791 | * |
||
2792 | * @param mixed $value |
||
2793 | * |
||
2794 | * @return PHPUnit_Framework_Constraint_IsIdentical |
||
2795 | * |
||
2796 | * @since Method available since Release 3.0.0 |
||
2797 | */ |
||
2798 | public static function identicalTo($value) |
||
2799 | { |
||
2800 | return new PHPUnit_Framework_Constraint_IsIdentical($value); |
||
2801 | } |
||
2802 | |||
2803 | /** |
||
2804 | * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object. |
||
2805 | * |
||
2806 | * @param string $className |
||
2807 | * |
||
2808 | * @return PHPUnit_Framework_Constraint_IsInstanceOf |
||
2809 | * |
||
2810 | * @since Method available since Release 3.0.0 |
||
2811 | */ |
||
2812 | public static function isInstanceOf($className) |
||
2813 | { |
||
2814 | return new PHPUnit_Framework_Constraint_IsInstanceOf($className); |
||
2815 | } |
||
2816 | |||
2817 | /** |
||
2818 | * Returns a PHPUnit_Framework_Constraint_IsType matcher object. |
||
2819 | * |
||
2820 | * @param string $type |
||
2821 | * |
||
2822 | * @return PHPUnit_Framework_Constraint_IsType |
||
2823 | * |
||
2824 | * @since Method available since Release 3.0.0 |
||
2825 | */ |
||
2826 | public static function isType($type) |
||
2827 | { |
||
2828 | return new PHPUnit_Framework_Constraint_IsType($type); |
||
2829 | } |
||
2830 | |||
2831 | /** |
||
2832 | * Returns a PHPUnit_Framework_Constraint_LessThan matcher object. |
||
2833 | * |
||
2834 | * @param mixed $value |
||
2835 | * |
||
2836 | * @return PHPUnit_Framework_Constraint_LessThan |
||
2837 | * |
||
2838 | * @since Method available since Release 3.0.0 |
||
2839 | */ |
||
2840 | public static function lessThan($value) |
||
2841 | { |
||
2842 | return new PHPUnit_Framework_Constraint_LessThan($value); |
||
2843 | } |
||
2844 | |||
2845 | /** |
||
2846 | * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps |
||
2847 | * a PHPUnit_Framework_Constraint_IsEqual and a |
||
2848 | * PHPUnit_Framework_Constraint_LessThan matcher object. |
||
2849 | * |
||
2850 | * @param mixed $value |
||
2851 | * |
||
2852 | * @return PHPUnit_Framework_Constraint_Or |
||
2853 | * |
||
2854 | * @since Method available since Release 3.1.0 |
||
2855 | */ |
||
2856 | public static function lessThanOrEqual($value) |
||
2857 | { |
||
2858 | return self::logicalOr( |
||
2859 | new PHPUnit_Framework_Constraint_IsEqual($value), |
||
2860 | new PHPUnit_Framework_Constraint_LessThan($value) |
||
2861 | ); |
||
2862 | } |
||
2863 | |||
2864 | /** |
||
2865 | * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object. |
||
2866 | * |
||
2867 | * @param string $pattern |
||
2868 | * |
||
2869 | * @return PHPUnit_Framework_Constraint_PCREMatch |
||
2870 | * |
||
2871 | * @since Method available since Release 3.0.0 |
||
2872 | */ |
||
2873 | public static function matchesRegularExpression($pattern) |
||
2874 | { |
||
2875 | return new PHPUnit_Framework_Constraint_PCREMatch($pattern); |
||
2876 | } |
||
2877 | |||
2878 | /** |
||
2879 | * Returns a PHPUnit_Framework_Constraint_StringMatches matcher object. |
||
2880 | * |
||
2881 | * @param string $string |
||
2882 | * |
||
2883 | * @return PHPUnit_Framework_Constraint_StringMatches |
||
2884 | * |
||
2885 | * @since Method available since Release 3.5.0 |
||
2886 | */ |
||
2887 | public static function matches($string) |
||
2888 | { |
||
2889 | return new PHPUnit_Framework_Constraint_StringMatches($string); |
||
2890 | } |
||
2891 | |||
2892 | /** |
||
2893 | * Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object. |
||
2894 | * |
||
2895 | * @param mixed $prefix |
||
2896 | * |
||
2897 | * @return PHPUnit_Framework_Constraint_StringStartsWith |
||
2898 | * |
||
2899 | * @since Method available since Release 3.4.0 |
||
2900 | */ |
||
2901 | public static function stringStartsWith($prefix) |
||
2902 | { |
||
2903 | return new PHPUnit_Framework_Constraint_StringStartsWith($prefix); |
||
2904 | } |
||
2905 | |||
2906 | /** |
||
2907 | * Returns a PHPUnit_Framework_Constraint_StringContains matcher object. |
||
2908 | * |
||
2909 | * @param string $string |
||
2910 | * @param bool $case |
||
2911 | * |
||
2912 | * @return PHPUnit_Framework_Constraint_StringContains |
||
2913 | * |
||
2914 | * @since Method available since Release 3.0.0 |
||
2915 | */ |
||
2916 | public static function stringContains($string, $case = true) |
||
2917 | { |
||
2918 | return new PHPUnit_Framework_Constraint_StringContains($string, $case); |
||
2919 | } |
||
2920 | |||
2921 | /** |
||
2922 | * Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object. |
||
2923 | * |
||
2924 | * @param mixed $suffix |
||
2925 | * |
||
2926 | * @return PHPUnit_Framework_Constraint_StringEndsWith |
||
2927 | * |
||
2928 | * @since Method available since Release 3.4.0 |
||
2929 | */ |
||
2930 | public static function stringEndsWith($suffix) |
||
2931 | { |
||
2932 | return new PHPUnit_Framework_Constraint_StringEndsWith($suffix); |
||
2933 | } |
||
2934 | |||
2935 | /** |
||
2936 | * Returns a PHPUnit_Framework_Constraint_Count matcher object. |
||
2937 | * |
||
2938 | * @param int $count |
||
2939 | * |
||
2940 | * @return PHPUnit_Framework_Constraint_Count |
||
2941 | */ |
||
2942 | public static function countOf($count) |
||
2943 | { |
||
2944 | return new PHPUnit_Framework_Constraint_Count($count); |
||
2945 | } |
||
2946 | /** |
||
2947 | * Fails a test with the given message. |
||
2948 | * |
||
2949 | * @param string $message |
||
2950 | * |
||
2951 | * @throws PHPUnit_Framework_AssertionFailedError |
||
2952 | */ |
||
2953 | public static function fail($message = '') |
||
2954 | { |
||
2955 | throw new PHPUnit_Framework_AssertionFailedError($message); |
||
2956 | } |
||
2957 | |||
2958 | /** |
||
2959 | * Returns the value of an attribute of a class or an object. |
||
2960 | * This also works for attributes that are declared protected or private. |
||
2961 | * |
||
2962 | * @param mixed $classOrObject |
||
2963 | * @param string $attributeName |
||
2964 | * |
||
2965 | * @return mixed |
||
2966 | * |
||
2967 | * @throws PHPUnit_Framework_Exception |
||
2968 | */ |
||
2969 | public static function readAttribute($classOrObject, $attributeName) |
||
2970 | { |
||
2971 | if (!is_string($attributeName)) { |
||
2972 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
2973 | } |
||
2974 | |||
2975 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
2976 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name'); |
||
2977 | } |
||
2978 | |||
2979 | if (is_string($classOrObject)) { |
||
2980 | if (!class_exists($classOrObject)) { |
||
2981 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
2982 | 1, |
||
2983 | 'class name' |
||
2984 | ); |
||
2985 | } |
||
2986 | |||
2987 | return self::getStaticAttribute( |
||
2988 | $classOrObject, |
||
2989 | $attributeName |
||
2990 | ); |
||
2991 | } elseif (is_object($classOrObject)) { |
||
2992 | return self::getObjectAttribute( |
||
2993 | $classOrObject, |
||
2994 | $attributeName |
||
2995 | ); |
||
2996 | } else { |
||
2997 | throw PHPUnit_Util_InvalidArgumentHelper::factory( |
||
2998 | 1, |
||
2999 | 'class name or object' |
||
3000 | ); |
||
3001 | } |
||
3002 | } |
||
3003 | |||
3004 | /** |
||
3005 | * Returns the value of a static attribute. |
||
3006 | * This also works for attributes that are declared protected or private. |
||
3007 | * |
||
3008 | * @param string $className |
||
3009 | * @param string $attributeName |
||
3010 | * |
||
3011 | * @return mixed |
||
3012 | * |
||
3013 | * @throws PHPUnit_Framework_Exception |
||
3014 | * |
||
3015 | * @since Method available since Release 4.0.0 |
||
3016 | */ |
||
3017 | public static function getStaticAttribute($className, $attributeName) |
||
3018 | { |
||
3019 | if (!is_string($className)) { |
||
3020 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); |
||
3021 | } |
||
3022 | |||
3023 | if (!class_exists($className)) { |
||
3024 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name'); |
||
3025 | } |
||
3026 | |||
3027 | if (!is_string($attributeName)) { |
||
3028 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
3029 | } |
||
3030 | |||
3031 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
3032 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name'); |
||
3033 | } |
||
3034 | |||
3035 | $class = new ReflectionClass($className); |
||
3036 | |||
3037 | while ($class) { |
||
3038 | $attributes = $class->getStaticProperties(); |
||
3039 | |||
3040 | if (array_key_exists($attributeName, $attributes)) { |
||
3041 | return $attributes[$attributeName]; |
||
3042 | } |
||
3043 | |||
3044 | $class = $class->getParentClass(); |
||
3045 | } |
||
3046 | |||
3047 | throw new PHPUnit_Framework_Exception( |
||
3048 | sprintf( |
||
3049 | 'Attribute "%s" not found in class.', |
||
3050 | $attributeName |
||
3051 | ) |
||
3052 | ); |
||
3053 | } |
||
3054 | |||
3055 | /** |
||
3056 | * Returns the value of an object's attribute. |
||
3057 | * This also works for attributes that are declared protected or private. |
||
3058 | * |
||
3059 | * @param object $object |
||
3060 | * @param string $attributeName |
||
3061 | * |
||
3062 | * @return mixed |
||
3063 | * |
||
3064 | * @throws PHPUnit_Framework_Exception |
||
3065 | * |
||
3066 | * @since Method available since Release 4.0.0 |
||
3067 | */ |
||
3068 | public static function getObjectAttribute($object, $attributeName) |
||
3069 | { |
||
3070 | if (!is_object($object)) { |
||
3071 | throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object'); |
||
3072 | } |
||
3073 | |||
3074 | if (!is_string($attributeName)) { |
||
3075 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string'); |
||
3076 | } |
||
3077 | |||
3078 | if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { |
||
3079 | throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name'); |
||
3080 | } |
||
3081 | |||
3082 | try { |
||
3083 | $attribute = new ReflectionProperty($object, $attributeName); |
||
3084 | } catch (ReflectionException $e) { |
||
3085 | $reflector = new ReflectionObject($object); |
||
3086 | |||
3087 | while ($reflector = $reflector->getParentClass()) { |
||
3088 | try { |
||
3089 | $attribute = $reflector->getProperty($attributeName); |
||
3090 | break; |
||
3091 | } catch (ReflectionException $e) { |
||
3092 | } |
||
3093 | } |
||
3094 | } |
||
3095 | |||
3096 | if (isset($attribute)) { |
||
3097 | if (!$attribute || $attribute->isPublic()) { |
||
3098 | return $object->$attributeName; |
||
3099 | } |
||
3100 | |||
3101 | $attribute->setAccessible(true); |
||
3102 | $value = $attribute->getValue($object); |
||
3103 | $attribute->setAccessible(false); |
||
3104 | |||
3105 | return $value; |
||
3106 | } |
||
3107 | |||
3108 | throw new PHPUnit_Framework_Exception( |
||
3109 | sprintf( |
||
3110 | 'Attribute "%s" not found in object.', |
||
3111 | $attributeName |
||
3112 | ) |
||
3113 | ); |
||
3114 | } |
||
3115 | |||
3116 | /** |
||
3117 | * Mark the test as incomplete. |
||
3118 | * |
||
3119 | * @param string $message |
||
3120 | * |
||
3121 | * @throws PHPUnit_Framework_IncompleteTestError |
||
3122 | * |
||
3123 | * @since Method available since Release 3.0.0 |
||
3124 | */ |
||
3125 | public static function markTestIncomplete($message = '') |
||
3126 | { |
||
3127 | throw new PHPUnit_Framework_IncompleteTestError($message); |
||
3128 | } |
||
3129 | |||
3130 | /** |
||
3131 | * Mark the test as skipped. |
||
3132 | * |
||
3133 | * @param string $message |
||
3134 | * |
||
3135 | * @throws PHPUnit_Framework_SkippedTestError |
||
3136 | * |
||
3137 | * @since Method available since Release 3.0.0 |
||
3138 | */ |
||
3139 | public static function markTestSkipped($message = '') |
||
3140 | { |
||
3141 | throw new PHPUnit_Framework_SkippedTestError($message); |
||
3142 | } |
||
3143 | |||
3144 | /** |
||
3145 | * Return the current assertion count. |
||
3146 | * |
||
3147 | * @return int |
||
3148 | * |
||
3149 | * @since Method available since Release 3.3.3 |
||
3150 | */ |
||
3151 | public static function getCount() |
||
3152 | { |
||
3153 | return self::$count; |
||
3154 | } |
||
3155 | |||
3156 | /** |
||
3157 | * Reset the assertion counter. |
||
3158 | * |
||
3159 | * @since Method available since Release 3.3.3 |
||
3160 | */ |
||
3161 | public static function resetCount() |
||
3162 | { |
||
3163 | self::$count = 0; |
||
3164 | } |
||
3165 | } |
||
3166 |