PHPUnit教程(四)断言详解

发布时间:2019-11-12编辑:脚本学堂
本文是PHPUnit系列教程的第四篇,详细介绍了PHPUnit断言的相关知识,感兴趣的朋友参考下。

PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
约束允许另外一个约束类为一个类或对象的属性    

Constraint that applies another constraint to an attribute of a class or an object.
PHPUnit_Framework_Constraint_IsAnything anything()
约束接受任意的输入值
Constraint that accepts any input value.

PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key)    
Constraint that asserts that the array it is evaluated for has a given key.
约束断言评估数组有传入的$key

PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value)    
Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains a given value.
约束断言一个数组或者实现迭代器接口的对象含有$value

PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10)    
Constraint that checks if one value is equal to another.
约束断言$value和其他的相同

PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)    
Constraint that checks if a value is equal to an attribute of a class or of an object.
约束断言$value和一个类或对象的属性的值相同

PHPUnit_Framework_Constraint_FileExists fileExists()    
Constraint that checks if the file(name) that it is evaluated for exists.
约束断言文件是存在的

PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value)    
Constraint that asserts that the value it is evaluated for is greater than a given value.
约束断言$value是大于传入的值的

PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value)    
Constraint that asserts that the value it is evaluated for is greater than or equal to a given value.
约束断言$value是大于或等于传入的值的

PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName)    
Constraint that asserts that the class it is evaluated for has a given attribute.
约束断言类含有属性$attributeName

PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)    
Constraint that asserts that the class it is evaluated for has a given static attribute.
约束断言类含有静态属性$attributeName

PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName)    
Constraint that asserts that the object it is evaluated for has a given attribute.
约束断言对象含有属性$attributeName

PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value)    
Constraint that asserts that one value is identical to another.
约束断言$value和其他的完全相同

PHPUnit_Framework_Constraint_IsFalse isFalse()    
Constraint that asserts that the value it is evaluated is FALSE.
约束断言$value的值为false

PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className)    
Constraint that asserts that the object it is evaluated for is an instance of a given class.
约束断言对象是$className的实例

PHPUnit_Framework_Constraint_IsNull isNull()    
Constraint that asserts that the value it is evaluated is NULL.
约束断言$value的值为NULL

PHPUnit_Framework_Constraint_IsTrue isTrue()    
Constraint that asserts that the value it is evaluated is TRUE.
约束断言$value的值为TRUE

PHPUnit_Framework_Constraint_IsType isType(string $type)    
Constraint that asserts that the value it is evaluated for is of a specified type.
约束断言对象的类型的为$type

PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value)    
Constraint that asserts that the value it is evaluated for is smaller than a given value.
约束断言对象的值小于$value

PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value)    
Constraint that asserts that the value it is evaluated for is smaller than or equal to a given value.
约束断言对象的值小于等于$value

logicalAnd()    
Logical AND.
逻辑的And

logicalNot(PHPUnit_Framework_Constraint $constraint)    
Logical NOT.
逻辑的

logicalOr()    
Logical OR.
逻辑的OR

logicalXor()    
Logical XOR.
逻辑的XOR

PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern)    
Constraint that asserts that the string it is evaluated for matches a regular expression.
约束断言字符串符合传入的正则表达式$pattern

PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case)    
Constraint that asserts that the string it is evaluated for contains a given string.
约束断言字符串中含有$string

PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix)    
Constraint that asserts that the string it is evaluated for ends with a given suffix.
约束断言字符串由$string结尾

PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix)    
Constraint that asserts that the string it is evaluated for starts with a given prefix.
约束断言字符串由$string开头

assertThat的用法,使用多个约束和约束逻辑来实现断言(图片点击放大)
 

PHPUnit断言图二

assertTrue(bool $condition[, string $message = ''])
断言$condition为True,否则就报告错误

assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile[, string $message = ''])
断言$actualFile和$expectedFile的xml文件的内容相同,否则就报告错误

assertXmlStringEqualsXmlFile(string $expectedFile, string $actualXml[, string $message = ''])
断言$actualXml的内容和$expectedFile相同,否则就报告错误

assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml[, string $message = ''])
断言$actualXml的内容和$expectedXml相同,否则就报告错误

有关PHPUnit断言的内容就介绍这些了,下篇将介绍PHPUnit类中的工具方法,敬请关注。