Assertions
断言
本附录列出了可用的各种声明方法。
声明方法的静态与非静态使用
PHPUnit 的声明在PHPUnit\Framework\Assert
上被执行。PHPUnit\Framework\TestCase
继承了PHPUnit\Framework\Assert
。
声明方法是经过声明的static和可以从任何环境中调用PHPUnit\Framework\Assert::assertTrue(),例如,使用$this->assertTrue()或者self::assertTrue(),例如,在扩展PHPUnit\Framework\TestCase的类中。
实际上,当你(手动)包含 PHPUnit 附带的src/Framework/Assert/Functions.php
源代码文件时,甚至可以assertTrue()
在任何上下文中使用全局函数包装器(包括扩展的类PHPUnit\Framework\TestCase
)。
一个常见的问题,特别是来自 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()
是这个声明的反面,并采用相同的论点。
例 A.1:使用 assertArrayHasKey()
<?php
use PHPUnit\Framework\TestCase;
class ArrayHasKeyTest extends TestCase
{
public function testFailure()
{
$this->assertArrayHasKey('foo', ['bar' => 'baz']
}
}
?>
phpunit ArrayHasKeyTest
PHPUnit 6.4.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 6.4.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 6.4.0 by Sebastian Bergmann and contributors.
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 6.4.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()
是更便利的包装,他们使用一个类或对象作为haystack 的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 6.4.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 6.4.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 6.4.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()
是更便利的包装,他们使用一个类或对象作为 haystack 的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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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()
是更便利的包装,他们可以应用到一个类或对象的public
,protected
或private
属性。
例 A.14:assertEmpty()的使用
<?php
use PHPUnit\Framework\TestCase;
class EmptyTest extends TestCase
{
public function testFailure()
{
$this->assertEmpty(['foo']
}
}
?>
phpunit EmptyTest
PHPUnit 6.4.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 6.4.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()
是一个更便利的包装,他们使用一个类或对象的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 6.4.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()和float
<?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 6.4.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 = ''])
如果 XML 文档的取消注释的规范形式由两个DOM文档表示$expected
和$actual
的对象不相等,报告通过$message
确认的错误。
例 A.18:在DOMDocument对象中使用assertEquals()
<?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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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()
是一个便利的包装,它使用一个类或对象作为实际值的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 6.4.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()
是一个便利的包装,使用一个类或对象作为实际值的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 6.4.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 = ''])
如果$variable
不是INF
,报告由$message
标识的错误。
assertFinite()
是这个声明的反面,并采用相同的论点。
例 A.28:assertInfinite()的使用
<?php
use PHPUnit\Framework\TestCase;
class InfiniteTest extends TestCase
{
public function testFailure()
{
$this->assertInfinite(1
}
}
?>
phpunit InfiniteTest
PHPUnit 6.4.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()
是可以更便利的包装,应用到一个类或对象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 6.4.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()
是可以更便利的包装,应用到一个类或对象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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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()
是一个便利的包装,使用一个类或对象作为实际值的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 6.4.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()
是一个便利的包装,使用一个类或对象作为实际值的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 6.4.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 6.4.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 = ''])
如果$variable
不是null
,报告由$message
标识的错误。
assertNotNull()
是这个声明的反面,并采用相同的论点。
例 A.39:assertNull()的使用
<?php
use PHPUnit\Framework\TestCase;
class NullTest extends TestCase
{
public function testFailure()
{
$this->assertNull('foo'
}
}
?>
phpunit NotNullTest
PHPUnit 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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()
是一个更便利的包装,使用类或对象的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 6.4.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 6.4.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 6.4.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 6.4.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 6.4.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) | Constraint that applies another constraint to an attribute of a class or an object. |
PHPUnit_Framework_Constraint_IsAnything anything() | Constraint that accepts any input value. |
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key) | Constraint that asserts that the array it is evaluated for has a given key. |
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value) | Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains a given value. |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnly(string $type) | Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains only values of a given type. |
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnlyInstancesOf(string $classname) | Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains only instances of a given classname. |
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10) | Constraint that checks if one value is equal to another. |
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10) | Constraint that checks if a value is equal to an attribute of a class or of an object. |
PHPUnit_Framework_Constraint_DirectoryExists directoryExists() | Constraint that checks if the directory that it is evaluated for exists. |
PHPUnit_Framework_Constraint_FileExists fileExists() | Constraint that checks if the file(name) that it is evaluated for exists. |
PHPUnit_Framework_Constraint_IsReadable isReadable() | Constraint that checks if the file(name) that it is evaluated for is readable. |
PHPUnit_Framework_Constraint_IsWritable isWritable() | Constraint that checks if the file(name) that it is evaluated for is writable. |
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value) | Constraint that asserts that the value it is evaluated for is greater than a given value. |
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value) | Constraint that asserts that the value it is evaluated for is greater than or equal to a given value. |
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName) | Constraint that asserts that the class it is evaluated for has a given attribute. |
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName) | Constraint that asserts that the class it is evaluated for has a given static attribute. |
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName) | Constraint that asserts that the object it is evaluated for has a given attribute. |
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value) | Constraint that asserts that one value is identical to another. |
PHPUnit_Framework_Constraint_IsFalse isFalse() | Constraint that asserts that the value it is evaluated is false. |
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className) | Constraint that asserts that the object it is evaluated for is an instance of a given class. |
PHPUnit_Framework_Constraint_IsNull isNull() | Constraint that asserts that the value it is evaluated is null. |
PHPUnit_Framework_Constraint_IsTrue isTrue() | Constraint that asserts that the value it is evaluated is true. |
PHPUnit_Framework_Constraint_IsType isType(string $type) | Constraint that asserts that the value it is evaluated for is of a specified type. |
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value) | Constraint that asserts that the value it is evaluated for is smaller than a given value. |
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value) | Constraint that asserts that the value it is evaluated for is smaller than or equal to a given value. |
logicalAnd() | Logical AND. |
logicalNot(PHPUnit_Framework_Constraint $constraint) | Logical NOT. |
logicalOr() | Logical OR. |
logicalXor() | Logical XOR. |
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern) | Constraint that asserts that the string it is evaluated for matches a regular expression. |
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case) | Constraint that asserts that the string it is evaluated for contains a given string. |
PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix) | Constraint that asserts that the string it is evaluated for ends with a given suffix. |
PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix) | Constraint that asserts that the string it is evaluated for starts with a given 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 6.4.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 6.4.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 6.4.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 6.4.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.