附录 A. 断言

本附录列举可用的各种断言方法。

assertArrayHasKey()

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

$array 不包含 $key 时,报告一个错误,错误讯息的内容由 $message 指定。

assertArrayNotHasKey() 是与之相反的断言,并接受相同的参数。

例 A.1: assertArrayHasKey() 的用法

<?php
class ArrayHasKeyTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertArrayHasKey('foo', array('bar' => 'baz'));
    }
}
?>
phpunit ArrayHasKeyTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) ArrayHasKeyTest::testFailure
Failed asserting that an array has the key 'foo'.

/home/sb/ArrayHasKeyTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertClassHasAttribute()

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

$className::attributeName 不存在时,报告一个错误,错误讯息的内容由 $message 指定。

assertClassNotHasAttribute() 是与之相反的断言,并接受相同的参数。

例 A.2: assertClassHasAttribute() 的用法

<?php
class ClassHasAttributeTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertClassHasAttribute('foo', 'stdClass');
    }
}
?>
phpunit ClassHasAttributeTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) ClassHasAttributeTest::testFailure
Failed asserting that class "stdClass" has attribute "foo".

/home/sb/ClassHasAttributeTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertClassHasStaticAttribute()

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

$className::attributeName 不存在时,报告一个错误,错误讯息的内容由 $message 指定。

assertClassNotHasStaticAttribute() 是与之相反的断言,并接受相同的参数。

例 A.3: assertClassHasStaticAttribute() 的用法

<?php
class ClassHasStaticAttributeTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertClassHasStaticAttribute('foo', 'stdClass');
    }
}
?>
phpunit ClassHasStaticAttributeTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) ClassHasStaticAttributeTest::testFailure
Failed asserting that class "stdClass" has static attribute "foo".

/home/sb/ClassHasStaticAttributeTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertContains()

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

$needle 不是 $haystack 的某个元素时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotContains() 是与之相反的断言,并接受相同的参数。

assertAttributeContains()assertAttributeNotContains() 是两个便捷包装(convenience wrappers),以某个类或对象的 publicprotectedprivate 属性为搜索范围。

例 A.4: assertContains() 的用法

<?php
class ContainsTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertContains(4, array(1, 2, 3));
    }
}
?>
phpunit ContainsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) ContainsTest::testFailure
Failed asserting that an array contains 4.

/home/sb/ContainsTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


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

$needle 不是 $haystack 的子字符串时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.5: assertContains() 的用法

<?php
class ContainsTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertContains('baz', 'foobar');
    }
}
?>
phpunit ContainsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) ContainsTest::testFailure
Failed asserting that 'foobar' contains "baz".

/home/sb/ContainsTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertContainsOnly()

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

$haystack 并非仅包含类型为 $type 的变量时,报告一个错误,错误讯息的内容由 $message 指定。

$isNativeType 是一个标志,用来表明 $type 是否是原生 PHP 类型。

assertNotContainsOnly() 是与之相反的断言,并接受相同的参数。

assertAttributeContainsOnly()assertAttributeNotContainsOnly() 是两个便捷包装(convenience wrappers),以某个类或对象的 publicprotectedprivate 属性为搜索范围。

例 A.6: assertContainsOnly() 的用法

<?php
class ContainsOnlyTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertContainsOnly('string', array('1', '2', 3));
    }
}
?>
phpunit ContainsOnlyTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There 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:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertContainsOnlyInstancesOf()

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

$haystack 并非仅包含类 $classname 的实例时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.7: assertContainsOnlyInstancesOf() 的用法

<?php
class ContainsOnlyInstancesOfTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertContainsOnlyInstancesOf('Foo', array(new Foo(), new Bar(), new Foo()));
    }
}
?>
phpunit ContainsOnlyInstancesOfTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) ContainsOnlyInstancesOfTest::testFailure
Failed asserting that Array ([0]=> Bar Object(...)) is an instance of class "Foo".

/home/sb/ContainsOnlyInstancesOfTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertCount()

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

$haystack 中的元素数量不是 $expectedCount 时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotCount() 是与之相反的断言,并接受相同的参数。

例 A.8: assertCount() 的用法

<?php
class CountTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertCount(0, array('foo'));
    }
}
?>
phpunit CountTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) CountTest::testFailure
Failed asserting that actual size 1 matches expected size 0.

/home/sb/CountTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertEmpty()

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

$actual 不是空的时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotEmpty() 是与之相反的断言,并接受相同的参数。

assertAttributeEmpty()assertAttributeNotEmpty() 是便捷包装(convenience wrappers), 可以应用于某个类或对象的某个 publicprotectedprivate 属性。

例 A.9: assertEmpty() 的用法

<?php
class EmptyTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertEmpty(array('foo'));
    }
}
?>
phpunit EmptyTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) EmptyTest::testFailure
Failed asserting that an array is empty.

/home/sb/EmptyTest.php:6

FAILURES!
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.10: assertEqualXMLStructure() 的用法

<?php
class EqualXMLStructureTest extends PHPUnit_Framework_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 4.2.0 by Sebastian Bergmann.

FFFF

Time: 0 seconds, Memory: 5.75Mb

There were 4 failures:

1) EqualXMLStructureTest::testFailureWithDifferentNodeNames
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'foo'
+'bar'

/home/sb/EqualXMLStructureTest.php:9

2) EqualXMLStructureTest::testFailureWithDifferentNodeAttributes
Number of attributes on node "foo" does not match
Failed asserting that 0 matches expected 1.

/home/sb/EqualXMLStructureTest.php:22

3) EqualXMLStructureTest::testFailureWithDifferentChildrenCount
Number of child nodes of "foo" differs
Failed asserting that 1 matches expected 3.

/home/sb/EqualXMLStructureTest.php:35

4) EqualXMLStructureTest::testFailureWithDifferentChildren
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'bar'
+'baz'

/home/sb/EqualXMLStructureTest.php:48

FAILURES!
Tests: 4, Assertions: 8, Failures: 4.


assertEquals()

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

当两个变量 $expected$actual 不相等时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotEquals() 是与之相反的断言,并接受相同的参数。

assertAttributeEquals()assertAttributeNotEquals() 是便捷包装(convenience wrappers), 以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.11: assertEquals() 的用法

<?php
class EqualsTest extends PHPUnit_Framework_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 4.2.0 by Sebastian Bergmann.

FFF

Time: 0 seconds, Memory: 5.25Mb

There were 3 failures:

1) EqualsTest::testFailure
Failed asserting that 0 matches expected 1.

/home/sb/EqualsTest.php:6

2) EqualsTest::testFailure2
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'bar'
+'baz'

/home/sb/EqualsTest.php:11

3) EqualsTest::testFailure3
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
 'foo
-bar
+bah
 baz
 '

/home/sb/EqualsTest.php:16

FAILURES!
Tests: 3, Assertions: 3, Failures: 3.


如果 $expected$actual 是某些特定的类型,将使用更加专门的比较方式,请参阅下文。

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

当两个浮点数 $expected$actual 之间的差值(的绝对值)大于 $delta 时,报告一个错误,错误讯息的内容由 $message 指定。

关于为什么 $delta 参数是必须的,请阅读《关于浮点运算,每一位计算机科学家都应该知道的事实

例 A.12: 将assertEquals()应用于浮点数时的用法

<?php
class EqualsTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess()
    {
        $this->assertEquals(1.0, 1.1, '', 0.2);
    }

    public function testFailure()
    {
        $this->assertEquals(1.0, 1.1);
    }
}
?>
phpunit EqualsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 5.75Mb

There was 1 failure:

1) EqualsTest::testFailure
Failed asserting that 1.1 matches expected 1.0.

/home/sb/EqualsTest.php:11

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.


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

$expected$actual 这两个 DOMDocument 对象所表示的 XML 文档的无注释规范形式不相同时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.13: assertEquals()应用于 DOMDocument 对象时的用法

<?php
class EqualsTest extends PHPUnit_Framework_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 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There 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:12

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


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

$expected$actual 这两个对象的属性值不相等时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.14: assertEquals()应用于对象时的用法

<?php
class EqualsTest extends PHPUnit_Framework_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 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There 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:14

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


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

$expected$actual 这两个数组不相等时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.15: assertEquals() 应用于数组时的用法

<?php
class EqualsTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertEquals(array('a', 'b', 'c'), array('a', 'c', 'd'));
    }
}
?>
phpunit EqualsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There 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:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertFalse()

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

$conditionTRUE 时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotFalse() 是此断言的逆断言,接受相同的参数。

例 A.16: assertFalse() 的用法

<?php
class FalseTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertFalse(TRUE);
    }
}
?>
phpunit FalseTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) FalseTest::testFailure
Failed asserting that true is false.

/home/sb/FalseTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertFileEquals()

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

$expected 所指定的文件与 $actual 所指定的文件其内容不同时,报告一个错误,错误讯息的内容由 $message 指定。

assertFileNotEquals() 是与之相反的断言,并接受相同的参数。

例 A.17: assertFileEquals() 的用法

<?php
class FileEqualsTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertFileEquals('/home/sb/expected', '/home/sb/actual');
    }
}
?>
phpunit FileEqualsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) FileEqualsTest::testFailure
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'expected
+'actual
 '

/home/sb/FileEqualsTest.php:6

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.


assertFileExists()

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

$filename 所指定的文件不存在时,报告一个错误,错误讯息的内容由 $message 指定。

assertFileNotExists() 是与之相反的断言,并接受相同的参数。

例 A.18: assertFileExists() 的用法

<?php
class FileExistsTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertFileExists('/path/to/file');
    }
}
?>
phpunit FileExistsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) FileExistsTest::testFailure
Failed asserting that file "/path/to/file" exists.

/home/sb/FileExistsTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertGreaterThan()

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

$actual 的值不大于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。

assertAttributeGreaterThan() 是便捷包装(convenience wrappers),以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.19: assertGreaterThan() 的用法

<?php
class GreaterThanTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertGreaterThan(2, 1);
    }
}
?>
phpunit GreaterThanTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) GreaterThanTest::testFailure
Failed asserting that 1 is greater than 2.

/home/sb/GreaterThanTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertGreaterThanOrEqual()

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

$actual 的值不大于且不等于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。

assertAttributeGreaterThanOrEqual() 是便捷包装(convenience wrappers),以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.20: assertGreaterThanOrEqual() 的用法

<?php
class GreatThanOrEqualTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertGreaterThanOrEqual(2, 1);
    }
}
?>
phpunit GreaterThanOrEqualTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) GreatThanOrEqualTest::testFailure
Failed asserting that 1 is equal to 2 or is greater than 2.

/home/sb/GreaterThanOrEqualTest.php:6

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.


assertInstanceOf()

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

$actual 不是 $expected的实例时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotInstanceOf() 是与之相反的断言,并接受相同的参数。

assertAttributeInstanceOf() and assertAttributeNotInstanceOf() 是便捷包装(convenience wrappers), 可以应用于某个类或对象的某个 publicprotectedprivate 属性。

例 A.21: assertInstanceOf() 的用法

<?php
class InstanceOfTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertInstanceOf('RuntimeException', new Exception);
    }
}
?>
phpunit InstanceOfTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) InstanceOfTest::testFailure
Failed asserting that Exception Object (...) is an instance of class "RuntimeException".

/home/sb/InstanceOfTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertInternalType()

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

$actual 不是 $expected 所指明的类型时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotInternalType() 是与之相反的断言,并接受相同的参数。

assertAttributeInternalType() and assertAttributeNotInternalType() 是便捷包装(convenience wrappers), 可以应用于某个类或对象的某个 publicprotectedprivate 属性.

例 A.22: assertInternalType() 的用法

<?php
class InternalTypeTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertInternalType('string', 42);
    }
}
?>
phpunit InternalTypeTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) InternalTypeTest::testFailure
Failed asserting that 42 is of type "string".

/home/sb/InternalTypeTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertJsonFileEqualsJsonFile()

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

$actualFile 的值与 $expectedFile 的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.23: assertJsonFileEqualsJsonFile() 的用法

<?php
class JsonFileEqualsJsonFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertJsonFileEqualsJsonFile(
          'path/to/fixture/file', 'path/to/actual/file');
    }
}
?>
phpunit JsonFileEqualsJsonFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) JsonFileEqualsJsonFile::testFailure
Failed asserting that '{"Mascott":"Tux"}' matches JSON string "["Mascott", "Tux", "OS", "Linux"]".

/home/sb/JsonFileEqualsJsonFileTest.php:5

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.


assertJsonStringEqualsJsonFile()

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

$actualJson 的值与 $expectedFile的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.24: assertJsonStringEqualsJsonFile() 的用法

<?php
class JsonStringEqualsJsonFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertJsonStringEqualsJsonFile(
          'path/to/fixture/file', json_encode(array("Mascott" => "ux"))
        );
    }
}
?>
phpunit JsonStringEqualsJsonFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) JsonStringEqualsJsonFile::testFailure
Failed asserting that '{"Mascott":"ux"}' matches JSON string "{"Mascott":"Tux"}".

/home/sb/JsonStringEqualsJsonFileTest.php:5

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.


assertJsonStringEqualsJsonString()

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

$actualJson 的值与 $expectedJson 的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.25: assertJsonStringEqualsJsonString() 的用法

<?php
class JsonStringEqualsJsonStringTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertJsonStringEqualsJsonString(
          json_encode(array("Mascott" => "Tux")), json_encode(array("Mascott" => "ux"))
        );
    }
}
?>
phpunit JsonStringEqualsJsonStringTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) JsonStringEqualsJsonStringTest::testFailure
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 stdClass Object (
 -    'Mascott' => 'Tux'
 +    'Mascott' => 'ux'
)

/home/sb/JsonStringEqualsJsonStringTest.php:5

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.


assertLessThan()

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

$actual 的值不小于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。

assertAttributeLessThan() 是便捷包装(convenience wrappers),以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.26: assertLessThan() 的用法

<?php
class LessThanTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertLessThan(1, 2);
    }
}
?>
phpunit LessThanTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) LessThanTest::testFailure
Failed asserting that 2 is less than 1.

/home/sb/LessThanTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertLessThanOrEqual()

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

$actual 的值不小于且不等于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。

assertAttributeLessThanOrEqual() 是便捷包装(convenience wrappers),以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.27: assertLessThanOrEqual() 的用法

<?php
class LessThanOrEqualTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertLessThanOrEqual(1, 2);
    }
}
?>
phpunit LessThanOrEqualTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) LessThanOrEqualTest::testFailure
Failed asserting that 2 is equal to 1 or is less than 1.

/home/sb/LessThanOrEqualTest.php:6

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.


assertNull()

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

$variable 不是 NULL 时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotNull() 是与之相反的断言,并接受相同的参数。

例 A.28: assertNull() 的用法

<?php
class NullTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertNull('foo');
    }
}
?>
phpunit NotNullTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) NullTest::testFailure
Failed asserting that 'foo' is null.

/home/sb/NotNullTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertObjectHasAttribute()

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

$object->attributeName 不存在时,报告一个错误,错误讯息的内容由 $message 指定。

assertObjectNotHasAttribute() 是与之相反的断言,并接受相同的参数。

例 A.29: assertObjectHasAttribute() 的用法

<?php
class ObjectHasAttributeTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertObjectHasAttribute('foo', new stdClass);
    }
}
?>
phpunit ObjectHasAttributeTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) ObjectHasAttributeTest::testFailure
Failed asserting that object of class "stdClass" has attribute "foo".

/home/sb/ObjectHasAttributeTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertRegExp()

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

$string 与正则表达式 $pattern 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotRegExp() 是与之相反的断言,并接受相同的参数。

例 A.30: assertRegExp() 的用法

<?php
class RegExpTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertRegExp('/foo/', 'bar');
    }
}
?>
phpunit RegExpTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) RegExpTest::testFailure
Failed asserting that 'bar' matches PCRE pattern "/foo/".

/home/sb/RegExpTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertStringMatchesFormat()

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

$string 与格式串 $format 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

assertStringNotMatchesFormat() 是与之相反的断言,并接受相同的参数。

例 A.31: assertStringMatchesFormat() 的用法

<?php
class StringMatchesFormatTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertStringMatchesFormat('%i', 'foo');
    }
}
?>
phpunit StringMatchesFormatTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) StringMatchesFormatTest::testFailure
Failed asserting that 'foo' matches PCRE pattern "/^[+-]?\d+$/s".

/home/sb/StringMatchesFormatTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


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

  • %e:表示目录分隔符,以 Linux 系统为例,是 /

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

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

  • %a:一个或多个包括换行符在内的任意字符(非空白字符或者空白字符)。

  • %A:零个或多个包括换行符在内的任意字符(非空白字符或者空白字符)。

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

  • %i:带符号整数值,例如 +3142-3142

  • %d:无符号整数值,例如 123456

  • %x:一个或多个十六进制字符。所谓十六进制字符,指的是在以下范围内的字符:0-9a-fA-F

  • %f:浮点数,例如 3.142-3.1423.142E-103.142e+10

  • %c:单个任意字符。

assertStringMatchesFormatFile()

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

$string$formatFile 的内容不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

assertStringNotMatchesFormatFile() 是与之相反的断言,并接受相同的参数。

例 A.32: assertStringMatchesFormatFile() 的用法

<?php
class StringMatchesFormatFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo');
    }
}
?>
phpunit StringMatchesFormatFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) StringMatchesFormatFileTest::testFailure
Failed asserting that 'foo' matches PCRE pattern "/^[+-]?\d+
$/s".

/home/sb/StringMatchesFormatFileTest.php:6

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.


assertSame()

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

当两个变量 $expected$actual 的类型与值不完全相同时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotSame() 是与之相反的断言,并接受相同的参数。

assertAttributeSame() and assertAttributeNotSame() 是便捷包装(convenience wrappers), 以某个类或对象的某个 publicprotectedprivate 属性作为实际值来进行比较。

例 A.33: assertSame() 的用法

<?php
class SameTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertSame('2204', 2204);
    }
}
?>
phpunit SameTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) SameTest::testFailure
Failed asserting that 2204 is identical to '2204'.

/home/sb/SameTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


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

当两个变量 $expected$actual 不是指向同一个对象的引用时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.34: assertSame() 应用于对象时的用法

<?php
class SameTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertSame(new stdClass, new stdClass);
    }
}
?>
phpunit SameTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) SameTest::testFailure
Failed asserting that two variables reference the same object.

/home/sb/SameTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertSelectCount()

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

将 CSS 选择器 $selector 应用于 DOMNode $actual,当匹配元素的数量与 $count 不相符时,报告一个错误,错误讯息的内容由 $message 指定。

$count 可以是以下类型之一:

  • boolean:断言存在符合条件的元素(TRUE)或不存在这样的元素(FALSE)。
  • integer:断言符合条件的元素的数量。
  • array:断言符合条件的元素的数量所在的区间,用 <><=>= 作为键名来指定相关信息。

例 A.35: assertSelectCount() 的用法

<?php
class SelectCountTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $this->xml = new DomDocument;
        $this->xml->loadXML('<foo><bar/><bar/><bar/></foo>');
    }

    public function testAbsenceFailure()
    {
        $this->assertSelectCount('foo bar', FALSE, $this->xml);
    }

    public function testPresenceFailure()
    {
        $this->assertSelectCount('foo baz', TRUE, $this->xml);
    }

    public function testExactCountFailure()
    {
        $this->assertSelectCount('foo bar', 5, $this->xml);
    }

    public function testRangeFailure()
    {
        $this->assertSelectCount('foo bar', array('>'=>6, '<'=>8), $this->xml);
    }
}
?>
phpunit SelectCountTest
PHPUnit 4.2.0 by Sebastian Bergmann.

FFFF

Time: 0 seconds, Memory: 5.50Mb

There were 4 failures:

1) SelectCountTest::testAbsenceFailure
Failed asserting that true is false.

/home/sb/SelectCountTest.php:12

2) SelectCountTest::testPresenceFailure
Failed asserting that false is true.

/home/sb/SelectCountTest.php:17

3) SelectCountTest::testExactCountFailure
Failed asserting that 3 matches expected 5.

/home/sb/SelectCountTest.php:22

4) SelectCountTest::testRangeFailure
Failed asserting that false is true.

/home/sb/SelectCountTest.php:27

FAILURES!
Tests: 4, Assertions: 4, Failures: 4.


assertSelectEquals()

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

将 CSS 选择器 $selector 应用于 DOMNode $actual,当匹配的元素中内容为 $content 的元素的数量与 $count 不相符时,报告一个错误,错误讯息的内容由 $message 指定。

$count 可以是以下类型之一:

  • boolean:断言存在符合条件的元素(TRUE)或不存在这样的元素(FALSE)。
  • integer:断言符合条件的元素的数量。
  • array:断言符合条件的元素的数量所在的区间,用 <><=>= 作为键名来指定相关信息。

例 A.36: assertSelectEquals() 的用法

<?php
class SelectEqualsTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $this->xml = new DomDocument;
        $this->xml->loadXML('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
    }

    public function testAbsenceFailure()
    {
        $this->assertSelectEquals('foo bar', 'Baz', FALSE, $this->xml);
    }

    public function testPresenceFailure()
    {
        $this->assertSelectEquals('foo bar', 'Bat', TRUE, $this->xml);
    }

    public function testExactCountFailure()
    {
        $this->assertSelectEquals('foo bar', 'Baz', 5, $this->xml);
    }

    public function testRangeFailure()
    {
        $this->assertSelectEquals('foo bar', 'Baz', array('>'=>6, '<'=>8), $this->xml);
    }
}
?>
phpunit SelectEqualsTest
PHPUnit 4.2.0 by Sebastian Bergmann.

FFFF

Time: 0 seconds, Memory: 5.50Mb

There were 4 failures:

1) SelectEqualsTest::testAbsenceFailure
Failed asserting that true is false.

/home/sb/SelectEqualsTest.php:12

2) SelectEqualsTest::testPresenceFailure
Failed asserting that false is true.

/home/sb/SelectEqualsTest.php:17

3) SelectEqualsTest::testExactCountFailure
Failed asserting that 2 matches expected 5.

/home/sb/SelectEqualsTest.php:22

4) SelectEqualsTest::testRangeFailure
Failed asserting that false is true.

/home/sb/SelectEqualsTest.php:27

FAILURES!
Tests: 4, Assertions: 4, Failures: 4.


assertSelectRegExp()

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

将 CSS 选择器 $selector 应用于 DOMNode $actual,当匹配的元素中内容与正则表达式 $pattern 匹配的元素的数量与 $count 不相符时,报告一个错误,错误讯息的内容由 $message 指定。

$count 可以是以下类型之一:

  • boolean:断言存在符合条件的元素(TRUE)或不存在这样的元素(FALSE)。
  • integer:断言符合条件的元素的数量。
  • array:断言符合条件的元素的数量所在的区间,用 <><=>= 作为键名来指定相关信息。

例 A.37: assertSelectRegExp() 的用法

<?php
class SelectRegExpTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $this->xml = new DomDocument;
        $this->xml->loadXML('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
    }

    public function testAbsenceFailure()
    {
        $this->assertSelectRegExp('foo bar', '/Ba.*/', FALSE, $this->xml);
    }

    public function testPresenceFailure()
    {
        $this->assertSelectRegExp('foo bar', '/B[oe]z]/', TRUE, $this->xml);
    }

    public function testExactCountFailure()
    {
        $this->assertSelectRegExp('foo bar', '/Ba.*/', 5, $this->xml);
    }

    public function testRangeFailure()
    {
        $this->assertSelectRegExp('foo bar', '/Ba.*/', array('>'=>6, '<'=>8), $this->xml);
    }
}
?>
phpunit SelectRegExpTest
PHPUnit 4.2.0 by Sebastian Bergmann.

FFFF

Time: 0 seconds, Memory: 5.50Mb

There were 4 failures:

1) SelectRegExpTest::testAbsenceFailure
Failed asserting that true is false.

/home/sb/SelectRegExpTest.php:12

2) SelectRegExpTest::testPresenceFailure
Failed asserting that false is true.

/home/sb/SelectRegExpTest.php:17

3) SelectRegExpTest::testExactCountFailure
Failed asserting that 2 matches expected 5.

/home/sb/SelectRegExpTest.php:22

4) SelectRegExpTest::testRangeFailure
Failed asserting that false is true.

/home/sb/SelectRegExpTest.php:27

FAILURES!
Tests: 4, Assertions: 4, Failures: 4.


assertStringEndsWith()

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

$string 不以 $suffix 结尾时,报告一个错误,错误讯息的内容由 $message 指定。

assertStringEndsNotWith() 是与之相反的断言,并接受相同的参数。

例 A.38: assertStringEndsWith() 的用法

<?php
class StringEndsWithTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertStringEndsWith('suffix', 'foo');
    }
}
?>
phpunit StringEndsWithTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 1 second, Memory: 5.00Mb

There was 1 failure:

1) StringEndsWithTest::testFailure
Failed asserting that 'foo' ends with "suffix".

/home/sb/StringEndsWithTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertStringEqualsFile()

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

$expectedFile 所指定的文件其内容与 $actualString 不相同时,报告一个错误,错误讯息的内容由 $message 指定。

assertStringNotEqualsFile() 是与之相反的断言,并接受相同的参数。

例 A.39: assertStringEqualsFile() 的用法

<?php
class StringEqualsFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertStringEqualsFile('/home/sb/expected', 'actual');
    }
}
?>
phpunit StringEqualsFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) StringEqualsFileTest::testFailure
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'expected
-'
+'actual'

/home/sb/StringEqualsFileTest.php:6

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.


assertStringStartsWith()

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

$string 不以 $prefix 开头时,报告一个错误,错误讯息的内容由 $message 指定。

assertStringStartsNotWith() 是与之相反的断言,并接受相同的参数。

例 A.40: assertStringStartsWith() 的用法

<?php
class StringStartsWithTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertStringStartsWith('prefix', 'foo');
    }
}
?>
phpunit StringStartsWithTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) StringStartsWithTest::testFailure
Failed asserting that 'foo' starts with "prefix".

/home/sb/StringStartsWithTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertTag()

assertTag(array $matcher, string $actual[, string $message = '', boolean $isHtml = TRUE])

$actual$matcher 所指定的信息不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

$matcher 是关联数组,用来为断言指定匹配准则:

  • id: 节点必须具有指定 id 属性值。
  • tag:节点的类型必须与对应值相匹配。
  • attributes:节点的属性必须与 $attributes 关联数组中所描述的对应值相匹配。
  • content:文本内容必须与指定值相匹配。
  • parent:节点的父节点必须匹配于 $parent 关联数组。
  • child: 节点的直接子节点(immediate children,即节点的下一级节点)中要至少有一个与 $child 关联数组所描述的准则一致。
  • ancestor:节点的祖先节点中要至少有一个与 $ancestor 关联数组所描述的准则一致。
  • descendant:节点的后代节点中要至少有一个与 $descendant 关联数组所描述的准则一致。
  • children:关联数组,节点的子节点的计数信息。
    • count:匹配的子节点数量必须等于此数字。
    • less_than:匹配的子节点数量必须小于此数字。
    • greater_than: 匹配的子节点数量必须大于此数字。
    • only:另一个关联数组,由用来对子节点进行匹配的键名组成,只有匹配的子节点才会进行计数。

assertNotTag() 是与之相反的断言,并接受相同的参数。

例 A.41: assertTag() 的用法

<?php
// 这个匹配器断言存在 id="my_id" 的元素。
$matcher = array('id' => 'my_id');

// 这个匹配器断言存在 "span" 标签。
$matcher = array('tag' => 'span');

// 这个匹配器断言存在内容为 "Hello World" 的 "span" 标签。
$matcher = array('tag' => 'span', 'content' => 'Hello World');

// 这个匹配器断言存在其内容与正则表达式模式相匹配的 "span" 标签。
$matcher = array('tag' => 'span', 'content' => 'regexp:/Try P(HP|ython)/');

// 这个匹配器断言存在class属性为 "list" 的 "span"。
$matcher = array(
  'tag'        => 'span',
  'attributes' => array('class' => 'list')
);

// 这个匹配器断言存在父元素为 "div" 的 "span"。
$matcher = array(
  'tag'    => 'span',
  'parent' => array('tag' => 'div')
);

// 这个匹配器断言存在某个 "span",其祖先元素中有 "table"。
$matcher = array(
  'tag'      => 'span',
  'ancestor' => array('tag' => 'table')
);

// 这个匹配器断言存在某个 "span",其直接子节点中至少有一个 "em"。
$matcher = array(
  'tag'   => 'span',
  'child' => array('tag' => 'em')
);

// 这个匹配器断言存在某个 "span",其所有后代子节点中存在 "strong" 标签。
$matcher = array(
  'tag'        => 'span',
  'descendant' => array('tag' => 'strong')
);

// 这个匹配器断言存在某个 "span",其直接子节点中包含有 5 到 10 个 "em" 标签。
$matcher = array(
  'tag'      => 'span',
  'children' => array(
    'less_than'    => 11,
    'greater_than' => 4,
    'only'         => array('tag' => 'em')
  )
);

// 这个匹配器断言存在一个 "div",其祖先元素中有 "ul",且其父元素是 class="enum" 的 "li",
// 且其后代子节点中存在一个 id="my_test" 同时文本内容为 "Hello World" 的 "span"。
$matcher = array(
  'tag'        => 'div',
  'ancestor'   => array('tag' => 'ul'),
  'parent'     => array(
    'tag'        => 'li',
    'attributes' => array('class' => 'enum')
  ),
  'descendant' => array(
    'tag'   => 'span',
    'child' => array(
      'id'      => 'my_test',
      'content' => 'Hello World'
    )
  )
);

// 使用 assertTag() 来将 $matcher 应用到 $html 片段上。
$this->assertTag($matcher, $html);

// 使用 assertTag() 来将 $matcher 应用到 $xml 片段上。
$this->assertTag($matcher, $xml, '', FALSE);
?>


assertThat()

可以用 PHPUnit_Framework_Constraint 类来订立更加复杂的断言。这些断言可以用 assertThat() 方法对其进行评定。例 A.42展示了如何用 logicalNot()equalTo() 约束条件来表达与 assertNotEquals() 等价的断言。

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

$value$constraint 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。

例 A.42: assertThat() 的用法

<?php
class BiscuitTest extends PHPUnit_Framework_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_FileExists fileExists()此约束检验所评定的文件(名)是否存在。
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()逻辑与。
logicalNot(PHPUnit_Framework_Constraint $constraint)逻辑非。
logicalOr()逻辑或。
logicalXor()逻辑异或。
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 = ''])

$conditionFALSE时,报告一个错误,错误讯息的内容由 $message 指定。

assertNotTrue() 是此断言的逆断言,接受相同的参数。

例 A.43: assertTrue() 的用法

<?php
class TrueTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertTrue(FALSE);
    }
}
?>
phpunit TrueTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) TrueTest::testFailure
Failed asserting that false is true.

/home/sb/TrueTest.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


assertXmlFileEqualsXmlFile()

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

$actualFile 中的 XML 文档与 $expectedFile 中的 XML 文档不相等时,报告一个错误,错误讯息的内容由 $message 指定。

assertXmlFileNotEqualsXmlFile() 是与之相反的断言,并接受相同的参数。

例 A.44: assertXmlFileEqualsXmlFile() 的用法

<?php
class XmlFileEqualsXmlFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertXmlFileEqualsXmlFile(
          '/home/sb/expected.xml', '/home/sb/actual.xml');
    }
}
?>
phpunit XmlFileEqualsXmlFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There 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:7

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.


assertXmlStringEqualsXmlFile()

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

$actualXml 中的 XML 文档与 $expectedFile 中的 XML 文档不相等时,报告一个错误,错误讯息的内容由 $message 指定。

assertXmlStringNotEqualsXmlFile() 是与之相反的断言,并接受相同的参数。

例 A.45: assertXmlStringEqualsXmlFile() 的用法

<?php
class XmlStringEqualsXmlFileTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertXmlStringEqualsXmlFile(
          '/home/sb/expected.xml', '<foo><baz/></foo>');
    }
}
?>
phpunit XmlStringEqualsXmlFileTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.25Mb

There 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:7

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.


assertXmlStringEqualsXmlString()

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

$actualXml 中的 XML 文档与 $expectedXml 中的 XML 文档不相等时,报告一个错误,错误讯息的内容由 $message 指定。

assertXmlStringNotEqualsXmlString() 是与之相反的断言,并接受相同的参数。

例 A.46: assertXmlStringEqualsXmlString() 的用法

<?php
class XmlStringEqualsXmlStringTest extends PHPUnit_Framework_TestCase
{
    public function testFailure()
    {
        $this->assertXmlStringEqualsXmlString(
          '<foo><bar/></foo>', '<foo><baz/></foo>');
    }
}
?>
phpunit XmlStringEqualsXmlStringTest
PHPUnit 4.2.0 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There 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:7

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.