PHPUnit-附录 A. 断言 (assert)
【http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html】
本附录列举可用的各种断言方法。
assertArrayHasKey()
assertArrayHasKey(mixed $key, array $array[, string $message = ''])
当 $array
不包含 $key
时报告错误,错误讯息由 $message
指定。
assertArrayNotHasKey()
是与之相反的断言,接受相同的参数。
例 A.1: assertArrayHasKey() 的用法
<?php
use PHPUnit\Framework\TestCase; class ArrayHasKeyTest extends TestCase
{
public function testFailure()
{
$this->assertArrayHasKey('foo', ['bar' => 'baz']);
}
}
?>
phpunit ArrayHasKeyTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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
use PHPUnit\Framework\TestCase; class ClassHasAttributeTest extends TestCase
{
public function testFailure()
{
$this->assertClassHasAttribute('foo', stdClass::class);
}
}
?>
phpunit ClassHasAttributeTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertArraySubset()
assertArraySubset(array $subset, array $array[, bool $strict = '', string $message = ''])
当 $array
不包含 $subset
时报告错误,错误讯息由 $message
指定。
$strict
是一个标志,用于表明是否需要对数组中的对象进行全等判定。
例 A.3: assertArraySubset() 的用法
<?php
use PHPUnit\Framework\TestCase; class ArraySubsetTest extends TestCase
{
public function testFailure()
{
$this->assertArraySubset(['config' => ['key-a', 'key-b']], ['config' => ['key-a']]);
}
}
?>
phpunit ArrayHasKeyTest
PHPUnit 4.4.0 by Sebastian Bergmann. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) Epilog\EpilogTest::testNoFollowOption
Failed asserting that an array has the subset Array &0 (
'config' => Array &1 (
0 => 'key-a'
1 => 'key-b'
)
). /home/sb/ArraySubsetTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertClassHasStaticAttribute()
assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = ''])
当 $className::attributeName
不存在时报告错误,错误讯息由 $message
指定。
assertClassNotHasStaticAttribute()
是与之相反的断言,接受相同的参数。
例 A.4: assertClassHasStaticAttribute() 的用法
<?php
use PHPUnit\Framework\TestCase; class ClassHasStaticAttributeTest extends TestCase
{
public function testFailure()
{
$this->assertClassHasStaticAttribute('foo', stdClass::class);
}
}
?>
phpunit ClassHasStaticAttributeTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 wrapper),以某个类或对象的 public
、protected
或 private
属性为搜索范围。
例 A.5: assertContains() 的用法
<?php
use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase
{
public function testFailure()
{
$this->assertContains(4, [1, 2, 3]);
}
}
?>
phpunit ContainsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 = '', boolean $ignoreCase = false])
当 $needle
不是 $haystack
的子字符串时报告错误,错误讯息由 $message
指定。
如果 $ignoreCase
为 true
,测试将按大小写不敏感的方式进行。
例 A.6: assertContains() 的用法
<?php
use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase
{
public function testFailure()
{
$this->assertContains('baz', 'foobar');
}
}
?>
phpunit ContainsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
例 A.7: 带有 $ignoreCase 参数的 assertContains() 的用法
<?php
use PHPUnit\Framework\TestCase; class ContainsTest extends TestCase
{
public function testFailure()
{
$this->assertContains('foo', 'FooBar');
} public function testOK()
{
$this->assertContains('foo', 'FooBar', '', true);
}
}
?>
phpunit ContainsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F. Time: 0 seconds, Memory: 2.75Mb There was 1 failure: 1) ContainsTest::testFailure
Failed asserting that 'FooBar' contains "foo". /home/sb/ContainsTest.php:6 FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
assertContainsOnly()
assertContainsOnly(string $type, Iterator|array $haystack[, boolean $isNativeType = null, string $message = ''])
当 $haystack
并非仅包含类型为 $type
的变量时报告错误,错误讯息由 $message
指定。
$isNativeType
是一个标志,用来表明 $type
是否是原生 PHP 类型。
assertNotContainsOnly()
是与之相反的断言,并接受相同的参数。
assertAttributeContainsOnly()
和 assertAttributeNotContainsOnly()
是便捷包装(convenience wrapper),以某个类或对象的 public
、protected
或 private
属性为搜索范围。
例 A.8: assertContainsOnly() 的用法
<?php
use PHPUnit\Framework\TestCase; class ContainsOnlyTest extends TestCase
{
public function testFailure()
{
$this->assertContainsOnly('string', ['1', '2', 3]);
}
}
?>
phpunit ContainsOnlyTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.9: assertContainsOnlyInstancesOf() 的用法
<?php
use PHPUnit\Framework\TestCase; class ContainsOnlyInstancesOfTest extends TestCase
{
public function testFailure()
{
$this->assertContainsOnlyInstancesOf(
Foo::class,
[new Foo, new Bar, new Foo]
);
}
}
?>
phpunit ContainsOnlyInstancesOfTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.10: assertCount() 的用法
<?php
use PHPUnit\Framework\TestCase; class CountTest extends TestCase
{
public function testFailure()
{
$this->assertCount(0, ['foo']);
}
}
?>
phpunit CountTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertDirectoryExists()
assertDirectoryExists(string $directory[, string $message = ''])
当 $directory
所指定的目录不存在时报告错误,错误讯息由 $message
指定。
assertDirectoryNotExists()
是与之相反的断言,并接受相同的参数。
例 A.11: assertDirectoryExists() 的用法
<?php
use PHPUnit\Framework\TestCase; class DirectoryExistsTest extends TestCase
{
public function testFailure()
{
$this->assertDirectoryExists('/path/to/directory');
}
}
?>
phpunit DirectoryExistsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryExistsTest::testFailure
Failed asserting that directory "/path/to/directory" exists. /home/sb/DirectoryExistsTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertDirectoryIsReadable()
assertDirectoryIsReadable(string $directory[, string $message = ''])
当 $directory
所指定的目录不是个目录或不可读时报告错误,错误讯息由 $message
指定。
assertDirectoryNotIsReadable()
是与之相反的断言,并接受相同的参数。
例 A.12: assertDirectoryIsReadable() 的用法
<?php
use PHPUnit\Framework\TestCase; class DirectoryIsReadableTest extends TestCase
{
public function testFailure()
{
$this->assertDirectoryIsReadable('/path/to/directory');
}
}
?>
phpunit DirectoryIsReadableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryIsReadableTest::testFailure
Failed asserting that "/path/to/directory" is readable. /home/sb/DirectoryIsReadableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertDirectoryIsWritable()
assertDirectoryIsWritable(string $directory[, string $message = ''])
当 $directory
所指定的目录不是个目录或不可写时报告错误,错误讯息由 $message
指定。
assertDirectoryNotIsWritable()
是与之相反的断言,并接受相同的参数。
例 A.13: assertDirectoryIsWritable() 的用法
<?php
use PHPUnit\Framework\TestCase; class DirectoryIsWritableTest extends TestCase
{
public function testFailure()
{
$this->assertDirectoryIsWritable('/path/to/directory');
}
}
?>
phpunit DirectoryIsWritableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) DirectoryIsWritableTest::testFailure
Failed asserting that "/path/to/directory" is writable. /home/sb/DirectoryIsWritableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertEmpty()
assertEmpty(mixed $actual[, string $message = ''])
当 $actual
非空时报告错误,错误讯息由 $message
指定。
assertNotEmpty()
是与之相反的断言,接受相同的参数。
assertAttributeEmpty()
和 assertAttributeNotEmpty()
是便捷包装(convenience wrapper),可以应用于某个类或对象的某个 public
、protected
或 private
属性。
例 A.14: assertEmpty() 的用法
<?php
use PHPUnit\Framework\TestCase; class EmptyTest extends TestCase
{
public function testFailure()
{
$this->assertEmpty(['foo']);
}
}
?>
phpunit EmptyTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.15: assertEqualXMLStructure() 的用法
<?php
use PHPUnit\Framework\TestCase; class EqualXMLStructureTest extends TestCase
{
public function testFailureWithDifferentNodeNames()
{
$expected = new DOMElement('foo');
$actual = new DOMElement('bar'); $this->assertEqualXMLStructure($expected, $actual);
} public function testFailureWithDifferentNodeAttributes()
{
$expected = new DOMDocument;
$expected->loadXML('<foo bar="true" />'); $actual = new DOMDocument;
$actual->loadXML('<foo/>'); $this->assertEqualXMLStructure(
$expected->firstChild, $actual->firstChild, true
);
} public function testFailureWithDifferentChildrenCount()
{
$expected = new DOMDocument;
$expected->loadXML('<foo><bar/><bar/><bar/></foo>'); $actual = new DOMDocument;
$actual->loadXML('<foo><bar/></foo>'); $this->assertEqualXMLStructure(
$expected->firstChild, $actual->firstChild
);
} public function testFailureWithDifferentChildren()
{
$expected = new DOMDocument;
$expected->loadXML('<foo><bar/><bar/><bar/></foo>'); $actual = new DOMDocument;
$actual->loadXML('<foo><baz/><baz/><baz/></foo>'); $this->assertEqualXMLStructure(
$expected->firstChild, $actual->firstChild
);
}
}
?>
phpunit EqualXMLStructureTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.16: assertEquals() 的用法
<?php
use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase
{
public function testFailure()
{
$this->assertEquals(1, 0);
} public function testFailure2()
{
$this->assertEquals('bar', 'baz');
} public function testFailure3()
{
$this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");
}
}
?>
phpunit EqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.17: 将assertEquals()用于浮点数时的用法
<?php
use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase
{
public function testSuccess()
{
$this->assertEquals(1.0, 1.1, '', 0.2);
} public function testFailure()
{
$this->assertEquals(1.0, 1.1);
}
}
?>
phpunit EqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. .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.18: assertEquals()应用于 DOMDocument 对象时的用法
<?php
use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase
{
public function testFailure()
{
$expected = new DOMDocument;
$expected->loadXML('<foo><bar/></foo>'); $actual = new DOMDocument;
$actual->loadXML('<bar><foo/></bar>'); $this->assertEquals($expected, $actual);
}
}
?>
phpunit EqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.19: assertEquals()应用于对象时的用法
<?php
use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase
{
public function testFailure()
{
$expected = new stdClass;
$expected->foo = 'foo';
$expected->bar = 'bar'; $actual = new stdClass;
$actual->foo = 'bar';
$actual->baz = 'bar'; $this->assertEquals($expected, $actual);
}
}
?>
phpunit EqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.20: assertEquals() 应用于数组时的用法
<?php
use PHPUnit\Framework\TestCase; class EqualsTest extends TestCase
{
public function testFailure()
{
$this->assertEquals(['a', 'b', 'c'], ['a', 'c', 'd']);
}
}
?>
phpunit EqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 = ''])
当 $condition
为 true
时报告错误,错误讯息由 $message
指定。
assertNotFalse()
是与之相反的断言,接受相同的参数。
例 A.21: assertFalse() 的用法
<?php
use PHPUnit\Framework\TestCase; class FalseTest extends TestCase
{
public function testFailure()
{
$this->assertFalse(true);
}
}
?>
phpunit FalseTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.22: assertFileEquals() 的用法
<?php
use PHPUnit\Framework\TestCase; class FileEqualsTest extends TestCase
{
public function testFailure()
{
$this->assertFileEquals('/home/sb/expected', '/home/sb/actual');
}
}
?>
phpunit FileEqualsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.23: assertFileExists() 的用法
<?php
use PHPUnit\Framework\TestCase; class FileExistsTest extends TestCase
{
public function testFailure()
{
$this->assertFileExists('/path/to/file');
}
}
?>
phpunit FileExistsTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertFileIsReadable()
assertFileIsReadable(string $filename[, string $message = ''])
当 $filename
所指定的文件不是个文件或不可读时报告错误,错误讯息由 $message
指定。
assertFileNotIsReadable()
是与之相反的断言,并接受相同的参数。
例 A.24: assertFileIsReadable() 的用法
<?php
use PHPUnit\Framework\TestCase; class FileIsReadableTest extends TestCase
{
public function testFailure()
{
$this->assertFileIsReadable('/path/to/file');
}
}
?>
phpunit FileIsReadableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) FileIsReadableTest::testFailure
Failed asserting that "/path/to/file" is readable. /home/sb/FileIsReadableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertFileIsWritable()
assertFileIsWritable(string $filename[, string $message = ''])
当 $filename
所指定的文件不是个文件或不可写时报告错误,错误讯息由 $message
指定。
assertFileNotIsWritable()
是与之相反的断言,并接受相同的参数。
例 A.25: assertFileIsWritable() 的用法
<?php
use PHPUnit\Framework\TestCase; class FileIsWritableTest extends TestCase
{
public function testFailure()
{
$this->assertFileIsWritable('/path/to/file');
}
}
?>
phpunit FileIsWritableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) FileIsWritableTest::testFailure
Failed asserting that "/path/to/file" is writable. /home/sb/FileIsWritableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertGreaterThan()
assertGreaterThan(mixed $expected, mixed $actual[, string $message = ''])
当 $actual
的值不大于 $expected
的值时报告错误,错误讯息由 $message
指定。
assertAttributeGreaterThan()
是便捷包装(convenience wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.26: assertGreaterThan() 的用法
<?php
use PHPUnit\Framework\TestCase; class GreaterThanTest extends TestCase
{
public function testFailure()
{
$this->assertGreaterThan(2, 1);
}
}
?>
phpunit GreaterThanTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.27: assertGreaterThanOrEqual() 的用法
<?php
use PHPUnit\Framework\TestCase; class GreatThanOrEqualTest extends TestCase
{
public function testFailure()
{
$this->assertGreaterThanOrEqual(2, 1);
}
}
?>
phpunit GreaterThanOrEqualTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertInfinite()
assertInfinite(mixed $variable[, string $message = ''])
当 $actual
不是 INF
时报告错误,错误讯息由 $message
指定。
assertFinite()
是与之相反的断言,接受相同的参数。
例 A.28: assertInfinite() 的用法
<?php
use PHPUnit\Framework\TestCase; class InfiniteTest extends TestCase
{
public function testFailure()
{
$this->assertInfinite(1);
}
}
?>
phpunit InfiniteTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) InfiniteTest::testFailure
Failed asserting that 1 is infinite. /home/sb/InfiniteTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertInstanceOf()
assertInstanceOf($expected, $actual[, $message = ''])
当 $actual
不是 $expected
的实例时报告错误,错误讯息由 $message
指定。
assertNotInstanceOf()
是与之相反的断言,接受相同的参数。
assertAttributeInstanceOf()
和 assertAttributeNotInstanceOf()
是便捷包装(convenience wrapper),可以应用于某个类或对象的某个 public
、protected
或 private
属性。
例 A.29: assertInstanceOf() 的用法
<?php
use PHPUnit\Framework\TestCase; class InstanceOfTest extends TestCase
{
public function testFailure()
{
$this->assertInstanceOf(RuntimeException::class, new Exception);
}
}
?>
phpunit InstanceOfTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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()
和 assertAttributeNotInternalType()
是便捷包装(convenience wrapper),可以应用于某个类或对象的某个 public
、protected
或 private
属性。
例 A.30: assertInternalType() 的用法
<?php
use PHPUnit\Framework\TestCase; class InternalTypeTest extends TestCase
{
public function testFailure()
{
$this->assertInternalType('string', 42);
}
}
?>
phpunit InternalTypeTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertIsReadable()
assertIsReadable(string $filename[, string $message = ''])
当 $filename
所指定的文件或目录不可读时报告错误,错误讯息由 $message
指定。
assertNotIsReadable()
是与之相反的断言,并接受相同的参数。
例 A.31: assertIsReadable() 的用法
<?php
use PHPUnit\Framework\TestCase; class IsReadableTest extends TestCase
{
public function testFailure()
{
$this->assertIsReadable('/path/to/unreadable');
}
}
?>
phpunit IsReadableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) IsReadableTest::testFailure
Failed asserting that "/path/to/unreadable" is readable. /home/sb/IsReadableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertIsWritable()
assertIsWritable(string $filename[, string $message = ''])
当 $filename
所指定的文件或目录不可写时报告错误,错误讯息由 $message
指定。
assertNotIsWritable()
是与之相反的断言,并接受相同的参数。
例 A.32: assertIsWritable() 的用法
<?php
use PHPUnit\Framework\TestCase; class IsWritableTest extends TestCase
{
public function testFailure()
{
$this->assertIsWritable('/path/to/unwritable');
}
}
?>
phpunit IsWritableTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 4.75Mb There was 1 failure: 1) IsWritableTest::testFailure
Failed asserting that "/path/to/unwritable" is writable. /home/sb/IsWritableTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertJsonFileEqualsJsonFile()
assertJsonFileEqualsJsonFile(mixed $expectedFile, mixed $actualFile[, string $message = ''])
当 $actualFile
对应的值与 $expectedFile
对应的值不匹配时报告错误,错误讯息由 $message
指定。
例 A.33: assertJsonFileEqualsJsonFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class JsonFileEqualsJsonFileTest extends TestCase
{
public function testFailure()
{
$this->assertJsonFileEqualsJsonFile(
'path/to/fixture/file', 'path/to/actual/file');
}
}
?>
phpunit JsonFileEqualsJsonFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) JsonFileEqualsJsonFile::testFailure
Failed asserting that '{"Mascot":"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.34: assertJsonStringEqualsJsonFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonFileTest extends TestCase
{
public function testFailure()
{
$this->assertJsonStringEqualsJsonFile(
'path/to/fixture/file', json_encode(['Mascot' => 'ux'])
);
}
}
?>
phpunit JsonStringEqualsJsonFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) JsonStringEqualsJsonFile::testFailure
Failed asserting that '{"Mascot":"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.35: assertJsonStringEqualsJsonString() 的用法
<?php
use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonStringTest extends TestCase
{
public function testFailure()
{
$this->assertJsonStringEqualsJsonString(
json_encode(['Mascot' => 'Tux']),
json_encode(['Mascot' => 'ux'])
);
}
}
?>
phpunit JsonStringEqualsJsonStringTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 (
- 'Mascot' => 'Tux'
+ 'Mascot' => '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 wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.36: assertLessThan() 的用法
<?php
use PHPUnit\Framework\TestCase; class LessThanTest extends TestCase
{
public function testFailure()
{
$this->assertLessThan(1, 2);
}
}
?>
phpunit LessThanTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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 wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.37: assertLessThanOrEqual() 的用法
<?php
use PHPUnit\Framework\TestCase; class LessThanOrEqualTest extends TestCase
{
public function testFailure()
{
$this->assertLessThanOrEqual(1, 2);
}
}
?>
phpunit LessThanOrEqualTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertNan()
assertNan(mixed $variable[, string $message = ''])
当 $variable
不是 NAN
时报告错误,错误讯息由 $message
指定。
例 A.38: assertNan() 的用法
<?php
use PHPUnit\Framework\TestCase; class NanTest extends TestCase
{
public function testFailure()
{
$this->assertNan(1);
}
}
?>
phpunit NanTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) NanTest::testFailure
Failed asserting that 1 is nan. /home/sb/NanTest.php:6 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
assertNull()
assertNull(mixed $variable[, string $message = ''])
当 $actual
不是 null
时报告错误,错误讯息由 $message
指定。
assertNotNull()
是与之相反的断言,接受相同的参数。
例 A.39: assertNull() 的使用
<?php
use PHPUnit\Framework\TestCase; class NullTest extends TestCase
{
public function testFailure()
{
$this->assertNull('foo');
}
}
?>
phpunit NotNullTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.40: assertObjectHasAttribute() 的用法
<?php
use PHPUnit\Framework\TestCase; class ObjectHasAttributeTest extends TestCase
{
public function testFailure()
{
$this->assertObjectHasAttribute('foo', new stdClass);
}
}
?>
phpunit ObjectHasAttributeTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.41: assertRegExp() 的用法
<?php
use PHPUnit\Framework\TestCase; class RegExpTest extends TestCase
{
public function testFailure()
{
$this->assertRegExp('/foo/', 'bar');
}
}
?>
phpunit RegExpTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.42: assertStringMatchesFormat() 的用法
<?php
use PHPUnit\Framework\TestCase; class StringMatchesFormatTest extends TestCase
{
public function testFailure()
{
$this->assertStringMatchesFormat('%i', 'foo');
}
}
?>
phpunit StringMatchesFormatTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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-9
、a-f
、A-F
。%f
:浮点数,例如3.142
、-3.142
、3.142E-10
、3.142e+10
。%c
:单个任意字符。
assertStringMatchesFormatFile()
assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = ''])
当 $string
不匹配于 $formatFile
的内容所定义的格式时报告错误,错误讯息由 $message
指定。
assertStringNotMatchesFormatFile()
是与之相反的断言,接受相同的参数。
例 A.43: assertStringMatchesFormatFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class StringMatchesFormatFileTest extends TestCase
{
public function testFailure()
{
$this->assertStringMatchesFormatFile('/path/to/expected.txt', 'foo');
}
}
?>
phpunit StringMatchesFormatFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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()
和 assertAttributeNotSame()
是便捷包装(convenience wrapper),以某个类或对象的某个 public
、protected
或 private
属性作为实际值来进行比较。
例 A.44: assertSame() 的用法
<?php
use PHPUnit\Framework\TestCase; class SameTest extends TestCase
{
public function testFailure()
{
$this->assertSame('2204', 2204);
}
}
?>
phpunit SameTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.45: assertSame() 应用于对象时的用法
<?php
use PHPUnit\Framework\TestCase; class SameTest extends TestCase
{
public function testFailure()
{
$this->assertSame(new stdClass, new stdClass);
}
}
?>
phpunit SameTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertStringEndsWith()
assertStringEndsWith(string $suffix, string $string[, string $message = ''])
当 $string
不以 $suffix
结尾时报告错误,错误讯息由 $message
指定。
assertStringEndsNotWith()
是与之相反的断言,接受相同的参数。
例 A.46: assertStringEndsWith() 的用法
<?php
use PHPUnit\Framework\TestCase; class StringEndsWithTest extends TestCase
{
public function testFailure()
{
$this->assertStringEndsWith('suffix', 'foo');
}
}
?>
phpunit StringEndsWithTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.47: assertStringEqualsFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class StringEqualsFileTest extends TestCase
{
public function testFailure()
{
$this->assertStringEqualsFile('/home/sb/expected', 'actual');
}
}
?>
phpunit StringEqualsFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.48: assertStringStartsWith() 的用法
<?php
use PHPUnit\Framework\TestCase; class StringStartsWithTest extends TestCase
{
public function testFailure()
{
$this->assertStringStartsWith('prefix', 'foo');
}
}
?>
phpunit StringStartsWithTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
assertThat()
可以用 PHPUnit_Framework_Constraint
类来订立更加复杂的断言。随后可以用 assertThat()
方法来评定这些断言。例 A.49 展示了如何用 logicalNot()
和 equalTo()
约束条件来表达与 assertNotEquals()
等价的断言。
assertThat(mixed $value, PHPUnit_Framework_Constraint $constraint[, $message = ''])
当 $value
不符合约束条件 $constraint
时报告错误,错误讯息由 $message
指定。
例 A.49: assertThat() 的用法
<?php
use PHPUnit\Framework\TestCase; class BiscuitTest extends TestCase
{
public function testEquals()
{
$theBiscuit = new Biscuit('Ginger');
$myBiscuit = new Biscuit('Ginger'); $this->assertThat(
$theBiscuit,
$this->logicalNot(
$this->equalTo($myBiscuit)
)
);
}
}
?>
表 A.1列举了所有可用的 PHPUnit_Framework_Constraint
类。
表 A.1. 约束条件
约束条件 | 含义 |
---|---|
PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName) |
此约束将另外一个约束应用于某个类或对象的某个属性。 |
PHPUnit_Framework_Constraint_IsAnything anything() |
此约束接受任意输入值。 |
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key) |
此约束断言所评定的数组拥有指定键名。 |
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象包含有给定值。 |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnly(string $type) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象仅包含给定类型的值。 |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnlyInstancesOf(string $classname) |
此约束断言所评定的 array 或实现了 Iterator 接口的对象仅包含给定类名的类的实例。 |
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10) |
此约束检验一个值是否等于另外一个。 |
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10) |
此约束检验一个值是否等于某个类或对象的某个属性。 |
PHPUnit_Framework_Constraint_DirectoryExists directoryExists() |
此约束检验所评定的目录是否存在。 |
PHPUnit_Framework_Constraint_FileExists fileExists() |
此约束检验所评定的文件名对应的文件是否存在。 |
PHPUnit_Framework_Constraint_IsReadable isReadable() |
此约束检验所评定的文件名对应的文件是否可读。 |
PHPUnit_Framework_Constraint_IsWritable isWritable() |
此约束检验所评定的文件名对应的文件是否可写。 |
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value) |
此约束断言所评定的值大于给定值。 |
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value) |
此约束断言所评定的值大于或等于给定值。 |
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName) |
此约束断言所评定的类具有给定属性。 |
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName) |
此约束断言所评定的类具有给定静态属性。 |
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName) |
此约束断言所评定的对象具有给定属性。 |
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value) |
此约束断言所评定的值与另外一个值全等。 |
PHPUnit_Framework_Constraint_IsFalse isFalse() |
此约束断言所评定的值为 false 。 |
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className) |
此约束断言所评定的对象是给定类的实例。 |
PHPUnit_Framework_Constraint_IsNull isNull() |
此约束断言所评定的值为 null 。 |
PHPUnit_Framework_Constraint_IsTrue isTrue() |
此约束断言所评定的值为 true 。 |
PHPUnit_Framework_Constraint_IsType isType(string $type) |
此约束断言所评定的值是指定类型的。 |
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value) |
此约束断言所评定的值小于给定值。 |
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value) |
此约束断言所评定的值小于或等于给定值。 |
logicalAnd() |
逻辑与(AND)。 |
logicalNot(PHPUnit_Framework_Constraint $constraint) |
逻辑非(NOT)。 |
logicalOr() |
逻辑或(OR)。 |
logicalXor() |
逻辑异或(XOR)。 |
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern) |
此约束断言所评定的字符串匹配于正则表达式。 |
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case) |
此约束断言所评定的字符串包含指定字符串。 |
PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix) |
此约束断言所评定的字符串以给定后缀结尾。 |
PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix) |
此约束断言所评定的字符串以给定前缀开头。 |
assertTrue()
assertTrue(bool $condition[, string $message = ''])
当 $condition
为 false
时报告错误,错误讯息由 $message
指定。
assertNotTrue()
是与之相反的断言,接受相同的参数。
例 A.50: assertTrue() 的用法
<?php
use PHPUnit\Framework\TestCase; class TrueTest extends TestCase
{
public function testFailure()
{
$this->assertTrue(false);
}
}
?>
phpunit TrueTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.51: assertXmlFileEqualsXmlFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class XmlFileEqualsXmlFileTest extends TestCase
{
public function testFailure()
{
$this->assertXmlFileEqualsXmlFile(
'/home/sb/expected.xml', '/home/sb/actual.xml');
}
}
?>
phpunit XmlFileEqualsXmlFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.52: assertXmlStringEqualsXmlFile() 的用法
<?php
use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlFileTest extends TestCase
{
public function testFailure()
{
$this->assertXmlStringEqualsXmlFile(
'/home/sb/expected.xml', '<foo><baz/></foo>');
}
}
?>
phpunit XmlStringEqualsXmlFileTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.53: assertXmlStringEqualsXmlString() 的用法
<?php
use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlStringTest extends TestCase
{
public function testFailure()
{
$this->assertXmlStringEqualsXmlString(
'<foo><bar/></foo>', '<foo><baz/></foo>');
}
}
?>
phpunit XmlStringEqualsXmlStringTest
PHPUnit 5.7.0 by Sebastian Bergmann and contributors. 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.
PHPUnit-附录 A. 断言 (assert)的更多相关文章
- 断言(assert)的用法
我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非“报错”. 在经过对其进行一定了解之后,对其作用及用法有了一定的了解,assert()的用法像是一种“契约式编程”,在我的理解 ...
- C语言中断言ASSERT
我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非"报错". 在经过对其进行一定了解之后,对其作用及用法有了一定的了解,assert()的用法像是一种&qu ...
- 使用断言assert
之前有看过关于Assert的书,但是不懂得如何去用,最近看别人写的代码有用这个断言(assert),今天自己动手看看如何使用断言. 断言(assert)的语义如下:如果表达式的值为0(假),则输出错误 ...
- 断言Assert的使用
转载地址:http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数,事实上,它居然是个宏 ...
- K:java 断言 assert 初步使用:断言开启、断言使用
@转自天地悠悠的个人博客 主要总结一下在eclipse中如何使用断言. (一)首先明确: java断言Assert是jdk1.4引入的. jvm 断言默认是关闭的. 断言是可以局部开启的,如:父类禁止 ...
- java的断言(assert)
概述 在C和C++语言中都有assert关键,表示断言.在Java中,同样也有assert关键字,表示断言,用法和含义都差不多.在Java中,assert关键字是从JAVA SE 1.4 引入的,为了 ...
- C# 断言 Assert
重构-断言 现象:某一段代码需要对程序状态做出某种假设 做法:以断言明确表现这种假设 动机: 常常有这种一段代码:只有某个条件为真是,该改名才能正常运行. 通常假设这样的假设并没有代码中明确表现出来, ...
- JavaScript之调试工具之断言assert
1.单点断言 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- 断言(assert)
断言(assert):用来调试.测试代码. 格式: assert 布尔表达式: 字符串 (如果布尔表达式为false时,这个字符串才会显示). 注意: assert默认是关闭的,使用时需要使用&quo ...
- java断言assert初步使用:断言开启、断言使用
1 说明 java断言assert是jdk1.4引入的. jvm断言默认是关闭的. 断言可以局部开启的,如:父类禁止断言,而子类开启断言,所以一般说“断言不具有继承性”. 断言只适用复杂的调式过程. ...
随机推荐
- [转载]织梦CMS首页调用分类信息栏目及列表方法
原文地址:织梦CMS首页调用分类信息栏目及列表方法作者:小武哥 不懂代码,搜索学习一晚上,都是说调用特定栏目分类信息列表的,用这个代码 {dede:arclistsg row='10' titlele ...
- Core Graphics 和Quartz 2D的区别
quartz是一个通用的术语,用于描述在IOS和MAC OS X中整个媒体层用到的多种技术 包括图形.动画.音频.适配. Quart 2D 是一组二位绘图和渲染API,Core Graphic会使用 ...
- Cannot complete the install because one or more required items could not be found
弄了一天的subclipse也没装上,郁闷~~~~~~~~ 无论采用本地安装还是站点安装都不行,在安装的时候显示错误: Cannot complete the install because one ...
- "Cache-control”常见的取值private、no-cache、max-age、must-revalidate及其用意
http://www.cnblogs.com/kaima/archive/2009/10/13/1582337.html 网页的缓存是由HTTP消息头中的"Cache-control&quo ...
- Redis持久化磁盘IO方式及其带来的问题 有Redis线上运维经验的人会发现Redis在物理内存使用比较多,但还没有超过实际物理内存总容量时就会发生不稳定甚至崩溃的问题,有人认为是基于快照方式持
转自:http://blog.csdn.net/kaosini/article/details/9176961 一.对Redis持久化的探讨与理解 redis是一个支持持久化的内存数据库,也就是 ...
- java —— equals 与 ==
equals 众所周知,java 中的所有的类都继承自 Object 这个超类 ,他就是Java所有类的父类或祖先类,Object类里面有一个equals方法,并且提供了默认的实现,如下所示. pub ...
- cursor的moveToNext()与moveToFirst()
String sql = "select count(*) as c from sqlite_master where type ='table' and name ='" + t ...
- yum升级mysql
已安装mysql升级 升级mysql到5.6:下载源wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm安装源:rpm ...
- Python字符串详解
字符串 作用: 名字,性别,国籍,地址等描述信息 定义: 在单引号.双引号.三引号内,由一串字符组成 优先掌握的操作: 按索引取值(正向取+反向取):只能取 切片(顾头不顾尾,步长) 长度len 成员 ...
- tomcat部署最佳实践(一)
Tomcat部署最佳实践 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 tomcat是玩web软件必会技能之一,今天我给大家介绍一下tomc ...