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 Framework_ConstraintTest 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 Framework_ConstraintTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Framework_ConstraintTest extends PHPUnit_Framework_TestCase |
||
15 | { |
||
16 | /** |
||
17 | * @covers PHPUnit_Framework_Constraint_ArrayHasKey |
||
18 | * @covers PHPUnit_Framework_Assert::arrayHasKey |
||
19 | * @covers PHPUnit_Framework_Constraint::count |
||
20 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
21 | */ |
||
22 | public function testConstraintArrayHasKey() |
||
23 | { |
||
24 | $constraint = PHPUnit_Framework_Assert::arrayHasKey(0); |
||
25 | |||
26 | $this->assertFalse($constraint->evaluate(array(), '', true)); |
||
27 | $this->assertEquals('has the key 0', $constraint->toString()); |
||
28 | $this->assertEquals(1, count($constraint)); |
||
29 | |||
30 | try { |
||
31 | $constraint->evaluate(array()); |
||
32 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
33 | $this->assertEquals(<<<EOF |
||
34 | Failed asserting that an array has the key 0. |
||
35 | |||
36 | EOF |
||
37 | , |
||
38 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
39 | ); |
||
40 | |||
41 | return; |
||
42 | } |
||
43 | |||
44 | $this->fail(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @covers PHPUnit_Framework_Constraint_ArrayHasKey |
||
49 | * @covers PHPUnit_Framework_Assert::arrayHasKey |
||
50 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
51 | */ |
||
52 | public function testConstraintArrayHasKey2() |
||
53 | { |
||
54 | $constraint = PHPUnit_Framework_Assert::arrayHasKey(0); |
||
55 | |||
56 | try { |
||
57 | $constraint->evaluate(array(), 'custom message'); |
||
58 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
59 | $this->assertEquals( |
||
60 | <<<EOF |
||
61 | custom message\nFailed asserting that an array has the key 0. |
||
62 | |||
63 | EOF |
||
64 | , |
||
65 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
66 | ); |
||
67 | |||
68 | return; |
||
69 | } |
||
70 | |||
71 | $this->fail(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @covers PHPUnit_Framework_Constraint_ArrayHasKey |
||
76 | * @covers PHPUnit_Framework_Constraint_Not |
||
77 | * @covers PHPUnit_Framework_Assert::arrayHasKey |
||
78 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
79 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
80 | */ |
||
81 | public function testConstraintArrayNotHasKey() |
||
82 | { |
||
83 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
84 | PHPUnit_Framework_Assert::arrayHasKey(0) |
||
85 | ); |
||
86 | |||
87 | $this->assertFalse($constraint->evaluate(array(0 => 1), '', true)); |
||
88 | $this->assertEquals('does not have the key 0', $constraint->toString()); |
||
89 | $this->assertEquals(1, count($constraint)); |
||
90 | |||
91 | try { |
||
92 | $constraint->evaluate(array(0 => 1)); |
||
93 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
94 | $this->assertEquals( |
||
95 | <<<EOF |
||
96 | Failed asserting that an array does not have the key 0. |
||
97 | |||
98 | EOF |
||
99 | , |
||
100 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
101 | ); |
||
102 | |||
103 | return; |
||
104 | } |
||
105 | |||
106 | $this->fail(); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @covers PHPUnit_Framework_Constraint_ArrayHasKey |
||
111 | * @covers PHPUnit_Framework_Constraint_Not |
||
112 | * @covers PHPUnit_Framework_Assert::arrayHasKey |
||
113 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
114 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
115 | */ |
||
116 | public function testConstraintArrayNotHasKey2() |
||
117 | { |
||
118 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
119 | PHPUnit_Framework_Assert::arrayHasKey(0) |
||
120 | ); |
||
121 | |||
122 | try { |
||
123 | $constraint->evaluate(array(0), 'custom message'); |
||
124 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
125 | $this->assertEquals( |
||
126 | <<<EOF |
||
127 | custom message |
||
128 | Failed asserting that an array does not have the key 0. |
||
129 | |||
130 | EOF |
||
131 | , |
||
132 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
133 | ); |
||
134 | |||
135 | return; |
||
136 | } |
||
137 | |||
138 | $this->fail(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @covers PHPUnit_Framework_Constraint_FileExists |
||
143 | * @covers PHPUnit_Framework_Assert::fileExists |
||
144 | * @covers PHPUnit_Framework_Constraint::count |
||
145 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
146 | */ |
||
147 | public function testConstraintFileExists() |
||
148 | { |
||
149 | $constraint = PHPUnit_Framework_Assert::fileExists(); |
||
150 | |||
151 | $this->assertFalse($constraint->evaluate('foo', '', true)); |
||
152 | $this->assertEquals('file exists', $constraint->toString()); |
||
153 | $this->assertEquals(1, count($constraint)); |
||
154 | |||
155 | try { |
||
156 | $constraint->evaluate('foo'); |
||
157 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
158 | $this->assertEquals( |
||
159 | <<<EOF |
||
160 | Failed asserting that file "foo" exists. |
||
161 | |||
162 | EOF |
||
163 | , |
||
164 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
165 | ); |
||
166 | |||
167 | return; |
||
168 | } |
||
169 | |||
170 | $this->fail(); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @covers PHPUnit_Framework_Constraint_FileExists |
||
175 | * @covers PHPUnit_Framework_Assert::fileExists |
||
176 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
177 | */ |
||
178 | public function testConstraintFileExists2() |
||
179 | { |
||
180 | $constraint = PHPUnit_Framework_Assert::fileExists(); |
||
181 | |||
182 | try { |
||
183 | $constraint->evaluate('foo', 'custom message'); |
||
184 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
185 | $this->assertEquals(<<<EOF |
||
186 | custom message |
||
187 | Failed asserting that file "foo" exists. |
||
188 | |||
189 | EOF |
||
190 | , |
||
191 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
192 | ); |
||
193 | |||
194 | return; |
||
195 | } |
||
196 | |||
197 | $this->fail(); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @covers PHPUnit_Framework_Constraint_FileExists |
||
202 | * @covers PHPUnit_Framework_Constraint_Not |
||
203 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
204 | * @covers PHPUnit_Framework_Assert::fileExists |
||
205 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
206 | */ |
||
207 | public function testConstraintFileNotExists() |
||
208 | { |
||
209 | $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php'; |
||
210 | |||
211 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
212 | PHPUnit_Framework_Assert::fileExists() |
||
213 | ); |
||
214 | |||
215 | $this->assertFalse($constraint->evaluate($file, '', true)); |
||
216 | $this->assertEquals('file does not exist', $constraint->toString()); |
||
217 | $this->assertEquals(1, count($constraint)); |
||
218 | |||
219 | try { |
||
220 | $constraint->evaluate($file); |
||
221 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
222 | $this->assertEquals( |
||
223 | <<<EOF |
||
224 | Failed asserting that file "$file" does not exist. |
||
225 | |||
226 | EOF |
||
227 | , |
||
228 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
229 | ); |
||
230 | |||
231 | return; |
||
232 | } |
||
233 | |||
234 | $this->fail(); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @covers PHPUnit_Framework_Constraint_FileExists |
||
239 | * @covers PHPUnit_Framework_Constraint_Not |
||
240 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
241 | * @covers PHPUnit_Framework_Assert::fileExists |
||
242 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
243 | */ |
||
244 | public function testConstraintFileNotExists2() |
||
245 | { |
||
246 | $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php'; |
||
247 | |||
248 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
249 | PHPUnit_Framework_Assert::fileExists() |
||
250 | ); |
||
251 | |||
252 | try { |
||
253 | $constraint->evaluate($file, 'custom message'); |
||
254 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
255 | $this->assertEquals(<<<EOF |
||
256 | custom message |
||
257 | Failed asserting that file "$file" does not exist. |
||
258 | |||
259 | EOF |
||
260 | , |
||
261 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
262 | ); |
||
263 | |||
264 | return; |
||
265 | } |
||
266 | |||
267 | $this->fail(); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
272 | * @covers PHPUnit_Framework_Assert::greaterThan |
||
273 | * @covers PHPUnit_Framework_Constraint::count |
||
274 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
275 | */ |
||
276 | public function testConstraintGreaterThan() |
||
277 | { |
||
278 | $constraint = PHPUnit_Framework_Assert::greaterThan(1); |
||
279 | |||
280 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
281 | $this->assertTrue($constraint->evaluate(2, '', true)); |
||
282 | $this->assertEquals('is greater than 1', $constraint->toString()); |
||
283 | $this->assertEquals(1, count($constraint)); |
||
284 | |||
285 | try { |
||
286 | $constraint->evaluate(0); |
||
287 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
288 | $this->assertEquals( |
||
289 | <<<EOF |
||
290 | Failed asserting that 0 is greater than 1. |
||
291 | |||
292 | EOF |
||
293 | , |
||
294 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
295 | ); |
||
296 | |||
297 | return; |
||
298 | } |
||
299 | |||
300 | $this->fail(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
305 | * @covers PHPUnit_Framework_Assert::greaterThan |
||
306 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
307 | */ |
||
308 | public function testConstraintGreaterThan2() |
||
309 | { |
||
310 | $constraint = PHPUnit_Framework_Assert::greaterThan(1); |
||
311 | |||
312 | try { |
||
313 | $constraint->evaluate(0, 'custom message'); |
||
314 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
315 | $this->assertEquals( |
||
316 | <<<EOF |
||
317 | custom message |
||
318 | Failed asserting that 0 is greater than 1. |
||
319 | |||
320 | EOF |
||
321 | , |
||
322 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
323 | ); |
||
324 | |||
325 | return; |
||
326 | } |
||
327 | |||
328 | $this->fail(); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
333 | * @covers PHPUnit_Framework_Constraint_Not |
||
334 | * @covers PHPUnit_Framework_Assert::greaterThan |
||
335 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
336 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
337 | */ |
||
338 | public function testConstraintNotGreaterThan() |
||
339 | { |
||
340 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
341 | PHPUnit_Framework_Assert::greaterThan(1) |
||
342 | ); |
||
343 | |||
344 | $this->assertTrue($constraint->evaluate(1, '', true)); |
||
345 | $this->assertEquals('is not greater than 1', $constraint->toString()); |
||
346 | $this->assertEquals(1, count($constraint)); |
||
347 | |||
348 | try { |
||
349 | $constraint->evaluate(2); |
||
350 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
351 | $this->assertEquals( |
||
352 | <<<EOF |
||
353 | Failed asserting that 2 is not greater than 1. |
||
354 | |||
355 | EOF |
||
356 | , |
||
357 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
358 | ); |
||
359 | |||
360 | return; |
||
361 | } |
||
362 | |||
363 | $this->fail(); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
368 | * @covers PHPUnit_Framework_Constraint_Not |
||
369 | * @covers PHPUnit_Framework_Assert::greaterThan |
||
370 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
371 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
372 | */ |
||
373 | public function testConstraintNotGreaterThan2() |
||
374 | { |
||
375 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
376 | PHPUnit_Framework_Assert::greaterThan(1) |
||
377 | ); |
||
378 | |||
379 | try { |
||
380 | $constraint->evaluate(2, 'custom message'); |
||
381 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
382 | $this->assertEquals( |
||
383 | <<<EOF |
||
384 | custom message |
||
385 | Failed asserting that 2 is not greater than 1. |
||
386 | |||
387 | EOF |
||
388 | , |
||
389 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
390 | ); |
||
391 | |||
392 | return; |
||
393 | } |
||
394 | |||
395 | $this->fail(); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
400 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
401 | * @covers PHPUnit_Framework_Constraint_Or |
||
402 | * @covers PHPUnit_Framework_Assert::greaterThanOrEqual |
||
403 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
404 | */ |
||
405 | public function testConstraintGreaterThanOrEqual() |
||
406 | { |
||
407 | $constraint = PHPUnit_Framework_Assert::greaterThanOrEqual(1); |
||
408 | |||
409 | $this->assertTrue($constraint->evaluate(1, '', true)); |
||
410 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
411 | $this->assertEquals('is equal to 1 or is greater than 1', $constraint->toString()); |
||
412 | $this->assertEquals(2, count($constraint)); |
||
413 | |||
414 | try { |
||
415 | $constraint->evaluate(0); |
||
416 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
417 | $this->assertEquals( |
||
418 | <<<EOF |
||
419 | Failed asserting that 0 is equal to 1 or is greater than 1. |
||
420 | |||
421 | EOF |
||
422 | , |
||
423 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
424 | ); |
||
425 | |||
426 | return; |
||
427 | } |
||
428 | |||
429 | $this->fail(); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
434 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
435 | * @covers PHPUnit_Framework_Constraint_Or |
||
436 | * @covers PHPUnit_Framework_Assert::greaterThanOrEqual |
||
437 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
438 | */ |
||
439 | public function testConstraintGreaterThanOrEqual2() |
||
440 | { |
||
441 | $constraint = PHPUnit_Framework_Assert::greaterThanOrEqual(1); |
||
442 | |||
443 | try { |
||
444 | $constraint->evaluate(0, 'custom message'); |
||
445 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
446 | $this->assertEquals( |
||
447 | <<<EOF |
||
448 | custom message |
||
449 | Failed asserting that 0 is equal to 1 or is greater than 1. |
||
450 | |||
451 | EOF |
||
452 | , |
||
453 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
454 | ); |
||
455 | |||
456 | return; |
||
457 | } |
||
458 | |||
459 | $this->fail(); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
464 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
465 | * @covers PHPUnit_Framework_Constraint_Or |
||
466 | * @covers PHPUnit_Framework_Constraint_Not |
||
467 | * @covers PHPUnit_Framework_Assert::greaterThanOrEqual |
||
468 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
469 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
470 | */ |
||
471 | public function testConstraintNotGreaterThanOrEqual() |
||
472 | { |
||
473 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
474 | PHPUnit_Framework_Assert::greaterThanOrEqual(1) |
||
475 | ); |
||
476 | |||
477 | $this->assertFalse($constraint->evaluate(1, '', true)); |
||
478 | $this->assertEquals('not( is equal to 1 or is greater than 1 )', $constraint->toString()); |
||
479 | $this->assertEquals(2, count($constraint)); |
||
480 | |||
481 | try { |
||
482 | $constraint->evaluate(1); |
||
483 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
484 | $this->assertEquals( |
||
485 | <<<EOF |
||
486 | Failed asserting that not( 1 is equal to 1 or is greater than 1 ). |
||
487 | |||
488 | EOF |
||
489 | , |
||
490 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
491 | ); |
||
492 | |||
493 | return; |
||
494 | } |
||
495 | |||
496 | $this->fail(); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
501 | * @covers PHPUnit_Framework_Constraint_GreaterThan |
||
502 | * @covers PHPUnit_Framework_Constraint_Or |
||
503 | * @covers PHPUnit_Framework_Constraint_Not |
||
504 | * @covers PHPUnit_Framework_Assert::greaterThanOrEqual |
||
505 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
506 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
507 | */ |
||
508 | public function testConstraintNotGreaterThanOrEqual2() |
||
509 | { |
||
510 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
511 | PHPUnit_Framework_Assert::greaterThanOrEqual(1) |
||
512 | ); |
||
513 | |||
514 | try { |
||
515 | $constraint->evaluate(1, 'custom message'); |
||
516 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
517 | $this->assertEquals( |
||
518 | <<<EOF |
||
519 | custom message |
||
520 | Failed asserting that not( 1 is equal to 1 or is greater than 1 ). |
||
521 | |||
522 | EOF |
||
523 | , |
||
524 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
525 | ); |
||
526 | |||
527 | return; |
||
528 | } |
||
529 | |||
530 | $this->fail(); |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @covers PHPUnit_Framework_Constraint_IsAnything |
||
535 | * @covers PHPUnit_Framework_Assert::anything |
||
536 | * @covers PHPUnit_Framework_Constraint::count |
||
537 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
538 | */ |
||
539 | public function testConstraintIsAnything() |
||
540 | { |
||
541 | $constraint = PHPUnit_Framework_Assert::anything(); |
||
542 | |||
543 | $this->assertTrue($constraint->evaluate(null, '', true)); |
||
544 | $this->assertNull($constraint->evaluate(null)); |
||
545 | $this->assertEquals('is anything', $constraint->toString()); |
||
546 | $this->assertEquals(0, count($constraint)); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * @covers PHPUnit_Framework_Constraint_IsAnything |
||
551 | * @covers PHPUnit_Framework_Constraint_Not |
||
552 | * @covers PHPUnit_Framework_Assert::anything |
||
553 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
554 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
555 | */ |
||
556 | public function testConstraintNotIsAnything() |
||
557 | { |
||
558 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
559 | PHPUnit_Framework_Assert::anything() |
||
560 | ); |
||
561 | |||
562 | $this->assertFalse($constraint->evaluate(null, '', true)); |
||
563 | $this->assertEquals('is not anything', $constraint->toString()); |
||
564 | $this->assertEquals(0, count($constraint)); |
||
565 | |||
566 | try { |
||
567 | $constraint->evaluate(null); |
||
568 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
569 | $this->assertEquals( |
||
570 | <<<EOF |
||
571 | Failed asserting that null is not anything. |
||
572 | |||
573 | EOF |
||
574 | , |
||
575 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
576 | ); |
||
577 | |||
578 | return; |
||
579 | } |
||
580 | |||
581 | $this->fail(); |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
586 | * @covers PHPUnit_Framework_Assert::equalTo |
||
587 | * @covers PHPUnit_Framework_Constraint::count |
||
588 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
589 | */ |
||
590 | public function testConstraintIsEqual() |
||
591 | { |
||
592 | $constraint = PHPUnit_Framework_Assert::equalTo(1); |
||
593 | |||
594 | $this->assertTrue($constraint->evaluate(1, '', true)); |
||
595 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
596 | $this->assertEquals('is equal to 1', $constraint->toString()); |
||
597 | $this->assertEquals(1, count($constraint)); |
||
598 | |||
599 | try { |
||
600 | $constraint->evaluate(0); |
||
601 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
602 | $this->assertEquals( |
||
603 | <<<EOF |
||
604 | Failed asserting that 0 matches expected 1. |
||
605 | |||
606 | EOF |
||
607 | , |
||
608 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
609 | ); |
||
610 | |||
611 | return; |
||
612 | } |
||
613 | |||
614 | $this->fail(); |
||
615 | } |
||
616 | |||
617 | public function isEqualProvider() |
||
618 | { |
||
619 | $a = new stdClass; |
||
620 | $a->foo = 'bar'; |
||
621 | $b = new stdClass; |
||
622 | $ahash = spl_object_hash($a); |
||
623 | $bhash = spl_object_hash($b); |
||
624 | |||
625 | $c = new stdClass; |
||
626 | $c->foo = 'bar'; |
||
627 | $c->int = 1; |
||
628 | $c->array = array(0, array(1), array(2), 3); |
||
629 | $c->related = new stdClass; |
||
630 | $c->related->foo = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk"; |
||
631 | $c->self = $c; |
||
632 | $c->c = $c; |
||
633 | $d = new stdClass; |
||
634 | $d->foo = 'bar'; |
||
635 | $d->int = 2; |
||
636 | $d->array = array(0, array(4), array(2), 3); |
||
637 | $d->related = new stdClass; |
||
638 | $d->related->foo = "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk"; |
||
639 | $d->self = $d; |
||
640 | $d->c = $c; |
||
641 | |||
642 | $storage1 = new SplObjectStorage; |
||
643 | $storage1->attach($a); |
||
644 | $storage1->attach($b); |
||
645 | $storage2 = new SplObjectStorage; |
||
646 | $storage2->attach($b); |
||
647 | $storage1hash = spl_object_hash($storage1); |
||
648 | $storage2hash = spl_object_hash($storage2); |
||
649 | |||
650 | $dom1 = new DOMDocument; |
||
651 | $dom1->preserveWhiteSpace = false; |
||
652 | $dom1->loadXML('<root></root>'); |
||
653 | $dom2 = new DOMDocument; |
||
654 | $dom2->preserveWhiteSpace = false; |
||
655 | $dom2->loadXML('<root><foo/></root>'); |
||
656 | |||
657 | $data = array( |
||
658 | array(1, 0, <<<EOF |
||
659 | Failed asserting that 0 matches expected 1. |
||
660 | |||
661 | EOF |
||
662 | ), |
||
663 | array(1.1, 0, <<<EOF |
||
664 | Failed asserting that 0 matches expected 1.1. |
||
665 | |||
666 | EOF |
||
667 | ), |
||
668 | array('a', 'b', <<<EOF |
||
669 | Failed asserting that two strings are equal. |
||
670 | --- Expected |
||
671 | +++ Actual |
||
672 | @@ @@ |
||
673 | -'a' |
||
674 | +'b' |
||
675 | |||
676 | EOF |
||
677 | ), |
||
678 | array("a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk", "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk", <<<EOF |
||
679 | Failed asserting that two strings are equal. |
||
680 | --- Expected |
||
681 | +++ Actual |
||
682 | @@ @@ |
||
683 | 'a |
||
684 | -b |
||
685 | +p |
||
686 | |||
687 | @@ @@ |
||
688 | i |
||
689 | -j |
||
690 | +w |
||
691 | k' |
||
692 | |||
693 | EOF |
||
694 | ), |
||
695 | array(1, array(0), <<<EOF |
||
696 | Array (...) does not match expected type "integer". |
||
697 | |||
698 | EOF |
||
699 | ), |
||
700 | array(array(0), 1, <<<EOF |
||
701 | 1 does not match expected type "array". |
||
702 | |||
703 | EOF |
||
704 | ), |
||
705 | array(array(0), array(1), <<<EOF |
||
706 | Failed asserting that two arrays are equal. |
||
707 | --- Expected |
||
708 | +++ Actual |
||
709 | @@ @@ |
||
710 | Array ( |
||
711 | - 0 => 0 |
||
712 | + 0 => 1 |
||
713 | ) |
||
714 | |||
715 | EOF |
||
716 | ), |
||
717 | array(array(true), array('true'), <<<EOF |
||
718 | Failed asserting that two arrays are equal. |
||
719 | --- Expected |
||
720 | +++ Actual |
||
721 | @@ @@ |
||
722 | Array ( |
||
723 | - 0 => true |
||
724 | + 0 => 'true' |
||
725 | ) |
||
726 | |||
727 | EOF |
||
728 | ), |
||
729 | array(array(0, array(1), array(2), 3), array(0, array(4), array(2), 3), <<<EOF |
||
730 | Failed asserting that two arrays are equal. |
||
731 | --- Expected |
||
732 | +++ Actual |
||
733 | @@ @@ |
||
734 | Array ( |
||
735 | 0 => 0 |
||
736 | 1 => Array ( |
||
737 | - 0 => 1 |
||
738 | + 0 => 4 |
||
739 | ) |
||
740 | 2 => Array (...) |
||
741 | 3 => 3 |
||
742 | ) |
||
743 | |||
744 | EOF |
||
745 | ), |
||
746 | array($a, array(0), <<<EOF |
||
747 | Array (...) does not match expected type "object". |
||
748 | |||
749 | EOF |
||
750 | ), |
||
751 | array(array(0), $a, <<<EOF |
||
752 | stdClass Object (...) does not match expected type "array". |
||
753 | |||
754 | EOF |
||
755 | ), |
||
756 | array($a, $b, <<<EOF |
||
757 | Failed asserting that two objects are equal. |
||
758 | --- Expected |
||
759 | +++ Actual |
||
760 | @@ @@ |
||
761 | stdClass Object ( |
||
762 | - 'foo' => 'bar' |
||
763 | ) |
||
764 | |||
765 | EOF |
||
766 | ), |
||
767 | array($c, $d, <<<EOF |
||
768 | Failed asserting that two objects are equal. |
||
769 | --- Expected |
||
770 | +++ Actual |
||
771 | @@ @@ |
||
772 | stdClass Object ( |
||
773 | 'foo' => 'bar' |
||
774 | - 'int' => 1 |
||
775 | + 'int' => 2 |
||
776 | 'array' => Array ( |
||
777 | 0 => 0 |
||
778 | 1 => Array ( |
||
779 | - 0 => 1 |
||
780 | + 0 => 4 |
||
781 | |||
782 | @@ @@ |
||
783 | 'foo' => 'a |
||
784 | - b |
||
785 | + p |
||
786 | |||
787 | @@ @@ |
||
788 | i |
||
789 | - j |
||
790 | + w |
||
791 | k' |
||
792 | ) |
||
793 | 'self' => stdClass Object (...) |
||
794 | 'c' => stdClass Object (...) |
||
795 | ) |
||
796 | |||
797 | EOF |
||
798 | ), |
||
799 | array($dom1, $dom2, <<<EOF |
||
800 | Failed asserting that two DOM documents are equal. |
||
801 | --- Expected |
||
802 | +++ Actual |
||
803 | @@ @@ |
||
804 | <?xml version="1.0"?> |
||
805 | -<root/> |
||
806 | +<root> |
||
807 | + <foo/> |
||
808 | +</root> |
||
809 | |||
810 | EOF |
||
811 | ), |
||
812 | array( |
||
813 | new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')), |
||
814 | new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')), |
||
815 | <<<EOF |
||
816 | Failed asserting that two DateTime objects are equal. |
||
817 | --- Expected |
||
818 | +++ Actual |
||
819 | @@ @@ |
||
820 | -2013-03-29T04:13:35-0400 |
||
821 | +2013-03-29T04:13:35-0500 |
||
822 | |||
823 | EOF |
||
824 | ), |
||
825 | ); |
||
826 | |||
827 | if (PHP_MAJOR_VERSION < 7) { |
||
828 | $data[] = array($storage1, $storage2, <<<EOF |
||
829 | Failed asserting that two objects are equal. |
||
830 | --- Expected |
||
831 | +++ Actual |
||
832 | @@ @@ |
||
833 | -SplObjectStorage Object &$storage1hash ( |
||
834 | - '$ahash' => Array &0 ( |
||
835 | - 'obj' => stdClass Object &$ahash ( |
||
836 | - 'foo' => 'bar' |
||
837 | - ) |
||
838 | +SplObjectStorage Object &$storage2hash ( |
||
839 | + '$bhash' => Array &0 ( |
||
840 | + 'obj' => stdClass Object &$bhash () |
||
841 | 'inf' => null |
||
842 | ) |
||
843 | - '$bhash' => Array &0 |
||
844 | ) |
||
845 | |||
846 | EOF |
||
847 | ); |
||
848 | } else { |
||
849 | $data[] = array($storage1, $storage2, <<<EOF |
||
850 | Failed asserting that two objects are equal. |
||
851 | --- Expected |
||
852 | +++ Actual |
||
853 | @@ @@ |
||
854 | -SplObjectStorage Object &$storage1hash ( |
||
855 | - '$ahash' => Array &0 ( |
||
856 | - 'obj' => stdClass Object &$ahash ( |
||
857 | - 'foo' => 'bar' |
||
858 | - ) |
||
859 | +SplObjectStorage Object &$storage2hash ( |
||
860 | + '$bhash' => Array &0 ( |
||
861 | + 'obj' => stdClass Object &$bhash () |
||
862 | 'inf' => null |
||
863 | ) |
||
864 | - '$bhash' => Array &0 |
||
865 | ) |
||
866 | |||
867 | EOF |
||
868 | ); |
||
869 | } |
||
870 | |||
871 | return $data; |
||
872 | } |
||
873 | |||
874 | /** |
||
875 | * @dataProvider isEqualProvider |
||
876 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
877 | * @covers PHPUnit_Framework_Assert::equalTo |
||
878 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
879 | */ |
||
880 | public function testConstraintIsEqual2($expected, $actual, $message) |
||
881 | { |
||
882 | $constraint = PHPUnit_Framework_Assert::equalTo($expected); |
||
883 | |||
884 | try { |
||
885 | $constraint->evaluate($actual, 'custom message'); |
||
886 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
887 | $this->assertEquals( |
||
888 | "custom message\n$message", |
||
889 | $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e)) |
||
890 | ); |
||
891 | |||
892 | return; |
||
893 | } |
||
894 | |||
895 | $this->fail(); |
||
896 | } |
||
897 | |||
898 | /** |
||
899 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
900 | * @covers PHPUnit_Framework_Constraint_Not |
||
901 | * @covers PHPUnit_Framework_Assert::equalTo |
||
902 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
903 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
904 | */ |
||
905 | public function testConstraintIsNotEqual() |
||
906 | { |
||
907 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
908 | PHPUnit_Framework_Assert::equalTo(1) |
||
909 | ); |
||
910 | |||
911 | $this->assertTrue($constraint->evaluate(0, '', true)); |
||
912 | $this->assertFalse($constraint->evaluate(1, '', true)); |
||
913 | $this->assertEquals('is not equal to 1', $constraint->toString()); |
||
914 | $this->assertEquals(1, count($constraint)); |
||
915 | |||
916 | try { |
||
917 | $constraint->evaluate(1); |
||
918 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
919 | $this->assertEquals( |
||
920 | <<<EOF |
||
921 | Failed asserting that 1 is not equal to 1. |
||
922 | |||
923 | EOF |
||
924 | , |
||
925 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
926 | ); |
||
927 | |||
928 | return; |
||
929 | } |
||
930 | |||
931 | $this->fail(); |
||
932 | } |
||
933 | |||
934 | /** |
||
935 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
936 | * @covers PHPUnit_Framework_Constraint_Not |
||
937 | * @covers PHPUnit_Framework_Assert::equalTo |
||
938 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
939 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
940 | */ |
||
941 | public function testConstraintIsNotEqual2() |
||
942 | { |
||
943 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
944 | PHPUnit_Framework_Assert::equalTo(1) |
||
945 | ); |
||
946 | |||
947 | try { |
||
948 | $constraint->evaluate(1, 'custom message'); |
||
949 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
950 | $this->assertEquals( |
||
951 | <<<EOF |
||
952 | custom message |
||
953 | Failed asserting that 1 is not equal to 1. |
||
954 | |||
955 | EOF |
||
956 | , |
||
957 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
958 | ); |
||
959 | |||
960 | return; |
||
961 | } |
||
962 | |||
963 | $this->fail(); |
||
964 | } |
||
965 | |||
966 | /** |
||
967 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
968 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
969 | * @covers PHPUnit_Framework_Constraint::count |
||
970 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
971 | */ |
||
972 | public function testConstraintIsIdentical() |
||
973 | { |
||
974 | $a = new stdClass; |
||
975 | $b = new stdClass; |
||
976 | |||
977 | $constraint = PHPUnit_Framework_Assert::identicalTo($a); |
||
978 | |||
979 | $this->assertFalse($constraint->evaluate($b, '', true)); |
||
980 | $this->assertTrue($constraint->evaluate($a, '', true)); |
||
981 | $this->assertEquals('is identical to an object of class "stdClass"', $constraint->toString()); |
||
982 | $this->assertEquals(1, count($constraint)); |
||
983 | |||
984 | try { |
||
985 | $constraint->evaluate($b); |
||
986 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
987 | $this->assertEquals(<<<EOF |
||
988 | Failed asserting that two variables reference the same object. |
||
989 | |||
990 | EOF |
||
991 | , |
||
992 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
993 | ); |
||
994 | |||
995 | return; |
||
996 | } |
||
997 | |||
998 | $this->fail(); |
||
999 | } |
||
1000 | |||
1001 | /** |
||
1002 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
1003 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
1004 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1005 | */ |
||
1006 | public function testConstraintIsIdentical2() |
||
1007 | { |
||
1008 | $a = new stdClass; |
||
1009 | $b = new stdClass; |
||
1010 | |||
1011 | $constraint = PHPUnit_Framework_Assert::identicalTo($a); |
||
1012 | |||
1013 | try { |
||
1014 | $constraint->evaluate($b, 'custom message'); |
||
1015 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1016 | $this->assertEquals(<<<EOF |
||
1017 | custom message |
||
1018 | Failed asserting that two variables reference the same object. |
||
1019 | |||
1020 | EOF |
||
1021 | , |
||
1022 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1023 | ); |
||
1024 | |||
1025 | return; |
||
1026 | } |
||
1027 | |||
1028 | $this->fail(); |
||
1029 | } |
||
1030 | |||
1031 | /** |
||
1032 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
1033 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
1034 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1035 | */ |
||
1036 | public function testConstraintIsIdentical3() |
||
1037 | { |
||
1038 | $constraint = PHPUnit_Framework_Assert::identicalTo('a'); |
||
1039 | |||
1040 | try { |
||
1041 | $constraint->evaluate('b', 'custom message'); |
||
1042 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1043 | $this->assertEquals(<<<EOF |
||
1044 | custom message |
||
1045 | Failed asserting that two strings are identical. |
||
1046 | --- Expected |
||
1047 | +++ Actual |
||
1048 | @@ @@ |
||
1049 | -a |
||
1050 | +b |
||
1051 | |||
1052 | EOF |
||
1053 | , |
||
1054 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1055 | ); |
||
1056 | |||
1057 | return; |
||
1058 | } |
||
1059 | |||
1060 | $this->fail(); |
||
1061 | } |
||
1062 | |||
1063 | /** |
||
1064 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
1065 | * @covers PHPUnit_Framework_Constraint_Not |
||
1066 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
1067 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1068 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1069 | */ |
||
1070 | public function testConstraintIsNotIdentical() |
||
1071 | { |
||
1072 | $a = new stdClass; |
||
1073 | $b = new stdClass; |
||
1074 | |||
1075 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1076 | PHPUnit_Framework_Assert::identicalTo($a) |
||
1077 | ); |
||
1078 | |||
1079 | $this->assertTrue($constraint->evaluate($b, '', true)); |
||
1080 | $this->assertFalse($constraint->evaluate($a, '', true)); |
||
1081 | $this->assertEquals('is not identical to an object of class "stdClass"', $constraint->toString()); |
||
1082 | $this->assertEquals(1, count($constraint)); |
||
1083 | |||
1084 | try { |
||
1085 | $constraint->evaluate($a); |
||
1086 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1087 | $this->assertEquals(<<<EOF |
||
1088 | Failed asserting that two variables don't reference the same object. |
||
1089 | |||
1090 | EOF |
||
1091 | , |
||
1092 | $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e)) |
||
1093 | ); |
||
1094 | |||
1095 | return; |
||
1096 | } |
||
1097 | |||
1098 | $this->fail(); |
||
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
1103 | * @covers PHPUnit_Framework_Constraint_Not |
||
1104 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
1105 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1106 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1107 | */ |
||
1108 | public function testConstraintIsNotIdentical2() |
||
1109 | { |
||
1110 | $a = new stdClass; |
||
1111 | |||
1112 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1113 | PHPUnit_Framework_Assert::identicalTo($a) |
||
1114 | ); |
||
1115 | |||
1116 | try { |
||
1117 | $constraint->evaluate($a, 'custom message'); |
||
1118 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1119 | $this->assertEquals(<<<EOF |
||
1120 | custom message |
||
1121 | Failed asserting that two variables don't reference the same object. |
||
1122 | |||
1123 | EOF |
||
1124 | , |
||
1125 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1126 | ); |
||
1127 | |||
1128 | return; |
||
1129 | } |
||
1130 | |||
1131 | $this->fail(); |
||
1132 | } |
||
1133 | |||
1134 | /** |
||
1135 | * @covers PHPUnit_Framework_Constraint_IsIdentical |
||
1136 | * @covers PHPUnit_Framework_Constraint_Not |
||
1137 | * @covers PHPUnit_Framework_Assert::identicalTo |
||
1138 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1139 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1140 | */ |
||
1141 | public function testConstraintIsNotIdentical3() |
||
1142 | { |
||
1143 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1144 | PHPUnit_Framework_Assert::identicalTo('a') |
||
1145 | ); |
||
1146 | |||
1147 | try { |
||
1148 | $constraint->evaluate('a', 'custom message'); |
||
1149 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1150 | $this->assertEquals(<<<EOF |
||
1151 | custom message |
||
1152 | Failed asserting that two strings are not identical. |
||
1153 | |||
1154 | EOF |
||
1155 | , |
||
1156 | $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e)) |
||
1157 | ); |
||
1158 | |||
1159 | return; |
||
1160 | } |
||
1161 | |||
1162 | $this->fail(); |
||
1163 | } |
||
1164 | |||
1165 | /** |
||
1166 | * @covers PHPUnit_Framework_Constraint_IsInstanceOf |
||
1167 | * @covers PHPUnit_Framework_Assert::isInstanceOf |
||
1168 | * @covers PHPUnit_Framework_Constraint::count |
||
1169 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1170 | */ |
||
1171 | public function testConstraintIsInstanceOf() |
||
1172 | { |
||
1173 | $constraint = PHPUnit_Framework_Assert::isInstanceOf('Exception'); |
||
1174 | |||
1175 | $this->assertFalse($constraint->evaluate(new stdClass, '', true)); |
||
1176 | $this->assertTrue($constraint->evaluate(new Exception, '', true)); |
||
1177 | $this->assertEquals('is instance of class "Exception"', $constraint->toString()); |
||
1178 | $this->assertEquals(1, count($constraint)); |
||
1179 | |||
1180 | $interfaceConstraint = PHPUnit_Framework_Assert::isInstanceOf('Countable'); |
||
1181 | $this->assertFalse($interfaceConstraint->evaluate(new stdClass, '', true)); |
||
1182 | $this->assertTrue($interfaceConstraint->evaluate(new ArrayObject, '', true)); |
||
1183 | $this->assertEquals('is instance of interface "Countable"', $interfaceConstraint->toString()); |
||
1184 | |||
1185 | try { |
||
1186 | $constraint->evaluate(new stdClass); |
||
1187 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1188 | $this->assertEquals( |
||
1189 | <<<EOF |
||
1190 | Failed asserting that stdClass Object () is an instance of class "Exception". |
||
1191 | |||
1192 | EOF |
||
1193 | , |
||
1194 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1195 | ); |
||
1196 | |||
1197 | return; |
||
1198 | } |
||
1199 | |||
1200 | $this->fail(); |
||
1201 | } |
||
1202 | |||
1203 | /** |
||
1204 | * @covers PHPUnit_Framework_Constraint_IsInstanceOf |
||
1205 | * @covers PHPUnit_Framework_Assert::isInstanceOf |
||
1206 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1207 | */ |
||
1208 | public function testConstraintIsInstanceOf2() |
||
1209 | { |
||
1210 | $constraint = PHPUnit_Framework_Assert::isInstanceOf('Exception'); |
||
1211 | |||
1212 | try { |
||
1213 | $constraint->evaluate(new stdClass, 'custom message'); |
||
1214 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1215 | $this->assertEquals(<<<EOF |
||
1216 | custom message |
||
1217 | Failed asserting that stdClass Object () is an instance of class "Exception". |
||
1218 | |||
1219 | EOF |
||
1220 | , |
||
1221 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1222 | ); |
||
1223 | |||
1224 | return; |
||
1225 | } |
||
1226 | |||
1227 | $this->fail(); |
||
1228 | } |
||
1229 | |||
1230 | /** |
||
1231 | * @covers PHPUnit_Framework_Constraint_IsInstanceOf |
||
1232 | * @covers PHPUnit_Framework_Constraint_Not |
||
1233 | * @covers PHPUnit_Framework_Assert::isInstanceOf |
||
1234 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1235 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1236 | */ |
||
1237 | public function testConstraintIsNotInstanceOf() |
||
1238 | { |
||
1239 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1240 | PHPUnit_Framework_Assert::isInstanceOf('stdClass') |
||
1241 | ); |
||
1242 | |||
1243 | $this->assertFalse($constraint->evaluate(new stdClass, '', true)); |
||
1244 | $this->assertTrue($constraint->evaluate(new Exception, '', true)); |
||
1245 | $this->assertEquals('is not instance of class "stdClass"', $constraint->toString()); |
||
1246 | $this->assertEquals(1, count($constraint)); |
||
1247 | |||
1248 | try { |
||
1249 | $constraint->evaluate(new stdClass); |
||
1250 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1251 | $this->assertEquals( |
||
1252 | <<<EOF |
||
1253 | Failed asserting that stdClass Object () is not an instance of class "stdClass". |
||
1254 | |||
1255 | EOF |
||
1256 | , |
||
1257 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1258 | ); |
||
1259 | |||
1260 | return; |
||
1261 | } |
||
1262 | |||
1263 | $this->fail(); |
||
1264 | } |
||
1265 | |||
1266 | /** |
||
1267 | * @covers PHPUnit_Framework_Constraint_IsInstanceOf |
||
1268 | * @covers PHPUnit_Framework_Constraint_Not |
||
1269 | * @covers PHPUnit_Framework_Assert::isInstanceOf |
||
1270 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1271 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1272 | */ |
||
1273 | public function testConstraintIsNotInstanceOf2() |
||
1274 | { |
||
1275 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1276 | PHPUnit_Framework_Assert::isInstanceOf('stdClass') |
||
1277 | ); |
||
1278 | |||
1279 | try { |
||
1280 | $constraint->evaluate(new stdClass, 'custom message'); |
||
1281 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1282 | $this->assertEquals(<<<EOF |
||
1283 | custom message |
||
1284 | Failed asserting that stdClass Object () is not an instance of class "stdClass". |
||
1285 | |||
1286 | EOF |
||
1287 | , |
||
1288 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1289 | ); |
||
1290 | |||
1291 | return; |
||
1292 | } |
||
1293 | |||
1294 | $this->fail(); |
||
1295 | } |
||
1296 | |||
1297 | /** |
||
1298 | * @covers PHPUnit_Framework_Constraint_IsType |
||
1299 | * @covers PHPUnit_Framework_Assert::isType |
||
1300 | * @covers PHPUnit_Framework_Constraint::count |
||
1301 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1302 | */ |
||
1303 | public function testConstraintIsType() |
||
1304 | { |
||
1305 | $constraint = PHPUnit_Framework_Assert::isType('string'); |
||
1306 | |||
1307 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
1308 | $this->assertTrue($constraint->evaluate('', '', true)); |
||
1309 | $this->assertEquals('is of type "string"', $constraint->toString()); |
||
1310 | $this->assertEquals(1, count($constraint)); |
||
1311 | |||
1312 | try { |
||
1313 | $constraint->evaluate(new stdClass); |
||
1314 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1315 | $this->assertStringMatchesFormat(<<<EOF |
||
1316 | Failed asserting that stdClass Object &%x () is of type "string". |
||
1317 | |||
1318 | EOF |
||
1319 | , |
||
1320 | $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e)) |
||
1321 | ); |
||
1322 | |||
1323 | return; |
||
1324 | } |
||
1325 | |||
1326 | $this->fail(); |
||
1327 | } |
||
1328 | |||
1329 | /** |
||
1330 | * @covers PHPUnit_Framework_Constraint_IsType |
||
1331 | * @covers PHPUnit_Framework_Assert::isType |
||
1332 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1333 | */ |
||
1334 | public function testConstraintIsType2() |
||
1335 | { |
||
1336 | $constraint = PHPUnit_Framework_Assert::isType('string'); |
||
1337 | |||
1338 | try { |
||
1339 | $constraint->evaluate(new stdClass, 'custom message'); |
||
1340 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1341 | $this->assertStringMatchesFormat(<<<EOF |
||
1342 | custom message |
||
1343 | Failed asserting that stdClass Object &%x () is of type "string". |
||
1344 | |||
1345 | EOF |
||
1346 | , |
||
1347 | $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e)) |
||
1348 | ); |
||
1349 | |||
1350 | return; |
||
1351 | } |
||
1352 | |||
1353 | $this->fail(); |
||
1354 | } |
||
1355 | |||
1356 | public function resources() |
||
1357 | { |
||
1358 | $fh = fopen(__FILE__, 'r'); |
||
1359 | fclose($fh); |
||
1360 | |||
1361 | return array( |
||
1362 | 'open resource' => array(fopen(__FILE__, 'r')), |
||
1363 | 'closed resource' => array($fh), |
||
1364 | ); |
||
1365 | } |
||
1366 | |||
1367 | /** |
||
1368 | * @dataProvider resources |
||
1369 | * @covers PHPUnit_Framework_Constraint_IsType |
||
1370 | * @covers PHPUnit_Framework_Assert::isType |
||
1371 | */ |
||
1372 | public function testConstraintIsResourceTypeEvaluatesCorrectlyWithResources($resource) |
||
1373 | { |
||
1374 | $constraint = PHPUnit_Framework_Assert::isType('resource'); |
||
1375 | |||
1376 | $this->assertTrue($constraint->evaluate($resource, '', true)); |
||
1377 | |||
1378 | @fclose($resource); |
||
1379 | } |
||
1380 | |||
1381 | /** |
||
1382 | * @covers PHPUnit_Framework_Constraint_IsType |
||
1383 | * @covers PHPUnit_Framework_Constraint_Not |
||
1384 | * @covers PHPUnit_Framework_Assert::isType |
||
1385 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1386 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1387 | */ |
||
1388 | public function testConstraintIsNotType() |
||
1389 | { |
||
1390 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1391 | PHPUnit_Framework_Assert::isType('string') |
||
1392 | ); |
||
1393 | |||
1394 | $this->assertTrue($constraint->evaluate(0, '', true)); |
||
1395 | $this->assertFalse($constraint->evaluate('', '', true)); |
||
1396 | $this->assertEquals('is not of type "string"', $constraint->toString()); |
||
1397 | $this->assertEquals(1, count($constraint)); |
||
1398 | |||
1399 | try { |
||
1400 | $constraint->evaluate(''); |
||
1401 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1402 | $this->assertEquals( |
||
1403 | <<<EOF |
||
1404 | Failed asserting that '' is not of type "string". |
||
1405 | |||
1406 | EOF |
||
1407 | , |
||
1408 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1409 | ); |
||
1410 | |||
1411 | return; |
||
1412 | } |
||
1413 | |||
1414 | $this->fail(); |
||
1415 | } |
||
1416 | |||
1417 | /** |
||
1418 | * @covers PHPUnit_Framework_Constraint_IsType |
||
1419 | * @covers PHPUnit_Framework_Constraint_Not |
||
1420 | * @covers PHPUnit_Framework_Assert::isType |
||
1421 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1422 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1423 | */ |
||
1424 | public function testConstraintIsNotType2() |
||
1425 | { |
||
1426 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1427 | PHPUnit_Framework_Assert::isType('string') |
||
1428 | ); |
||
1429 | |||
1430 | try { |
||
1431 | $constraint->evaluate('', 'custom message'); |
||
1432 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1433 | $this->assertEquals(<<<EOF |
||
1434 | custom message |
||
1435 | Failed asserting that '' is not of type "string". |
||
1436 | |||
1437 | EOF |
||
1438 | , |
||
1439 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1440 | ); |
||
1441 | |||
1442 | return; |
||
1443 | } |
||
1444 | |||
1445 | $this->fail(); |
||
1446 | } |
||
1447 | |||
1448 | /** |
||
1449 | * @covers PHPUnit_Framework_Constraint_IsNull |
||
1450 | * @covers PHPUnit_Framework_Assert::isNull |
||
1451 | * @covers PHPUnit_Framework_Constraint::count |
||
1452 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1453 | */ |
||
1454 | public function testConstraintIsNull() |
||
1455 | { |
||
1456 | $constraint = PHPUnit_Framework_Assert::isNull(); |
||
1457 | |||
1458 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
1459 | $this->assertTrue($constraint->evaluate(null, '', true)); |
||
1460 | $this->assertEquals('is null', $constraint->toString()); |
||
1461 | $this->assertEquals(1, count($constraint)); |
||
1462 | |||
1463 | try { |
||
1464 | $constraint->evaluate(0); |
||
1465 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1466 | $this->assertEquals(<<<EOF |
||
1467 | Failed asserting that 0 is null. |
||
1468 | |||
1469 | EOF |
||
1470 | , |
||
1471 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1472 | ); |
||
1473 | |||
1474 | return; |
||
1475 | } |
||
1476 | |||
1477 | $this->fail(); |
||
1478 | } |
||
1479 | |||
1480 | /** |
||
1481 | * @covers PHPUnit_Framework_Constraint_IsNull |
||
1482 | * @covers PHPUnit_Framework_Assert::isNull |
||
1483 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1484 | */ |
||
1485 | public function testConstraintIsNull2() |
||
1486 | { |
||
1487 | $constraint = PHPUnit_Framework_Assert::isNull(); |
||
1488 | |||
1489 | try { |
||
1490 | $constraint->evaluate(0, 'custom message'); |
||
1491 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1492 | $this->assertEquals(<<<EOF |
||
1493 | custom message |
||
1494 | Failed asserting that 0 is null. |
||
1495 | |||
1496 | EOF |
||
1497 | , |
||
1498 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1499 | ); |
||
1500 | |||
1501 | return; |
||
1502 | } |
||
1503 | |||
1504 | $this->fail(); |
||
1505 | } |
||
1506 | |||
1507 | /** |
||
1508 | * @covers PHPUnit_Framework_Constraint_IsNull |
||
1509 | * @covers PHPUnit_Framework_Constraint_Not |
||
1510 | * @covers PHPUnit_Framework_Assert::isNull |
||
1511 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1512 | * @covers PHPUnit_Framework_Constraint::count |
||
1513 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1514 | */ |
||
1515 | public function testConstraintIsNotNull() |
||
1516 | { |
||
1517 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1518 | PHPUnit_Framework_Assert::isNull() |
||
1519 | ); |
||
1520 | |||
1521 | $this->assertFalse($constraint->evaluate(null, '', true)); |
||
1522 | $this->assertTrue($constraint->evaluate(0, '', true)); |
||
1523 | $this->assertEquals('is not null', $constraint->toString()); |
||
1524 | $this->assertEquals(1, count($constraint)); |
||
1525 | |||
1526 | try { |
||
1527 | $constraint->evaluate(null); |
||
1528 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1529 | $this->assertEquals(<<<EOF |
||
1530 | Failed asserting that null is not null. |
||
1531 | |||
1532 | EOF |
||
1533 | , |
||
1534 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1535 | ); |
||
1536 | |||
1537 | return; |
||
1538 | } |
||
1539 | |||
1540 | $this->fail(); |
||
1541 | } |
||
1542 | |||
1543 | /** |
||
1544 | * @covers PHPUnit_Framework_Constraint_IsNull |
||
1545 | * @covers PHPUnit_Framework_Constraint_Not |
||
1546 | * @covers PHPUnit_Framework_Assert::isNull |
||
1547 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1548 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1549 | */ |
||
1550 | public function testConstraintIsNotNull2() |
||
1551 | { |
||
1552 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1553 | PHPUnit_Framework_Assert::isNull() |
||
1554 | ); |
||
1555 | |||
1556 | try { |
||
1557 | $constraint->evaluate(null, 'custom message'); |
||
1558 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1559 | $this->assertEquals(<<<EOF |
||
1560 | custom message |
||
1561 | Failed asserting that null is not null. |
||
1562 | |||
1563 | EOF |
||
1564 | , |
||
1565 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1566 | ); |
||
1567 | |||
1568 | return; |
||
1569 | } |
||
1570 | |||
1571 | $this->fail(); |
||
1572 | } |
||
1573 | |||
1574 | /** |
||
1575 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1576 | * @covers PHPUnit_Framework_Assert::lessThan |
||
1577 | * @covers PHPUnit_Framework_Constraint::count |
||
1578 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1579 | */ |
||
1580 | public function testConstraintLessThan() |
||
1581 | { |
||
1582 | $constraint = PHPUnit_Framework_Assert::lessThan(1); |
||
1583 | |||
1584 | $this->assertTrue($constraint->evaluate(0, '', true)); |
||
1585 | $this->assertFalse($constraint->evaluate(1, '', true)); |
||
1586 | $this->assertEquals('is less than 1', $constraint->toString()); |
||
1587 | $this->assertEquals(1, count($constraint)); |
||
1588 | |||
1589 | try { |
||
1590 | $constraint->evaluate(1); |
||
1591 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1592 | $this->assertEquals( |
||
1593 | <<<EOF |
||
1594 | Failed asserting that 1 is less than 1. |
||
1595 | |||
1596 | EOF |
||
1597 | , |
||
1598 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1599 | ); |
||
1600 | |||
1601 | return; |
||
1602 | } |
||
1603 | |||
1604 | $this->fail(); |
||
1605 | } |
||
1606 | |||
1607 | /** |
||
1608 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1609 | * @covers PHPUnit_Framework_Assert::lessThan |
||
1610 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1611 | */ |
||
1612 | public function testConstraintLessThan2() |
||
1613 | { |
||
1614 | $constraint = PHPUnit_Framework_Assert::lessThan(1); |
||
1615 | |||
1616 | try { |
||
1617 | $constraint->evaluate(1, 'custom message'); |
||
1618 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1619 | $this->assertEquals( |
||
1620 | <<<EOF |
||
1621 | custom message |
||
1622 | Failed asserting that 1 is less than 1. |
||
1623 | |||
1624 | EOF |
||
1625 | , |
||
1626 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1627 | ); |
||
1628 | |||
1629 | return; |
||
1630 | } |
||
1631 | |||
1632 | $this->fail(); |
||
1633 | } |
||
1634 | |||
1635 | /** |
||
1636 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1637 | * @covers PHPUnit_Framework_Constraint_Not |
||
1638 | * @covers PHPUnit_Framework_Assert::lessThan |
||
1639 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1640 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1641 | */ |
||
1642 | public function testConstraintNotLessThan() |
||
1643 | { |
||
1644 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1645 | PHPUnit_Framework_Assert::lessThan(1) |
||
1646 | ); |
||
1647 | |||
1648 | $this->assertTrue($constraint->evaluate(1, '', true)); |
||
1649 | $this->assertFalse($constraint->evaluate(0, '', true)); |
||
1650 | $this->assertEquals('is not less than 1', $constraint->toString()); |
||
1651 | $this->assertEquals(1, count($constraint)); |
||
1652 | |||
1653 | try { |
||
1654 | $constraint->evaluate(0); |
||
1655 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1656 | $this->assertEquals( |
||
1657 | <<<EOF |
||
1658 | Failed asserting that 0 is not less than 1. |
||
1659 | |||
1660 | EOF |
||
1661 | , |
||
1662 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1663 | ); |
||
1664 | |||
1665 | return; |
||
1666 | } |
||
1667 | |||
1668 | $this->fail(); |
||
1669 | } |
||
1670 | |||
1671 | /** |
||
1672 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1673 | * @covers PHPUnit_Framework_Constraint_Not |
||
1674 | * @covers PHPUnit_Framework_Assert::lessThan |
||
1675 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1676 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1677 | */ |
||
1678 | public function testConstraintNotLessThan2() |
||
1679 | { |
||
1680 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1681 | PHPUnit_Framework_Assert::lessThan(1) |
||
1682 | ); |
||
1683 | |||
1684 | try { |
||
1685 | $constraint->evaluate(0, 'custom message'); |
||
1686 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1687 | $this->assertEquals( |
||
1688 | <<<EOF |
||
1689 | custom message |
||
1690 | Failed asserting that 0 is not less than 1. |
||
1691 | |||
1692 | EOF |
||
1693 | , |
||
1694 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1695 | ); |
||
1696 | |||
1697 | return; |
||
1698 | } |
||
1699 | |||
1700 | $this->fail(); |
||
1701 | } |
||
1702 | |||
1703 | /** |
||
1704 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
1705 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1706 | * @covers PHPUnit_Framework_Constraint_Or |
||
1707 | * @covers PHPUnit_Framework_Assert::lessThanOrEqual |
||
1708 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1709 | */ |
||
1710 | public function testConstraintLessThanOrEqual() |
||
1711 | { |
||
1712 | $constraint = PHPUnit_Framework_Assert::lessThanOrEqual(1); |
||
1713 | |||
1714 | $this->assertTrue($constraint->evaluate(1, '', true)); |
||
1715 | $this->assertFalse($constraint->evaluate(2, '', true)); |
||
1716 | $this->assertEquals('is equal to 1 or is less than 1', $constraint->toString()); |
||
1717 | $this->assertEquals(2, count($constraint)); |
||
1718 | |||
1719 | try { |
||
1720 | $constraint->evaluate(2); |
||
1721 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1722 | $this->assertEquals( |
||
1723 | <<<EOF |
||
1724 | Failed asserting that 2 is equal to 1 or is less than 1. |
||
1725 | |||
1726 | EOF |
||
1727 | , |
||
1728 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1729 | ); |
||
1730 | |||
1731 | return; |
||
1732 | } |
||
1733 | |||
1734 | $this->fail(); |
||
1735 | } |
||
1736 | |||
1737 | /** |
||
1738 | * @covers PHPUnit_Framework_Constraint_Callback |
||
1739 | */ |
||
1740 | public function testConstraintCallback() |
||
1741 | { |
||
1742 | $closureReflect = function ($parameter) { |
||
1743 | return $parameter; |
||
1744 | }; |
||
1745 | |||
1746 | $closureWithoutParameter = function () { |
||
1747 | return true; |
||
1748 | }; |
||
1749 | |||
1750 | $constraint = PHPUnit_Framework_Assert::callback($closureWithoutParameter); |
||
1751 | $this->assertTrue($constraint->evaluate('', '', true)); |
||
1752 | |||
1753 | $constraint = PHPUnit_Framework_Assert::callback($closureReflect); |
||
1754 | $this->assertTrue($constraint->evaluate(true, '', true)); |
||
1755 | $this->assertFalse($constraint->evaluate(false, '', true)); |
||
1756 | |||
1757 | $callback = array($this, 'callbackReturningTrue'); |
||
1758 | $constraint = PHPUnit_Framework_Assert::callback($callback); |
||
1759 | $this->assertTrue($constraint->evaluate(false, '', true)); |
||
1760 | |||
1761 | $callback = array('Framework_ConstraintTest', 'staticCallbackReturningTrue'); |
||
1762 | $constraint = PHPUnit_Framework_Assert::callback($callback); |
||
1763 | $this->assertTrue($constraint->evaluate(null, '', true)); |
||
1764 | |||
1765 | $this->assertEquals('is accepted by specified callback', $constraint->toString()); |
||
1766 | } |
||
1767 | |||
1768 | /** |
||
1769 | * @covers PHPUnit_Framework_Constraint_Callback |
||
1770 | * @expectedException PHPUnit_Framework_ExpectationFailedException |
||
1771 | * @expectedExceptionMessage Failed asserting that 'This fails' is accepted by specified callback. |
||
1772 | */ |
||
1773 | public function testConstraintCallbackFailure() |
||
1774 | { |
||
1775 | $constraint = PHPUnit_Framework_Assert::callback(function () { |
||
1776 | return false; |
||
1777 | }); |
||
1778 | $constraint->evaluate('This fails'); |
||
1779 | } |
||
1780 | |||
1781 | public function callbackReturningTrue() |
||
1782 | { |
||
1783 | return true; |
||
1784 | } |
||
1785 | |||
1786 | public static function staticCallbackReturningTrue() |
||
1787 | { |
||
1788 | return true; |
||
1789 | } |
||
1790 | |||
1791 | /** |
||
1792 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
1793 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1794 | * @covers PHPUnit_Framework_Constraint_Or |
||
1795 | * @covers PHPUnit_Framework_Assert::lessThanOrEqual |
||
1796 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1797 | */ |
||
1798 | public function testConstraintLessThanOrEqual2() |
||
1799 | { |
||
1800 | $constraint = PHPUnit_Framework_Assert::lessThanOrEqual(1); |
||
1801 | |||
1802 | try { |
||
1803 | $constraint->evaluate(2, 'custom message'); |
||
1804 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1805 | $this->assertEquals( |
||
1806 | <<<EOF |
||
1807 | custom message |
||
1808 | Failed asserting that 2 is equal to 1 or is less than 1. |
||
1809 | |||
1810 | EOF |
||
1811 | , |
||
1812 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1813 | ); |
||
1814 | |||
1815 | return; |
||
1816 | } |
||
1817 | |||
1818 | $this->fail(); |
||
1819 | } |
||
1820 | |||
1821 | /** |
||
1822 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
1823 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1824 | * @covers PHPUnit_Framework_Constraint_Or |
||
1825 | * @covers PHPUnit_Framework_Constraint_Not |
||
1826 | * @covers PHPUnit_Framework_Assert::lessThanOrEqual |
||
1827 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1828 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1829 | */ |
||
1830 | public function testConstraintNotLessThanOrEqual() |
||
1831 | { |
||
1832 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1833 | PHPUnit_Framework_Assert::lessThanOrEqual(1) |
||
1834 | ); |
||
1835 | |||
1836 | $this->assertTrue($constraint->evaluate(2, '', true)); |
||
1837 | $this->assertFalse($constraint->evaluate(1, '', true)); |
||
1838 | $this->assertEquals('not( is equal to 1 or is less than 1 )', $constraint->toString()); |
||
1839 | $this->assertEquals(2, count($constraint)); |
||
1840 | |||
1841 | try { |
||
1842 | $constraint->evaluate(1); |
||
1843 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1844 | $this->assertEquals( |
||
1845 | <<<EOF |
||
1846 | Failed asserting that not( 1 is equal to 1 or is less than 1 ). |
||
1847 | |||
1848 | EOF |
||
1849 | , |
||
1850 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1851 | ); |
||
1852 | |||
1853 | return; |
||
1854 | } |
||
1855 | |||
1856 | $this->fail(); |
||
1857 | } |
||
1858 | |||
1859 | /** |
||
1860 | * @covers PHPUnit_Framework_Constraint_IsEqual |
||
1861 | * @covers PHPUnit_Framework_Constraint_LessThan |
||
1862 | * @covers PHPUnit_Framework_Constraint_Or |
||
1863 | * @covers PHPUnit_Framework_Constraint_Not |
||
1864 | * @covers PHPUnit_Framework_Assert::lessThanOrEqual |
||
1865 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1866 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1867 | */ |
||
1868 | public function testConstraintNotLessThanOrEqual2() |
||
1869 | { |
||
1870 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1871 | PHPUnit_Framework_Assert::lessThanOrEqual(1) |
||
1872 | ); |
||
1873 | |||
1874 | try { |
||
1875 | $constraint->evaluate(1, 'custom message'); |
||
1876 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1877 | $this->assertEquals( |
||
1878 | <<<EOF |
||
1879 | custom message |
||
1880 | Failed asserting that not( 1 is equal to 1 or is less than 1 ). |
||
1881 | |||
1882 | EOF |
||
1883 | , |
||
1884 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1885 | ); |
||
1886 | |||
1887 | return; |
||
1888 | } |
||
1889 | |||
1890 | $this->fail(); |
||
1891 | } |
||
1892 | |||
1893 | /** |
||
1894 | * @covers PHPUnit_Framework_Constraint_ClassHasAttribute |
||
1895 | * @covers PHPUnit_Framework_Assert::classHasAttribute |
||
1896 | * @covers PHPUnit_Framework_Constraint::count |
||
1897 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1898 | */ |
||
1899 | public function testConstraintClassHasAttribute() |
||
1900 | { |
||
1901 | $constraint = PHPUnit_Framework_Assert::classHasAttribute('privateAttribute'); |
||
1902 | |||
1903 | $this->assertTrue($constraint->evaluate('ClassWithNonPublicAttributes', '', true)); |
||
1904 | $this->assertFalse($constraint->evaluate('stdClass', '', true)); |
||
1905 | $this->assertEquals('has attribute "privateAttribute"', $constraint->toString()); |
||
1906 | $this->assertEquals(1, count($constraint)); |
||
1907 | |||
1908 | try { |
||
1909 | $constraint->evaluate('stdClass'); |
||
1910 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1911 | $this->assertEquals( |
||
1912 | <<<EOF |
||
1913 | Failed asserting that class "stdClass" has attribute "privateAttribute". |
||
1914 | |||
1915 | EOF |
||
1916 | , |
||
1917 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1918 | ); |
||
1919 | |||
1920 | return; |
||
1921 | } |
||
1922 | |||
1923 | $this->fail(); |
||
1924 | } |
||
1925 | |||
1926 | /** |
||
1927 | * @covers PHPUnit_Framework_Constraint_ClassHasAttribute |
||
1928 | * @covers PHPUnit_Framework_Assert::classHasAttribute |
||
1929 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1930 | */ |
||
1931 | public function testConstraintClassHasAttribute2() |
||
1932 | { |
||
1933 | $constraint = PHPUnit_Framework_Assert::classHasAttribute('privateAttribute'); |
||
1934 | |||
1935 | try { |
||
1936 | $constraint->evaluate('stdClass', 'custom message'); |
||
1937 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1938 | $this->assertEquals(<<<EOF |
||
1939 | custom message |
||
1940 | Failed asserting that class "stdClass" has attribute "privateAttribute". |
||
1941 | |||
1942 | EOF |
||
1943 | , |
||
1944 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1945 | ); |
||
1946 | |||
1947 | return; |
||
1948 | } |
||
1949 | |||
1950 | $this->fail(); |
||
1951 | } |
||
1952 | |||
1953 | /** |
||
1954 | * @covers PHPUnit_Framework_Constraint_ClassHasAttribute |
||
1955 | * @covers PHPUnit_Framework_Constraint_Not |
||
1956 | * @covers PHPUnit_Framework_Assert::classHasAttribute |
||
1957 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1958 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1959 | */ |
||
1960 | public function testConstraintClassNotHasAttribute() |
||
1961 | { |
||
1962 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1963 | PHPUnit_Framework_Assert::classHasAttribute('privateAttribute') |
||
1964 | ); |
||
1965 | |||
1966 | $this->assertTrue($constraint->evaluate('stdClass', '', true)); |
||
1967 | $this->assertFalse($constraint->evaluate('ClassWithNonPublicAttributes', '', true)); |
||
1968 | $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString()); |
||
1969 | $this->assertEquals(1, count($constraint)); |
||
1970 | |||
1971 | try { |
||
1972 | $constraint->evaluate('ClassWithNonPublicAttributes'); |
||
1973 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
1974 | $this->assertEquals( |
||
1975 | <<<EOF |
||
1976 | Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". |
||
1977 | |||
1978 | EOF |
||
1979 | , |
||
1980 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
1981 | ); |
||
1982 | |||
1983 | return; |
||
1984 | } |
||
1985 | |||
1986 | $this->fail(); |
||
1987 | } |
||
1988 | |||
1989 | /** |
||
1990 | * @covers PHPUnit_Framework_Constraint_ClassHasAttribute |
||
1991 | * @covers PHPUnit_Framework_Constraint_Not |
||
1992 | * @covers PHPUnit_Framework_Assert::classHasAttribute |
||
1993 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
1994 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
1995 | */ |
||
1996 | public function testConstraintClassNotHasAttribute2() |
||
1997 | { |
||
1998 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
1999 | PHPUnit_Framework_Assert::classHasAttribute('privateAttribute') |
||
2000 | ); |
||
2001 | |||
2002 | try { |
||
2003 | $constraint->evaluate('ClassWithNonPublicAttributes', 'custom message'); |
||
2004 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2005 | $this->assertEquals(<<<EOF |
||
2006 | custom message |
||
2007 | Failed asserting that class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". |
||
2008 | |||
2009 | EOF |
||
2010 | , |
||
2011 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2012 | ); |
||
2013 | |||
2014 | return; |
||
2015 | } |
||
2016 | |||
2017 | $this->fail(); |
||
2018 | } |
||
2019 | |||
2020 | /** |
||
2021 | * @covers PHPUnit_Framework_Constraint_ClassHasStaticAttribute |
||
2022 | * @covers PHPUnit_Framework_Assert::classHasStaticAttribute |
||
2023 | * @covers PHPUnit_Framework_Constraint::count |
||
2024 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2025 | */ |
||
2026 | public function testConstraintClassHasStaticAttribute() |
||
2027 | { |
||
2028 | $constraint = PHPUnit_Framework_Assert::classHasStaticAttribute('privateStaticAttribute'); |
||
2029 | |||
2030 | $this->assertTrue($constraint->evaluate('ClassWithNonPublicAttributes', '', true)); |
||
2031 | $this->assertFalse($constraint->evaluate('stdClass', '', true)); |
||
2032 | $this->assertEquals('has static attribute "privateStaticAttribute"', $constraint->toString()); |
||
2033 | $this->assertEquals(1, count($constraint)); |
||
2034 | |||
2035 | try { |
||
2036 | $constraint->evaluate('stdClass'); |
||
2037 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2038 | $this->assertEquals( |
||
2039 | <<<EOF |
||
2040 | Failed asserting that class "stdClass" has static attribute "privateStaticAttribute". |
||
2041 | |||
2042 | EOF |
||
2043 | , |
||
2044 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2045 | ); |
||
2046 | |||
2047 | return; |
||
2048 | } |
||
2049 | |||
2050 | $this->fail(); |
||
2051 | } |
||
2052 | |||
2053 | /** |
||
2054 | * @covers PHPUnit_Framework_Constraint_ClassHasStaticAttribute |
||
2055 | * @covers PHPUnit_Framework_Assert::classHasStaticAttribute |
||
2056 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2057 | */ |
||
2058 | public function testConstraintClassHasStaticAttribute2() |
||
2059 | { |
||
2060 | $constraint = PHPUnit_Framework_Assert::classHasStaticAttribute('foo'); |
||
2061 | |||
2062 | try { |
||
2063 | $constraint->evaluate('stdClass', 'custom message'); |
||
2064 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2065 | $this->assertEquals(<<<EOF |
||
2066 | custom message |
||
2067 | Failed asserting that class "stdClass" has static attribute "foo". |
||
2068 | |||
2069 | EOF |
||
2070 | , |
||
2071 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2072 | ); |
||
2073 | |||
2074 | return; |
||
2075 | } |
||
2076 | |||
2077 | $this->fail(); |
||
2078 | } |
||
2079 | |||
2080 | /** |
||
2081 | * @covers PHPUnit_Framework_Constraint_ClassHasStaticAttribute |
||
2082 | * @covers PHPUnit_Framework_Constraint_Not |
||
2083 | * @covers PHPUnit_Framework_Assert::classHasStaticAttribute |
||
2084 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2085 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2086 | */ |
||
2087 | public function testConstraintClassNotHasStaticAttribute() |
||
2088 | { |
||
2089 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2090 | PHPUnit_Framework_Assert::classHasStaticAttribute('privateStaticAttribute') |
||
2091 | ); |
||
2092 | |||
2093 | $this->assertTrue($constraint->evaluate('stdClass', '', true)); |
||
2094 | $this->assertFalse($constraint->evaluate('ClassWithNonPublicAttributes', '', true)); |
||
2095 | $this->assertEquals('does not have static attribute "privateStaticAttribute"', $constraint->toString()); |
||
2096 | $this->assertEquals(1, count($constraint)); |
||
2097 | |||
2098 | try { |
||
2099 | $constraint->evaluate('ClassWithNonPublicAttributes'); |
||
2100 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2101 | $this->assertEquals( |
||
2102 | <<<EOF |
||
2103 | Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute". |
||
2104 | |||
2105 | EOF |
||
2106 | , |
||
2107 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2108 | ); |
||
2109 | |||
2110 | return; |
||
2111 | } |
||
2112 | |||
2113 | $this->fail(); |
||
2114 | } |
||
2115 | |||
2116 | /** |
||
2117 | * @covers PHPUnit_Framework_Constraint_ClassHasStaticAttribute |
||
2118 | * @covers PHPUnit_Framework_Constraint_Not |
||
2119 | * @covers PHPUnit_Framework_Assert::classHasStaticAttribute |
||
2120 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2121 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2122 | */ |
||
2123 | public function testConstraintClassNotHasStaticAttribute2() |
||
2124 | { |
||
2125 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2126 | PHPUnit_Framework_Assert::classHasStaticAttribute('privateStaticAttribute') |
||
2127 | ); |
||
2128 | |||
2129 | try { |
||
2130 | $constraint->evaluate('ClassWithNonPublicAttributes', 'custom message'); |
||
2131 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2132 | $this->assertEquals(<<<EOF |
||
2133 | custom message |
||
2134 | Failed asserting that class "ClassWithNonPublicAttributes" does not have static attribute "privateStaticAttribute". |
||
2135 | |||
2136 | EOF |
||
2137 | , |
||
2138 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2139 | ); |
||
2140 | |||
2141 | return; |
||
2142 | } |
||
2143 | |||
2144 | $this->fail(); |
||
2145 | } |
||
2146 | |||
2147 | /** |
||
2148 | * @covers PHPUnit_Framework_Constraint_ObjectHasAttribute |
||
2149 | * @covers PHPUnit_Framework_Assert::objectHasAttribute |
||
2150 | * @covers PHPUnit_Framework_Constraint::count |
||
2151 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2152 | */ |
||
2153 | public function testConstraintObjectHasAttribute() |
||
2154 | { |
||
2155 | $constraint = PHPUnit_Framework_Assert::objectHasAttribute('privateAttribute'); |
||
2156 | |||
2157 | $this->assertTrue($constraint->evaluate(new ClassWithNonPublicAttributes, '', true)); |
||
2158 | $this->assertFalse($constraint->evaluate(new stdClass, '', true)); |
||
2159 | $this->assertEquals('has attribute "privateAttribute"', $constraint->toString()); |
||
2160 | $this->assertEquals(1, count($constraint)); |
||
2161 | |||
2162 | try { |
||
2163 | $constraint->evaluate(new stdClass); |
||
2164 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2165 | $this->assertEquals( |
||
2166 | <<<EOF |
||
2167 | Failed asserting that object of class "stdClass" has attribute "privateAttribute". |
||
2168 | |||
2169 | EOF |
||
2170 | , |
||
2171 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2172 | ); |
||
2173 | |||
2174 | return; |
||
2175 | } |
||
2176 | |||
2177 | $this->fail(); |
||
2178 | } |
||
2179 | |||
2180 | /** |
||
2181 | * @covers PHPUnit_Framework_Constraint_ObjectHasAttribute |
||
2182 | * @covers PHPUnit_Framework_Assert::objectHasAttribute |
||
2183 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2184 | */ |
||
2185 | public function testConstraintObjectHasAttribute2() |
||
2186 | { |
||
2187 | $constraint = PHPUnit_Framework_Assert::objectHasAttribute('privateAttribute'); |
||
2188 | |||
2189 | try { |
||
2190 | $constraint->evaluate(new stdClass, 'custom message'); |
||
2191 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2192 | $this->assertEquals(<<<EOF |
||
2193 | custom message |
||
2194 | Failed asserting that object of class "stdClass" has attribute "privateAttribute". |
||
2195 | |||
2196 | EOF |
||
2197 | , |
||
2198 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2199 | ); |
||
2200 | |||
2201 | return; |
||
2202 | } |
||
2203 | |||
2204 | $this->fail(); |
||
2205 | } |
||
2206 | |||
2207 | /** |
||
2208 | * @covers PHPUnit_Framework_Constraint_ObjectHasAttribute |
||
2209 | * @covers PHPUnit_Framework_Constraint_Not |
||
2210 | * @covers PHPUnit_Framework_Assert::objectHasAttribute |
||
2211 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2212 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2213 | */ |
||
2214 | public function testConstraintObjectNotHasAttribute() |
||
2215 | { |
||
2216 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2217 | PHPUnit_Framework_Assert::objectHasAttribute('privateAttribute') |
||
2218 | ); |
||
2219 | |||
2220 | $this->assertTrue($constraint->evaluate(new stdClass, '', true)); |
||
2221 | $this->assertFalse($constraint->evaluate(new ClassWithNonPublicAttributes, '', true)); |
||
2222 | $this->assertEquals('does not have attribute "privateAttribute"', $constraint->toString()); |
||
2223 | $this->assertEquals(1, count($constraint)); |
||
2224 | |||
2225 | try { |
||
2226 | $constraint->evaluate(new ClassWithNonPublicAttributes); |
||
2227 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2228 | $this->assertEquals( |
||
2229 | <<<EOF |
||
2230 | Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". |
||
2231 | |||
2232 | EOF |
||
2233 | , |
||
2234 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2235 | ); |
||
2236 | |||
2237 | return; |
||
2238 | } |
||
2239 | |||
2240 | $this->fail(); |
||
2241 | } |
||
2242 | |||
2243 | /** |
||
2244 | * @covers PHPUnit_Framework_Constraint_ObjectHasAttribute |
||
2245 | * @covers PHPUnit_Framework_Constraint_Not |
||
2246 | * @covers PHPUnit_Framework_Assert::objectHasAttribute |
||
2247 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2248 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2249 | */ |
||
2250 | public function testConstraintObjectNotHasAttribute2() |
||
2251 | { |
||
2252 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2253 | PHPUnit_Framework_Assert::objectHasAttribute('privateAttribute') |
||
2254 | ); |
||
2255 | |||
2256 | try { |
||
2257 | $constraint->evaluate(new ClassWithNonPublicAttributes, 'custom message'); |
||
2258 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2259 | $this->assertEquals(<<<EOF |
||
2260 | custom message |
||
2261 | Failed asserting that object of class "ClassWithNonPublicAttributes" does not have attribute "privateAttribute". |
||
2262 | |||
2263 | EOF |
||
2264 | , |
||
2265 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2266 | ); |
||
2267 | |||
2268 | return; |
||
2269 | } |
||
2270 | |||
2271 | $this->fail(); |
||
2272 | } |
||
2273 | |||
2274 | /** |
||
2275 | * @covers PHPUnit_Framework_Constraint_PCREMatch |
||
2276 | * @covers PHPUnit_Framework_Assert::matchesRegularExpression |
||
2277 | * @covers PHPUnit_Framework_Constraint::count |
||
2278 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2279 | */ |
||
2280 | public function testConstraintPCREMatch() |
||
2281 | { |
||
2282 | $constraint = PHPUnit_Framework_Assert::matchesRegularExpression('/foo/'); |
||
2283 | |||
2284 | $this->assertFalse($constraint->evaluate('barbazbar', '', true)); |
||
2285 | $this->assertTrue($constraint->evaluate('barfoobar', '', true)); |
||
2286 | $this->assertEquals('matches PCRE pattern "/foo/"', $constraint->toString()); |
||
2287 | $this->assertEquals(1, count($constraint)); |
||
2288 | |||
2289 | try { |
||
2290 | $constraint->evaluate('barbazbar'); |
||
2291 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2292 | $this->assertEquals( |
||
2293 | <<<EOF |
||
2294 | Failed asserting that 'barbazbar' matches PCRE pattern "/foo/". |
||
2295 | |||
2296 | EOF |
||
2297 | , |
||
2298 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2299 | ); |
||
2300 | |||
2301 | return; |
||
2302 | } |
||
2303 | |||
2304 | $this->fail(); |
||
2305 | } |
||
2306 | |||
2307 | /** |
||
2308 | * @covers PHPUnit_Framework_Constraint_PCREMatch |
||
2309 | * @covers PHPUnit_Framework_Assert::matchesRegularExpression |
||
2310 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2311 | */ |
||
2312 | public function testConstraintPCREMatch2() |
||
2313 | { |
||
2314 | $constraint = PHPUnit_Framework_Assert::matchesRegularExpression('/foo/'); |
||
2315 | |||
2316 | try { |
||
2317 | $constraint->evaluate('barbazbar', 'custom message'); |
||
2318 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2319 | $this->assertEquals(<<<EOF |
||
2320 | custom message |
||
2321 | Failed asserting that 'barbazbar' matches PCRE pattern "/foo/". |
||
2322 | |||
2323 | EOF |
||
2324 | , |
||
2325 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2326 | ); |
||
2327 | |||
2328 | return; |
||
2329 | } |
||
2330 | |||
2331 | $this->fail(); |
||
2332 | } |
||
2333 | |||
2334 | /** |
||
2335 | * @covers PHPUnit_Framework_Constraint_PCREMatch |
||
2336 | * @covers PHPUnit_Framework_Constraint_Not |
||
2337 | * @covers PHPUnit_Framework_Assert::matchesRegularExpression |
||
2338 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2339 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2340 | */ |
||
2341 | public function testConstraintPCRENotMatch() |
||
2342 | { |
||
2343 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2344 | PHPUnit_Framework_Assert::matchesRegularExpression('/foo/') |
||
2345 | ); |
||
2346 | |||
2347 | $this->assertTrue($constraint->evaluate('barbazbar', '', true)); |
||
2348 | $this->assertFalse($constraint->evaluate('barfoobar', '', true)); |
||
2349 | $this->assertEquals('does not match PCRE pattern "/foo/"', $constraint->toString()); |
||
2350 | $this->assertEquals(1, count($constraint)); |
||
2351 | |||
2352 | try { |
||
2353 | $constraint->evaluate('barfoobar'); |
||
2354 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2355 | $this->assertEquals( |
||
2356 | <<<EOF |
||
2357 | Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/". |
||
2358 | |||
2359 | EOF |
||
2360 | , |
||
2361 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2362 | ); |
||
2363 | |||
2364 | return; |
||
2365 | } |
||
2366 | |||
2367 | $this->fail(); |
||
2368 | } |
||
2369 | |||
2370 | /** |
||
2371 | * @covers PHPUnit_Framework_Constraint_PCREMatch |
||
2372 | * @covers PHPUnit_Framework_Constraint_Not |
||
2373 | * @covers PHPUnit_Framework_Assert::matchesRegularExpression |
||
2374 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2375 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2376 | */ |
||
2377 | public function testConstraintPCRENotMatch2() |
||
2378 | { |
||
2379 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2380 | PHPUnit_Framework_Assert::matchesRegularExpression('/foo/') |
||
2381 | ); |
||
2382 | |||
2383 | try { |
||
2384 | $constraint->evaluate('barfoobar', 'custom message'); |
||
2385 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2386 | $this->assertEquals(<<<EOF |
||
2387 | custom message |
||
2388 | Failed asserting that 'barfoobar' does not match PCRE pattern "/foo/". |
||
2389 | |||
2390 | EOF |
||
2391 | , |
||
2392 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2393 | ); |
||
2394 | |||
2395 | return; |
||
2396 | } |
||
2397 | |||
2398 | $this->fail(); |
||
2399 | } |
||
2400 | |||
2401 | /** |
||
2402 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2403 | * @covers PHPUnit_Framework_Assert::matches |
||
2404 | * @covers PHPUnit_Framework_Constraint::count |
||
2405 | */ |
||
2406 | public function testConstraintStringMatches() |
||
2407 | { |
||
2408 | $constraint = PHPUnit_Framework_Assert::matches('*%c*'); |
||
2409 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2410 | $this->assertTrue($constraint->evaluate('***', '', true)); |
||
2411 | $this->assertEquals('matches PCRE pattern "/^\*.\*$/s"', $constraint->toString()); |
||
2412 | $this->assertEquals(1, count($constraint)); |
||
2413 | } |
||
2414 | |||
2415 | /** |
||
2416 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2417 | * @covers PHPUnit_Framework_Assert::matches |
||
2418 | * @covers PHPUnit_Framework_Constraint::count |
||
2419 | */ |
||
2420 | public function testConstraintStringMatches2() |
||
2421 | { |
||
2422 | $constraint = PHPUnit_Framework_Assert::matches('*%s*'); |
||
2423 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2424 | $this->assertTrue($constraint->evaluate('***', '', true)); |
||
2425 | $this->assertEquals('matches PCRE pattern "/^\*[^\r\n]+\*$/s"', $constraint->toString()); |
||
2426 | $this->assertEquals(1, count($constraint)); |
||
2427 | } |
||
2428 | |||
2429 | /** |
||
2430 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2431 | * @covers PHPUnit_Framework_Assert::matches |
||
2432 | * @covers PHPUnit_Framework_Constraint::count |
||
2433 | */ |
||
2434 | public function testConstraintStringMatches3() |
||
2435 | { |
||
2436 | $constraint = PHPUnit_Framework_Assert::matches('*%i*'); |
||
2437 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2438 | $this->assertTrue($constraint->evaluate('*0*', '', true)); |
||
2439 | $this->assertEquals('matches PCRE pattern "/^\*[+-]?\d+\*$/s"', $constraint->toString()); |
||
2440 | $this->assertEquals(1, count($constraint)); |
||
2441 | } |
||
2442 | |||
2443 | /** |
||
2444 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2445 | * @covers PHPUnit_Framework_Assert::matches |
||
2446 | * @covers PHPUnit_Framework_Constraint::count |
||
2447 | */ |
||
2448 | public function testConstraintStringMatches4() |
||
2449 | { |
||
2450 | $constraint = PHPUnit_Framework_Assert::matches('*%d*'); |
||
2451 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2452 | $this->assertTrue($constraint->evaluate('*0*', '', true)); |
||
2453 | $this->assertEquals('matches PCRE pattern "/^\*\d+\*$/s"', $constraint->toString()); |
||
2454 | $this->assertEquals(1, count($constraint)); |
||
2455 | } |
||
2456 | |||
2457 | /** |
||
2458 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2459 | * @covers PHPUnit_Framework_Assert::matches |
||
2460 | * @covers PHPUnit_Framework_Constraint::count |
||
2461 | */ |
||
2462 | public function testConstraintStringMatches5() |
||
2463 | { |
||
2464 | $constraint = PHPUnit_Framework_Assert::matches('*%x*'); |
||
2465 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2466 | $this->assertTrue($constraint->evaluate('*0f0f0f*', '', true)); |
||
2467 | $this->assertEquals('matches PCRE pattern "/^\*[0-9a-fA-F]+\*$/s"', $constraint->toString()); |
||
2468 | $this->assertEquals(1, count($constraint)); |
||
2469 | } |
||
2470 | |||
2471 | /** |
||
2472 | * @covers PHPUnit_Framework_Constraint_StringMatches |
||
2473 | * @covers PHPUnit_Framework_Assert::matches |
||
2474 | * @covers PHPUnit_Framework_Constraint::count |
||
2475 | */ |
||
2476 | public function testConstraintStringMatches6() |
||
2477 | { |
||
2478 | $constraint = PHPUnit_Framework_Assert::matches('*%f*'); |
||
2479 | $this->assertFalse($constraint->evaluate('**', '', true)); |
||
2480 | $this->assertTrue($constraint->evaluate('*1.0*', '', true)); |
||
2481 | $this->assertEquals('matches PCRE pattern "/^\*[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?\*$/s"', $constraint->toString()); |
||
2482 | $this->assertEquals(1, count($constraint)); |
||
2483 | } |
||
2484 | |||
2485 | /** |
||
2486 | * @covers PHPUnit_Framework_Constraint_StringStartsWith |
||
2487 | * @covers PHPUnit_Framework_Assert::stringStartsWith |
||
2488 | * @covers PHPUnit_Framework_Constraint::count |
||
2489 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2490 | */ |
||
2491 | public function testConstraintStringStartsWith() |
||
2492 | { |
||
2493 | $constraint = PHPUnit_Framework_Assert::stringStartsWith('prefix'); |
||
2494 | |||
2495 | $this->assertFalse($constraint->evaluate('foo', '', true)); |
||
2496 | $this->assertTrue($constraint->evaluate('prefixfoo', '', true)); |
||
2497 | $this->assertEquals('starts with "prefix"', $constraint->toString()); |
||
2498 | $this->assertEquals(1, count($constraint)); |
||
2499 | |||
2500 | try { |
||
2501 | $constraint->evaluate('foo'); |
||
2502 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2503 | $this->assertEquals( |
||
2504 | <<<EOF |
||
2505 | Failed asserting that 'foo' starts with "prefix". |
||
2506 | |||
2507 | EOF |
||
2508 | , |
||
2509 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2510 | ); |
||
2511 | |||
2512 | return; |
||
2513 | } |
||
2514 | |||
2515 | $this->fail(); |
||
2516 | } |
||
2517 | |||
2518 | /** |
||
2519 | * @covers PHPUnit_Framework_Constraint_StringStartsWith |
||
2520 | * @covers PHPUnit_Framework_Assert::stringStartsWith |
||
2521 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2522 | */ |
||
2523 | public function testConstraintStringStartsWith2() |
||
2524 | { |
||
2525 | $constraint = PHPUnit_Framework_Assert::stringStartsWith('prefix'); |
||
2526 | |||
2527 | try { |
||
2528 | $constraint->evaluate('foo', 'custom message'); |
||
2529 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2530 | $this->assertEquals( |
||
2531 | <<<EOF |
||
2532 | custom message\nFailed asserting that 'foo' starts with "prefix". |
||
2533 | |||
2534 | EOF |
||
2535 | , |
||
2536 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2537 | ); |
||
2538 | |||
2539 | return; |
||
2540 | } |
||
2541 | |||
2542 | $this->fail(); |
||
2543 | } |
||
2544 | |||
2545 | /** |
||
2546 | * @covers PHPUnit_Framework_Constraint_StringStartsWith |
||
2547 | * @covers PHPUnit_Framework_Constraint_Not |
||
2548 | * @covers PHPUnit_Framework_Assert::stringStartsWith |
||
2549 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2550 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2551 | */ |
||
2552 | public function testConstraintStringStartsNotWith() |
||
2553 | { |
||
2554 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2555 | PHPUnit_Framework_Assert::stringStartsWith('prefix') |
||
2556 | ); |
||
2557 | |||
2558 | $this->assertTrue($constraint->evaluate('foo', '', true)); |
||
2559 | $this->assertFalse($constraint->evaluate('prefixfoo', '', true)); |
||
2560 | $this->assertEquals('starts not with "prefix"', $constraint->toString()); |
||
2561 | $this->assertEquals(1, count($constraint)); |
||
2562 | |||
2563 | try { |
||
2564 | $constraint->evaluate('prefixfoo'); |
||
2565 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2566 | $this->assertEquals( |
||
2567 | <<<EOF |
||
2568 | Failed asserting that 'prefixfoo' starts not with "prefix". |
||
2569 | |||
2570 | EOF |
||
2571 | , |
||
2572 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2573 | ); |
||
2574 | |||
2575 | return; |
||
2576 | } |
||
2577 | |||
2578 | $this->fail(); |
||
2579 | } |
||
2580 | |||
2581 | /** |
||
2582 | * @covers PHPUnit_Framework_Constraint_StringStartsWith |
||
2583 | * @covers PHPUnit_Framework_Assert::stringStartsWith |
||
2584 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2585 | */ |
||
2586 | public function testConstraintStringStartsNotWith2() |
||
2587 | { |
||
2588 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2589 | PHPUnit_Framework_Assert::stringStartsWith('prefix') |
||
2590 | ); |
||
2591 | |||
2592 | try { |
||
2593 | $constraint->evaluate('prefixfoo', 'custom message'); |
||
2594 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2595 | $this->assertEquals( |
||
2596 | <<<EOF |
||
2597 | custom message |
||
2598 | Failed asserting that 'prefixfoo' starts not with "prefix". |
||
2599 | |||
2600 | EOF |
||
2601 | , |
||
2602 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2603 | ); |
||
2604 | |||
2605 | return; |
||
2606 | } |
||
2607 | |||
2608 | $this->fail(); |
||
2609 | } |
||
2610 | |||
2611 | /** |
||
2612 | * @covers PHPUnit_Framework_Constraint_StringContains |
||
2613 | * @covers PHPUnit_Framework_Assert::stringContains |
||
2614 | * @covers PHPUnit_Framework_Constraint::count |
||
2615 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2616 | */ |
||
2617 | public function testConstraintStringContains() |
||
2618 | { |
||
2619 | $constraint = PHPUnit_Framework_Assert::stringContains('foo'); |
||
2620 | |||
2621 | $this->assertFalse($constraint->evaluate('barbazbar', '', true)); |
||
2622 | $this->assertTrue($constraint->evaluate('barfoobar', '', true)); |
||
2623 | $this->assertEquals('contains "foo"', $constraint->toString()); |
||
2624 | $this->assertEquals(1, count($constraint)); |
||
2625 | |||
2626 | try { |
||
2627 | $constraint->evaluate('barbazbar'); |
||
2628 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2629 | $this->assertEquals( |
||
2630 | <<<EOF |
||
2631 | Failed asserting that 'barbazbar' contains "foo". |
||
2632 | |||
2633 | EOF |
||
2634 | , |
||
2635 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2636 | ); |
||
2637 | |||
2638 | return; |
||
2639 | } |
||
2640 | |||
2641 | $this->fail(); |
||
2642 | } |
||
2643 | |||
2644 | /** |
||
2645 | * @covers PHPUnit_Framework_Constraint_StringContains |
||
2646 | * @covers PHPUnit_Framework_Assert::stringContains |
||
2647 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2648 | */ |
||
2649 | public function testConstraintStringContains2() |
||
2650 | { |
||
2651 | $constraint = PHPUnit_Framework_Assert::stringContains('foo'); |
||
2652 | |||
2653 | try { |
||
2654 | $constraint->evaluate('barbazbar', 'custom message'); |
||
2655 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2656 | $this->assertEquals( |
||
2657 | <<<EOF |
||
2658 | custom message |
||
2659 | Failed asserting that 'barbazbar' contains "foo". |
||
2660 | |||
2661 | EOF |
||
2662 | , |
||
2663 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2664 | ); |
||
2665 | |||
2666 | return; |
||
2667 | } |
||
2668 | |||
2669 | $this->fail(); |
||
2670 | } |
||
2671 | |||
2672 | /** |
||
2673 | * @covers PHPUnit_Framework_Constraint_StringContains |
||
2674 | * @covers PHPUnit_Framework_Constraint_Not |
||
2675 | * @covers PHPUnit_Framework_Assert::stringContains |
||
2676 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2677 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2678 | */ |
||
2679 | public function testConstraintStringNotContains() |
||
2680 | { |
||
2681 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2682 | PHPUnit_Framework_Assert::stringContains('foo') |
||
2683 | ); |
||
2684 | |||
2685 | $this->assertTrue($constraint->evaluate('barbazbar', '', true)); |
||
2686 | $this->assertFalse($constraint->evaluate('barfoobar', '', true)); |
||
2687 | $this->assertEquals('does not contain "foo"', $constraint->toString()); |
||
2688 | $this->assertEquals(1, count($constraint)); |
||
2689 | |||
2690 | try { |
||
2691 | $constraint->evaluate('barfoobar'); |
||
2692 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2693 | $this->assertEquals( |
||
2694 | <<<EOF |
||
2695 | Failed asserting that 'barfoobar' does not contain "foo". |
||
2696 | |||
2697 | EOF |
||
2698 | , |
||
2699 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2700 | ); |
||
2701 | |||
2702 | return; |
||
2703 | } |
||
2704 | |||
2705 | $this->fail(); |
||
2706 | } |
||
2707 | |||
2708 | /** |
||
2709 | * @covers PHPUnit_Framework_Constraint_StringContains |
||
2710 | * @covers PHPUnit_Framework_Constraint_Not |
||
2711 | * @covers PHPUnit_Framework_Assert::stringContains |
||
2712 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2713 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2714 | */ |
||
2715 | public function testConstraintStringNotContains2() |
||
2716 | { |
||
2717 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2718 | PHPUnit_Framework_Assert::stringContains('foo') |
||
2719 | ); |
||
2720 | |||
2721 | try { |
||
2722 | $constraint->evaluate('barfoobar', 'custom message'); |
||
2723 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2724 | $this->assertEquals( |
||
2725 | <<<EOF |
||
2726 | custom message |
||
2727 | Failed asserting that 'barfoobar' does not contain "foo". |
||
2728 | |||
2729 | EOF |
||
2730 | , |
||
2731 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2732 | ); |
||
2733 | |||
2734 | return; |
||
2735 | } |
||
2736 | |||
2737 | $this->fail(); |
||
2738 | } |
||
2739 | |||
2740 | /** |
||
2741 | * @covers PHPUnit_Framework_Constraint_StringEndsWith |
||
2742 | * @covers PHPUnit_Framework_Assert::stringEndsWith |
||
2743 | * @covers PHPUnit_Framework_Constraint::count |
||
2744 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2745 | */ |
||
2746 | public function testConstraintStringEndsWith() |
||
2747 | { |
||
2748 | $constraint = PHPUnit_Framework_Assert::stringEndsWith('suffix'); |
||
2749 | |||
2750 | $this->assertFalse($constraint->evaluate('foo', '', true)); |
||
2751 | $this->assertTrue($constraint->evaluate('foosuffix', '', true)); |
||
2752 | $this->assertEquals('ends with "suffix"', $constraint->toString()); |
||
2753 | $this->assertEquals(1, count($constraint)); |
||
2754 | |||
2755 | try { |
||
2756 | $constraint->evaluate('foo'); |
||
2757 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2758 | $this->assertEquals( |
||
2759 | <<<EOF |
||
2760 | Failed asserting that 'foo' ends with "suffix". |
||
2761 | |||
2762 | EOF |
||
2763 | , |
||
2764 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2765 | ); |
||
2766 | |||
2767 | return; |
||
2768 | } |
||
2769 | |||
2770 | $this->fail(); |
||
2771 | } |
||
2772 | |||
2773 | /** |
||
2774 | * @covers PHPUnit_Framework_Constraint_StringEndsWith |
||
2775 | * @covers PHPUnit_Framework_Assert::stringEndsWith |
||
2776 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2777 | */ |
||
2778 | public function testConstraintStringEndsWith2() |
||
2779 | { |
||
2780 | $constraint = PHPUnit_Framework_Assert::stringEndsWith('suffix'); |
||
2781 | |||
2782 | try { |
||
2783 | $constraint->evaluate('foo', 'custom message'); |
||
2784 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2785 | $this->assertEquals( |
||
2786 | <<<EOF |
||
2787 | custom message |
||
2788 | Failed asserting that 'foo' ends with "suffix". |
||
2789 | |||
2790 | EOF |
||
2791 | , |
||
2792 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2793 | ); |
||
2794 | |||
2795 | return; |
||
2796 | } |
||
2797 | |||
2798 | $this->fail(); |
||
2799 | } |
||
2800 | |||
2801 | /** |
||
2802 | * @covers PHPUnit_Framework_Constraint_StringEndsWith |
||
2803 | * @covers PHPUnit_Framework_Constraint_Not |
||
2804 | * @covers PHPUnit_Framework_Assert::stringEndsWith |
||
2805 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2806 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2807 | */ |
||
2808 | public function testConstraintStringEndsNotWith() |
||
2809 | { |
||
2810 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2811 | PHPUnit_Framework_Assert::stringEndsWith('suffix') |
||
2812 | ); |
||
2813 | |||
2814 | $this->assertTrue($constraint->evaluate('foo', '', true)); |
||
2815 | $this->assertFalse($constraint->evaluate('foosuffix', '', true)); |
||
2816 | $this->assertEquals('ends not with "suffix"', $constraint->toString()); |
||
2817 | $this->assertEquals(1, count($constraint)); |
||
2818 | |||
2819 | try { |
||
2820 | $constraint->evaluate('foosuffix'); |
||
2821 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2822 | $this->assertEquals( |
||
2823 | <<<EOF |
||
2824 | Failed asserting that 'foosuffix' ends not with "suffix". |
||
2825 | |||
2826 | EOF |
||
2827 | , |
||
2828 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2829 | ); |
||
2830 | |||
2831 | return; |
||
2832 | } |
||
2833 | |||
2834 | $this->fail(); |
||
2835 | } |
||
2836 | |||
2837 | /** |
||
2838 | * @covers PHPUnit_Framework_Constraint_StringEndsWith |
||
2839 | * @covers PHPUnit_Framework_Assert::stringEndsWith |
||
2840 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2841 | */ |
||
2842 | public function testConstraintStringEndsNotWith2() |
||
2843 | { |
||
2844 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2845 | PHPUnit_Framework_Assert::stringEndsWith('suffix') |
||
2846 | ); |
||
2847 | |||
2848 | try { |
||
2849 | $constraint->evaluate('foosuffix', 'custom message'); |
||
2850 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2851 | $this->assertEquals( |
||
2852 | <<<EOF |
||
2853 | custom message |
||
2854 | Failed asserting that 'foosuffix' ends not with "suffix". |
||
2855 | |||
2856 | EOF |
||
2857 | , |
||
2858 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2859 | ); |
||
2860 | |||
2861 | return; |
||
2862 | } |
||
2863 | |||
2864 | $this->fail(); |
||
2865 | } |
||
2866 | |||
2867 | /** |
||
2868 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
2869 | */ |
||
2870 | public function testConstraintArrayContainsCheckForObjectIdentity() |
||
2871 | { |
||
2872 | // Check for primitive type. |
||
2873 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo', true, true); |
||
2874 | |||
2875 | $this->assertFalse($constraint->evaluate(array(0), '', true)); |
||
2876 | $this->assertFalse($constraint->evaluate(array(true), '', true)); |
||
2877 | |||
2878 | // Default case. |
||
2879 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo'); |
||
2880 | |||
2881 | $this->assertTrue($constraint->evaluate(array(0), '', true)); |
||
2882 | $this->assertTrue($constraint->evaluate(array(true), '', true)); |
||
2883 | } |
||
2884 | |||
2885 | /** |
||
2886 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
2887 | * @covers PHPUnit_Framework_Constraint::count |
||
2888 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2889 | */ |
||
2890 | public function testConstraintArrayContains() |
||
2891 | { |
||
2892 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo'); |
||
2893 | |||
2894 | $this->assertFalse($constraint->evaluate(array('bar'), '', true)); |
||
2895 | $this->assertTrue($constraint->evaluate(array('foo'), '', true)); |
||
2896 | $this->assertEquals("contains 'foo'", $constraint->toString()); |
||
2897 | $this->assertEquals(1, count($constraint)); |
||
2898 | |||
2899 | try { |
||
2900 | $constraint->evaluate(array('bar')); |
||
2901 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2902 | $this->assertEquals( |
||
2903 | <<<EOF |
||
2904 | Failed asserting that an array contains 'foo'. |
||
2905 | |||
2906 | EOF |
||
2907 | , |
||
2908 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2909 | ); |
||
2910 | |||
2911 | return; |
||
2912 | } |
||
2913 | |||
2914 | $this->fail(); |
||
2915 | } |
||
2916 | |||
2917 | /** |
||
2918 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
2919 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2920 | */ |
||
2921 | public function testConstraintArrayContains2() |
||
2922 | { |
||
2923 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains('foo'); |
||
2924 | |||
2925 | try { |
||
2926 | $constraint->evaluate(array('bar'), 'custom message'); |
||
2927 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2928 | $this->assertEquals( |
||
2929 | <<<EOF |
||
2930 | custom message |
||
2931 | Failed asserting that an array contains 'foo'. |
||
2932 | |||
2933 | EOF |
||
2934 | , |
||
2935 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2936 | ); |
||
2937 | |||
2938 | return; |
||
2939 | } |
||
2940 | |||
2941 | $this->fail(); |
||
2942 | } |
||
2943 | |||
2944 | /** |
||
2945 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
2946 | * @covers PHPUnit_Framework_Constraint_Not |
||
2947 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2948 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2949 | */ |
||
2950 | public function testConstraintArrayNotContains() |
||
2951 | { |
||
2952 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2953 | new PHPUnit_Framework_Constraint_TraversableContains('foo') |
||
2954 | ); |
||
2955 | |||
2956 | $this->assertTrue($constraint->evaluate(array('bar'), '', true)); |
||
2957 | $this->assertFalse($constraint->evaluate(array('foo'), '', true)); |
||
2958 | $this->assertEquals("does not contain 'foo'", $constraint->toString()); |
||
2959 | $this->assertEquals(1, count($constraint)); |
||
2960 | |||
2961 | try { |
||
2962 | $constraint->evaluate(array('foo')); |
||
2963 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2964 | $this->assertEquals( |
||
2965 | <<<EOF |
||
2966 | Failed asserting that an array does not contain 'foo'. |
||
2967 | |||
2968 | EOF |
||
2969 | , |
||
2970 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
2971 | ); |
||
2972 | |||
2973 | return; |
||
2974 | } |
||
2975 | |||
2976 | $this->fail(); |
||
2977 | } |
||
2978 | |||
2979 | /** |
||
2980 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
2981 | * @covers PHPUnit_Framework_Constraint_Not |
||
2982 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
2983 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
2984 | */ |
||
2985 | public function testConstraintArrayNotContains2() |
||
2986 | { |
||
2987 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
2988 | new PHPUnit_Framework_Constraint_TraversableContains('foo') |
||
2989 | ); |
||
2990 | |||
2991 | try { |
||
2992 | $constraint->evaluate(array('foo'), 'custom message'); |
||
2993 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
2994 | $this->assertEquals( |
||
2995 | <<<EOF |
||
2996 | custom message |
||
2997 | Failed asserting that an array does not contain 'foo'. |
||
2998 | |||
2999 | EOF |
||
3000 | , |
||
3001 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3002 | ); |
||
3003 | |||
3004 | return; |
||
3005 | } |
||
3006 | |||
3007 | $this->fail(); |
||
3008 | } |
||
3009 | |||
3010 | /** |
||
3011 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
3012 | * @covers PHPUnit_Framework_Constraint::count |
||
3013 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3014 | */ |
||
3015 | public function testConstraintSplObjectStorageContains() |
||
3016 | { |
||
3017 | $object = new StdClass; |
||
3018 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object); |
||
3019 | $this->assertStringMatchesFormat('contains stdClass Object &%s ()', $constraint->toString()); |
||
3020 | |||
3021 | $storage = new SplObjectStorage; |
||
3022 | $this->assertFalse($constraint->evaluate($storage, '', true)); |
||
3023 | |||
3024 | $storage->attach($object); |
||
3025 | $this->assertTrue($constraint->evaluate($storage, '', true)); |
||
3026 | |||
3027 | try { |
||
3028 | $constraint->evaluate(new SplObjectStorage); |
||
3029 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3030 | $this->assertStringMatchesFormat( |
||
3031 | <<<EOF |
||
3032 | Failed asserting that a traversable contains stdClass Object &%x (). |
||
3033 | |||
3034 | EOF |
||
3035 | , |
||
3036 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3037 | ); |
||
3038 | |||
3039 | return; |
||
3040 | } |
||
3041 | |||
3042 | $this->fail(); |
||
3043 | } |
||
3044 | |||
3045 | /** |
||
3046 | * @covers PHPUnit_Framework_Constraint_TraversableContains |
||
3047 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3048 | */ |
||
3049 | public function testConstraintSplObjectStorageContains2() |
||
3050 | { |
||
3051 | $object = new StdClass; |
||
3052 | $constraint = new PHPUnit_Framework_Constraint_TraversableContains($object); |
||
3053 | |||
3054 | try { |
||
3055 | $constraint->evaluate(new SplObjectStorage, 'custom message'); |
||
3056 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3057 | $this->assertStringMatchesFormat( |
||
3058 | <<<EOF |
||
3059 | custom message |
||
3060 | Failed asserting that a traversable contains stdClass Object &%x (). |
||
3061 | |||
3062 | EOF |
||
3063 | , |
||
3064 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3065 | ); |
||
3066 | |||
3067 | return; |
||
3068 | } |
||
3069 | |||
3070 | $this->fail(); |
||
3071 | } |
||
3072 | |||
3073 | /** |
||
3074 | * @covers PHPUnit_Framework_Assert::attributeEqualTo |
||
3075 | * @covers PHPUnit_Framework_Constraint_Attribute |
||
3076 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3077 | */ |
||
3078 | public function testAttributeEqualTo() |
||
3079 | { |
||
3080 | $object = new ClassWithNonPublicAttributes; |
||
3081 | $constraint = PHPUnit_Framework_Assert::attributeEqualTo('foo', 1); |
||
3082 | |||
3083 | $this->assertTrue($constraint->evaluate($object, '', true)); |
||
3084 | $this->assertEquals('attribute "foo" is equal to 1', $constraint->toString()); |
||
3085 | $this->assertEquals(1, count($constraint)); |
||
3086 | |||
3087 | $constraint = PHPUnit_Framework_Assert::attributeEqualTo('foo', 2); |
||
3088 | |||
3089 | $this->assertFalse($constraint->evaluate($object, '', true)); |
||
3090 | |||
3091 | try { |
||
3092 | $constraint->evaluate($object); |
||
3093 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3094 | $this->assertEquals( |
||
3095 | <<<EOF |
||
3096 | Failed asserting that attribute "foo" is equal to 2. |
||
3097 | |||
3098 | EOF |
||
3099 | , |
||
3100 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3101 | ); |
||
3102 | |||
3103 | return; |
||
3104 | } |
||
3105 | |||
3106 | $this->fail(); |
||
3107 | } |
||
3108 | |||
3109 | /** |
||
3110 | * @covers PHPUnit_Framework_Assert::attributeEqualTo |
||
3111 | * @covers PHPUnit_Framework_Constraint_Attribute |
||
3112 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3113 | */ |
||
3114 | public function testAttributeEqualTo2() |
||
3115 | { |
||
3116 | $object = new ClassWithNonPublicAttributes; |
||
3117 | $constraint = PHPUnit_Framework_Assert::attributeEqualTo('foo', 2); |
||
3118 | |||
3119 | try { |
||
3120 | $constraint->evaluate($object, 'custom message'); |
||
3121 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3122 | $this->assertEquals( |
||
3123 | <<<EOF |
||
3124 | custom message\nFailed asserting that attribute "foo" is equal to 2. |
||
3125 | |||
3126 | EOF |
||
3127 | , |
||
3128 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3129 | ); |
||
3130 | |||
3131 | return; |
||
3132 | } |
||
3133 | |||
3134 | $this->fail(); |
||
3135 | } |
||
3136 | |||
3137 | /** |
||
3138 | * @covers PHPUnit_Framework_Assert::attributeEqualTo |
||
3139 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
3140 | * @covers PHPUnit_Framework_Constraint_Attribute |
||
3141 | * @covers PHPUnit_Framework_Constraint_Not |
||
3142 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3143 | */ |
||
3144 | public function testAttributeNotEqualTo() |
||
3145 | { |
||
3146 | $object = new ClassWithNonPublicAttributes; |
||
3147 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
3148 | PHPUnit_Framework_Assert::attributeEqualTo('foo', 2) |
||
3149 | ); |
||
3150 | |||
3151 | $this->assertTrue($constraint->evaluate($object, '', true)); |
||
3152 | $this->assertEquals('attribute "foo" is not equal to 2', $constraint->toString()); |
||
3153 | $this->assertEquals(1, count($constraint)); |
||
3154 | |||
3155 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
3156 | PHPUnit_Framework_Assert::attributeEqualTo('foo', 1) |
||
3157 | ); |
||
3158 | |||
3159 | $this->assertFalse($constraint->evaluate($object, '', true)); |
||
3160 | |||
3161 | try { |
||
3162 | $constraint->evaluate($object); |
||
3163 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3164 | $this->assertEquals( |
||
3165 | <<<EOF |
||
3166 | Failed asserting that attribute "foo" is not equal to 1. |
||
3167 | |||
3168 | EOF |
||
3169 | , |
||
3170 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3171 | ); |
||
3172 | |||
3173 | return; |
||
3174 | } |
||
3175 | |||
3176 | $this->fail(); |
||
3177 | } |
||
3178 | |||
3179 | /** |
||
3180 | * @covers PHPUnit_Framework_Assert::attributeEqualTo |
||
3181 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
3182 | * @covers PHPUnit_Framework_Constraint_Attribute |
||
3183 | * @covers PHPUnit_Framework_Constraint_Not |
||
3184 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3185 | */ |
||
3186 | public function testAttributeNotEqualTo2() |
||
3187 | { |
||
3188 | $object = new ClassWithNonPublicAttributes; |
||
3189 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
3190 | PHPUnit_Framework_Assert::attributeEqualTo('foo', 1) |
||
3191 | ); |
||
3192 | |||
3193 | try { |
||
3194 | $constraint->evaluate($object, 'custom message'); |
||
3195 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3196 | $this->assertEquals( |
||
3197 | <<<EOF |
||
3198 | custom message\nFailed asserting that attribute "foo" is not equal to 1. |
||
3199 | |||
3200 | EOF |
||
3201 | , |
||
3202 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3203 | ); |
||
3204 | |||
3205 | return; |
||
3206 | } |
||
3207 | |||
3208 | $this->fail(); |
||
3209 | } |
||
3210 | |||
3211 | /** |
||
3212 | * @covers PHPUnit_Framework_Constraint_IsEmpty |
||
3213 | * @covers PHPUnit_Framework_Constraint::count |
||
3214 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3215 | */ |
||
3216 | public function testConstraintIsEmpty() |
||
3217 | { |
||
3218 | $constraint = new PHPUnit_Framework_Constraint_IsEmpty; |
||
3219 | |||
3220 | $this->assertFalse($constraint->evaluate(array('foo'), '', true)); |
||
3221 | $this->assertTrue($constraint->evaluate(array(), '', true)); |
||
3222 | $this->assertFalse($constraint->evaluate(new ArrayObject(array('foo')), '', true)); |
||
3223 | $this->assertTrue($constraint->evaluate(new ArrayObject(array()), '', true)); |
||
3224 | $this->assertEquals('is empty', $constraint->toString()); |
||
3225 | $this->assertEquals(1, count($constraint)); |
||
3226 | |||
3227 | try { |
||
3228 | $constraint->evaluate(array('foo')); |
||
3229 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3230 | $this->assertEquals( |
||
3231 | <<<EOF |
||
3232 | Failed asserting that an array is empty. |
||
3233 | |||
3234 | EOF |
||
3235 | , |
||
3236 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3237 | ); |
||
3238 | |||
3239 | return; |
||
3240 | } |
||
3241 | |||
3242 | $this->fail(); |
||
3243 | } |
||
3244 | |||
3245 | /** |
||
3246 | * @covers PHPUnit_Framework_Constraint_IsEmpty |
||
3247 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3248 | */ |
||
3249 | public function testConstraintIsEmpty2() |
||
3250 | { |
||
3251 | $constraint = new PHPUnit_Framework_Constraint_IsEmpty; |
||
3252 | |||
3253 | try { |
||
3254 | $constraint->evaluate(array('foo'), 'custom message'); |
||
3255 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3256 | $this->assertEquals( |
||
3257 | <<<EOF |
||
3258 | custom message\nFailed asserting that an array is empty. |
||
3259 | |||
3260 | EOF |
||
3261 | , |
||
3262 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3263 | ); |
||
3264 | |||
3265 | return; |
||
3266 | } |
||
3267 | |||
3268 | $this->fail(); |
||
3269 | } |
||
3270 | |||
3271 | /** |
||
3272 | * @covers PHPUnit_Framework_Constraint_Count |
||
3273 | */ |
||
3274 | public function testConstraintCountWithAnArray() |
||
3275 | { |
||
3276 | $constraint = new PHPUnit_Framework_Constraint_Count(5); |
||
3277 | |||
3278 | $this->assertTrue($constraint->evaluate(array(1, 2, 3, 4, 5), '', true)); |
||
3279 | $this->assertFalse($constraint->evaluate(array(1, 2, 3, 4), '', true)); |
||
3280 | } |
||
3281 | |||
3282 | /** |
||
3283 | * @covers PHPUnit_Framework_Constraint_Count |
||
3284 | */ |
||
3285 | public function testConstraintCountWithAnIteratorWhichDoesNotImplementCountable() |
||
3286 | { |
||
3287 | $constraint = new PHPUnit_Framework_Constraint_Count(5); |
||
3288 | |||
3289 | $this->assertTrue($constraint->evaluate(new TestIterator(array(1, 2, 3, 4, 5)), '', true)); |
||
3290 | $this->assertFalse($constraint->evaluate(new TestIterator(array(1, 2, 3, 4)), '', true)); |
||
3291 | } |
||
3292 | |||
3293 | /** |
||
3294 | * @covers PHPUnit_Framework_Constraint_Count |
||
3295 | */ |
||
3296 | public function testConstraintCountWithAnObjectImplementingCountable() |
||
3297 | { |
||
3298 | $constraint = new PHPUnit_Framework_Constraint_Count(5); |
||
3299 | |||
3300 | $this->assertTrue($constraint->evaluate(new ArrayObject(array(1, 2, 3, 4, 5)), '', true)); |
||
3301 | $this->assertFalse($constraint->evaluate(new ArrayObject(array(1, 2, 3, 4)), '', true)); |
||
3302 | } |
||
3303 | |||
3304 | /** |
||
3305 | * @covers PHPUnit_Framework_Constraint_Count |
||
3306 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3307 | */ |
||
3308 | public function testConstraintCountFailing() |
||
3309 | { |
||
3310 | $constraint = new PHPUnit_Framework_Constraint_Count(5); |
||
3311 | |||
3312 | try { |
||
3313 | $constraint->evaluate(array(1, 2)); |
||
3314 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3315 | $this->assertEquals( |
||
3316 | <<<EOF |
||
3317 | Failed asserting that actual size 2 matches expected size 5. |
||
3318 | |||
3319 | EOF |
||
3320 | , |
||
3321 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3322 | ); |
||
3323 | |||
3324 | return; |
||
3325 | } |
||
3326 | |||
3327 | $this->fail(); |
||
3328 | } |
||
3329 | |||
3330 | /** |
||
3331 | * @covers PHPUnit_Framework_Constraint_Count |
||
3332 | * @covers PHPUnit_Framework_Constraint_Not |
||
3333 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
3334 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3335 | */ |
||
3336 | public function testConstraintNotCountFailing() |
||
3337 | { |
||
3338 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
3339 | new PHPUnit_Framework_Constraint_Count(2) |
||
3340 | ); |
||
3341 | |||
3342 | try { |
||
3343 | $constraint->evaluate(array(1, 2)); |
||
3344 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3345 | $this->assertEquals( |
||
3346 | <<<EOF |
||
3347 | Failed asserting that actual size 2 does not match expected size 2. |
||
3348 | |||
3349 | EOF |
||
3350 | , |
||
3351 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3352 | ); |
||
3353 | |||
3354 | return; |
||
3355 | } |
||
3356 | |||
3357 | $this->fail(); |
||
3358 | } |
||
3359 | |||
3360 | /** |
||
3361 | * @covers PHPUnit_Framework_Constraint_SameSize |
||
3362 | */ |
||
3363 | public function testConstraintSameSizeWithAnArray() |
||
3364 | { |
||
3365 | $constraint = new PHPUnit_Framework_Constraint_SameSize(array(1, 2, 3, 4, 5)); |
||
3366 | |||
3367 | $this->assertTrue($constraint->evaluate(array(6, 7, 8, 9, 10), '', true)); |
||
3368 | $this->assertFalse($constraint->evaluate(array(1, 2, 3, 4), '', true)); |
||
3369 | } |
||
3370 | |||
3371 | /** |
||
3372 | * @covers PHPUnit_Framework_Constraint_SameSize |
||
3373 | */ |
||
3374 | public function testConstraintSameSizeWithAnIteratorWhichDoesNotImplementCountable() |
||
3375 | { |
||
3376 | $constraint = new PHPUnit_Framework_Constraint_SameSize(new TestIterator(array(1, 2, 3, 4, 5))); |
||
3377 | |||
3378 | $this->assertTrue($constraint->evaluate(new TestIterator(array(6, 7, 8, 9, 10)), '', true)); |
||
3379 | $this->assertFalse($constraint->evaluate(new TestIterator(array(1, 2, 3, 4)), '', true)); |
||
3380 | } |
||
3381 | |||
3382 | /** |
||
3383 | * @covers PHPUnit_Framework_Constraint_SameSize |
||
3384 | */ |
||
3385 | public function testConstraintSameSizeWithAnObjectImplementingCountable() |
||
3386 | { |
||
3387 | $constraint = new PHPUnit_Framework_Constraint_SameSize(new ArrayObject(array(1, 2, 3, 4, 5))); |
||
3388 | |||
3389 | $this->assertTrue($constraint->evaluate(new ArrayObject(array(6, 7, 8, 9, 10)), '', true)); |
||
3390 | $this->assertFalse($constraint->evaluate(new ArrayObject(array(1, 2, 3, 4)), '', true)); |
||
3391 | } |
||
3392 | |||
3393 | /** |
||
3394 | * @covers PHPUnit_Framework_Constraint_SameSize |
||
3395 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3396 | */ |
||
3397 | public function testConstraintSameSizeFailing() |
||
3398 | { |
||
3399 | $constraint = new PHPUnit_Framework_Constraint_SameSize(array(1, 2, 3, 4, 5)); |
||
3400 | |||
3401 | try { |
||
3402 | $constraint->evaluate(array(1, 2)); |
||
3403 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3404 | $this->assertEquals( |
||
3405 | <<<EOF |
||
3406 | Failed asserting that actual size 2 matches expected size 5. |
||
3407 | |||
3408 | EOF |
||
3409 | , |
||
3410 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3411 | ); |
||
3412 | |||
3413 | return; |
||
3414 | } |
||
3415 | |||
3416 | $this->fail(); |
||
3417 | } |
||
3418 | |||
3419 | /** |
||
3420 | * @covers PHPUnit_Framework_Constraint_SameSize |
||
3421 | * @covers PHPUnit_Framework_Constraint_Not |
||
3422 | * @covers PHPUnit_Framework_Assert::logicalNot |
||
3423 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3424 | */ |
||
3425 | public function testConstraintNotSameSizeFailing() |
||
3426 | { |
||
3427 | $constraint = PHPUnit_Framework_Assert::logicalNot( |
||
3428 | new PHPUnit_Framework_Constraint_SameSize(array(1, 2)) |
||
3429 | ); |
||
3430 | |||
3431 | try { |
||
3432 | $constraint->evaluate(array(3, 4)); |
||
3433 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3434 | $this->assertEquals( |
||
3435 | <<<EOF |
||
3436 | Failed asserting that actual size 2 does not match expected size 2. |
||
3437 | |||
3438 | EOF |
||
3439 | , |
||
3440 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3441 | ); |
||
3442 | |||
3443 | return; |
||
3444 | } |
||
3445 | |||
3446 | $this->fail(); |
||
3447 | } |
||
3448 | |||
3449 | /** |
||
3450 | * @covers PHPUnit_Framework_Constraint_Exception |
||
3451 | * @covers PHPUnit_Framework_TestFailure::exceptionToString |
||
3452 | */ |
||
3453 | public function testConstraintException() |
||
3454 | { |
||
3455 | $constraint = new PHPUnit_Framework_Constraint_Exception('FoobarException'); |
||
3456 | $exception = new DummyException('Test'); |
||
3457 | $stackTrace = $exception->getTraceAsString(); |
||
3458 | |||
3459 | try { |
||
3460 | $constraint->evaluate($exception); |
||
3461 | } catch (PHPUnit_Framework_ExpectationFailedException $e) { |
||
3462 | $this->assertEquals( |
||
3463 | <<<EOF |
||
3464 | Failed asserting that exception of type "DummyException" matches expected exception "FoobarException". Message was: "Test" at |
||
3465 | $stackTrace. |
||
3466 | |||
3467 | EOF |
||
3468 | , |
||
3469 | PHPUnit_Framework_TestFailure::exceptionToString($e) |
||
3470 | ); |
||
3471 | |||
3472 | return; |
||
3473 | } |
||
3474 | |||
3475 | $this->fail(); |
||
3476 | } |
||
3477 | |||
3478 | /** |
||
3479 | * Removes spaces in front of newlines |
||
3480 | * |
||
3481 | * @param string $string |
||
3482 | * |
||
3483 | * @return string |
||
3484 | */ |
||
3485 | private function trimnl($string) |
||
3486 | { |
||
3487 | return preg_replace('/[ ]*\n/', "\n", $string); |
||
3488 | } |
||
3489 | } |
||
3490 |