-
Notifications
You must be signed in to change notification settings - Fork 27
Asserts
This is a tutorial for using https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Assert.
All of these asserts accept an optional $message
parameter as the third parameter of the assert. The $message
parameter is used in the same manner as in a PHPUnit_Framework_Assert
.
This assert takes an array of expected items and checks to make sure at least one occurrence (disregarding duplicates) is contained in the actual array.
Example: // This will pass because $actual contains at least the a 1, a 2, and a 3. PHPUnit_Extensions_Assert_More::assertHasItems(array(1, 2, 3), array(3, 1, 0, 2));
// This will fail because the $actual array does not contain a 4 PHPUnit_Extensions_Assert_More::assertHasItems(array(4), array(1, 2, 3));
More Examples: https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Assert/MoreTest.php
This assert takes an array of expected length and checks that the actual array is the same length.
Please use version in the PHPUnit Framework: $this->assertCount()
This assert compares to arrays as equals if both arrays contain the same items and are of the same size. This is functionally equivalent to using the 'canonicalize' parameter in PHPUnit_Framework_Assert::assertEquals
, but the comparison does not depend on sort()
and is more readable than a long parameter list.
Example: // This will pass because both arrays are of size 3, and both contain a 1, a 2, and a 3 PHPUnit_Extensions_Assert_More::assertArrayEqualsNoOrder(array(1, 2, 3), array(3, 1, 2));
// This will fail because even though the arrays are the same size, the arrays contain different elements PHPUnit_Extensions_Assert_More::assertArrayEqualsNoOrder(array(1, 2, 3), array(4, 5, 6)); // This will fail because even though the arrays both contain a 1, a 2, and a 3, the arrays are not the same size. PHPUnit_Extensions_Assert_More::assertArrayEqualsNoOrder(array(1, 2, 3), array(1, 1, 2, 3));
More Examples: https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Assert/MoreTest.php
This assert compares to strings as equal by trimming all leading and trailing whitespace, and by converting all other whitespace characters to a single space before comparing the two strings.
Example: // This will pass because both string after reducing white space are "spaces do not matter" PHPUnit_Extensions_Assert_More::assertStringMatchIgnoreWhitespace("spaces do not matter", "spaces\ndo\tnot matter ");
// This will fail bacause one string is equivalent to "spaces do not matter" // and the other to "but the not whitespace characters do!" PHPUnit_Extensions_Assert_More::assertStringMatchIgnoreWhitespace("spaces do not matter", "but the not whitespace characters do!");
More Examples: https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Assert/MoreTest.php
This assert takes an array of expected items and checks to make sure that a particular key and value pair exists.
Example: // This will pass because $actual contains 'key' paired with 'value' PHPUnit_Extensions_Assert_More::assertArrayHasKeyValuePair( 'key', 'value', array('key' => 'value', 'key1' =>'value1') );
// This will fail because the $actual array does not pair 'key' with 'value' PHPUnit_Extensions_Assert_More::assertHasItems( 'key', 'value', array('key' => 'not_value', 'not_key' => 'value') );
More Examples: https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/Assert/MoreTest.php