Complex classes like AssertTest 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.
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 AssertTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class AssertTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | public static function dataInvalidFloat() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @dataProvider dataInvalidFloat |
||
| 23 | */ |
||
| 24 | public function testInvalidFloat($nonFloat) |
||
| 29 | |||
| 30 | public function testValidFloat() |
||
| 31 | { |
||
| 32 | Assertion::float(1.0); |
||
| 33 | Assertion::float(0.1); |
||
| 34 | Assertion::float(-1.1); |
||
| 35 | } |
||
| 36 | |||
| 37 | public static function dataInvalidInteger() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @dataProvider dataInvalidInteger |
||
| 52 | */ |
||
| 53 | public function testInvalidInteger($nonInteger) |
||
| 58 | |||
| 59 | public function testValidInteger() |
||
| 64 | |||
| 65 | public function testValidIntegerish() |
||
| 70 | |||
| 71 | public static function dataInvalidIntegerish() |
||
| 72 | { |
||
| 73 | return array( |
||
| 74 | array(1.23), |
||
| 75 | array(false), |
||
| 76 | array("test"), |
||
| 77 | array(null), |
||
| 78 | array("1.23"), |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @dataProvider dataInvalidIntegerish |
||
| 84 | */ |
||
| 85 | public function testInvalidIntegerish($nonInteger) |
||
| 90 | |||
| 91 | public function testValidBoolean() |
||
| 96 | |||
| 97 | public function testInvalidBoolean() |
||
| 102 | |||
| 103 | public function testInvalidScalar() |
||
| 104 | { |
||
| 105 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SCALAR); |
||
| 106 | Assertion::scalar(new \stdClass); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testValidScalar() |
||
| 110 | { |
||
| 111 | Assertion::scalar("foo"); |
||
| 112 | Assertion::scalar(52); |
||
| 113 | Assertion::scalar(12.34); |
||
| 114 | Assertion::scalar(false); |
||
| 115 | } |
||
| 116 | |||
| 117 | public static function dataInvalidNotEmpty() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @dataProvider dataInvalidNotEmpty |
||
| 130 | */ |
||
| 131 | public function testInvalidNotEmpty($value) |
||
| 136 | |||
| 137 | public function testNotEmpty() |
||
| 144 | |||
| 145 | public function testEmpty() |
||
| 146 | { |
||
| 147 | Assertion::noContent(""); |
||
| 148 | Assertion::noContent(0); |
||
| 149 | Assertion::noContent(false); |
||
| 150 | Assertion::noContent( array() ); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function dataInvalidEmpty() |
||
| 154 | { |
||
| 155 | return array( |
||
| 156 | array("foo"), |
||
| 157 | array(true), |
||
| 158 | array(12), |
||
| 159 | array( array('foo') ), |
||
| 160 | array( new \stdClass() ), |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @dataProvider dataInvalidEmpty |
||
| 166 | */ |
||
| 167 | public function testInvalidEmpty($value) |
||
| 168 | { |
||
| 169 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_EMPTY); |
||
| 170 | Assertion::noContent($value); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function testNotNull() |
||
| 181 | |||
| 182 | public function testInvalidNotNull() |
||
| 187 | |||
| 188 | public function testString() |
||
| 189 | { |
||
| 190 | Assertion::string("test-string"); |
||
| 191 | Assertion::string(""); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @dataProvider dataInvalidString |
||
| 196 | */ |
||
| 197 | public function testInvalidString($invalidString) |
||
| 198 | { |
||
| 199 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
||
| 200 | Assertion::string($invalidString); |
||
| 201 | } |
||
| 202 | |||
| 203 | public static function dataInvalidString() |
||
| 204 | { |
||
| 205 | return array( |
||
| 206 | array(1.23), |
||
| 207 | array(false), |
||
| 208 | array(new \ArrayObject), |
||
| 209 | array(null), |
||
| 210 | array(10), |
||
| 211 | array(true), |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function testInvalidRegex() |
||
| 220 | |||
| 221 | public function testInvalidRegexValueNotString() |
||
| 226 | |||
| 227 | public function testInvalidMinLength() |
||
| 232 | |||
| 233 | public function testValidMinLength() |
||
| 234 | { |
||
| 235 | Assertion::minLength("foo", 3); |
||
| 236 | Assertion::minLength("foo", 1); |
||
| 237 | Assertion::minLength("foo", 0); |
||
| 238 | Assertion::minLength("", 0); |
||
| 239 | Assertion::minLength("址址", 2); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testInvalidMaxLength() |
||
| 247 | |||
| 248 | public function testValidMaxLength() |
||
| 249 | { |
||
| 250 | Assertion::maxLength("foo", 10); |
||
| 251 | Assertion::maxLength("foo", 3); |
||
| 252 | Assertion::maxLength("", 0); |
||
| 253 | Assertion::maxLength("址址", 2); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function testInvalidBetweenLengthMin() |
||
| 261 | |||
| 262 | public function testInvalidBetweenLengthMax() |
||
| 267 | |||
| 268 | public function testValidBetweenLength() |
||
| 269 | { |
||
| 270 | Assertion::betweenLength("foo", 0, 3); |
||
| 271 | Assertion::betweenLength("址址", 2, 2); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function testInvalidStartsWith() |
||
| 279 | |||
| 280 | public function testInvalidStartsWithDueToWrongEncoding() |
||
| 281 | { |
||
| 282 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START); |
||
| 283 | Assertion::startsWith("址", "址址", null, null, 'ASCII'); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function testValidStartsWith() |
||
| 287 | { |
||
| 288 | Assertion::startsWith("foo", "foo"); |
||
| 289 | Assertion::startsWith("foo", "fo"); |
||
| 290 | Assertion::startsWith("foo", "f"); |
||
| 291 | Assertion::startsWith("址foo", "址"); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function testInvalidEndsWith() |
||
| 295 | { |
||
| 296 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
| 297 | Assertion::endsWith("foo", "bar"); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testInvalidEndsWithDueToWrongEncoding() |
||
| 301 | { |
||
| 302 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
| 303 | Assertion::endsWith("址", "址址", null, null, 'ASCII'); |
||
| 304 | } |
||
| 305 | |||
| 306 | public function testValidEndsWith() |
||
| 307 | { |
||
| 308 | Assertion::endsWith("foo", "foo"); |
||
| 309 | Assertion::endsWith("sonderbar", "bar"); |
||
| 310 | Assertion::endsWith("opp", "p"); |
||
| 311 | Assertion::endsWith("foo址", "址"); |
||
| 312 | } |
||
| 313 | |||
| 314 | public function testInvalidContains() |
||
| 319 | |||
| 320 | public function testValidContains() |
||
| 321 | { |
||
| 322 | Assertion::contains("foo", "foo"); |
||
| 323 | Assertion::contains("foo", "oo"); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function testInvalidChoice() |
||
| 331 | |||
| 332 | public function testValidChoice() |
||
| 336 | |||
| 337 | public function testInvalidInArray() |
||
| 338 | { |
||
| 339 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); |
||
| 340 | Assertion::inArray("bar", array("baz")); |
||
| 341 | } |
||
| 342 | |||
| 343 | public function testValidInArray() |
||
| 344 | { |
||
| 345 | Assertion::inArray("foo", array("foo")); |
||
| 346 | } |
||
| 347 | |||
| 348 | public function testInvalidNumeric() |
||
| 349 | { |
||
| 350 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NUMERIC); |
||
| 351 | Assertion::numeric("foo"); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function testValidNumeric() |
||
| 360 | |||
| 361 | public static function dataInvalidArray() |
||
| 362 | { |
||
| 363 | return array( |
||
| 364 | array(null), |
||
| 365 | array(false), |
||
| 366 | array("test"), |
||
| 367 | array(1), |
||
| 368 | array(1.23), |
||
| 369 | array(new \StdClass), |
||
| 370 | array(fopen('php://memory', 'r')), |
||
| 371 | ); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @dataProvider dataInvalidArray |
||
| 376 | */ |
||
| 377 | public function testInvalidArray($value) |
||
| 378 | { |
||
| 379 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ARRAY); |
||
| 380 | Assertion::isArray($value); |
||
| 381 | } |
||
| 382 | |||
| 383 | public function testValidArray() |
||
| 384 | { |
||
| 385 | Assertion::isArray(array()); |
||
| 386 | Assertion::isArray(array(1,2,3)); |
||
| 387 | Assertion::isArray(array(array(),array())); |
||
| 388 | } |
||
| 389 | |||
| 390 | public function testInvalidKeyExists() |
||
| 395 | |||
| 396 | public function testValidKeyExists() |
||
| 400 | |||
| 401 | public function testInvalidKeyNotExists() |
||
| 402 | { |
||
| 403 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS); |
||
| 404 | Assertion::keyNotExists(array("foo" => "bar"), "foo"); |
||
| 405 | } |
||
| 406 | |||
| 407 | public function testValidKeyNotExists() |
||
| 408 | { |
||
| 409 | Assertion::keyNotExists(array("foo" => "bar"), "baz"); |
||
| 410 | } |
||
| 411 | |||
| 412 | public static function dataInvalidNotBlank() |
||
| 413 | { |
||
| 414 | return array( |
||
| 415 | array(""), |
||
| 416 | array(" "), |
||
| 417 | array("\t"), |
||
| 418 | array("\n"), |
||
| 419 | array("\r"), |
||
| 420 | array(false), |
||
| 421 | array(null), |
||
| 422 | array( array() ), |
||
| 423 | ); |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @dataProvider dataInvalidNotBlank |
||
| 428 | */ |
||
| 429 | public function testInvalidNotBlank($notBlank) |
||
| 430 | { |
||
| 431 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_BLANK); |
||
| 432 | Assertion::notBlank($notBlank); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function testValidNotBlank() |
||
| 439 | |||
| 440 | public function testInvalidNotInstanceOf() |
||
| 441 | { |
||
| 442 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_INSTANCE_OF); |
||
| 443 | Assertion::notIsInstanceOf(new \stdClass, 'stdClass'); |
||
| 444 | } |
||
| 445 | |||
| 446 | public function testValidNotIsInstanceOf() |
||
| 447 | { |
||
| 448 | Assertion::notIsInstanceOf(new \stdClass, 'PDO'); |
||
| 449 | } |
||
| 450 | |||
| 451 | public function testInvalidInstanceOf() |
||
| 456 | |||
| 457 | public function testValidInstanceOf() |
||
| 461 | |||
| 462 | public function testInvalidSubclassOf() |
||
| 467 | |||
| 468 | public function testValidSubclassOf() |
||
| 472 | |||
| 473 | public function testInvalidRange() |
||
| 479 | |||
| 480 | public function testValidRange() |
||
| 487 | |||
| 488 | public function testInvalidEmail() |
||
| 493 | |||
| 494 | public function testValidEmail() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @dataProvider dataInvalidUrl |
||
| 501 | */ |
||
| 502 | public function testInvalidUrl($url) |
||
| 503 | { |
||
| 504 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL); |
||
| 505 | |||
| 506 | Assertion::url($url); |
||
| 507 | } |
||
| 508 | |||
| 509 | public static function dataInvalidUrl() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @dataProvider dataValidUrl |
||
| 523 | */ |
||
| 524 | public function testValidUrl($url) |
||
| 525 | { |
||
| 526 | Assertion::url($url); |
||
| 527 | } |
||
| 528 | |||
| 529 | public static function dataValidUrl() |
||
| 530 | { |
||
| 531 | return array( |
||
| 532 | 'straight with Http' => array("http://example.org"), |
||
| 533 | 'Http with path' => array("http://example.org/do/something"), |
||
| 534 | 'Http with query' => array("http://example.org/index.php?do=something"), |
||
| 535 | 'Http with port' => array("http://example.org:8080"), |
||
| 536 | 'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"), |
||
| 537 | 'straight with Https' => array("https://example.org"), |
||
| 538 | ); |
||
| 539 | } |
||
| 540 | |||
| 541 | public function testInvalidDigit() |
||
| 546 | |||
| 547 | public function testValidDigit() |
||
| 553 | |||
| 554 | public function testValidAlnum() |
||
| 555 | { |
||
| 556 | Assertion::alnum("a"); |
||
| 557 | Assertion::alnum("a1"); |
||
| 558 | Assertion::alnum("aasdf1234"); |
||
| 559 | Assertion::alnum("a1b2c3"); |
||
| 560 | } |
||
| 561 | |||
| 562 | public function testInvalidAlnum() |
||
| 563 | { |
||
| 564 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ALNUM); |
||
| 565 | Assertion::alnum("1a"); |
||
| 566 | } |
||
| 567 | |||
| 568 | public function testValidTrue() |
||
| 569 | { |
||
| 570 | Assertion::true(1 == 1); |
||
| 571 | } |
||
| 572 | |||
| 573 | public function testInvalidTrue() |
||
| 574 | { |
||
| 575 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); |
||
| 576 | Assertion::true(false); |
||
| 577 | } |
||
| 578 | |||
| 579 | public function testValidFalse() |
||
| 580 | { |
||
| 581 | Assertion::false(1 == 0); |
||
| 582 | } |
||
| 583 | |||
| 584 | public function testInvalidFalse() |
||
| 585 | { |
||
| 586 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FALSE); |
||
| 587 | Assertion::false(true); |
||
| 588 | } |
||
| 589 | |||
| 590 | public function testInvalidClass() |
||
| 591 | { |
||
| 592 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CLASS); |
||
| 593 | Assertion::classExists("Foo"); |
||
| 594 | } |
||
| 595 | |||
| 596 | public function testValidClass() |
||
| 597 | { |
||
| 598 | Assertion::classExists("\\Exception"); |
||
| 599 | } |
||
| 600 | |||
| 601 | public function testSame() |
||
| 602 | { |
||
| 603 | Assertion::same(1,1); |
||
| 604 | Assertion::same("foo","foo"); |
||
| 605 | Assertion::same($obj = new \stdClass(), $obj); |
||
| 606 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SAME); |
||
| 607 | Assertion::same(new \stdClass(), new \stdClass()); |
||
| 608 | } |
||
| 609 | |||
| 610 | public function testEq() |
||
| 611 | { |
||
| 612 | Assertion::eq(1,"1"); |
||
| 613 | Assertion::eq("foo",true); |
||
| 614 | Assertion::eq($obj = new \stdClass(), $obj); |
||
| 615 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EQ); |
||
| 616 | Assertion::eq("2", 1); |
||
| 617 | } |
||
| 618 | |||
| 619 | public function testNotEq() |
||
| 626 | |||
| 627 | public function testNotSame() |
||
| 628 | { |
||
| 629 | Assertion::notSame("1", 2); |
||
| 630 | Assertion::notSame(new \stdClass(), array()); |
||
| 631 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME); |
||
| 632 | Assertion::notSame(1, 1); |
||
| 633 | } |
||
| 634 | |||
| 635 | public function testNotInArray() |
||
| 636 | { |
||
| 637 | Assertion::notInArray(6, range(1, 5)); |
||
| 638 | |||
| 639 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY); |
||
| 640 | Assertion::notInArray(1, range(1, 5)); |
||
| 641 | Assertion::notInArray(range('a', 'c'), range('a', 'd')); |
||
| 642 | } |
||
| 643 | |||
| 644 | public function testMin() |
||
| 645 | { |
||
| 646 | Assertion::min(1, 1); |
||
| 647 | Assertion::min(2, 1); |
||
| 648 | Assertion::min(2.5, 1); |
||
| 649 | |||
| 650 | try { |
||
| 651 | Assertion::min(0, 1); |
||
| 652 | $this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
||
| 653 | } catch (AssertionFailedException $e){ |
||
| 654 | $this->assertEquals(Assertion::INVALID_MIN, $e->getCode()); |
||
| 655 | $this->assertEquals('Number "0" was expected to be at least "1".', $e->getMessage()); |
||
| 656 | } |
||
| 657 | |||
| 658 | try { |
||
| 659 | Assertion::min(0.5, 2.5); |
||
| 660 | $this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
||
| 661 | } catch (AssertionFailedException $e){ |
||
| 662 | $this->assertEquals(Assertion::INVALID_MIN, $e->getCode()); |
||
| 663 | $this->assertEquals('Number "0.5" was expected to be at least "2.5".', $e->getMessage()); |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | public function testMax() |
||
| 668 | { |
||
| 669 | Assertion::max(1, 1); |
||
| 670 | Assertion::max(0.5, 1); |
||
| 671 | Assertion::max(0, 1); |
||
| 672 | |||
| 673 | try { |
||
| 674 | Assertion::max(2, 1); |
||
| 675 | $this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
||
| 676 | } catch (AssertionFailedException $e){ |
||
| 677 | $this->assertEquals(Assertion::INVALID_MAX, $e->getCode()); |
||
| 678 | $this->assertEquals('Number "2" was expected to be at most "1".', $e->getMessage()); |
||
| 679 | } |
||
| 680 | |||
| 681 | try { |
||
| 682 | Assertion::max(2.5, 0.5); |
||
| 683 | $this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
||
| 684 | } catch (AssertionFailedException $e){ |
||
| 685 | $this->assertEquals(Assertion::INVALID_MAX, $e->getCode()); |
||
| 686 | $this->assertEquals('Number "2.5" was expected to be at most "0.5".', $e->getMessage()); |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | public function testNullOr() |
||
| 695 | |||
| 696 | public function testNullOrWithNoValueThrows() |
||
| 701 | |||
| 702 | public function testLength() |
||
| 707 | |||
| 708 | public static function dataLengthUtf8Characters() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @dataProvider dataLengthUtf8Characters |
||
| 718 | */ |
||
| 719 | public function testLenghtUtf8Characters($value, $expected) |
||
| 723 | |||
| 724 | public function testLengthFailed() |
||
| 729 | |||
| 730 | public function testLengthFailedForWrongEncoding() |
||
| 735 | |||
| 736 | public function testLengthValidForGivenEncoding() |
||
| 740 | |||
| 741 | public function testFile() |
||
| 745 | |||
| 746 | public function testFileWithEmptyFilename() |
||
| 751 | |||
| 752 | public function testFileDoesNotExists() |
||
| 757 | |||
| 758 | public function testDirectory() |
||
| 765 | |||
| 766 | public function testReadable() |
||
| 773 | |||
| 774 | public function testWriteable() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @expectedException \BadMethodCallException |
||
| 784 | * @expectedExceptionMessage No assertion |
||
| 785 | */ |
||
| 786 | public function testFailedNullOrMethodCall() |
||
| 790 | |||
| 791 | public function testImplementsInterface() |
||
| 792 | { |
||
| 793 | Assertion::implementsInterface( |
||
| 794 | '\ArrayIterator', |
||
| 795 | '\Traversable' |
||
| 796 | ); |
||
| 797 | |||
| 798 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED); |
||
| 799 | Assertion::implementsInterface( |
||
| 800 | '\Exception', |
||
| 801 | '\Traversable' |
||
| 802 | ); |
||
| 803 | } |
||
| 804 | |||
| 805 | public function testImplementsInterfaceWithClassObject() |
||
| 806 | { |
||
| 807 | $class = new \ArrayObject(); |
||
| 808 | |||
| 809 | Assertion::implementsInterface( |
||
| 810 | $class, |
||
| 811 | '\Traversable' |
||
| 812 | ); |
||
| 813 | |||
| 814 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED); |
||
| 815 | Assertion::implementsInterface( |
||
| 816 | $class, |
||
| 817 | '\SplObserver' |
||
| 818 | ); |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * @dataProvider isJsonStringDataprovider |
||
| 823 | */ |
||
| 824 | public function testIsJsonString($content) |
||
| 825 | { |
||
| 826 | Assertion::isJsonString($content); |
||
| 827 | } |
||
| 828 | |||
| 829 | public static function isJsonStringDataprovider() |
||
| 838 | |||
| 839 | /** |
||
| 840 | * @dataProvider isJsonStringInvalidStringDataprovider |
||
| 841 | */ |
||
| 842 | public function testIsJsonStringExpectingException($invalidString) |
||
| 843 | { |
||
| 844 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_JSON_STRING); |
||
| 845 | Assertion::isJsonString($invalidString); |
||
| 846 | } |
||
| 847 | |||
| 848 | public static function isJsonStringInvalidStringDataprovider() |
||
| 849 | { |
||
| 850 | return array( |
||
| 851 | 'no json string' => array('invalid json encoded string'), |
||
| 852 | 'error in json string' => array('{invalid json encoded string}'), |
||
| 853 | ); |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @dataProvider providesValidUuids |
||
| 858 | */ |
||
| 859 | public function testValidUuids($uuid) |
||
| 860 | { |
||
| 861 | Assertion::uuid($uuid); |
||
| 862 | } |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @dataProvider providesInvalidUuids |
||
| 866 | */ |
||
| 867 | public function testInvalidUuids($uuid) |
||
| 868 | { |
||
| 869 | $this->setExpectedException('Assert\InvalidArgumentException'); |
||
| 870 | Assertion::uuid($uuid); |
||
| 871 | } |
||
| 872 | |||
| 873 | static public function providesValidUuids() |
||
| 874 | { |
||
| 875 | return array( |
||
| 876 | array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), |
||
| 877 | array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), |
||
| 878 | array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), |
||
| 879 | array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), |
||
| 880 | array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), |
||
| 881 | array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), |
||
| 882 | ); |
||
| 883 | } |
||
| 884 | |||
| 885 | static public function providesInvalidUuids() |
||
| 886 | { |
||
| 887 | return array( |
||
| 888 | array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), |
||
| 889 | array('af6f8cb0c57d11e19b210800200c9a66'), |
||
| 890 | array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), |
||
| 891 | array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), |
||
| 892 | array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), |
||
| 893 | ); |
||
| 894 | } |
||
| 895 | |||
| 896 | public function testValidNotEmptyKey() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @dataProvider invalidNotEmptyKeyDataprovider |
||
| 903 | */ |
||
| 904 | public function testInvalidNotEmptyKey($invalidArray, $key) |
||
| 905 | { |
||
| 906 | $this->setExpectedException('Assert\InvalidArgumentException'); |
||
| 907 | Assertion::notEmptyKey($invalidArray, $key); |
||
| 908 | } |
||
| 909 | |||
| 910 | public static function invalidNotEmptyKeyDataprovider() |
||
| 917 | |||
| 918 | public function testAllWithSimpleAssertion() |
||
| 919 | { |
||
| 920 | Assertion::allTrue(array(true, true)); |
||
| 921 | } |
||
| 922 | |||
| 923 | public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
| 924 | { |
||
| 925 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE); |
||
| 926 | Assertion::allTrue(array(true, false)); |
||
| 927 | } |
||
| 928 | |||
| 929 | public function testAllWithComplexAssertion() |
||
| 930 | { |
||
| 931 | Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass'); |
||
| 932 | } |
||
| 933 | |||
| 934 | public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
| 935 | { |
||
| 936 | $this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF); |
||
| 937 | |||
| 938 | Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos'); |
||
| 939 | } |
||
| 940 | |||
| 941 | public function testAllWithNoValueThrows() |
||
| 946 | |||
| 947 | public function testValidCount() |
||
| 948 | { |
||
| 949 | Assertion::count(array('Hi'), 1); |
||
| 950 | Assertion::count(new OneCountable(), 1); |
||
| 951 | } |
||
| 952 | |||
| 953 | public static function dataInvalidCount() |
||
| 954 | { |
||
| 955 | return array( |
||
| 956 | array(array('Hi', 'There'), 3), |
||
| 957 | array(new OneCountable(), 2), |
||
| 958 | ); |
||
| 959 | } |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @dataProvider dataInvalidCount |
||
| 963 | */ |
||
| 964 | public function testInvalidCount($countable, $count) |
||
| 965 | { |
||
| 966 | $this->setExpectedException('Assert\AssertionFailedException', 'List does not contain exactly "'.$count.'" elements.', Assertion::INVALID_COUNT); |
||
| 967 | Assertion::count($countable, $count); |
||
| 968 | } |
||
| 969 | |||
| 970 | public function testChoicesNotEmpty() |
||
| 971 | { |
||
| 972 | Assertion::choicesNotEmpty( |
||
| 973 | array('tux' => 'linux', 'Gnu' => 'dolphin'), |
||
| 974 | array('tux') |
||
| 975 | ); |
||
| 976 | } |
||
| 977 | |||
| 978 | /** |
||
| 979 | * @dataProvider invalidChoicesProvider |
||
| 980 | */ |
||
| 981 | public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode) |
||
| 982 | { |
||
| 983 | $this->setExpectedException('Assert\AssertionFailedException', null, $exceptionCode); |
||
| 984 | Assertion::choicesNotEmpty( |
||
| 985 | $values, |
||
| 986 | $choices |
||
| 987 | ); |
||
| 988 | } |
||
| 989 | |||
| 990 | public function invalidChoicesProvider() |
||
| 991 | { |
||
| 992 | return array( |
||
| 998 | |||
| 999 | public function testIsObject() |
||
| 1003 | |||
| 1004 | public function testIsObjectExpectingException() |
||
| 1009 | |||
| 1010 | public function testMethodExists() |
||
| 1014 | |||
| 1015 | public function testMethodExistsFailure() |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * @test |
||
| 1023 | */ |
||
| 1024 | public function it_passes_values_and_constraints_to_exception() |
||
| 1035 | |||
| 1036 | public function testLessThan() |
||
| 1043 | |||
| 1044 | public function invalidLessProvider() |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @dataProvider invalidLessProvider |
||
| 1058 | */ |
||
| 1059 | public function testLessThanThrowsException($value, $limit) |
||
| 1064 | |||
| 1065 | public function testLessOrEqualThan() |
||
| 1075 | |||
| 1076 | public function invalidLessOrEqualProvider() |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * @dataProvider invalidLessOrEqualProvider |
||
| 1087 | */ |
||
| 1088 | public function testLessOrEqualThanThrowsException($value, $limit) |
||
| 1093 | |||
| 1094 | public function testGreaterThan() |
||
| 1101 | |||
| 1102 | public function invalidGreaterProvider() |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * @dataProvider validDateProvider |
||
| 1116 | */ |
||
| 1117 | public function testValidDate($value, $format) |
||
| 1121 | |||
| 1122 | public function validDateProvider() |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * @dataProvider invalidGreaterProvider |
||
| 1134 | */ |
||
| 1135 | public function testGreaterThanThrowsException($value, $limit) |
||
| 1140 | |||
| 1141 | public function testGreaterOrEqualThan() |
||
| 1151 | |||
| 1152 | public function invalidGreaterOrEqualProvider() |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * @dataProvider invalidGreaterOrEqualProvider |
||
| 1163 | * |
||
| 1164 | * @param mixed $value |
||
| 1165 | * @param mixed $limit |
||
| 1166 | */ |
||
| 1167 | public function testGreaterOrEqualThanThrowsException($value, $limit) |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * @dataProvider invalidDateProvider |
||
| 1175 | */ |
||
| 1176 | public function testInvalidDate($value, $format) |
||
| 1181 | |||
| 1182 | public function invalidDateProvider() |
||
| 1190 | |||
| 1191 | public function testInvalidTraversable() |
||
| 1196 | |||
| 1197 | public function testInvalidArrayAccessible() |
||
| 1202 | |||
| 1203 | public function testInvalidCallable() |
||
| 1208 | |||
| 1209 | public function testValidCallable() |
||
| 1217 | |||
| 1218 | public function testInvalidSatisfy() |
||
| 1225 | |||
| 1226 | public function testValidSatisfy() |
||
| 1240 | |||
| 1241 | public function testInvalidInterfaceExists() |
||
| 1246 | |||
| 1247 | public function testValidInterfaceExists() |
||
| 1252 | } |
||
| 1253 | |||
| 1271 |