PHP教程:PHPUnit學習筆記(四)斷言詳解

斷言(Assertions)是PHPUnit提供的一系列對程序執行結果測試的方法。通俗的講,就是斷言執行程序結果爲咱們期待的值,若是不是則測試失敗,下面是斷言方法的詳細介紹,內容所有來翻譯自PHPUnit的官方文檔,部分方法官方介紹的很模糊,我根據官方的源碼註釋增長了說明和註釋css

assertArrayHasKey(mixed $key, array $array[, string $message = '']) 
斷言數組$array含有索引$key, $message用於自定義輸出的錯誤信息,後同

assertClassHasAttribute(string $attributeName, string $className[, string $message = '']) 
斷言類$className含有屬性$attributeName

assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = '']) 
斷言類$className含有靜態屬性$attributeName

assertContains(mixed $needle, Iterator|array $haystack[, string $message = '']) 
斷言迭代器對象$haystack/數組$haystack含有$needle

assertNotContains() 
與上條相反

assertAttributeContains(mixed $needle, Class|Object $haystack[, string $message = '']) 
斷言$needle爲一個類/對象$haystack可訪問到的屬性(public, protected 和 private)

assertAttributeNotContains() 
與上條相反

assertContains(string $needle, string $haystack[, string $message = '']) 
斷言字符串$needle在字符串$haystack中

assertContainsOnly(string $type, Iterator|array $haystack[, boolean $isNativeType = NULL, string $message = '']) 
斷言迭代器對象/數組$haystack中只有$type類型的值, $isNativeType 設定爲PHP原生類型,$message同上

assertNotContainsOnly() 
與上條相反

assertAttributeContainsOnly() 和 assertAttributeNotContainsOnly() 
斷言對象的屬性只有$type類型和非含有$type類型

assertEmpty(mixed $actual[, string $message = '']) 
斷言$actual爲空

assertNotEmpty() 
趕上條相反

assertAttributeEmpty() 和 assertAttributeNotEmpty() 
斷言對象的全部屬性爲空或不爲空

assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode[, boolean $checkAttributes = FALSE, string $message = '']) 
斷言Dom節點$actualNode和DOM節點$expectedNode相同,$checkAttributes FALSE 不斷言節點屬性,TRUE則斷言屬性$message同上

assertEquals(mixed $expected, mixed $actual[, string $message = '']) 
斷言複合類型$actual與$expected相同

assertNotEquals() 
與上條相反

assertAttributeEquals() and assertAttributeNotEquals() 
斷言類屬性$actual與$expected相同

assertEquals(array $expected, array $actual[, string $message = '']) 
斷言數組$actual和數組$expected相同

assertFalse(bool $condition[, string $message = '']) 
斷言$condition的結果爲false

assertFileEquals(string $expected, string $actual[, string $message = '']) 
斷言文件$actual和$expected相同

assertFileExists(string $filename[, string $message = '']) 
斷言文件$filename存在

assertFileNotExists() 
與上條相反

assertGreaterThan(mixed $expected, mixed $actual[, string $message = '']) 
斷言$actual比$expected大

assertAttributeGreaterThan() 
斷言類的屬性用

assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 
斷言$actual大於等於$expected

assertAttributeGreaterThanOrEqual() 
斷言類的屬性

assertInstanceOf($expected, $actual[, $message = '']) 
斷言$actual爲$expected的實例

assertNotInstanceOf() 
與上相反

assertAttributeInstanceOf() and assertAttributeNotInstanceOf() 
斷言類屬性用

assertInternalType($expected, $actual[, $message = '']) 
斷言$actual的類型爲$expected

assertNotInternalType() 
與上相反

assertAttributeInternalType() and assertAttributeNotInternalType() 
斷言類屬性用

assertLessThan(mixed $expected, mixed $actual[, string $message = '']) 
斷言$actual小於$expected

assertAttributeLessThan() 
斷言類屬性小於$expected

assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 
斷言$actual小於等於$expected

assertAttributeLessThanOrEqual() 
斷言類屬性小於等於$expected

assertNull(mixed $variable[, string $message = '']) 
斷言$variable的值爲null

assertNotNull() 
與上條相反

assertObjectHasAttribute(string $attributeName, object $object[, string $message = '']) 
斷言$object含有屬性$attributeName

assertObjectNotHasAttribute() 
與上條相反

assertRegExp(string $pattern, string $string[, string $message = '']) 
斷言字符串$string符合正則表達式$pattern

assertNotRegExp() 
與上條相反

assertStringMatchesFormat(string $format, string $string[, string $message = '']) 
斷言$string符合$format定義的格式,例如 %i %s等等html


assertStringNotMatchesFormat() 
與上條相反node


assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = '']) 
斷言$string路徑的文件的格式和$formatFile文件的格式相同正則表達式


assertStringNotMatchesFormatFile() 
與上條相反express


assertSame(mixed $expected, mixed $actual[, string $message = '']) 
斷言$actual和$expected的類型和值相同數組


assertNotSame() 
與上條相反app


assertAttributeSame() and assertAttributeNotSame() 
斷言類屬性用less


assertSame(object $expected, object $actual[, string $message = '']) 
斷言對象$actual和對象$expected相同ide


assertSelectCount(array $selector, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE]) 
斷言在$actual文檔中(格式爲html或xml)css選擇器$selector有$count個,或有符合$selector的元素(設定$count爲true),或沒有符合$selector的元素(設定$count爲false)工具

assertSelectCount("#binder", true, $xml);  // 有一個就行
assertSelectCount(".binder", 3, $xml);     // 必須有3個?


assertSelectEquals(array $selector, string $content, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE]) 
斷言在文檔$actual中有符合根據$selector的找到符合$content的$count個元素,當$count等於true和false的時候做用以下:

assertSelectEquals("#binder .name", "Chuck", true,  $xml);  // 全部的name等於Chuck
assertSelectEquals("#binder .name", "Chuck", false, $xml);  // 全部的name不等於Chuck


assertSelectRegExp(array $selector, string $pattern, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])


assertStringEndsWith(string $suffix, string $string[, string $message = '']) 
斷言$string的末尾爲$suffix結束


assertStringEndsNotWith() 
與上條相反


assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = ''])
斷言$actualString在文件$expectedFile的內容中


assertStringNotEqualsFile() 
與上條相反


assertStringStartsWith(string $prefix, string $string[, string $message = '']) 
斷言$string的開頭爲$suffix


assertStringStartsNotWith() 
與上條相反

assertTag(array $matcher, string $actual[, string $message = '', boolean $isHtml = TRUE]) 
斷言$actual的內容符合$matcher的定義,matcher的定義以下:
# id: 節點必須帶有id屬性且名稱與id設定的相同
# tags: 節點的名稱必須與tags的值匹配
# attributes: 節點的屬性必須與$attributes數組中的值相匹配
# content: 文本內容必須與$content的值相同.
# parent: 節點的父節點必須符合$parent數組中定義的內容.
# child: 節點的字節點中有至少一個直系子節點知足 $child 數組中定義的內容. 
At least one of the node's immediate children must meet the criteria described by the $child associative array.
# ancestor: 節點的父節點中有至少一個節點知足 $ancestor 數組中定義的內容.
At least one of the node's ancestors must meet the criteria described by the $ancestor associative array.
# descendant: 節點的字節點中有至少一個子節點知足 $descendant 數組中定義的內容. 
At least one of the node's descendants must meet the criteria described by the $descendant associative array.
# children: 用於計算字節點的聯合數組 
Associative array for counting children of a node.
    * count: 符合匹配標準的字節點數目須要和count的值相同 
The number of matching children must be equal to this number.
    * less_than: 符合匹配標準的字節點數目須要比count的值少 
The number of matching children must be less than this number.
    * greater_than: 符合匹配標準的字節點數目須要比count的值多 
The number of matching children must be greater than this number.
    * only: 另一個聯合數組用於定義配合標準的節點,只有這些節點纔會被計算入內 
Another associative array consisting of the keys to use to match on the children, and only matching children will be counted


assertTag的代碼例子(圖片點擊放大):

點擊查看原圖
點擊查看原圖 

More complex assertions can be formulated using the PHPUnit_Framework_Constraint classes
更加複雜的斷言能夠經過PHPUnit_Framework_Constraint類來制定

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的用法,使用多個約束和約束邏輯來實現斷言(圖片點擊放大)
點擊查看原圖 

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類中的工具方法

相關文章
相關標籤/搜索