1. 断言

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

断言方法的用法:静态 vs. 非静态

PHPUnit 的各个断言是在 PHPUnit\Framework\Assert 中实现的。PHPUnit\Framework\TestCase 则继承于 PHPUnit\Framework\Assert

各个断言方法均声明为 static,可以从任何上下文以类似于 PHPUnit\Framework\Assert::assertTrue() 的方式调用,或者也可以用类似于 $this->assertTrue()self::assertTrue() 的方式在扩展自 PHPUnit\Framework\TestCase 的类内调用。甚至可以用全局函数封装,例如 assertTrue()

有个常见的疑问——对于那些 PHPUnit 的新手尤甚——是究竟应该用诸如 $this->assertTrue() 还是诸如 self::assertTrue() 这样的形式来调用断言才是“正确的方式”?简而言之:没有正确方式。同时,也没有错误方式。这基本上是个人喜好问题。

对于大多数人而言,由于测试方法是在测试对象上调用,因此用 $this->assertTrue() 会“觉的更正确”。然而请记住断言方法是声明为 static 的,这使其可以(重)用于测试对象的作用域之外。最后,全局函数封装让开发者能再少打一些字(用 assertTrue() 代替 $this->assertTrue() 或者 self::assertTrue())。

assertArrayHasKey()

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

$array 不包含 $key 时报告错误,错误讯息由 $message 指定。

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

示例 1.1 assertArrayHasKey() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ArrayHasKeyTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertArrayHasKey('foo', ['bar' => 'baz']);
  8. }
  9. }
  1. $ phpunit ArrayHasKeyTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ArrayHasKeyTest::testFailure
  7. Failed asserting that an array has the key 'foo'.
  8. /home/sb/ArrayHasKeyTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertClassHasAttribute()

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

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

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

示例 1.2 assertClassHasAttribute() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ClassHasAttributeTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertClassHasAttribute('foo', stdClass::class);
  8. }
  9. }
  1. $ phpunit ClassHasAttributeTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) ClassHasAttributeTest::testFailure
  7. Failed asserting that class "stdClass" has attribute "foo".
  8. /home/sb/ClassHasAttributeTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertClassHasStaticAttribute()

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

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

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

示例 1.3 assertClassHasStaticAttribute() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ClassHasStaticAttributeTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertClassHasStaticAttribute('foo', stdClass::class);
  8. }
  9. }
  1. $ phpunit ClassHasStaticAttributeTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) ClassHasStaticAttributeTest::testFailure
  7. Failed asserting that class "stdClass" has static attribute "foo".
  8. /home/sb/ClassHasStaticAttributeTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertContains()

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

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

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

示例 1.4 assertContains() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ContainsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertContains(4, [1, 2, 3]);
  8. }
  9. }
  1. $ phpunit ContainsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ContainsTest::testFailure
  7. Failed asserting that an array contains 4.
  8. /home/sb/ContainsTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertStringContainsString()

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

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

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

示例 1.5 assertStringContainsString() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringContainsStringTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringContainsString('foo', 'bar');
  8. }
  9. }
  1. $ phpunit StringContainsStringTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 37 ms, Memory: 6.00 MB
  5. There was 1 failure:
  6. 1) StringContainsStringTest::testFailure
  7. Failed asserting that 'bar' contains "foo".
  8. /home/sb/StringContainsStringTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertStringContainsStringIgnoringCase()

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

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

$haystack 中搜索 $needle 时,忽略大小写差异。

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

示例 1.6 assertStringContainsStringIgnoringCase() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringContainsStringIgnoringCaseTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringContainsStringIgnoringCase('foo', 'bar');
  8. }
  9. }
  1. $ phpunit StringContainsStringIgnoringCaseTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 40 ms, Memory: 6.00 MB
  5. There was 1 failure:
  6. 1) StringContainsStringTest::testFailure
  7. Failed asserting that 'bar' contains "foo".
  8. /home/sb/StringContainsStringIgnoringCaseTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertContainsOnly()

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

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

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

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

示例 1.7 assertContainsOnly() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ContainsOnlyTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertContainsOnly('string', ['1', '2', 3]);
  8. }
  9. }
  1. $ phpunit ContainsOnlyTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ContainsOnlyTest::testFailure
  7. Failed asserting that Array (
  8. 0 => '1'
  9. 1 => '2'
  10. 2 => 3
  11. ) contains only values of type "string".
  12. /home/sb/ContainsOnlyTest.php:6
  13. FAILURES!
  14. Tests: 1, Assertions: 1, Failures: 1.

assertContainsOnlyInstancesOf()

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

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

示例 1.8 assertContainsOnlyInstancesOf() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ContainsOnlyInstancesOfTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertContainsOnlyInstancesOf(
  8. Foo::class,
  9. [new Foo, new Bar, new Foo]
  10. );
  11. }
  12. }
  1. $ phpunit ContainsOnlyInstancesOfTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ContainsOnlyInstancesOfTest::testFailure
  7. Failed asserting that Array ([0]=> Bar Object(...)) is an instance of class "Foo".
  8. /home/sb/ContainsOnlyInstancesOfTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertCount()

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

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

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

示例 1.9 assertCount() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class CountTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertCount(0, ['foo']);
  8. }
  9. }
  1. $ phpunit CountTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) CountTest::testFailure
  7. Failed asserting that actual size 1 matches expected size 0.
  8. /home/sb/CountTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryExists()

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

$directory 所指定的目录不存在时报告错误,错误讯息由 $message 指定。

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

示例 1.10 assertDirectoryExists() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class DirectoryExistsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertDirectoryExists('/path/to/directory');
  8. }
  9. }
  1. $ phpunit DirectoryExistsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) DirectoryExistsTest::testFailure
  7. Failed asserting that directory "/path/to/directory" exists.
  8. /home/sb/DirectoryExistsTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryIsReadable()

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

$directory 所指定的目录不是个目录或不可读时报告错误,错误讯息由 $message 指定。

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

示例 1.11 assertDirectoryIsReadable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class DirectoryIsReadableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertDirectoryIsReadable('/path/to/directory');
  8. }
  9. }
  1. $ phpunit DirectoryIsReadableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) DirectoryIsReadableTest::testFailure
  7. Failed asserting that "/path/to/directory" is readable.
  8. /home/sb/DirectoryIsReadableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertDirectoryIsWritable()

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

$directory 所指定的目录不是个目录或不可写时报告错误,错误讯息由 $message 指定。

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

示例 1.12 assertDirectoryIsWritable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class DirectoryIsWritableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertDirectoryIsWritable('/path/to/directory');
  8. }
  9. }
  1. $ phpunit DirectoryIsWritableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) DirectoryIsWritableTest::testFailure
  7. Failed asserting that "/path/to/directory" is writable.
  8. /home/sb/DirectoryIsWritableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertEmpty()

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

$actual 非空时报告错误,错误讯息由 $message 指定。

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

示例 1.13 assertEmpty() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EmptyTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEmpty(['foo']);
  8. }
  9. }
  1. $ phpunit EmptyTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) EmptyTest::testFailure
  7. Failed asserting that an array is empty.
  8. /home/sb/EmptyTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertEquals()

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

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

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

示例 1.14 assertEquals() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEquals(1, 0);
  8. }
  9. public function testFailure2(): void
  10. {
  11. $this->assertEquals('bar', 'baz');
  12. }
  13. public function testFailure3(): void
  14. {
  15. $this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");
  16. }
  17. }
  1. $ phpunit EqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. FFF
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There were 3 failures:
  6. 1) EqualsTest::testFailure
  7. Failed asserting that 0 matches expected 1.
  8. /home/sb/EqualsTest.php:6
  9. 2) EqualsTest::testFailure2
  10. Failed asserting that two strings are equal.
  11. --- Expected
  12. +++ Actual
  13. @@ @@
  14. -'bar'
  15. +'baz'
  16. /home/sb/EqualsTest.php:11
  17. 3) EqualsTest::testFailure3
  18. Failed asserting that two strings are equal.
  19. --- Expected
  20. +++ Actual
  21. @@ @@
  22. 'foo
  23. -bar
  24. +bah
  25. baz
  26. '
  27. /home/sb/EqualsTest.php:16
  28. FAILURES!
  29. Tests: 3, Assertions: 3, Failures: 3.

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

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

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

示例 1.15 对 DOMDocument 对象使用 assertEquals() 时的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $expected = new DOMDocument;
  8. $expected->loadXML('<foo><bar/></foo>');
  9. $actual = new DOMDocument;
  10. $actual->loadXML('<bar><foo/></bar>');
  11. $this->assertEquals($expected, $actual);
  12. }
  13. }
  1. $ phpunit EqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) EqualsTest::testFailure
  7. Failed asserting that two DOM documents are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. <?xml version="1.0"?>
  12. -<foo>
  13. - <bar/>
  14. -</foo>
  15. +<bar>
  16. + <foo/>
  17. +</bar>
  18. /home/sb/EqualsTest.php:12
  19. FAILURES!
  20. Tests: 1, Assertions: 1, Failures: 1.

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

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

示例 1.16 对对象使用 assertEquals() 时的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $expected = new stdClass;
  8. $expected->foo = 'foo';
  9. $expected->bar = 'bar';
  10. $actual = new stdClass;
  11. $actual->foo = 'bar';
  12. $actual->baz = 'bar';
  13. $this->assertEquals($expected, $actual);
  14. }
  15. }
  1. $ phpunit EqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) EqualsTest::testFailure
  7. Failed asserting that two objects are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. stdClass Object (
  12. - 'foo' => 'foo'
  13. - 'bar' => 'bar'
  14. + 'foo' => 'bar'
  15. + 'baz' => 'bar'
  16. )
  17. /home/sb/EqualsTest.php:14
  18. FAILURES!
  19. Tests: 1, Assertions: 1, Failures: 1.

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

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

示例 1.17 对数组使用 assertEquals() 时的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEquals(['a', 'b', 'c'], ['a', 'c', 'd']);
  8. }
  9. }
  1. $ phpunit EqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) EqualsTest::testFailure
  7. Failed asserting that two arrays are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. Array (
  12. 0 => 'a'
  13. - 1 => 'b'
  14. - 2 => 'c'
  15. + 1 => 'c'
  16. + 2 => 'd'
  17. )
  18. /home/sb/EqualsTest.php:6
  19. FAILURES!
  20. Tests: 1, Assertions: 1, Failures: 1.

assertEqualsCanonicalizing()

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

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

先对 $expected$actual 进行规范化,然后再进行比较。例如,如果两个变量 $expected$actual 是数组,则会先对这些数组进行排序然后再进行比较。当 $expected$actual 是对象时,则每个对象都会被转换为一个包含所有私有、保护、和公开属性的数组。

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

示例 1.18 assertEqualsCanonicalizing() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsCanonicalizingTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 0, 1]);
  8. }
  9. }
  1. $ phpunit EqualsCanonicalizingTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 42 ms, Memory: 6.00 MB
  5. There was 1 failure:
  6. 1) EqualsCanonicalizingTest::testFailure
  7. Failed asserting that two arrays are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. Array (
  12. - 0 => 1
  13. - 1 => 2
  14. - 2 => 3
  15. + 0 => 0
  16. + 1 => 1
  17. + 2 => 2
  18. + 3 => 3
  19. )
  20. /home/sb/EqualsCanonicalizingTest.php:8
  21. FAILURES!
  22. Tests: 1, Assertions: 1, Failures: 1.

assertEqualsIgnoringCase()

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

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

比较 $expected$actual 时,忽略大小写差异。

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

示例 1.19 assertEqualsIgnoringCase() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsIgnoringCaseTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertEqualsIgnoringCase('foo', 'BAR');
  8. }
  9. }
  1. $ phpunit EqualsIgnoringCaseTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 51 ms, Memory: 6.00 MB
  5. There was 1 failure:
  6. 1) EqualsIgnoringCaseTest::testFailure
  7. Failed asserting that two strings are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. -'foo'
  12. +'BAR'
  13. /home/sb/EqualsIgnoringCaseTest.php:8
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.

assertEqualsWithDelta()

assertEqualsWithDelta(mixed $expected, mixed $actual, float $delta[, string $message = ''])

$expected$actual 之间的差的绝对值大于 $delta 时报告错误,错误讯息由 $message 指定。

请阅读《What Every Computer Scientist Should Know About Floating-Point Arithmetic》来了解为什么需要有 $delta

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

示例 1.20 assertEqualsWithDelta() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithDeltaTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertEqualsWithDelta(1.0, 1.5, 0.1);
  8. }
  9. }
  1. $ phpunit EqualsWithDeltaTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 41 ms, Memory: 6.00 MB
  5. There was 1 failure:
  6. 1) EqualsWithDeltaTest::testFailure
  7. Failed asserting that 1.5 matches expected 1.0.
  8. /home/sb/EqualsWithDeltaTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertObjectEquals()

assertObjectEquals(object $expected, object $actual, string $method = 'equals', string $message = ''])

当按照 $actual->$method($expected) 判断出 $actual 不等于 $expected 时报告错误,错误讯息由 $message 指定。

在对象上使用 assertEquals()(以及其否断言形式 assertNotEquals())而不注册自定义比较器来定制对象的比较方式是一种不良做法。但是,很不幸地,为每个要在测试中进行断言的对象都实现自定义比较器无论如何至少也是不方便的。

自定义比较器最常见的用例是值对象。这类对象一般都有一个 equals(self $other): bool 方法(或者名称不同的类似方法)用于比较这个同为值对象类型的两个实例。对于这类用例,assertObjectEquals() 让自定义对象比较变得很方便:

示例 1.21 assertObjectEquals() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class SomethingThatUsesEmailTest extends TestCase
  4. {
  5. public function testSomething(): void
  6. {
  7. $a = new Email('user@example.org');
  8. $b = new Email('user@example.org');
  9. $c = new Email('user@example.com');
  10. // This passes
  11. $this->assertObjectEquals($a, $b);
  12. // This fails
  13. $this->assertObjectEquals($a, $c);
  14. }
  15. }

示例 1.22 带有 equals() 方法的 Email 值对象

  1. <?php declare(strict_types=1);
  2. final class Email
  3. {
  4. private string $email;
  5. public function __construct(string $email)
  6. {
  7. $this->ensureIsValidEmail($email);
  8. $this->email = $email;
  9. }
  10. public function asString(): string
  11. {
  12. return $this->email;
  13. }
  14. public function equals(self $other): bool
  15. {
  16. return $this->asString() === $other->asString();
  17. }
  18. private function ensureIsValidEmail(string $email): void
  19. {
  20. // ...
  21. }
  22. }
  1. $ phpunit EqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F 1 / 1 (100%)
  4. Time: 00:00.017, Memory: 4.00 MB
  5. There was 1 failure:
  6. 1) SomethingThatUsesEmailTest::testSomething
  7. Failed asserting that two objects are equal.
  8. The objects are not equal according to Email::equals().
  9. /home/sb/SomethingThatUsesEmailTest.php:16
  10. FAILURES!
  11. Tests: 1, Assertions: 2, Failures: 1.

请注意:

  • $actual 对象必须存在名叫 $method 的方法
  • 此方法必须只接受一个参数
  • 相应的参数必须有声明类型
  • $expected 对象必须与这个声明的类型兼容
  • 此方法必须有声明为 bool 的返回类型

如果以上假设中的任何一条不满足,或 $actual->$method($expected) 返回 false,则断言失败。

assertFalse()

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

$conditiontrue 时报告错误,错误讯息由 $message 指定。

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

示例 1.23 assertFalse() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FalseTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFalse(true);
  8. }
  9. }
  1. $ phpunit FalseTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) FalseTest::testFailure
  7. Failed asserting that true is false.
  8. /home/sb/FalseTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertFileEquals()

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

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

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

示例 1.24 assertFileEquals() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FileEqualsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFileEquals('/home/sb/expected', '/home/sb/actual');
  8. }
  9. }
  1. $ phpunit FileEqualsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) FileEqualsTest::testFailure
  7. Failed asserting that two strings are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. -'expected
  12. +'actual
  13. '
  14. /home/sb/FileEqualsTest.php:6
  15. FAILURES!
  16. Tests: 1, Assertions: 3, Failures: 1.

assertFileExists()

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

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

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

示例 1.25 assertFileExists() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FileExistsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFileExists('/path/to/file');
  8. }
  9. }
  1. $ phpunit FileExistsTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) FileExistsTest::testFailure
  7. Failed asserting that file "/path/to/file" exists.
  8. /home/sb/FileExistsTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertFileIsReadable()

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

$filename 所指定的文件不是个文件或不可读时报告错误,错误讯息由 $message 指定。

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

示例 1.26 assertFileIsReadable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FileIsReadableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFileIsReadable('/path/to/file');
  8. }
  9. }
  1. $ phpunit FileIsReadableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) FileIsReadableTest::testFailure
  7. Failed asserting that "/path/to/file" is readable.
  8. /home/sb/FileIsReadableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertFileIsWritable()

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

$filename 所指定的文件不是个文件或不可写时报告错误,错误讯息由 $message 指定。

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

示例 1.27 assertFileIsWritable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FileIsWritableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFileIsWritable('/path/to/file');
  8. }
  9. }
  1. $ phpunit FileIsWritableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) FileIsWritableTest::testFailure
  7. Failed asserting that "/path/to/file" is writable.
  8. /home/sb/FileIsWritableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertGreaterThan()

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

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

示例 1.28 assertGreaterThan() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class GreaterThanTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertGreaterThan(2, 1);
  8. }
  9. }
  1. $ phpunit GreaterThanTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) GreaterThanTest::testFailure
  7. Failed asserting that 1 is greater than 2.
  8. /home/sb/GreaterThanTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertGreaterThanOrEqual()

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

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

示例 1.29 assertGreaterThanOrEqual() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class GreatThanOrEqualTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertGreaterThanOrEqual(2, 1);
  8. }
  9. }
  1. $ phpunit GreaterThanOrEqualTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) GreatThanOrEqualTest::testFailure
  7. Failed asserting that 1 is equal to 2 or is greater than 2.
  8. /home/sb/GreaterThanOrEqualTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 2, Failures: 1.

assertInfinite()

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

$actual 不是 INF 时报告错误,错误讯息由 $message 指定。

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

示例 1.30 assertInfinite() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class InfiniteTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertInfinite(1);
  8. }
  9. }
  1. $ phpunit InfiniteTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) InfiniteTest::testFailure
  7. Failed asserting that 1 is infinite.
  8. /home/sb/InfiniteTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertInstanceOf()

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

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

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

示例 1.31 assertInstanceOf() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class InstanceOfTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertInstanceOf(RuntimeException::class, new Exception);
  8. }
  9. }
  1. $ phpunit InstanceOfTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) InstanceOfTest::testFailure
  7. Failed asserting that Exception Object (...) is an instance of class "RuntimeException".
  8. /home/sb/InstanceOfTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsArray()

assertIsArray($actual[, $message = ''])

$actual 的类型不是 array 时报告错误,错误讯息由 $message 指定。

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

示例 1.32 assertIsArray() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class ArrayTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsArray(null);
  8. }
  9. }
  1. $ phpunit ArrayTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ArrayTest::testFailure
  7. Failed asserting that null is of type "array".
  8. /home/sb/ArrayTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsBool()

assertIsBool($actual[, $message = ''])

$actual 的类型不是 bool 时报告错误,错误讯息由 $message 指定。

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

示例 1.33 assertIsBool() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class BoolTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsBool(null);
  8. }
  9. }
  1. $ phpunit BoolTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) BoolTest::testFailure
  7. Failed asserting that null is of type "bool".
  8. /home/sb/BoolTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsCallable()

assertIsCallable($actual[, $message = ''])

$actual 的类型不是 callable 时报告错误,错误讯息由 $message 指定。

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

示例 1.34 assertIsCallable() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class CallableTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsCallable(null);
  8. }
  9. }
  1. $ phpunit CallableTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) CallableTest::testFailure
  7. Failed asserting that null is of type "callable".
  8. /home/sb/CallableTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsFloat()

assertIsFloat($actual[, $message = ''])

$actual 的类型不是 float 时报告错误,错误讯息由 $message 指定。

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

示例 1.35 assertIsFloat() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class FloatTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsFloat(null);
  8. }
  9. }
  1. $ phpunit FloatTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) FloatTest::testFailure
  7. Failed asserting that null is of type "float".
  8. /home/sb/FloatTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsInt()

assertIsInt($actual[, $message = ''])

$actual 的类型不是 int 时报告错误,错误讯息由 $message 指定。

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

示例 1.36 assertIsInt() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class IntTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsInt(null);
  8. }
  9. }
  1. $ phpunit IntTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) IntTest::testFailure
  7. Failed asserting that null is of type "int".
  8. /home/sb/IntTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsIterable()

assertIsIterable($actual[, $message = ''])

$actual 的类型不是 iterable 时报告错误,错误讯息由 $message 指定。

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

示例 1.37 assertIsIterable() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class IterableTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsIterable(null);
  8. }
  9. }
  1. $ phpunit IterableTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) IterableTest::testFailure
  7. Failed asserting that null is of type "iterable".
  8. /home/sb/IterableTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsNumeric()

assertIsNumeric($actual[, $message = ''])

$actual 的类型不是 numeric 时报告错误,错误讯息由 $message 指定。

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

示例 1.38 assertIsNumeric() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class NumericTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsNumeric(null);
  8. }
  9. }
  1. $ phpunit NumericTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) NumericTest::testFailure
  7. Failed asserting that null is of type "numeric".
  8. /home/sb/NumericTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsObject()

assertIsObject($actual[, $message = ''])

$actual 的类型不是 object 时报告错误,错误讯息由 $message 指定。

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

示例 1.39 assertIsObject() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class ObjectTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsObject(null);
  8. }
  9. }
  1. $ phpunit ObjectTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ObjectTest::testFailure
  7. Failed asserting that null is of type "object".
  8. /home/sb/ObjectTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsResource()

assertIsResource($actual[, $message = ''])

$actual 的类型不是 resource 时报告错误,错误讯息由 $message 指定。

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

示例 1.40 assertIsResource() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class ResourceTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsResource(null);
  8. }
  9. }
  1. $ phpunit ResourceTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ResourceTest::testFailure
  7. Failed asserting that null is of type "resource".
  8. /home/sb/ResourceTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsScalar()

assertIsScalar($actual[, $message = ''])

$actual 的类型不是 scalar 时报告错误,错误讯息由 $message 指定。

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

示例 1.41 assertIsScalar() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class ScalarTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsScalar(null);
  8. }
  9. }
  1. $ phpunit ScalarTest
  2. PHPUnit |version|.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) ScalarTest::testFailure
  7. Failed asserting that null is of type "scalar".
  8. /home/sb/ScalarTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsString()

assertIsString($actual[, $message = ''])

$actual 的类型不是 string 时报告错误,错误讯息由 $message 指定。

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

示例 1.42 assertIsString() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class StringTest extends TestCase
  4. {
  5. public function testFailure()
  6. {
  7. $this->assertIsString(null);
  8. }
  9. }
  1. $ phpunit StringTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) StringTest::testFailure
  7. Failed asserting that null is of type "string".
  8. /home/sb/StringTest.php:8
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsReadable()

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

$filename 所指定的文件或目录不可读时报告错误,错误讯息由 $message 指定。

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

示例 1.43 assertIsReadable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsReadableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsReadable('/path/to/unreadable');
  8. }
  9. }
  1. $ phpunit IsReadableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) IsReadableTest::testFailure
  7. Failed asserting that "/path/to/unreadable" is readable.
  8. /home/sb/IsReadableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertIsWritable()

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

$filename 所指定的文件或目录不可写时报告错误,错误讯息由 $message 指定。

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

示例 1.44 assertIsWritable() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsWritableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsWritable('/path/to/unwritable');
  8. }
  9. }
  1. $ phpunit IsWritableTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) IsWritableTest::testFailure
  7. Failed asserting that "/path/to/unwritable" is writable.
  8. /home/sb/IsWritableTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertJsonFileEqualsJsonFile()

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

$actualFile 对应的值与 $expectedFile 对应的值不匹配时报告错误,错误讯息由 $message 指定。

示例 1.45 assertJsonFileEqualsJsonFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class JsonFileEqualsJsonFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertJsonFileEqualsJsonFile(
  8. 'path/to/fixture/file', 'path/to/actual/file');
  9. }
  10. }
  1. $ phpunit JsonFileEqualsJsonFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) JsonFileEqualsJsonFile::testFailure
  7. Failed asserting that '{"Mascot":"Tux"}' matches JSON string "["Mascott", "Tux", "OS", "Linux"]".
  8. /home/sb/JsonFileEqualsJsonFileTest.php:5
  9. FAILURES!
  10. Tests: 1, Assertions: 3, Failures: 1.

assertJsonStringEqualsJsonFile()

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

$actualJson 对应的值与 $expectedFile 对应的值不匹配时报告错误,错误讯息由 $message 指定。

示例 1.46 assertJsonStringEqualsJsonFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class JsonStringEqualsJsonFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertJsonStringEqualsJsonFile(
  8. 'path/to/fixture/file', json_encode(['Mascot' => 'ux'])
  9. );
  10. }
  11. }
  1. $ phpunit JsonStringEqualsJsonFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) JsonStringEqualsJsonFile::testFailure
  7. Failed asserting that '{"Mascot":"ux"}' matches JSON string "{"Mascott":"Tux"}".
  8. /home/sb/JsonStringEqualsJsonFileTest.php:5
  9. FAILURES!
  10. Tests: 1, Assertions: 3, Failures: 1.

assertJsonStringEqualsJsonString()

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

$actualJson 对应的值与 $expectedJson 对应的值不匹配时报告错误,错误讯息由 $message 指定。

示例 1.47 assertJsonStringEqualsJsonString() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class JsonStringEqualsJsonStringTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertJsonStringEqualsJsonString(
  8. json_encode(['Mascot' => 'Tux']),
  9. json_encode(['Mascot' => 'ux'])
  10. );
  11. }
  12. }
  1. $ phpunit JsonStringEqualsJsonStringTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) JsonStringEqualsJsonStringTest::testFailure
  7. Failed asserting that two objects are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. stdClass Object (
  12. - 'Mascot' => 'Tux'
  13. + 'Mascot' => 'ux'
  14. )
  15. /home/sb/JsonStringEqualsJsonStringTest.php:5
  16. FAILURES!
  17. Tests: 1, Assertions: 3, Failures: 1.

assertLessThan()

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

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

示例 1.48 assertLessThan() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class LessThanTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertLessThan(1, 2);
  8. }
  9. }
  1. $ phpunit LessThanTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) LessThanTest::testFailure
  7. Failed asserting that 2 is less than 1.
  8. /home/sb/LessThanTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertLessThanOrEqual()

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

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

示例 1.49 assertLessThanOrEqual() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class LessThanOrEqualTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertLessThanOrEqual(1, 2);
  8. }
  9. }
  1. $ phpunit LessThanOrEqualTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) LessThanOrEqualTest::testFailure
  7. Failed asserting that 2 is equal to 1 or is less than 1.
  8. /home/sb/LessThanOrEqualTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 2, Failures: 1.

assertNan()

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

$variable 不是 NAN 时报告错误,错误讯息由 $message 指定。

示例 1.50 assertNan() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class NanTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertNan(1);
  8. }
  9. }
  1. $ phpunit NanTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) NanTest::testFailure
  7. Failed asserting that 1 is nan.
  8. /home/sb/NanTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertNull()

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

$actual 不是 null 时报告错误,错误讯息由 $message 指定。

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

示例 1.51 assertNull() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class NullTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertNull('foo');
  8. }
  9. }
  1. $ phpunit NotNullTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) NullTest::testFailure
  7. Failed asserting that 'foo' is null.
  8. /home/sb/NotNullTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertObjectHasAttribute()

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

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

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

示例 1.52 assertObjectHasAttribute() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class ObjectHasAttributeTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertObjectHasAttribute('foo', new stdClass);
  8. }
  9. }
  1. $ phpunit ObjectHasAttributeTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) ObjectHasAttributeTest::testFailure
  7. Failed asserting that object of class "stdClass" has attribute "foo".
  8. /home/sb/ObjectHasAttributeTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertMatchesRegularExpression()

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

$string 不匹配于正则表达式 $pattern 时报告错误,错误讯息由 $message 指定。

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

示例 1.53 assertMatchesRegularExpression() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class RegExpTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertMatchesRegularExpression('/foo/', 'bar');
  8. }
  9. }
  1. $ phpunit RegExpTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) RegExpTest::testFailure
  7. Failed asserting that 'bar' matches PCRE pattern "/foo/".
  8. /home/sb/RegExpTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertStringMatchesFormat()

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

$string 不匹配于 $format 定义的格式时报告错误,错误讯息由 $message 指定。

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

示例 1.54 assertStringMatchesFormat() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringMatchesFormatTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringMatchesFormat('%i', 'foo');
  8. }
  9. }
  1. $ phpunit StringMatchesFormatTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) StringMatchesFormatTest::testFailure
  7. Failed asserting that 'foo' matches PCRE pattern "/^[+-]?d+$/s".
  8. /home/sb/StringMatchesFormatTest.php:6
  9. FAILURES!
  10. 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() 是与之相反的断言,接受相同的参数。

示例 1.55 assertStringMatchesFormatFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringMatchesFormatFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo');
  8. }
  9. }
  1. $ phpunit StringMatchesFormatFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) StringMatchesFormatFileTest::testFailure
  7. Failed asserting that 'foo' matches PCRE pattern "/^[+-]?d+
  8. $/s".
  9. /home/sb/StringMatchesFormatFileTest.php:6
  10. FAILURES!
  11. Tests: 1, Assertions: 2, Failures: 1.

assertSame()

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

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

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

示例 1.56 assertSame() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class SameTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertSame('2204', 2204);
  8. }
  9. }
  1. $ phpunit SameTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) SameTest::testFailure
  7. Failed asserting that 2204 is identical to '2204'.
  8. /home/sb/SameTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

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

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

示例 1.57 对对象使用 assertSame() 时的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class SameTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertSame(new stdClass, new stdClass);
  8. }
  9. }
  1. $ phpunit SameTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 4.75Mb
  5. There was 1 failure:
  6. 1) SameTest::testFailure
  7. Failed asserting that two variables reference the same object.
  8. /home/sb/SameTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertStringEndsWith()

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

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

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

示例 1.58 assertStringEndsWith() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringEndsWithTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringEndsWith('suffix', 'foo');
  8. }
  9. }
  1. $ phpunit StringEndsWithTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 1 second, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) StringEndsWithTest::testFailure
  7. Failed asserting that 'foo' ends with "suffix".
  8. /home/sb/StringEndsWithTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertStringEqualsFile()

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

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

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

示例 1.59 assertStringEqualsFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringEqualsFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringEqualsFile('/home/sb/expected', 'actual');
  8. }
  9. }
  1. $ phpunit StringEqualsFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) StringEqualsFileTest::testFailure
  7. Failed asserting that two strings are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. -'expected
  12. -'
  13. +'actual'
  14. /home/sb/StringEqualsFileTest.php:6
  15. FAILURES!
  16. Tests: 1, Assertions: 2, Failures: 1.

assertStringStartsWith()

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

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

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

示例 1.60 assertStringStartsWith() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class StringStartsWithTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertStringStartsWith('prefix', 'foo');
  8. }
  9. }
  1. $ phpunit StringStartsWithTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) StringStartsWithTest::testFailure
  7. Failed asserting that 'foo' starts with "prefix".
  8. /home/sb/StringStartsWithTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertThat()

可以用 PHPUnit\Framework\Constraint 类来订立更加复杂的断言。随后可以用 assertThat() 方法来评定这些断言。示例 1.61 展示了如何用 logicalNot()equalTo() 约束条件来表达与 assertNotEquals() 等价的断言。

assertThat(mixed $value, PHPUnit\Framework\Constraint $constraint[, $message = ''])

$value 不符合约束条件 $constraint 时报告错误,错误讯息由 $message 指定。

示例 1.61 assertThat() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class BiscuitTest extends TestCase
  4. {
  5. public function testEquals(): void
  6. {
  7. $theBiscuit = new Biscuit('Ginger');
  8. $myBiscuit = new Biscuit('Ginger');
  9. $this->assertThat(
  10. $theBiscuit,
  11. $this->logicalNot(
  12. $this->equalTo($myBiscuit)
  13. )
  14. );
  15. }
  16. }

表格 1.1 列举了所有可用的 PHPUnit\Framework\Constraint 类。

表格 1.1 约束条件
约束条件含义
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\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\LogicalOr greaterThanOrEqual(mixed $value)此约束断言值大于或等于给定值。
PHPUnit\Framework\Constraint\ClassHasAttribute classHasAttribute(string $attributeName)此约束断言类具有给定属性。
PHPUnit\Framework\Constraint\ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)此约束断言类具有给定静态属性。
PHPUnit\Framework\Constraint\ObjectHasAttribute objectHasAttribute(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\LogicalOr 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 = ''])

$conditionfalse 时报告错误,错误讯息由 $message 指定。

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

示例 1.62 assertTrue() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class TrueTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertTrue(false);
  8. }
  9. }
  1. $ phpunit TrueTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) TrueTest::testFailure
  7. Failed asserting that false is true.
  8. /home/sb/TrueTest.php:6
  9. FAILURES!
  10. Tests: 1, Assertions: 1, Failures: 1.

assertXmlFileEqualsXmlFile()

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

$actualFile 对应的 XML 文档与 $expectedFile 对应的 XML 文档不相同时报告错误,错误讯息由 $message 指定。

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

示例 1.63 assertXmlFileEqualsXmlFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class XmlFileEqualsXmlFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertXmlFileEqualsXmlFile(
  8. '/home/sb/expected.xml', '/home/sb/actual.xml');
  9. }
  10. }
  1. $ phpunit XmlFileEqualsXmlFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) XmlFileEqualsXmlFileTest::testFailure
  7. Failed asserting that two DOM documents are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. <?xml version="1.0"?>
  12. <foo>
  13. - <bar/>
  14. + <baz/>
  15. </foo>
  16. /home/sb/XmlFileEqualsXmlFileTest.php:7
  17. FAILURES!
  18. Tests: 1, Assertions: 3, Failures: 1.

assertXmlStringEqualsXmlFile()

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

$actualXml 对应的 XML 文档与 $expectedFile 对应的 XML 文档不相同时报告错误,错误讯息由 $message 指定。

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

示例 1.64 assertXmlStringEqualsXmlFile() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class XmlStringEqualsXmlFileTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertXmlStringEqualsXmlFile(
  8. '/home/sb/expected.xml', '<foo><baz/></foo>');
  9. }
  10. }
  1. $ phpunit XmlStringEqualsXmlFileTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.25Mb
  5. There was 1 failure:
  6. 1) XmlStringEqualsXmlFileTest::testFailure
  7. Failed asserting that two DOM documents are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. <?xml version="1.0"?>
  12. <foo>
  13. - <bar/>
  14. + <baz/>
  15. </foo>
  16. /home/sb/XmlStringEqualsXmlFileTest.php:7
  17. FAILURES!
  18. Tests: 1, Assertions: 2, Failures: 1.

assertXmlStringEqualsXmlString()

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

$actualXml 对应的 XML 文档与 $expectedXml 对应的 XML 文档不相同时报告错误,错误讯息由 $message 指定。

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

示例 1.65 assertXmlStringEqualsXmlString() 的用法

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class XmlStringEqualsXmlStringTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertXmlStringEqualsXmlString(
  8. '<foo><bar/></foo>', '<foo><baz/></foo>');
  9. }
  10. }
  1. $ phpunit XmlStringEqualsXmlStringTest
  2. PHPUnit latest.0 by Sebastian Bergmann and contributors.
  3. F
  4. Time: 0 seconds, Memory: 5.00Mb
  5. There was 1 failure:
  6. 1) XmlStringEqualsXmlStringTest::testFailure
  7. Failed asserting that two DOM documents are equal.
  8. --- Expected
  9. +++ Actual
  10. @@ @@
  11. <?xml version="1.0"?>
  12. <foo>
  13. - <bar/>
  14. + <baz/>
  15. </foo>
  16. /home/sb/XmlStringEqualsXmlStringTest.php:7
  17. FAILURES!
  18. Tests: 1, Assertions: 1, Failures: 1.