PHP Tutorial: PHPUnit Study Notes (iv) assertions

Source: Internet
Author: User
Tags php tutorial

Assertion (assertions) is a series of methods provided by PHPUnit to test the results of a program execution. In layman's terms, it is asserted that the execution of the program results for the value we expect, if not the test fails, the following is a detailed description of the assertion method, the content is translated from the official documents of the PHPUnit, some of the official introduction of the method is very vague, I added the official source code comments and comments

Assertarrayhaskey (mixed $key, array $array [, String $message = '])
The assertion array $array contains the index $key, $message the error message for the custom output, and the

Assertclasshasattribute (String $attributeName, String $className [, String $message = '])
Asserts that the class $classname contains attributes $attributename

Assertclasshasstaticattribute (String $attributeName, String $className [, String $message = '])
Asserts that the class $classname contains static properties $attributename

assertcontains (mixed $needle, Iterator|array $haystack [, String $message = '])
The assertion iterator Object $haystack/array $haystack contains $needle

assertnotcontains ()
Contrary to the previous article

assertattributecontains (mixed $needle, class| Object $haystack [, String $message = '])
Asserts that $needle is a class/object $haystack accessible property (public, protected, and private)

assertattributenotcontains ()
Contrary to the previous article

assertcontains (String $needle, String $haystack [, String $message = '])
Assertion string $needle in string $haystack

assertcontainsonly (String $type, Iterator|array $haystack [, Boolean $isNativeType = NULL, string $message = '])
Assert iterator object/array $haystack only $type type value, $isNativeType set to PHP native type, $message ibid.

assertnotcontainsonly ()
Contrary to the previous article

assertattributecontainsonly () and assertattributenotcontainsonly ()
Asserts that the properties of an object are only $type types and non-contained $type types

assertempty (mixed $actual [, string $message = '])
Assertion $actual is empty

Assertnotempty ()
In the opposite way

Assertattributeempty () and Assertattributenotempty ()
Asserts that all properties of an object are empty or not empty

assertequalxmlstructure (Domnode $expectedNode, Domnode $actualNode [, Boolean $checkAttributes = FALSE, String $ message = "])
Asserts that the DOM node $actualnode and the DOM node $expectednode the same, $checkAttributes FALSE to keep the node property, true to assert the attribute $message ibid.

assertequals (mixed $expected, mixed $actual [, string $message = '])
Assert that the composite type $actual is the same as $expected

assertnotequals ()
Contrary to the previous article

assertattributeequals () and assertattributenotequals ()
Assert class attribute $actual same as $expected

assertequals (array $expected, array $actual [, String $message = '])
Assertion array $actual and Arrays $expected same

Assertfalse (bool $condition [, String $message = '])
asserts that the result of $condition is false

assertfileequals (String $expected, String $actual [, String $message = '])
Assertion file $actual and $expected are the same

assertfileexists (String $filename [, String $message = '])
Assertion file $filename exists

assertfilenotexists ()
Contrary to the previous article

Assertgreaterthan (mixed $expected, mixed $actual [, string $message = '])
Assert that $actual is larger than $expected

Assertattributegreaterthan ()
Asserts the properties of a class with the

assertgreaterthanorequal (mixed $expected, mixed $actual [, string $message = '])
Assertion $actual is greater than or equal to $expected

assertattributegreaterthanorequal ()
Assert the properties of a class

assertinstanceof ($expected, $actual [, $message = '])
Assert $actual as an instance of $expected

assertnotinstanceof ()
Contrary to the above

assertattributeinstanceof () and assertattributenotinstanceof ()
Assert class properties with

Assertinternaltype ($expected, $actual [, $message = '])
Asserts that the type of $actual is $expected

Assertnotinternaltype ()
Contrary to the above

Assertattributeinternaltype () and Assertattributenotinternaltype ()
Assert class properties with

Assertlessthan (mixed $expected, mixed $actual [, string $message = '])
Assertion $actual less than $expected

Assertattributelessthan ()
Assertion class attribute is less than $expected

assertlessthanorequal (mixed $expected, mixed $actual [, string $message = '])
Assertion $actual is less than or equal to $expected

assertattributelessthanorequal ()
Assert class attribute is less than or equal to $expected

assertnull (mixed $variable [, string $message = '])
Asserts that the value of $variable is null

Assertnotnull ()

Contrary to the previous article

Assertobjecthasattribute (String $attributeName, Object $object [, String $message = '])

Assertion $object contains attributes $attributename

Assertobjectnothasattribute ()
Contrary to the previous article

assertregexp (String $pattern, String $string [, String $message = '])
asserts that the string $string conforms to the regular expression $pattern

assertnotregexp ()
Contrary to the previous article

Assertstringmatchesformat (String $format, String $string [, String $message = '])
Assertion $string conforms to $format-defined format, such as%i%s, and so on


Assertstringnotmatchesformat ()
Contrary to the previous article


assertstringmatchesformatfile (String $formatFile, String $string [, String $message = '])
Asserts that the $string path of the file is in the same format as the $formatfile file


Assertstringnotmatchesformatfile ()
Contrary to the previous article


assertsame (mixed $expected, mixed $actual [, string $message = '])
asserts that $actual and $expected have the same type and value


Assertnotsame ()
Contrary to the previous article


Assertattributesame () and Assertattributenotsame ()
Assert class properties with


Assertsame (Object $expected, Object $actual [, String $message = '])
Assertion object $actual and object $expected are the same


Assertselectcount (array $selector, Integer $count, mixed $actual [, string $message = ' ', Boolean $isHtml = TRUE])
asserts that the CSS selector $selector has $count in the $actual document (either formatted as HTML or XML) or has an element that conforms to the $selector (set $count to True), or does not conform to the $selector element (setting $ Count is False)

Assertselectcount ("#binder", true, $xml); There's one on it.
Assertselectcount (". Binder", 3, $xml); There must be 3 of them?


assertselectequals (array $selector, String $content, Integer $count, mixed $actual [, string $message = ', Boolean $is Html = TRUE])
The assertion that there is a $count element in document $actual that conforms to $content according to $selector, when $count equals True and False, functions as follows:

Assertselectequals ("#binder. Name", "Chuck", true, $xml); All name equals Chuck.
Assertselectequals ("#binder. Name", "Chuck", false, $xml); All the name does not equal Chuck


Assertselectregexp (array $selector, String $pattern, Integer $count, mixed $actual [, string $message = ', Boolean $isHtml = TRUE])


Assertstringendswith (String $suffix, String $string [, String $message = '])
Assertion $string ends at end of $suffix


Assertstringendsnotwith ()
Contrary to the previous article


Assertstringequalsfile (String $expectedFile, String $actualString [, String $message = '])
assert $actualstring in the contents of the file $expectedfile


Assertstringnotequalsfile ()
Contrary to the previous article


Assertstringstartswith (String $prefix, String $string [, String $message = '])

Assert that $string begins with $suffix


Assertstringstartsnotwith ()
Contrary to the previous article

Asserttag (array $matcher, String $actual [, String $message = ', Boolean $isHtml = TRUE])
Assert that the content of $actual conforms to the $matcher definition, Matcher is defined as follows:
# ID: The node must have an id attribute with the same name as the ID setting
# Tags: the name of the node must match the value of tags
# Attributes: The properties of the node must match the values in the $attributes array
# content: The text content must be the same as the value of $content.
# Parent: The parents of the node must conform to the content defined in the $parent array.
# Child: At least one immediate child node in the node's byte point satisfies the content defined in the $child array.
At least one of the node's immediate children must meet the criteria described by the $child associative array.
# ancestor: At least one node in the parent node of the node satisfies the content defined in the $ancestor array.
At least one of the node's ancestors must meet the criteria described by the $ancestor associative array.
# descendant: At least one child node in a node's byte point satisfies the content defined in the $descendant array.
At least one of the node's descendants must meet the criteria described by the $descendant associative array.
# Children: Union array for calculating byte points
Associative array for counting children of a node.
* Count: The number of byte points matching the match criteria needs to be the same as the value of Count
The number of matching children must is equal to this number.
* Less_than: The number of byte points that match the matching criteria needs to be less than the value of Count
The number of matching children must is less than this number.
* Greater_than: The number of byte points matching the match criteria needs to be more than the value of Count
The number of matching children must is greater than this number.
* Only: Another federated array is used to define the nodes that mate the criteria, and only those nodes are counted in
Another associative array consisting of the keys to use match on the children, and only matching children would be count Ed


Asserttag code example (image click to enlarge):


More complex assertions can be formulated using the Phpunit_framework_constraint classes
More complex assertions can be made through the Phpunit_framework_constraint class.

Phpunit_framework_constraint_attribute Attribute (Phpunit_framework_constraint $constraint, $attributeName)
Constraints allow another constraint class to be a property of a class or object

Constraint that applies another Constraint to an attribute of a class or an object.
Phpunit_framework_constraint_isanything Anything ()
Constrain to accept arbitrary input values
Constraint that accepts any input value.

Phpunit_framework_constraint_arrayhaskey Arrayhaskey (mixed $key)
Constraint that asserts the array it's evaluated for have a given key.
Constraint assertion evaluation array with incoming $key

Phpunit_framework_constraint_traversablecontains contains (mixed $value)
Constraint that asserts, the array or object that implements the Iterator interface it's evaluated for contains a GIV En value.
Constraints assert an array or object that implements an iterator interface containing $value

Phpunit_framework_constraint_isequal Equalto ($value, $delta = 0, $maxDepth = 10)
Constraint that checks if one value is equal to another.
Constraint assertion $value and others are the same

Phpunit_framework_constraint_attribute Attributeequalto ($attributeName, $value, $delta = 0, $maxDepth = 10)
Constraint that checks if a value was equal to an attribute of a class or of an object.
Constraint assertion $value The same value as a property of a class or object

Phpunit_framework_constraint_fileexists FileExists ()
Constraint that checks if the file (name), that it's evaluated for exists.
Constraint assertion file is present

Phpunit_framework_constraint_greaterthan GreaterThan (mixed $value)
Constraint that asserts, the value it is evaluated for is greater than a given value.
Constraint assertion $value is greater than the value passed in

Phpunit_framework_constraint_or greaterthanorequal (mixed $value)
Constraint that asserts, the value it is evaluated for was greater than or equal to a given value.
The constraint assertion $value is greater than or equal to the value passed in.

Phpunit_framework_constraint_classhasattribute Classhasattribute (String $attributeName)
Constraint that asserts, the class it is evaluated for have a given attribute.
Constraint assertion class contains attribute $attributename

Phpunit_framework_constraint_classhasstaticattribute Classhasstaticattribute (String $attributeName)
Constraint that asserts, the class it is evaluated for have a given static attribute.
Constraint assertion class contains static properties $attributename

Phpunit_framework_constraint_objecthasattribute Hasattribute (String $attributeName)
Constraint that asserts, the object it is evaluated for have a given attribute.
Constraint assertion object contains attribute $attributename

Phpunit_framework_constraint_isidentical Identicalto (mixed $value)
Constraint that asserts the one value is identical to another.
Constraint Assertion $value is exactly the same as the others

Phpunit_framework_constraint_isfalse IsFalse ()
Constraint that asserts, the value it is evaluated is FALSE.
The value of the constraint assertion $value is False

Phpunit_framework_constraint_isinstanceof isinstanceof (String $className)
Constraint that asserts, the object it is evaluated a instance of a given class.
Constraint Assertion object is an instance of $classname

Phpunit_framework_constraint_isnull IsNull ()
Constraint that asserts, the value it is evaluated is NULL.
The value of the constraint assertion $value is null

Phpunit_framework_constraint_istrue IsTrue ()
Constraint that asserts, the value it is evaluated is TRUE.
The value of the constraint assertion $value is True

Phpunit_framework_constraint_istype Istype (String $type)
Constraint that asserts, the value it is evaluated for is a specified type.
Constraint asserts that the type of the object is $type

Phpunit_framework_constraint_lessthan LessThan (mixed $value)
Constraint that asserts, the value it is evaluated for is smaller than a given value.
The value of the constraint assertion object is less than $value

Phpunit_framework_constraint_or lessthanorequal (mixed $value)
Constraint that asserts, the value it is evaluated for was smaller than or equal to a given value.
Constraint Assertion object has a value less than or equal to $value

LogicalAnd ()
Logical and.
Logical AND

Logicalnot (Phpunit_framework_constraint $constraint)
Logical not.
of logic

Logicalor ()
Logical OR.
Logical OR

Logicalxor ()
Logical XOR.
The Xor of Logic

Phpunit_framework_constraint_pcrematch matchesregularexpression (String $pattern)
Constraint that asserts, the string it is evaluated for matches a regular expression.
The constraint assertion string conforms to the passed-in regular expression $pattern

Phpunit_framework_constraint_stringcontains stringcontains (String $string, bool $case)
Constraint that asserts, the string it is evaluated for contains a given string.
The constraint assertion string contains $string

Phpunit_framework_constraint_stringendswith Stringendswith (String $suffix)
Constraint that asserts, the string it is evaluated for ends with a given suffix.
Constraint assertion string terminated by $string

Phpunit_framework_constraint_stringstartswith Stringstartswith (String $prefix)
Constraint that asserts, the string it is evaluated for starts with a given prefix.
Constraint assertion string starts with $string

Assertthat usage, using multiple constraints and constraints logic to implement assertions (image click to enlarge)


Asserttrue (bool $condition [, String $message = '])

Assertion $condition is true, otherwise an error is reported


assertxmlfileequalsxmlfile (String $expectedFile, String $actualFile [, String $message = '])
Asserts that the contents of the XML file for $actualfile and $expectedfile are the same, otherwise the error is reported


assertxmlstringequalsxmlfile (String $expectedFile, String $actualXml [, String $message = '])
Assert that the content of $actualxml is the same as $expectedfile, or report an error


assertxmlstringequalsxmlstring (String $expectedXml, String $actualXml [, String $message = '])
Assert that the content of $actualxml is the same as $expectedxml, or report an error

PHP Tutorial: PHPUnit Study Notes (iv) assertions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.