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