日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHPUnit-附录 A. 断言 (assert)

發布時間:2024/9/21 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHPUnit-附录 A. 断言 (assert) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html】

?

本附錄列舉可用的各種斷言方法。

assertArrayHasKey()

assertArrayHasKey(mixed $key, array $array[, string $message = ''])

當 $array 不包含 $key 時報告錯誤,錯誤訊息由 $message 指定。

assertArrayNotHasKey() 是與之相反的斷言,接受相同的參數。

例 A.1: assertArrayHasKey() 的用法

<?php use PHPUnit\Framework\TestCase; class ArrayHasKeyTest extends TestCase { public function testFailure() { $this->assertArrayHasKey('foo', ['bar' => 'baz']); } } ?> phpunit ArrayHasKeyTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) ArrayHasKeyTest::testFailure Failed asserting that an array has the key 'foo'./home/sb/ArrayHasKeyTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertClassHasAttribute()

assertClassHasAttribute(string $attributeName, string $className[, string $message = ''])

當 $className::attributeName 不存在時報告錯誤,錯誤訊息由 $message 指定。

assertClassNotHasAttribute() 是與之相反的斷言,接受相同的參數。

例 A.2: assertClassHasAttribute() 的用法

<?php use PHPUnit\Framework\TestCase; class ClassHasAttributeTest extends TestCase { public function testFailure() { $this->assertClassHasAttribute('foo', stdClass::class); } } ?> phpunit ClassHasAttributeTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) ClassHasAttributeTest::testFailure Failed asserting that class "stdClass" has attribute "foo"./home/sb/ClassHasAttributeTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertArraySubset()

assertArraySubset(array $subset, array $array[, bool $strict = '', string $message = ''])

當 $array 不包含 $subset 時報告錯誤,錯誤訊息由 $message 指定。

$strict 是一個標志,用于表明是否需要對數組中的對象進行全等判定。

例 A.3: assertArraySubset() 的用法

<?php use PHPUnit\Framework\TestCase; class ArraySubsetTest extends TestCase { public function testFailure() { $this->assertArraySubset(['config' => ['key-a', 'key-b']], ['config' => ['key-a']]); } } ?> phpunit ArrayHasKeyTest PHPUnit 4.4.0 by Sebastian Bergmann.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) Epilog\EpilogTest::testNoFollowOption Failed asserting that an array has the subset Array &0 ('config' => Array &1 (0 => 'key-a'1 => 'key-b') )./home/sb/ArraySubsetTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertClassHasStaticAttribute()

assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = ''])

當 $className::attributeName 不存在時報告錯誤,錯誤訊息由 $message 指定。

assertClassNotHasStaticAttribute() 是與之相反的斷言,接受相同的參數。

例 A.4: assertClassHasStaticAttribute() 的用法

<?php use PHPUnit\Framework\TestCase; class ClassHasStaticAttributeTest extends TestCase { public function testFailure() { $this->assertClassHasStaticAttribute('foo', stdClass::class); } } ?> phpunit ClassHasStaticAttributeTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) ClassHasStaticAttributeTest::testFailure Failed asserting that class "stdClass" has static attribute "foo"./home/sb/ClassHasStaticAttributeTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertContains()

assertContains(mixed $needle, Iterator|array $haystack[, string $message = ''])

當 $needle 不是 $haystack的元素時報告錯誤,錯誤訊息由 $message 指定。

assertNotContains() 是與之相反的斷言,接受相同的參數。

assertAttributeContains() 和 assertAttributeNotContains() 是便捷包裝(convenience wrapper),以某個類或對象的 public、protected 或 private 屬性為搜索范圍。

例 A.5: assertContains() 的用法

<?php use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase { public function testFailure() { $this->assertContains(4, [1, 2, 3]); } } ?> phpunit ContainsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) ContainsTest::testFailure Failed asserting that an array contains 4./home/sb/ContainsTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertContains(string $needle, string $haystack[, string $message = '', boolean $ignoreCase = false])

當 $needle 不是 $haystack 的子字符串時報告錯誤,錯誤訊息由 $message 指定。

如果 $ignoreCase 為 true,測試將按大小寫不敏感的方式進行。

例 A.6: assertContains() 的用法

<?php use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase { public function testFailure() { $this->assertContains('baz', 'foobar'); } } ?> phpunit ContainsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) ContainsTest::testFailure Failed asserting that 'foobar' contains "baz"./home/sb/ContainsTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

例 A.7: 帶有 $ignoreCase 參數的 assertContains() 的用法

<?php use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase { public function testFailure() { $this->assertContains('foo', 'FooBar'); } public function testOK() { $this->assertContains('foo', 'FooBar', '', true); } } ?> phpunit ContainsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.F.Time: 0 seconds, Memory: 2.75MbThere was 1 failure:1) ContainsTest::testFailure Failed asserting that 'FooBar' contains "foo"./home/sb/ContainsTest.php:6FAILURES! Tests: 2, Assertions: 2, Failures: 1.

assertContainsOnly()

assertContainsOnly(string $type, Iterator|array $haystack[, boolean $isNativeType = null, string $message = ''])

當 $haystack 并非僅包含類型為 $type 的變量時報告錯誤,錯誤訊息由 $message 指定。

$isNativeType 是一個標志,用來表明 $type 是否是原生 PHP 類型。

assertNotContainsOnly() 是與之相反的斷言,并接受相同的參數。

assertAttributeContainsOnly() 和 assertAttributeNotContainsOnly() 是便捷包裝(convenience wrapper),以某個類或對象的 public、protected 或 private 屬性為搜索范圍。

例 A.8: assertContainsOnly() 的用法

<?php use PHPUnit\Framework\TestCase; class ContainsOnlyTest extends TestCase { public function testFailure() { $this->assertContainsOnly('string', ['1', '2', 3]); } } ?> phpunit ContainsOnlyTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) ContainsOnlyTest::testFailure Failed asserting that Array (0 => '1'1 => '2'2 => 3 ) contains only values of type "string"./home/sb/ContainsOnlyTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertContainsOnlyInstancesOf()

assertContainsOnlyInstancesOf(string $classname, Traversable|array $haystack[, string $message = ''])

當 $haystack 并非僅包含類 $classname 的實例時報告錯誤,錯誤訊息由 $message 指定。

例 A.9: assertContainsOnlyInstancesOf() 的用法

<?php use PHPUnit\Framework\TestCase; class ContainsOnlyInstancesOfTest extends TestCase { public function testFailure() { $this->assertContainsOnlyInstancesOf( Foo::class, [new Foo, new Bar, new Foo] ); } } ?> phpunit ContainsOnlyInstancesOfTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) ContainsOnlyInstancesOfTest::testFailure Failed asserting that Array ([0]=> Bar Object(...)) is an instance of class "Foo"./home/sb/ContainsOnlyInstancesOfTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertCount()

assertCount($expectedCount, $haystack[, string $message = ''])

當 $haystack 中的元素數量不是 $expectedCount 時報告錯誤,錯誤訊息由 $message 指定。

assertNotCount() 是與之相反的斷言,接受相同的參數。

例 A.10: assertCount() 的用法

<?php use PHPUnit\Framework\TestCase; class CountTest extends TestCase { public function testFailure() { $this->assertCount(0, ['foo']); } } ?> phpunit CountTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) CountTest::testFailure Failed asserting that actual size 1 matches expected size 0./home/sb/CountTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryExists()

assertDirectoryExists(string $directory[, string $message = ''])

當 $directory 所指定的目錄不存在時報告錯誤,錯誤訊息由 $message 指定。

assertDirectoryNotExists() 是與之相反的斷言,并接受相同的參數。

例 A.11: assertDirectoryExists() 的用法

<?php use PHPUnit\Framework\TestCase; class DirectoryExistsTest extends TestCase { public function testFailure() { $this->assertDirectoryExists('/path/to/directory'); } } ?> phpunit DirectoryExistsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) DirectoryExistsTest::testFailure Failed asserting that directory "/path/to/directory" exists./home/sb/DirectoryExistsTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryIsReadable()

assertDirectoryIsReadable(string $directory[, string $message = ''])

當 $directory 所指定的目錄不是個目錄或不可讀時報告錯誤,錯誤訊息由 $message 指定。

assertDirectoryNotIsReadable() 是與之相反的斷言,并接受相同的參數。

例 A.12: assertDirectoryIsReadable() 的用法

<?php use PHPUnit\Framework\TestCase; class DirectoryIsReadableTest extends TestCase { public function testFailure() { $this->assertDirectoryIsReadable('/path/to/directory'); } } ?> phpunit DirectoryIsReadableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) DirectoryIsReadableTest::testFailure Failed asserting that "/path/to/directory" is readable./home/sb/DirectoryIsReadableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryIsWritable()

assertDirectoryIsWritable(string $directory[, string $message = ''])

當 $directory 所指定的目錄不是個目錄或不可寫時報告錯誤,錯誤訊息由 $message 指定。

assertDirectoryNotIsWritable() 是與之相反的斷言,并接受相同的參數。

例 A.13: assertDirectoryIsWritable() 的用法

<?php use PHPUnit\Framework\TestCase; class DirectoryIsWritableTest extends TestCase { public function testFailure() { $this->assertDirectoryIsWritable('/path/to/directory'); } } ?> phpunit DirectoryIsWritableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) DirectoryIsWritableTest::testFailure Failed asserting that "/path/to/directory" is writable./home/sb/DirectoryIsWritableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEmpty()

assertEmpty(mixed $actual[, string $message = ''])

當 $actual 非空時報告錯誤,錯誤訊息由 $message 指定。

assertNotEmpty() 是與之相反的斷言,接受相同的參數。

assertAttributeEmpty() 和 assertAttributeNotEmpty() 是便捷包裝(convenience wrapper),可以應用于某個類或對象的某個 public、protected 或 private 屬性。

例 A.14: assertEmpty() 的用法

<?php use PHPUnit\Framework\TestCase; class EmptyTest extends TestCase { public function testFailure() { $this->assertEmpty(['foo']); } } ?> phpunit EmptyTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) EmptyTest::testFailure Failed asserting that an array is empty./home/sb/EmptyTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEqualXMLStructure()

assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = false, string $message = ''])

當 $actualElement 中 DOMElement 的 XML 結構與 $expectedElement 中 DOMElement的 XML 結構不相同時報告錯誤,錯誤訊息由 $message 指定。

例 A.15: assertEqualXMLStructure() 的用法

<?php use PHPUnit\Framework\TestCase; class EqualXMLStructureTest extends TestCase { public function testFailureWithDifferentNodeNames() { $expected = new DOMElement('foo'); $actual = new DOMElement('bar'); $this->assertEqualXMLStructure($expected, $actual); } public function testFailureWithDifferentNodeAttributes() { $expected = new DOMDocument; $expected->loadXML('<foo bar="true" />'); $actual = new DOMDocument; $actual->loadXML('<foo/>'); $this->assertEqualXMLStructure( $expected->firstChild, $actual->firstChild, true ); } public function testFailureWithDifferentChildrenCount() { $expected = new DOMDocument; $expected->loadXML('<foo><bar/><bar/><bar/></foo>'); $actual = new DOMDocument; $actual->loadXML('<foo><bar/></foo>'); $this->assertEqualXMLStructure( $expected->firstChild, $actual->firstChild ); } public function testFailureWithDifferentChildren() { $expected = new DOMDocument; $expected->loadXML('<foo><bar/><bar/><bar/></foo>'); $actual = new DOMDocument; $actual->loadXML('<foo><baz/><baz/><baz/></foo>'); $this->assertEqualXMLStructure( $expected->firstChild, $actual->firstChild ); } } ?> phpunit EqualXMLStructureTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FFFFTime: 0 seconds, Memory: 5.75MbThere were 4 failures:1) EqualXMLStructureTest::testFailureWithDifferentNodeNames Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'foo' +'bar'/home/sb/EqualXMLStructureTest.php:92) EqualXMLStructureTest::testFailureWithDifferentNodeAttributes Number of attributes on node "foo" does not match Failed asserting that 0 matches expected 1./home/sb/EqualXMLStructureTest.php:223) EqualXMLStructureTest::testFailureWithDifferentChildrenCount Number of child nodes of "foo" differs Failed asserting that 1 matches expected 3./home/sb/EqualXMLStructureTest.php:354) EqualXMLStructureTest::testFailureWithDifferentChildren Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'bar' +'baz'/home/sb/EqualXMLStructureTest.php:48FAILURES! Tests: 4, Assertions: 8, Failures: 4.

assertEquals()

assertEquals(mixed $expected, mixed $actual[, string $message = ''])

當兩個變量 $expected 和 $actual 不相等時報告錯誤,錯誤訊息由 $message 指定。

assertNotEquals() 是與之相反的斷言,接受相同的參數。

assertAttributeEquals() 和 assertAttributeNotEquals() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.16: assertEquals() 的用法

<?php use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase { public function testFailure() { $this->assertEquals(1, 0); } public function testFailure2() { $this->assertEquals('bar', 'baz'); } public function testFailure3() { $this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n"); } } ?> phpunit EqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FFFTime: 0 seconds, Memory: 5.25MbThere were 3 failures:1) EqualsTest::testFailure Failed asserting that 0 matches expected 1./home/sb/EqualsTest.php:62) EqualsTest::testFailure2 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'bar' +'baz'/home/sb/EqualsTest.php:113) EqualsTest::testFailure3 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@'foo -bar +bahbaz'/home/sb/EqualsTest.php:16FAILURES! Tests: 3, Assertions: 3, Failures: 3.

如果 $expected 和 $actual 是某些特定的類型,將使用更加專門的比較方式,參閱下文。

assertEquals(float $expected, float $actual[, string $message = '', float $delta = 0])

當兩個浮點數 $expected 和 $actual 之間的差值(的絕對值)大于 $delta 時報告錯誤,錯誤訊息由 $message 指定。

關于為什么 $delta 參數是必須的,請閱讀《關于浮點運算,每一位計算機科學從業人員都應該知道的事實》。

例 A.17: 將assertEquals()用于浮點數時的用法

<?php use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase { public function testSuccess() { $this->assertEquals(1.0, 1.1, '', 0.2); } public function testFailure() { $this->assertEquals(1.0, 1.1); } } ?> phpunit EqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors..FTime: 0 seconds, Memory: 5.75MbThere was 1 failure:1) EqualsTest::testFailure Failed asserting that 1.1 matches expected 1.0./home/sb/EqualsTest.php:11FAILURES! Tests: 2, Assertions: 2, Failures: 1.

assertEquals(DOMDocument $expected, DOMDocument $actual[, string $message = ''])

當 $expected 和 $actual 這兩個 DOMDocument 對象所表示的 XML 文檔對應的無注釋規范形式不相同時報告錯誤,錯誤訊息由 $message 指定。

例 A.18: assertEquals()應用于 DOMDocument 對象時的用法

<?php use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase { public function testFailure() { $expected = new DOMDocument; $expected->loadXML('<foo><bar/></foo>'); $actual = new DOMDocument; $actual->loadXML('<bar><foo/></bar>'); $this->assertEquals($expected, $actual); } } ?> phpunit EqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) EqualsTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@<?xml version="1.0"?> -<foo> - <bar/> -</foo> +<bar> + <foo/> +</bar>/home/sb/EqualsTest.php:12FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEquals(object $expected, object $actual[, string $message = ''])

當 $expected 和 $actual 這兩個對象的屬性值不相等時報告錯誤,錯誤訊息由 $message 指定。

例 A.19: assertEquals()應用于對象時的用法

<?php use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase { public function testFailure() { $expected = new stdClass; $expected->foo = 'foo'; $expected->bar = 'bar'; $actual = new stdClass; $actual->foo = 'bar'; $actual->baz = 'bar'; $this->assertEquals($expected, $actual); } } ?> phpunit EqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) EqualsTest::testFailure Failed asserting that two objects are equal. --- Expected +++ Actual @@ @@stdClass Object ( - 'foo' => 'foo' - 'bar' => 'bar' + 'foo' => 'bar' + 'baz' => 'bar')/home/sb/EqualsTest.php:14FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEquals(array $expected, array $actual[, string $message = ''])

當 $expected 和 $actual 這兩個數組不相等時報告錯誤,錯誤訊息由 $message 指定。

例 A.20: assertEquals() 應用于數組時的用法

<?php use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase { public function testFailure() { $this->assertEquals(['a', 'b', 'c'], ['a', 'c', 'd']); } } ?> phpunit EqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) EqualsTest::testFailure Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@Array (0 => 'a' - 1 => 'b' - 2 => 'c' + 1 => 'c' + 2 => 'd')/home/sb/EqualsTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertFalse()

assertFalse(bool $condition[, string $message = ''])

當 $condition 為 true 時報告錯誤,錯誤訊息由 $message 指定。

assertNotFalse() 是與之相反的斷言,接受相同的參數。

例 A.21: assertFalse() 的用法

<?php use PHPUnit\Framework\TestCase; class FalseTest extends TestCase { public function testFailure() { $this->assertFalse(true); } } ?> phpunit FalseTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) FalseTest::testFailure Failed asserting that true is false./home/sb/FalseTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertFileEquals()

assertFileEquals(string $expected, string $actual[, string $message = ''])

當 $expected 所指定的文件與 $actual 所指定的文件內容不同時報告錯誤,錯誤訊息由 $message 指定。

assertFileNotEquals() 是與之相反的斷言,接受相同的參數。

例 A.22: assertFileEquals() 的用法

<?php use PHPUnit\Framework\TestCase; class FileEqualsTest extends TestCase { public function testFailure() { $this->assertFileEquals('/home/sb/expected', '/home/sb/actual'); } } ?> phpunit FileEqualsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) FileEqualsTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'expected +'actual'/home/sb/FileEqualsTest.php:6FAILURES! Tests: 1, Assertions: 3, Failures: 1.

assertFileExists()

assertFileExists(string $filename[, string $message = ''])

當 $filename 所指定的文件不存在時報告錯誤,錯誤訊息由 $message 指定。

assertFileNotExists() 是與之相反的斷言,接受相同的參數。

例 A.23: assertFileExists() 的用法

<?php use PHPUnit\Framework\TestCase; class FileExistsTest extends TestCase { public function testFailure() { $this->assertFileExists('/path/to/file'); } } ?> phpunit FileExistsTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) FileExistsTest::testFailure Failed asserting that file "/path/to/file" exists./home/sb/FileExistsTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertFileIsReadable()

assertFileIsReadable(string $filename[, string $message = ''])

當 $filename 所指定的文件不是個文件或不可讀時報告錯誤,錯誤訊息由 $message 指定。

assertFileNotIsReadable() 是與之相反的斷言,并接受相同的參數。

例 A.24: assertFileIsReadable() 的用法

<?php use PHPUnit\Framework\TestCase; class FileIsReadableTest extends TestCase { public function testFailure() { $this->assertFileIsReadable('/path/to/file'); } } ?> phpunit FileIsReadableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) FileIsReadableTest::testFailure Failed asserting that "/path/to/file" is readable./home/sb/FileIsReadableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertFileIsWritable()

assertFileIsWritable(string $filename[, string $message = ''])

當 $filename 所指定的文件不是個文件或不可寫時報告錯誤,錯誤訊息由 $message 指定。

assertFileNotIsWritable() 是與之相反的斷言,并接受相同的參數。

例 A.25: assertFileIsWritable() 的用法

<?php use PHPUnit\Framework\TestCase; class FileIsWritableTest extends TestCase { public function testFailure() { $this->assertFileIsWritable('/path/to/file'); } } ?> phpunit FileIsWritableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) FileIsWritableTest::testFailure Failed asserting that "/path/to/file" is writable./home/sb/FileIsWritableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertGreaterThan()

assertGreaterThan(mixed $expected, mixed $actual[, string $message = ''])

當 $actual 的值不大于 $expected 的值時報告錯誤,錯誤訊息由 $message 指定。

assertAttributeGreaterThan() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.26: assertGreaterThan() 的用法

<?php use PHPUnit\Framework\TestCase; class GreaterThanTest extends TestCase { public function testFailure() { $this->assertGreaterThan(2, 1); } } ?> phpunit GreaterThanTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) GreaterThanTest::testFailure Failed asserting that 1 is greater than 2./home/sb/GreaterThanTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertGreaterThanOrEqual()

assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = ''])

當 $actual 的值不大于且不等于 $expected 的值時報告錯誤,錯誤訊息由 $message 指定。

assertAttributeGreaterThanOrEqual() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.27: assertGreaterThanOrEqual() 的用法

<?php use PHPUnit\Framework\TestCase; class GreatThanOrEqualTest extends TestCase { public function testFailure() { $this->assertGreaterThanOrEqual(2, 1); } } ?> phpunit GreaterThanOrEqualTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) GreatThanOrEqualTest::testFailure Failed asserting that 1 is equal to 2 or is greater than 2./home/sb/GreaterThanOrEqualTest.php:6FAILURES! Tests: 1, Assertions: 2, Failures: 1.

assertInfinite()

assertInfinite(mixed $variable[, string $message = ''])

當 $actual 不是 INF 時報告錯誤,錯誤訊息由 $message 指定。

assertFinite() 是與之相反的斷言,接受相同的參數。

例 A.28: assertInfinite() 的用法

<?php use PHPUnit\Framework\TestCase; class InfiniteTest extends TestCase { public function testFailure() { $this->assertInfinite(1); } } ?> phpunit InfiniteTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) InfiniteTest::testFailure Failed asserting that 1 is infinite./home/sb/InfiniteTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertInstanceOf()

assertInstanceOf($expected, $actual[, $message = ''])

當 $actual 不是 $expected 的實例時報告錯誤,錯誤訊息由 $message 指定。

assertNotInstanceOf() 是與之相反的斷言,接受相同的參數。

assertAttributeInstanceOf() 和 assertAttributeNotInstanceOf() 是便捷包裝(convenience wrapper),可以應用于某個類或對象的某個 public、protected 或 private 屬性。

例 A.29: assertInstanceOf() 的用法

<?php use PHPUnit\Framework\TestCase; class InstanceOfTest extends TestCase { public function testFailure() { $this->assertInstanceOf(RuntimeException::class, new Exception); } } ?> phpunit InstanceOfTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) InstanceOfTest::testFailure Failed asserting that Exception Object (...) is an instance of class "RuntimeException"./home/sb/InstanceOfTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertInternalType()

assertInternalType($expected, $actual[, $message = ''])

當 $actual 不是 $expected 所指明的類型時報告錯誤,錯誤訊息由 $message 指定。

assertNotInternalType() 是與之相反的斷言,接受相同的參數。

assertAttributeInternalType() 和 assertAttributeNotInternalType() 是便捷包裝(convenience wrapper),可以應用于某個類或對象的某個 public、protected 或 private 屬性。

例 A.30: assertInternalType() 的用法

<?php use PHPUnit\Framework\TestCase; class InternalTypeTest extends TestCase { public function testFailure() { $this->assertInternalType('string', 42); } } ?> phpunit InternalTypeTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) InternalTypeTest::testFailure Failed asserting that 42 is of type "string"./home/sb/InternalTypeTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertIsReadable()

assertIsReadable(string $filename[, string $message = ''])

當 $filename 所指定的文件或目錄不可讀時報告錯誤,錯誤訊息由 $message 指定。

assertNotIsReadable() 是與之相反的斷言,并接受相同的參數。

例 A.31: assertIsReadable() 的用法

<?php use PHPUnit\Framework\TestCase; class IsReadableTest extends TestCase { public function testFailure() { $this->assertIsReadable('/path/to/unreadable'); } } ?> phpunit IsReadableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) IsReadableTest::testFailure Failed asserting that "/path/to/unreadable" is readable./home/sb/IsReadableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertIsWritable()

assertIsWritable(string $filename[, string $message = ''])

當 $filename 所指定的文件或目錄不可寫時報告錯誤,錯誤訊息由 $message 指定。

assertNotIsWritable() 是與之相反的斷言,并接受相同的參數。

例 A.32: assertIsWritable() 的用法

<?php use PHPUnit\Framework\TestCase; class IsWritableTest extends TestCase { public function testFailure() { $this->assertIsWritable('/path/to/unwritable'); } } ?> phpunit IsWritableTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) IsWritableTest::testFailure Failed asserting that "/path/to/unwritable" is writable./home/sb/IsWritableTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertJsonFileEqualsJsonFile()

assertJsonFileEqualsJsonFile(mixed $expectedFile, mixed $actualFile[, string $message = ''])

當 $actualFile 對應的值與 $expectedFile 對應的值不匹配時報告錯誤,錯誤訊息由 $message 指定。

例 A.33: assertJsonFileEqualsJsonFile() 的用法

<?php use PHPUnit\Framework\TestCase; class JsonFileEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonFileEqualsJsonFile( 'path/to/fixture/file', 'path/to/actual/file'); } } ?> phpunit JsonFileEqualsJsonFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) JsonFileEqualsJsonFile::testFailure Failed asserting that '{"Mascot":"Tux"}' matches JSON string "["Mascott", "Tux", "OS", "Linux"]"./home/sb/JsonFileEqualsJsonFileTest.php:5FAILURES! Tests: 1, Assertions: 3, Failures: 1.

assertJsonStringEqualsJsonFile()

assertJsonStringEqualsJsonFile(mixed $expectedFile, mixed $actualJson[, string $message = ''])

當 $actualJson 對應的值與 $expectedFile 對應的值不匹配時報告錯誤,錯誤訊息由 $message 指定。

例 A.34: assertJsonStringEqualsJsonFile() 的用法

<?php use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonStringEqualsJsonFile( 'path/to/fixture/file', json_encode(['Mascot' => 'ux']) ); } } ?> phpunit JsonStringEqualsJsonFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) JsonStringEqualsJsonFile::testFailure Failed asserting that '{"Mascot":"ux"}' matches JSON string "{"Mascott":"Tux"}"./home/sb/JsonStringEqualsJsonFileTest.php:5FAILURES! Tests: 1, Assertions: 3, Failures: 1.

assertJsonStringEqualsJsonString()

assertJsonStringEqualsJsonString(mixed $expectedJson, mixed $actualJson[, string $message = ''])

當 $actualJson 對應的值與 $expectedJson 對應的值不匹配時報告錯誤,錯誤訊息由 $message 指定。

例 A.35: assertJsonStringEqualsJsonString() 的用法

<?php use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonStringTest extends TestCase { public function testFailure() { $this->assertJsonStringEqualsJsonString( json_encode(['Mascot' => 'Tux']), json_encode(['Mascot' => 'ux']) ); } } ?> phpunit JsonStringEqualsJsonStringTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) JsonStringEqualsJsonStringTest::testFailure Failed asserting that two objects are equal. --- Expected +++ Actual @@ @@stdClass Object (- 'Mascot' => 'Tux'+ 'Mascot' => 'ux' )/home/sb/JsonStringEqualsJsonStringTest.php:5FAILURES! Tests: 1, Assertions: 3, Failures: 1.

assertLessThan()

assertLessThan(mixed $expected, mixed $actual[, string $message = ''])

當 $actual 的值不小于 $expected 的值時報告錯誤,錯誤訊息由 $message 指定。

assertAttributeLessThan() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.36: assertLessThan() 的用法

<?php use PHPUnit\Framework\TestCase; class LessThanTest extends TestCase { public function testFailure() { $this->assertLessThan(1, 2); } } ?> phpunit LessThanTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) LessThanTest::testFailure Failed asserting that 2 is less than 1./home/sb/LessThanTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertLessThanOrEqual()

assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = ''])

當 $actual 的值不小于且不等于 $expected 的值時報告錯誤,錯誤訊息由 $message 指定。

assertAttributeLessThanOrEqual() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.37: assertLessThanOrEqual() 的用法

<?php use PHPUnit\Framework\TestCase; class LessThanOrEqualTest extends TestCase { public function testFailure() { $this->assertLessThanOrEqual(1, 2); } } ?> phpunit LessThanOrEqualTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) LessThanOrEqualTest::testFailure Failed asserting that 2 is equal to 1 or is less than 1./home/sb/LessThanOrEqualTest.php:6FAILURES! Tests: 1, Assertions: 2, Failures: 1.

assertNan()

assertNan(mixed $variable[, string $message = ''])

當 $variable 不是 NAN 時報告錯誤,錯誤訊息由 $message 指定。

例 A.38: assertNan() 的用法

<?php use PHPUnit\Framework\TestCase; class NanTest extends TestCase { public function testFailure() { $this->assertNan(1); } } ?> phpunit NanTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) NanTest::testFailure Failed asserting that 1 is nan./home/sb/NanTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertNull()

assertNull(mixed $variable[, string $message = ''])

當 $actual 不是 null 時報告錯誤,錯誤訊息由 $message 指定。

assertNotNull() 是與之相反的斷言,接受相同的參數。

例 A.39: assertNull() 的使用

<?php use PHPUnit\Framework\TestCase; class NullTest extends TestCase { public function testFailure() { $this->assertNull('foo'); } } ?> phpunit NotNullTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) NullTest::testFailure Failed asserting that 'foo' is null./home/sb/NotNullTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertObjectHasAttribute()

assertObjectHasAttribute(string $attributeName, object $object[, string $message = ''])

當 $object->attributeName 不存在時報告錯誤,錯誤訊息由 $message 指定。

assertObjectNotHasAttribute() 是與之相反的斷言,接受相同的參數。

例 A.40: assertObjectHasAttribute() 的用法

<?php use PHPUnit\Framework\TestCase; class ObjectHasAttributeTest extends TestCase { public function testFailure() { $this->assertObjectHasAttribute('foo', new stdClass); } } ?> phpunit ObjectHasAttributeTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) ObjectHasAttributeTest::testFailure Failed asserting that object of class "stdClass" has attribute "foo"./home/sb/ObjectHasAttributeTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertRegExp()

assertRegExp(string $pattern, string $string[, string $message = ''])

當 $string 不匹配于正則表達式 $pattern 時報告錯誤,錯誤訊息由 $message 指定。

assertNotRegExp() 是與之相反的斷言,接受相同的參數。

例 A.41: assertRegExp() 的用法

<?php use PHPUnit\Framework\TestCase; class RegExpTest extends TestCase { public function testFailure() { $this->assertRegExp('/foo/', 'bar'); } } ?> phpunit RegExpTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) RegExpTest::testFailure Failed asserting that 'bar' matches PCRE pattern "/foo/"./home/sb/RegExpTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertStringMatchesFormat()

assertStringMatchesFormat(string $format, string $string[, string $message = ''])

當 $string 不匹配于 $format 定義的格式時報告錯誤,錯誤訊息由 $message 指定。

assertStringNotMatchesFormat() 是與之相反的斷言,接受相同的參數。

例 A.42: assertStringMatchesFormat() 的用法

<?php use PHPUnit\Framework\TestCase; class StringMatchesFormatTest extends TestCase { public function testFailure() { $this->assertStringMatchesFormat('%i', 'foo'); } } ?> phpunit StringMatchesFormatTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) StringMatchesFormatTest::testFailure Failed asserting that 'foo' matches PCRE pattern "/^[+-]?\d+$/s"./home/sb/StringMatchesFormatTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

格式定義字符串中可以使用如下占位符:

  • %e:表示目錄分隔符,例如在 Linux 系統中是 /。

  • %s:一個或多個除了換行符以外的任意字符(非空白字符或者空白字符)。

  • %S:零個或多個除了換行符以外的任意字符(非空白字符或者空白字符)。

  • %a:一個或多個包括換行符在內的任意字符(非空白字符或者空白字符)。

  • %A:零個或多個包括換行符在內的任意字符(非空白字符或者空白字符)。

  • %w:零個或多個空白字符。

  • %i:帶符號整數值,例如 +3142、-3142。

  • %d:無符號整數值,例如 123456。

  • %x:一個或多個十六進制字符。所謂十六進制字符,指的是在以下范圍內的字符:0-9、a-f、A-F。

  • %f:浮點數,例如 3.142、-3.142、3.142E-10、3.142e+10。

  • %c:單個任意字符。

assertStringMatchesFormatFile()

assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = ''])

當 $string 不匹配于 $formatFile 的內容所定義的格式時報告錯誤,錯誤訊息由 $message 指定。

assertStringNotMatchesFormatFile() 是與之相反的斷言,接受相同的參數。

例 A.43: assertStringMatchesFormatFile() 的用法

<?php use PHPUnit\Framework\TestCase; class StringMatchesFormatFileTest extends TestCase { public function testFailure() { $this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo'); } } ?> phpunit StringMatchesFormatFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) StringMatchesFormatFileTest::testFailure Failed asserting that 'foo' matches PCRE pattern "/^[+-]?\d+ $/s"./home/sb/StringMatchesFormatFileTest.php:6FAILURES! Tests: 1, Assertions: 2, Failures: 1.

assertSame()

assertSame(mixed $expected, mixed $actual[, string $message = ''])

當兩個變量 $expected 和 $actual 的值與類型不完全相同時報告錯誤,錯誤訊息由 $message 指定。

assertNotSame() 是與之相反的斷言,接受相同的參數。

assertAttributeSame() 和 assertAttributeNotSame() 是便捷包裝(convenience wrapper),以某個類或對象的某個 public、protected 或 private 屬性作為實際值來進行比較。

例 A.44: assertSame() 的用法

<?php use PHPUnit\Framework\TestCase; class SameTest extends TestCase { public function testFailure() { $this->assertSame('2204', 2204); } } ?> phpunit SameTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) SameTest::testFailure Failed asserting that 2204 is identical to '2204'./home/sb/SameTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertSame(object $expected, object $actual[, string $message = ''])

當兩個變量 $expected 和 $actual 不是指向同一個對象的引用時報告錯誤,錯誤訊息由 $message 指定。

例 A.45: assertSame() 應用于對象時的用法

<?php use PHPUnit\Framework\TestCase; class SameTest extends TestCase { public function testFailure() { $this->assertSame(new stdClass, new stdClass); } } ?> phpunit SameTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 4.75MbThere was 1 failure:1) SameTest::testFailure Failed asserting that two variables reference the same object./home/sb/SameTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertStringEndsWith()

assertStringEndsWith(string $suffix, string $string[, string $message = ''])

當 $string 不以 $suffix 結尾時報告錯誤,錯誤訊息由 $message 指定。

assertStringEndsNotWith() 是與之相反的斷言,接受相同的參數。

例 A.46: assertStringEndsWith() 的用法

<?php use PHPUnit\Framework\TestCase; class StringEndsWithTest extends TestCase { public function testFailure() { $this->assertStringEndsWith('suffix', 'foo'); } } ?> phpunit StringEndsWithTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 1 second, Memory: 5.00MbThere was 1 failure:1) StringEndsWithTest::testFailure Failed asserting that 'foo' ends with "suffix"./home/sb/StringEndsWithTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertStringEqualsFile()

assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = ''])

當 $expectedFile 所指定的文件其內容不是 $actualString 時報告錯誤,錯誤訊息由 $message 指定。

assertStringNotEqualsFile() 是與之相反的斷言,接受相同的參數。

例 A.47: assertStringEqualsFile() 的用法

<?php use PHPUnit\Framework\TestCase; class StringEqualsFileTest extends TestCase { public function testFailure() { $this->assertStringEqualsFile('/home/sb/expected', 'actual'); } } ?> phpunit StringEqualsFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) StringEqualsFileTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'expected -' +'actual'/home/sb/StringEqualsFileTest.php:6FAILURES! Tests: 1, Assertions: 2, Failures: 1.

assertStringStartsWith()

assertStringStartsWith(string $prefix, string $string[, string $message = ''])

當 $string 不以 $prefix 開頭時報告錯誤,錯誤訊息由 $message 指定。

assertStringStartsNotWith() 是與之相反的斷言,并接受相同的參數。

例 A.48: assertStringStartsWith() 的用法

<?php use PHPUnit\Framework\TestCase; class StringStartsWithTest extends TestCase { public function testFailure() { $this->assertStringStartsWith('prefix', 'foo'); } } ?> phpunit StringStartsWithTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) StringStartsWithTest::testFailure Failed asserting that 'foo' starts with "prefix"./home/sb/StringStartsWithTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertThat()

可以用 PHPUnit_Framework_Constraint 類來訂立更加復雜的斷言。隨后可以用 assertThat() 方法來評定這些斷言。例 A.49 展示了如何用 logicalNot() 和 equalTo() 約束條件來表達與 assertNotEquals() 等價的斷言。

assertThat(mixed $value, PHPUnit_Framework_Constraint $constraint[, $message = ''])

當 $value 不符合約束條件 $constraint 時報告錯誤,錯誤訊息由 $message 指定。

例 A.49: assertThat() 的用法

<?php use PHPUnit\Framework\TestCase; class BiscuitTest extends TestCase { public function testEquals() { $theBiscuit = new Biscuit('Ginger'); $myBiscuit = new Biscuit('Ginger'); $this->assertThat( $theBiscuit, $this->logicalNot( $this->equalTo($myBiscuit) ) ); } } ?>

表 A.1列舉了所有可用的 PHPUnit_Framework_Constraint 類。

表?A.1.?約束條件

約束條件含義
PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)此約束將另外一個約束應用于某個類或對象的某個屬性。
PHPUnit_Framework_Constraint_IsAnything anything()此約束接受任意輸入值。
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key)此約束斷言所評定的數組擁有指定鍵名。
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value)此約束斷言所評定的 array 或實現了 Iterator 接口的對象包含有給定值。
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnly(string $type)此約束斷言所評定的 array 或實現了 Iterator 接口的對象僅包含給定類型的值。
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnlyInstancesOf(string $classname)此約束斷言所評定的 array 或實現了 Iterator 接口的對象僅包含給定類名的類的實例。
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10)此約束檢驗一個值是否等于另外一個。
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)此約束檢驗一個值是否等于某個類或對象的某個屬性。
PHPUnit_Framework_Constraint_DirectoryExists directoryExists()此約束檢驗所評定的目錄是否存在。
PHPUnit_Framework_Constraint_FileExists fileExists()此約束檢驗所評定的文件名對應的文件是否存在。
PHPUnit_Framework_Constraint_IsReadable isReadable()此約束檢驗所評定的文件名對應的文件是否可讀。
PHPUnit_Framework_Constraint_IsWritable isWritable()此約束檢驗所評定的文件名對應的文件是否可寫。
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value)此約束斷言所評定的值大于給定值。
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value)此約束斷言所評定的值大于或等于給定值。
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName)此約束斷言所評定的類具有給定屬性。
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)此約束斷言所評定的類具有給定靜態屬性。
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName)此約束斷言所評定的對象具有給定屬性。
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value)此約束斷言所評定的值與另外一個值全等。
PHPUnit_Framework_Constraint_IsFalse isFalse()此約束斷言所評定的值為 false。
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className)此約束斷言所評定的對象是給定類的實例。
PHPUnit_Framework_Constraint_IsNull isNull()此約束斷言所評定的值為 null。
PHPUnit_Framework_Constraint_IsTrue isTrue()此約束斷言所評定的值為 true。
PHPUnit_Framework_Constraint_IsType isType(string $type)此約束斷言所評定的值是指定類型的。
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value)此約束斷言所評定的值小于給定值。
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value)此約束斷言所評定的值小于或等于給定值。
logicalAnd()邏輯與(AND)。
logicalNot(PHPUnit_Framework_Constraint $constraint)邏輯非(NOT)。
logicalOr()邏輯或(OR)。
logicalXor()邏輯異或(XOR)。
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern)此約束斷言所評定的字符串匹配于正則表達式。
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case)此約束斷言所評定的字符串包含指定字符串。
PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix)此約束斷言所評定的字符串以給定后綴結尾。
PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix)此約束斷言所評定的字符串以給定前綴開頭。


assertTrue()

assertTrue(bool $condition[, string $message = ''])

當 $condition 為 false 時報告錯誤,錯誤訊息由 $message 指定。

assertNotTrue() 是與之相反的斷言,接受相同的參數。

例 A.50: assertTrue() 的用法

<?php use PHPUnit\Framework\TestCase; class TrueTest extends TestCase { public function testFailure() { $this->assertTrue(false); } } ?> phpunit TrueTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) TrueTest::testFailure Failed asserting that false is true./home/sb/TrueTest.php:6FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertXmlFileEqualsXmlFile()

assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile[, string $message = ''])

當 $actualFile 對應的 XML 文檔與 $expectedFile 對應的 XML 文檔不相同時報告錯誤,錯誤訊息由 $message 指定。

assertXmlFileNotEqualsXmlFile() 是與之相反的斷言,接受相同的參數。

例 A.51: assertXmlFileEqualsXmlFile() 的用法

<?php use PHPUnit\Framework\TestCase; class XmlFileEqualsXmlFileTest extends TestCase { public function testFailure() { $this->assertXmlFileEqualsXmlFile( '/home/sb/expected.xml', '/home/sb/actual.xml'); } } ?> phpunit XmlFileEqualsXmlFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) XmlFileEqualsXmlFileTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@<?xml version="1.0"?><foo> - <bar/> + <baz/></foo>/home/sb/XmlFileEqualsXmlFileTest.php:7FAILURES! Tests: 1, Assertions: 3, Failures: 1.

assertXmlStringEqualsXmlFile()

assertXmlStringEqualsXmlFile(string $expectedFile, string $actualXml[, string $message = ''])

當 $actualXml 對應的 XML 文檔與 $expectedFile 對應的 XML 文檔不相同時報告錯誤,錯誤訊息由 $message 指定。

assertXmlStringNotEqualsXmlFile() 是與之相反的斷言,并接受相同的參數。

例 A.52: assertXmlStringEqualsXmlFile() 的用法

<?php use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlFileTest extends TestCase { public function testFailure() { $this->assertXmlStringEqualsXmlFile( '/home/sb/expected.xml', '<foo><baz/></foo>'); } } ?> phpunit XmlStringEqualsXmlFileTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.25MbThere was 1 failure:1) XmlStringEqualsXmlFileTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@<?xml version="1.0"?><foo> - <bar/> + <baz/></foo>/home/sb/XmlStringEqualsXmlFileTest.php:7FAILURES! Tests: 1, Assertions: 2, Failures: 1.

assertXmlStringEqualsXmlString()

assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml[, string $message = ''])

當 $actualXml 對應的 XML 文檔與 $expectedXml 對應的 XML 文檔不相同時報告錯誤,錯誤訊息由 $message 指定。

assertXmlStringNotEqualsXmlString() 是與之相反的斷言,接受相同的參數。

例 A.53: assertXmlStringEqualsXmlString() 的用法

<?php use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlStringTest extends TestCase { public function testFailure() { $this->assertXmlStringEqualsXmlString( '<foo><bar/></foo>', '<foo><baz/></foo>'); } } ?> phpunit XmlStringEqualsXmlStringTest PHPUnit 5.7.0 by Sebastian Bergmann and contributors.FTime: 0 seconds, Memory: 5.00MbThere was 1 failure:1) XmlStringEqualsXmlStringTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@<?xml version="1.0"?><foo> - <bar/> + <baz/></foo>/home/sb/XmlStringEqualsXmlStringTest.php:7FAILURES! Tests: 1, Assertions: 1, Failures: 1.

?

轉載于:https://www.cnblogs.com/rxbook/p/6767121.html

總結

以上是生活随笔為你收集整理的PHPUnit-附录 A. 断言 (assert)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。