1. Assertions

    PHPUnit’s assertions are implemented in . PHPUnit\Framework\TestCase inherits from PHPUnit\Framework\Assert.

    The assertion methods are declared static and can be invoked from any context using PHPUnit\Framework\Assert::assertTrue(), for instance, or using $this->assertTrue() or self::assertTrue(), for instance, in a class that extends PHPUnit\Framework\TestCase. You can even use global function wrappers such as assertTrue().

    A common question, especially from developers new to PHPUnit, is whether using $this->assertTrue() or self::assertTrue(), for instance, is “the right way” to invoke an assertion. The short answer is: there is no right way. And there is no wrong way, either. It is a matter of personal preference.

    For most people it just “feels right” to use $this->assertTrue() because the test method is invoked on a test object. The fact that the assertion methods are declared static allows for (re)using them outside the scope of a test object. Lastly, the global function wrappers allow developers to type less characters (assertTrue() instead of $this->assertTrue() or self::assertTrue()).

    assertArrayHasKey()

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

    Reports an error identified by $message if $array does not have the $key.

    assertArrayNotHasKey() is the inverse of this assertion and takes the same arguments.

    Example 1.1 Usage of assertArrayHasKey()

    1. $ phpunit ArrayHasKeyTest
    2. PHPUnit 9.6.0 by Sebastian Bergmann and contributors.
    3. F
    4. Time: 0 seconds, Memory: 5.00Mb
    5. There was 1 failure:
    6. 1) ArrayHasKeyTest::testFailure
    7. Failed asserting that an array has the key 'foo'.
    8. /home/sb/ArrayHasKeyTest.php:6
    9. FAILURES!
    10. Tests: 1, Assertions: 1, Failures: 1.

    assertClassHasAttribute()

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

    Reports an error identified by $message if $className::attributeName does not exist.

    assertClassNotHasAttribute() is the inverse of this assertion and takes the same arguments.

    Example 1.2 Usage of assertClassHasAttribute()

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

    assertClassHasStaticAttribute()

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

    Reports an error identified by $message if $className::attributeName does not exist.

    assertClassNotHasStaticAttribute() is the inverse of this assertion and takes the same arguments.

    Example 1.3 Usage of assertClassHasStaticAttribute()

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

    assertContains()

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

    Reports an error identified by $message if $needle is not an element of $haystack.

    assertNotContains() is the inverse of this assertion and takes the same arguments.

    Example 1.4 Usage of assertContains()

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

    assertStringContainsString()

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

    Reports an error identified by $message if $needle is not a substring of $haystack.

    assertStringNotContainsString() is the inverse of this assertion and takes the same arguments.

    Example 1.5 Usage of assertStringContainsString()

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

    assertStringContainsStringIgnoringCase()

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

    Reports an error identified by $message if $needle is not a substring of $haystack.

    Differences in casing are ignored when $needle is searched for in $haystack. This also works for Unicode characters with diacritics (accents, umlauts, circumflex, etc.) as long as both strings have the same .

    assertStringNotContainsStringIgnoringCase() is the inverse of this assertion and takes the same arguments.

    Example 1.6 Usage of assertStringContainsStringIgnoringCase()

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

    assertContainsOnly()

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

    Reports an error identified by $message if $haystack does not contain only variables of type $type.

    $isNativeType is a flag used to indicate whether $type is a native PHP type or not.

    assertNotContainsOnly() is the inverse of this assertion and takes the same arguments.

    Example 1.7 Usage of assertContainsOnly()

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

    assertContainsOnlyInstancesOf()

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

    Reports an error identified by $message if $haystack does not contain only instances of class $classname.

    Example 1.8 Usage of assertContainsOnlyInstancesOf()

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

    assertCount()

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

    Reports an error identified by $message if the number of elements in $haystack is not $expectedCount.

    assertNotCount() is the inverse of this assertion and takes the same arguments.

    Example 1.9 Usage of assertCount()

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

    assertDirectoryExists()

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

    Reports an error identified by $message if the directory specified by $directory does not exist.

    assertDirectoryDoesNotExist() is the inverse of this assertion and takes the same arguments.

    Example 1.10 Usage of assertDirectoryExists()

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

    assertDirectoryIsReadable()

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

    Reports an error identified by $message if the directory specified by $directory is not a directory or is not readable.

    assertDirectoryIsNotReadable() is the inverse of this assertion and takes the same arguments.

    Example 1.11 Usage of assertDirectoryIsReadable()

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

    assertDirectoryIsWritable()

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

    Reports an error identified by $message if the directory specified by $directory is not a directory or is not writable.

    assertDirectoryIsNotWritable() is the inverse of this assertion and takes the same arguments.

    Example 1.12 Usage of assertDirectoryIsWritable()

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

    assertEmpty()

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

    Reports an error identified by $message if $actual is not empty.

    assertNotEmpty() is the inverse of this assertion and takes the same arguments.

    Example 1.13 Usage of assertEmpty()

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

    assertEquals()

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

    Reports an error identified by $message if the two variables $expected and $actual are not equal.

    assertNotEquals() is the inverse of this assertion and takes the same arguments.

    Example 1.14 Usage of assertEquals()

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

    More specialized comparisons are used for specific argument types for $expected and $actual, see below.

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

    Reports an error identified by $message if the uncommented canonical form of the XML documents represented by the two DOMDocument objects $expected and $actual are not equal.

    Example 1.15 Usage of assertEquals() with DOMDocument objects

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

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

    Reports an error identified by $message if the two objects $expected and $actual do not have equal attribute values.

    Example 1.16 Usage of assertEquals() with objects

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

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

    Reports an error identified by $message if the two arrays $expected and $actual are not equal.

    Example 1.17 Usage of assertEquals() with arrays

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

    assertEqualsCanonicalizing()

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

    Reports an error identified by $message if the two variables $expected and $actual are not equal.

    The contents of $expected and $actual are canonicalized before they are compared. For instance, when the two variables $expected and $actual are arrays, then these arrays are sorted before they are compared. When $expected and $actual are objects, each object is converted to an array containing all private, protected and public attributes.

    assertNotEqualsCanonicalizing() is the inverse of this assertion and takes the same arguments.

    Example 1.18 Usage of assertEqualsCanonicalizing()

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

    assertEqualsIgnoringCase()

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

    Reports an error identified by $message if the two variables $expected and $actual are not equal.

    Differences in casing are ignored for the comparison of $expected and $actual.

    assertNotEqualsIgnoringCase() is the inverse of this assertion and takes the same arguments.

    Example 1.19 Usage of assertEqualsIgnoringCase()

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

    assertEqualsWithDelta()

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

    Reports an error identified by $message if the absolute difference between $expected and $actual is greater than $delta.

    Please read “” to understand why $delta is necessary.

    assertNotEqualsWithDelta() is the inverse of this assertion and takes the same arguments.

    Example 1.20 Usage of assertEqualsWithDelta()

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

    assertObjectEquals()

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

    Reports an error identified by $message if $actual is not equal to $expected according to $actual->$method($expected).

    It is a bad practice to use assertEquals() (and its inverse, assertNotEquals()) on objects without registering a custom comparator that customizes how objects are compared. Unfortunately, though, implementing custom comparators for each and every object you want to assert in your tests is inconvenient at best.

    The most common use case for custom comparators are Value Objects. These objects usually have an equals(self $other): bool method (or a method just like that but with a different name) for comparing two instances of the Value Object’s type. assertObjectEquals() makes custom comparison of objects convenient for this common use case:

    Example 1.21 Usage of assertObjectEquals()

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

    Example 1.22 Email value object with equals() method

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

    Please note:

    • A method with name $method must exist on the $actual object

    • The method must accept exactly one argument

    • The respective parameter must have a declared type

    • The $expected object must be compatible with this declared type

    • The method must have a declared bool return type

    If any of the aforementioned assumptions is not fulfilled or if $actual->$method($expected) returns false then the assertion fails.

    assertFalse()

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

    Reports an error identified by $message if $condition is true.

    assertNotFalse() is the inverse of this assertion and takes the same arguments.

    Example 1.23 Usage of assertFalse()

    1. $ phpunit FalseTest
    2. PHPUnit 9.6.0 by Sebastian Bergmann and contributors.
    3. F
    4. Time: 0 seconds, Memory: 5.00Mb
    5. There was 1 failure:
    6. 1) FalseTest::testFailure
    7. Failed asserting that true is false.
    8. /home/sb/FalseTest.php:6
    9. FAILURES!
    10. Tests: 1, Assertions: 1, Failures: 1.

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

    Reports an error identified by $message if the file specified by $expected does not have the same contents as the file specified by $actual.

    assertFileNotEquals() is the inverse of this assertion and takes the same arguments.

    Example 1.24 Usage of assertFileEquals()

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

    assertFileExists()

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

    Reports an error identified by $message if the file specified by $filename does not exist.

    assertFileDoesNotExist() is the inverse of this assertion and takes the same arguments.

    Example 1.25 Usage of assertFileExists()

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

    assertFileIsReadable()

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

    Reports an error identified by $message if the file specified by $filename is not a file or is not readable.

    assertFileIsNotReadable() is the inverse of this assertion and takes the same arguments.

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

    assertFileIsWritable()

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

    Reports an error identified by $message if the file specified by $filename is not a file or is not writable.

    assertFileIsNotWritable() is the inverse of this assertion and takes the same arguments.

    Example 1.27 Usage of assertFileIsWritable()

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

    assertGreaterThan()

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

    Reports an error identified by $message if the value of $actual is not greater than the value of $expected.

    Example 1.28 Usage of assertGreaterThan()

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

    assertGreaterThanOrEqual()

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

    Reports an error identified by $message if the value of $actual is not greater than or equal to the value of $expected.

    Example 1.29 Usage of assertGreaterThanOrEqual()

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

    assertInfinite()

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

    Reports an error identified by $message if $variable is not INF.

    assertFinite() is the inverse of this assertion and takes the same arguments.

    Example 1.30 Usage of assertInfinite()

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

    assertInstanceOf()

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

    Reports an error identified by $message if $actual is not an instance of $expected.

    assertNotInstanceOf() is the inverse of this assertion and takes the same arguments.

    Example 1.31 Usage of assertInstanceOf()

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

    assertIsArray()

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

    Reports an error identified by $message if $actual is not of type array.

    assertIsNotArray() is the inverse of this assertion and takes the same arguments.

    Example 1.32 Usage of assertIsArray()

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

    assertIsBool()

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

    Reports an error identified by $message if $actual is not of type bool.

    assertIsNotBool() is the inverse of this assertion and takes the same arguments.

    Example 1.33 Usage of assertIsBool()

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

    assertIsCallable()

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

    Reports an error identified by $message if $actual is not of type callable.

    assertIsNotCallable() is the inverse of this assertion and takes the same arguments.

    Example 1.34 Usage of assertIsCallable()

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

    assertIsFloat()

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

    Reports an error identified by $message if $actual is not of type float.

    assertIsNotFloat() is the inverse of this assertion and takes the same arguments.

    Example 1.35 Usage of assertIsFloat()

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

    assertIsInt()

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

    Reports an error identified by $message if $actual is not of type int.

    assertIsNotInt() is the inverse of this assertion and takes the same arguments.

    Example 1.36 Usage of assertIsInt()

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

    assertIsIterable()

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

    Reports an error identified by $message if $actual is not of type iterable.

    assertIsNotIterable() is the inverse of this assertion and takes the same arguments.

    Example 1.37 Usage of assertIsIterable()

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

    assertIsNumeric()

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

    Reports an error identified by $message if $actual is not of type numeric.

    assertIsNotNumeric() is the inverse of this assertion and takes the same arguments.

    Example 1.38 Usage of assertIsNumeric()

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

    assertIsObject()

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

    Reports an error identified by $message if $actual is not of type object.

    assertIsNotObject() is the inverse of this assertion and takes the same arguments.

    Example 1.39 Usage of assertIsObject()

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

    assertIsResource()

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

    Reports an error identified by $message if $actual is not of type resource.

    assertIsNotResource() is the inverse of this assertion and takes the same arguments.

    Example 1.40 Usage of assertIsResource()

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

    assertIsScalar()

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

    Reports an error identified by $message if $actual is not of type scalar.

    assertIsNotScalar() is the inverse of this assertion and takes the same arguments.

    Example 1.41 Usage of assertIsScalar()

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

    assertIsString()

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

    Reports an error identified by $message if $actual is not of type string.

    assertIsNotString() is the inverse of this assertion and takes the same arguments.

    Example 1.42 Usage of assertIsString()

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

    assertIsReadable()

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

    Reports an error identified by $message if the file or directory specified by $filename is not readable.

    assertIsNotReadable() is the inverse of this assertion and takes the same arguments.

    Example 1.43 Usage of assertIsReadable()

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

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

    Reports an error identified by $message if the file or directory specified by $filename is not writable.

    assertIsNotWritable() is the inverse of this assertion and takes the same arguments.

    Example 1.44 Usage of assertIsWritable()

    1. <?php declare(strict_types=1);
    2. use PHPUnit\Framework\TestCase;
    3. final class IsWritableTest extends TestCase
    4. {
    5. public function testFailure(): void
    6. {
    7. $this->assertIsWritable('/path/to/unwritable');
    8. }
    9. }

    assertJsonFileEqualsJsonFile()

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

    Reports an error identified by $message if the value of $actualFile does not match the value of $expectedFile.

    Example 1.45 Usage of assertJsonFileEqualsJsonFile()

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

    assertJsonStringEqualsJsonFile()

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

    Reports an error identified by $message if the value of $actualJson does not match the value of $expectedFile.

    Example 1.46 Usage of assertJsonStringEqualsJsonFile()

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

    assertJsonStringEqualsJsonString()

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

    Reports an error identified by $message if the value of $actualJson does not match the value of $expectedJson.

    Example 1.47 Usage of assertJsonStringEqualsJsonString()

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

    assertLessThan()

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

    Reports an error identified by $message if the value of $actual is not less than the value of $expected.

    Example 1.48 Usage of assertLessThan()

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

    assertLessThanOrEqual()

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

    Reports an error identified by $message if the value of $actual is not less than or equal to the value of $expected.

    Example 1.49 Usage of assertLessThanOrEqual()

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

    assertNan()

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

    Reports an error identified by $message if $variable is not NAN.

    Example 1.50 Usage of assertNan()

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

    assertNull()

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

    Reports an error identified by $message if $variable is not null.

    assertNotNull() is the inverse of this assertion and takes the same arguments.

    Example 1.51 Usage of assertNull()

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

    assertObjectHasAttribute()

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

    Reports an error identified by $message if $object->attributeName does not exist.

    assertObjectNotHasAttribute() is the inverse of this assertion and takes the same arguments.

    Example 1.52 Usage of assertObjectHasAttribute()

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

    assertMatchesRegularExpression()

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

    Reports an error identified by $message if $string does not match the regular expression $pattern.

    assertDoesNotMatchRegularExpression() is the inverse of this assertion and takes the same arguments.

    Example 1.53 Usage of assertMatchesRegularExpression()

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

    assertStringMatchesFormat()

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

    Reports an error identified by $message if the $string does not match the $format string.

    assertStringNotMatchesFormat() is the inverse of this assertion and takes the same arguments.

    Example 1.54 Usage of assertStringMatchesFormat()

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

    The format string may contain the following placeholders:

    • %e: Represents a directory separator, for example / on Linux.

    • %s: One or more of anything (character or white space) except the end of line character.

    • %S: Zero or more of anything (character or white space) except the end of line character.

    • %a: One or more of anything (character or white space) including the end of line character.

    • %A: Zero or more of anything (character or white space) including the end of line character.

    • %w: Zero or more white space characters.

    • %i: A signed integer value, for example +3142, -3142.

    • %d: An unsigned integer value, for example 123456.

    • %x: One or more hexadecimal character. That is, characters in the range 0-9, a-f, A-F.

    • %c: A single character of any sort.

    • %%: A literal percent character: %.

    assertStringMatchesFormatFile()

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

    Reports an error identified by $message if the $string does not match the contents of the $formatFile.

    assertStringNotMatchesFormatFile() is the inverse of this assertion and takes the same arguments.

    Example 1.55 Usage of assertStringMatchesFormatFile()

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

    assertSame()

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

    Reports an error identified by $message if the two variables $expected and $actual do not have the same type and value.

    assertNotSame() is the inverse of this assertion and takes the same arguments.

    Example 1.56 Usage of assertSame()

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

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

    Reports an error identified by $message if the two variables $expected and $actual do not reference the same object.

    Example 1.57 Usage of assertSame() with objects

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

    assertSameSize()

    assertSameSize($expected, $actual, string $message = '')

    Reports an error identified by $message if the sizes of $actual and $expected are not the same.

    assertNotSameSize() is the inverse of this assertion and takes the same arguments.

    Example 1.58 Usage of assertSameSize()

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

    assertStringEndsWith()

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

    Reports an error identified by $message if the $string does not end with $suffix.

    assertStringEndsNotWith() is the inverse of this assertion and takes the same arguments.

    Example 1.59 Usage of assertStringEndsWith()

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

    assertStringEqualsFile()

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

    Reports an error identified by $message if the file specified by $expectedFile does not have $actualString as its contents.

    assertStringNotEqualsFile() is the inverse of this assertion and takes the same arguments.

    Example 1.60 Usage of assertStringEqualsFile()

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

    assertStringStartsWith()

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

    Reports an error identified by $message if the $string does not start with $prefix.

    assertStringStartsNotWith() is the inverse of this assertion and takes the same arguments.

    Example 1.61 Usage of assertStringStartsWith()

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

    assertThat()

    More complex assertions can be formulated using the PHPUnit\Framework\Constraint classes. They can be evaluated using the assertThat() method. Example 1.62 shows how the logicalNot() and equalTo() constraints can be used to express the same assertion as assertNotEquals().

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

    Reports an error identified by $message if the $value does not match the $constraint.

    Example 1.62 Usage of assertThat()

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

    shows the available PHPUnit\Framework\Constraint classes.

    assertTrue()

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

    Reports an error identified by $message if $condition is false.

    assertNotTrue() is the inverse of this assertion and takes the same arguments.

    Example 1.63 Usage of assertTrue()

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

    assertXmlFileEqualsXmlFile()

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

    Reports an error identified by $message if the XML document in $actualFile is not equal to the XML document in $expectedFile.

    assertXmlFileNotEqualsXmlFile() is the inverse of this assertion and takes the same arguments.

    Example 1.64 Usage of assertXmlFileEqualsXmlFile()

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

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

    Reports an error identified by $message if the XML document in $actualXml is not equal to the XML document in $expectedFile.

    assertXmlStringNotEqualsXmlFile() is the inverse of this assertion and takes the same arguments.

    Example 1.65 Usage of assertXmlStringEqualsXmlFile()

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

    assertXmlStringEqualsXmlString()

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

    Reports an error identified by $message if the XML document in $actualXml is not equal to the XML document in $expectedXml.

    assertXmlStringNotEqualsXmlString() is the inverse of this assertion and takes the same arguments.

    Example 1.66 Usage of assertXmlStringEqualsXmlString()

    1. <?php declare(strict_types=1);
    2. use PHPUnit\Framework\TestCase;
    3. final class XmlStringEqualsXmlStringTest extends TestCase
    4. {
    5. public function testFailure(): void
    6. {
    7. $this->assertXmlStringEqualsXmlString(
    8. '<foo><bar/></foo>', '<foo><baz/></foo>');
    9. }