1. Assertions

This appendix lists the various assertion methods that are available.

Static vs. Non-Static Usage of Assertion Methods

PHPUnit’s assertions are implemented in PHPUnit\Framework\Assert. 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()).

Boolean

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.1 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/TrueTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) TrueTest::testFailure
  8. Failed asserting that false is true.
  9. /path/to/tests/TrueTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

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.2 Usage of assertFalse()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class FalseTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertFalse(true);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/FalseTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) FalseTest::testFailure
  8. Failed asserting that true is false.
  9. /path/to/tests/FalseTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Identity

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.3 Usage of assertSame()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/SameWithMixedTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) SameWithMixedTest::testFailure
  8. Failed asserting that 2204 is identical to '2204'.
  9. /path/to/tests/SameWithMixedTest.php:8
  10. FAILURES!
  11. 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.4 Usage of assertSame() with objects

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class SameWithObjectsTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertSame(new stdClass, new stdClass);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/SameWithObjectsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) SameWithObjectsTest::testFailure
  8. Failed asserting that two variables reference the same object.
  9. /path/to/tests/SameWithObjectsTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Identity is checked using the === operator.

Equality

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.5 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. FFF 3 / 3 (100%)
  5. Time: 00:00.001, Memory: 14.31 MB
  6. There were 3 failures:
  7. 1) EqualsTest::testFailure
  8. Failed asserting that 0 matches expected 1.
  9. /path/to/tests/EqualsTest.php:8
  10. 2) EqualsTest::testFailure2
  11. Failed asserting that two strings are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. -'bar'
  16. +'baz'
  17. /path/to/tests/EqualsTest.php:13
  18. 3) EqualsTest::testFailure3
  19. Failed asserting that two strings are equal.
  20. --- Expected
  21. +++ Actual
  22. @@ @@
  23. 'foo\n
  24. -bar\n
  25. +bah\n
  26. baz\n
  27. '
  28. /path/to/tests/EqualsTest.php:18
  29. FAILURES!
  30. Tests: 3, Assertions: 3, Failures: 3.

Equality is checked using the == operator, but more specialized comparisons are used for specific argument types for $expected and $actual, see below.

assertEquals(DateTimeInterface $expected, DateTimeInterface $actual[, string $message])

Reports an error identified by $message if the two points in time represented by the two DateTimeInterface objects $expected and $actual are not equal.

Example 1.6 Usage of assertEquals() with DateTimeImmutable objects

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithDateTimeImmutableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $expected = new DateTimeImmutable('2023-02-23 01:23:45');
  8. $actual = new DateTimeImmutable('2023-02-23 01:23:46');
  9. $this->assertEquals($expected, $actual);
  10. }
  11. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithDateTimeImmutableTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithDateTimeImmutableTest::testFailure
  8. Failed asserting that two DateTime objects are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -2023-02-23T01:23:45.000000+0100
  13. +2023-02-23T01:23:46.000000+0100
  14. /path/to/tests/EqualsWithDateTimeImmutableTest.php:11
  15. FAILURES!
  16. Tests: 1, Assertions: 1, Failures: 1.

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.7 Usage of assertEquals() with DOMDocument objects

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithDomDocumentTest 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithDomDocumentTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithDomDocumentTest::testFailure
  8. Failed asserting that two DOM documents are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. <?xml version="1.0"?>
  13. -<foo>
  14. - <bar/>
  15. -</foo>
  16. +<bar>
  17. + <foo/>
  18. +</bar>
  19. /path/to/tests/EqualsWithDomDocumentTest.php:14
  20. FAILURES!
  21. 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 property values.

Example 1.8 Usage of assertEquals() with objects

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithObjectsTest 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithObjectsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithObjectsTest::testFailure
  8. Failed asserting that two objects are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. stdClass Object (
  13. - 'foo' => 'foo'
  14. - 'bar' => 'bar'
  15. + 'foo' => 'bar'
  16. + 'baz' => 'bar'
  17. )
  18. /path/to/tests/EqualsWithObjectsTest.php:16
  19. FAILURES!
  20. 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.9 Usage of assertEquals() with arrays

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithArraysTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEquals(['a', 'b', 'c'], ['a', 'c', 'd']);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithArraysTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithArraysTest::testFailure
  8. Failed asserting that two arrays are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. Array (
  13. 0 => 'a'
  14. - 1 => 'b'
  15. - 2 => 'c'
  16. + 1 => 'c'
  17. + 2 => 'd'
  18. )
  19. /path/to/tests/EqualsWithArraysTest.php:8
  20. FAILURES!
  21. 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 properties.

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

Example 1.10 Usage of assertEqualsCanonicalizing()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithArraysCanonicalizingTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 0, 1]);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithArraysCanonicalizingTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithArraysCanonicalizingTest::testFailure
  8. Failed asserting that two arrays are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. Array (
  13. - 0 => 1
  14. - 1 => 2
  15. - 2 => 3
  16. + 0 => 0
  17. + 1 => 1
  18. + 2 => 2
  19. + 3 => 3
  20. )
  21. /path/to/tests/EqualsWithArraysCanonicalizingTest.php:8
  22. FAILURES!
  23. 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.11 Usage of assertEqualsIgnoringCase()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithStringsIgnoringCaseTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithStringsIgnoringCaseTest::testFailure
  8. Failed asserting that two strings are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -'foo'
  13. +'BAR'
  14. /path/to/tests/EqualsWithStringsIgnoringCaseTest.php:8
  15. FAILURES!
  16. 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 “What Every Computer Scientist Should Know About Floating-Point Arithmetic” to understand why $delta is necessary.

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

Example 1.12 Usage of assertEqualsWithDelta()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class EqualsWithFloatsAndDeltaTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertEqualsWithDelta(1.0, 1.5, 0.1);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EqualsWithFloatsAndDeltaTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EqualsWithFloatsAndDeltaTest::testFailure
  8. Failed asserting that 1.5 matches expected 1.0.
  9. /path/to/tests/EqualsWithFloatsAndDeltaTest.php:8
  10. FAILURES!
  11. 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.13 Usage of assertObjectEquals()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/ObjectEqualsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) ObjectEqualsTest::testSomething
  8. Failed asserting that two objects are equal.
  9. /path/to/tests/ObjectEqualsTest.php:16
  10. FAILURES!
  11. Tests: 1, Assertions: 2, Failures: 1.

Example 1.14 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. }

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.

assertFileEquals()

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.

assertFileEqualsCanonicalizing() (and assertFileNotEqualsCanonicalizing()) as well as assertFileEqualsIgnoringCase() (and assertFileNotEqualsIgnoringCase()) do for files what assertEqualsCanonicalizing() (and assertNotEqualsCanonicalizing()) as well as assertEqualsIgnoringCase() (and assertNotEqualsIgnoringCase()) do for strings.

Example 1.15 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(
  8. __DIR__ . '/expected.txt',
  9. __DIR__ . '/actual.txt'
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/FileEqualsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) FileEqualsTest::testFailure
  8. Failed asserting that two strings are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -'expected\n
  13. +'actual\n
  14. '
  15. /path/to/tests/FileEqualsTest.php:8
  16. FAILURES!
  17. Tests: 1, Assertions: 3, Failures: 1.

Iterable

assertArrayHasKey()

assertArrayHasKey(int|string $key, array|ArrayAccess $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.16 Usage of assertArrayHasKey()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/ArrayHasKeyTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) ArrayHasKeyTest::testFailure
  8. Failed asserting that an array has the key 'foo'.
  9. /path/to/tests/ArrayHasKeyTest.php:8
  10. FAILURES!
  11. 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.17 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/ContainsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) ContainsTest::testFailure
  8. Failed asserting that an array contains 4.
  9. /path/to/tests/ContainsTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Whether $needle is an element of $haystack is checked using the === operator. You can use assertContainsEquals() (and assertNotContainsEquals()) if you need the comparison logic implemented by the == operator.

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.18 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/ContainsOnlyTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) ContainsOnlyTest::testFailure
  8. Failed asserting that Array &0 (
  9. 0 => '1'
  10. 1 => '2'
  11. 2 => 3
  12. ) contains only values of type "string".
  13. /path/to/tests/ContainsOnlyTest.php:8
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.

assertContainsOnlyInstancesOf()

assertContainsOnlyInstancesOf(string $classname, iterable $haystack[, string $message])

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

Example 1.19 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/ContainsOnlyInstancesOfTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) ContainsOnlyInstancesOfTest::testFailure
  8. Failed asserting that Array &0 (
  9. 0 => Foo Object #388 ()
  10. 1 => Bar Object #382 ()
  11. 2 => Foo Object #77 ()
  12. ) contains only values of type "Foo".
  13. /path/to/tests/ContainsOnlyInstancesOfTest.php:8
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.

Cardinality

assertCount()

assertCount(int $expectedCount, Countable|iterable $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.20 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/CountTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) CountTest::testFailure
  8. Failed asserting that actual size 1 matches expected size 0.
  9. /path/to/tests/CountTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertSameSize()

assertSameSize(Countable|iterable $expected, Countable|iterable $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.21 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/SameSizeTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) SameSizeTest::testFailure
  8. Failed asserting that actual size 1 matches expected size 2.
  9. /path/to/tests/SameSizeTest.php:8
  10. FAILURES!
  11. 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.22 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/EmptyTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) EmptyTest::testFailure
  8. Failed asserting that an array is empty.
  9. /path/to/tests/EmptyTest.php:8
  10. FAILURES!
  11. 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.23 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/GreaterThanTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) GreaterThanTest::testFailure
  8. Failed asserting that 1 is greater than 2.
  9. /path/to/tests/GreaterThanTest.php:8
  10. FAILURES!
  11. 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.24 Usage of assertGreaterThanOrEqual()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/GreaterThanOrEqualTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) GreaterThanOrEqualTest::testFailure
  8. Failed asserting that 1 is equal to 2 or is greater than 2.
  9. /path/to/tests/GreaterThanOrEqualTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 2, 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.25 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/LessThanTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) LessThanTest::testFailure
  8. Failed asserting that 2 is less than 1.
  9. /path/to/tests/LessThanTest.php:8
  10. FAILURES!
  11. 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.26 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/LessThanOrEqualTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) LessThanOrEqualTest::testFailure
  8. Failed asserting that 2 is equal to 1 or is less than 1.
  9. /path/to/tests/LessThanOrEqualTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 2, Failures: 1.

Types

assertInstanceOf()

assertInstanceOf(string $expected, mixed $actual[, string $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.27 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/InstanceOfTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) InstanceOfTest::testFailure
  8. Failed asserting that Exception Object (...) is an instance of class RuntimeException.
  9. /path/to/tests/InstanceOfTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsArray()

assertIsArray(mixed $actual[, string $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.28 Usage of assertIsArray()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsArrayTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsArray(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsArrayTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsArrayTest::testFailure
  8. Failed asserting that null is of type array.
  9. /path/to/tests/IsArrayTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsList()

assertIsList(mixed $actual[, string $message])

Reports an error identified by $message if $actual is not an array where the keys are consecutive numbers from 0 to count($actual) - 1.

Example 1.29 Usage of assertIsList()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsListTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsListTest::testFailure
  8. Failed asserting that an array is a list.
  9. /path/to/tests/IsListTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsBool()

assertIsBool(mixed $actual[, string $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.30 Usage of assertIsBool()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsBoolTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsBool(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsBoolTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsBoolTest::testFailure
  8. Failed asserting that null is of type bool.
  9. /path/to/tests/IsBoolTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsCallable()

assertIsCallable(mixed $actual[, string $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.31 Usage of assertIsCallable()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsCallableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsCallable(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsCallableTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsCallableTest::testFailure
  8. Failed asserting that null is of type callable.
  9. /path/to/tests/IsCallableTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsFloat()

assertIsFloat(mixed $actual[, string $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.32 Usage of assertIsFloat()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsFloatTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsFloat(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsFloatTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsFloatTest::testFailure
  8. Failed asserting that null is of type float.
  9. /path/to/tests/IsFloatTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsInt()

assertIsInt(mixed $actual[, string $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.33 Usage of assertIsInt()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsIntTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsInt(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsIntTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsIntTest::testFailure
  8. Failed asserting that null is of type int.
  9. /path/to/tests/IsIntTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsIterable()

assertIsIterable(mixed $actual[, string $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.34 Usage of assertIsIterable()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsIterableTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsIterable(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsIterableTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsIterableTest::testFailure
  8. Failed asserting that null is of type iterable.
  9. /path/to/tests/IsIterableTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsNumeric()

assertIsNumeric(mixed $actual[, string $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.35 Usage of assertIsNumeric()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsNumericTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsNumeric(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsNumericTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsNumericTest::testFailure
  8. Failed asserting that null is of type numeric.
  9. /path/to/tests/IsNumericTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsObject()

assertIsObject(mixed $actual[, string $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.36 Usage of assertIsObject()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsObjectTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsObject(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsObjectTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsObjectTest::testFailure
  8. Failed asserting that null is of type object.
  9. /path/to/tests/IsObjectTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsResource()

assertIsResource(mixed $actual[, string $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.

assertIsClosedResource() (and assertIsNotClosedResource()) are provided to explicitly check for closed resources.

Example 1.37 Usage of assertIsResource()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsResourceTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsResource(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsResourceTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsResourceTest::testFailure
  8. Failed asserting that null is of type resource.
  9. /path/to/tests/IsResourceTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsScalar()

assertIsScalar(mixed $actual[, string $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.38 Usage of assertIsScalar()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsScalarTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsScalar(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsScalarTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsScalarTest::testFailure
  8. Failed asserting that null is of type scalar.
  9. /path/to/tests/IsScalarTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsString()

assertIsString(mixed $actual[, string $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.39 Usage of assertIsString()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class IsStringTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertIsString(null);
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsStringTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) IsStringTest::testFailure
  8. Failed asserting that null is of type string.
  9. /path/to/tests/IsStringTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertNull()

assertNull(mixed $actual[, string $message])

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

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

Example 1.40 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/NullTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) NullTest::testFailure
  8. Failed asserting that 'foo' is null.
  9. /path/to/tests/NullTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Strings

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.41 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringStartsWithTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringStartsWithTest::testFailure
  8. Failed asserting that 'foo' starts with "prefix".
  9. /path/to/tests/StringStartsWithTest.php:8
  10. FAILURES!
  11. 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.42 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringEndsWithTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringEndsWithTest::testFailure
  8. Failed asserting that 'foo' ends with "suffix".
  9. /path/to/tests/StringEndsWithTest.php:8
  10. FAILURES!
  11. 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.

assertStringContainsStringIgnoringLineEndings() takes the same arguments and can be used if line endings should be ignored.

Example 1.43 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringContainsStringTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringContainsStringTest::testFailure
  8. Failed asserting that 'bar' contains "foo".
  9. /path/to/tests/StringContainsStringTest.php:8
  10. FAILURES!
  11. 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 Normalization Form.

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

Example 1.44 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringContainsStringIgnoringCaseTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringContainsStringIgnoringCaseTest::testFailure
  8. Failed asserting that 'bar' contains "foo".
  9. /path/to/tests/StringContainsStringIgnoringCaseTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertStringEqualsStringIgnoringLineEndings

assertStringEqualsStringIgnoringLineEndings(string $expected, string $actual[, string $message])

Reports an error identified by $message if the two strings $expected and $actual are not equal while ignoring line endings.

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.45 Usage of assertMatchesRegularExpression()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/MatchesRegularExpressionTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) MatchesRegularExpressionTest::testFailure
  8. Failed asserting that 'bar' matches PCRE pattern "/foo/".
  9. /path/to/tests/MatchesRegularExpressionTest.php:8
  10. FAILURES!
  11. 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.46 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringMatchesFormatTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringMatchesFormatTest::testFailure
  8. Failed asserting that string matches format description.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -%i
  13. +foo
  14. /path/to/tests/StringMatchesFormatTest.php:8
  15. FAILURES!
  16. 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.

  • %f: A floating point number, for example: 3.142, -3.142, 3.142E-10, 3.142e+10.

  • %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.47 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(
  8. __DIR__ . '/expected-format.txt',
  9. 'foo'
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringMatchesFormatFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringMatchesFormatFileTest::testFailure
  8. Failed asserting that string matches format description.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -%i
  13. +foo
  14. /path/to/tests/StringMatchesFormatFileTest.php:8
  15. FAILURES!
  16. Tests: 1, Assertions: 2, 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.

assertStringEqualsFileCanonicalizing() (and assertStringNotEqualsFileCanonicalizing()) as well as assertStringEqualsFileIgnoringCase() (and assertStringNotEqualsFileIgnoringCase()) do for files what assertEqualsCanonicalizing() (and assertNotEqualsCanonicalizing()) as well as assertEqualsIgnoringCase() (and assertNotEqualsIgnoringCase()) do for strings.

Example 1.48 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(
  8. __DIR__ . '/expected.txt',
  9. 'actual'
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/StringEqualsFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) StringEqualsFileTest::testFailure
  8. Failed asserting that two strings are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. -'expected\n
  13. -'
  14. +'actual'
  15. /path/to/tests/StringEqualsFileTest.php:8
  16. FAILURES!
  17. Tests: 1, Assertions: 2, Failures: 1.

JSON

assertJson()

assertJson(string $actual[, string $message])

Reports an error identified by $message if the value of $actual is not valid JSON.

Example 1.49 Usage of assertJson()

  1. <?php declare(strict_types=1);
  2. use PHPUnit\Framework\TestCase;
  3. final class JsonTest extends TestCase
  4. {
  5. public function testFailure(): void
  6. {
  7. $this->assertJson('not-a-json-string');
  8. }
  9. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/JsonTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) JsonTest::testFailure
  8. Failed asserting that 'not-a-json-string' is valid JSON (Syntax error, malformed JSON).
  9. /path/to/tests/JsonTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertJsonFileEqualsJsonFile()

assertJsonFileEqualsJsonFile(string $expectedFile, string $actualFile[, string $message])

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

Example 1.50 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. __DIR__ . '/expected.json',
  9. __DIR__ . '/actual.json',
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/JsonFileEqualsJsonFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) JsonFileEqualsJsonFileTest::testFailure
  8. Failed asserting that '{"mascot":"elePHPant"}\n
  9. ' matches JSON string "{"mascot":"elephant"}
  10. ".
  11. --- Expected
  12. +++ Actual
  13. @@ @@
  14. {
  15. - "mascot": "elephant"
  16. + "mascot": "elePHPant"
  17. }
  18. /path/to/tests/JsonFileEqualsJsonFileTest.php:8
  19. FAILURES!
  20. Tests: 1, Assertions: 5, Failures: 1.

assertJsonStringEqualsJsonFile()

assertJsonStringEqualsJsonFile(string $expectedFile, string $actualJson[, string $message])

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

Example 1.51 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. __DIR__ . '/expected.json',
  9. json_encode(['mascot' => 'elephant'])
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/JsonStringEqualsJsonFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) JsonStringEqualsJsonFileTest::testFailure
  8. Failed asserting that '{"mascot":"elephant"}' matches JSON string "{"mascot":"elePHPant"}
  9. ".
  10. --- Expected
  11. +++ Actual
  12. @@ @@
  13. {
  14. - "mascot": "elePHPant"
  15. + "mascot": "elephant"
  16. }
  17. /path/to/tests/JsonStringEqualsJsonFileTest.php:8
  18. FAILURES!
  19. Tests: 1, Assertions: 4, Failures: 1.

assertJsonStringEqualsJsonString()

assertJsonStringEqualsJsonString(string $expectedJson, string $actualJson[, string $message])

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

Example 1.52 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' => 'elePHPant']),
  9. json_encode(['mascot' => 'elephant'])
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/JsonStringEqualsJsonStringTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) JsonStringEqualsJsonStringTest::testFailure
  8. Failed asserting that '{"mascot":"elephant"}' matches JSON string "{"mascot":"elePHPant"}".
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. {
  13. - "mascot": "elePHPant"
  14. + "mascot": "elephant"
  15. }
  16. /path/to/tests/JsonStringEqualsJsonStringTest.php:8
  17. FAILURES!
  18. Tests: 1, Assertions: 3, Failures: 1.

XML

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.53 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. __DIR__ . '/expected.xml',
  9. __DIR__ . '/actual.xml',
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/XmlFileEqualsXmlFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) XmlFileEqualsXmlFileTest::testFailure
  8. Failed asserting that two DOM documents are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. <?xml version="1.0"?>
  13. <foo>
  14. - <bar/>
  15. + <baz/>
  16. </foo>
  17. /path/to/tests/XmlFileEqualsXmlFileTest.php:8
  18. FAILURES!
  19. Tests: 1, Assertions: 1, Failures: 1.

assertXmlStringEqualsXmlFile()

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.54 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. __DIR__ . '/expected.xml',
  9. '<foo><baz/></foo>'
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/XmlStringEqualsXmlFileTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) XmlStringEqualsXmlFileTest::testFailure
  8. Failed asserting that two DOM documents are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. <?xml version="1.0"?>
  13. <foo>
  14. - <bar/>
  15. + <baz/>
  16. </foo>
  17. /path/to/tests/XmlStringEqualsXmlFileTest.php:8
  18. FAILURES!
  19. Tests: 1, Assertions: 1, 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.55 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>',
  9. '<foo><baz/></foo>'
  10. );
  11. }
  12. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/XmlStringEqualsXmlStringTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) XmlStringEqualsXmlStringTest::testFailure
  8. Failed asserting that two DOM documents are equal.
  9. --- Expected
  10. +++ Actual
  11. @@ @@
  12. <?xml version="1.0"?>
  13. <foo>
  14. - <bar/>
  15. + <baz/>
  16. </foo>
  17. /path/to/tests/XmlStringEqualsXmlStringTest.php:8
  18. FAILURES!
  19. Tests: 1, Assertions: 1, Failures: 1.

Filesystem

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.56 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/DirectoryExistsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) DirectoryExistsTest::testFailure
  8. Failed asserting that directory "/path/to/directory" exists.
  9. /path/to/tests/DirectoryExistsTest.php:8
  10. FAILURES!
  11. 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.57 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/DirectoryIsReadableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) DirectoryIsReadableTest::testFailure
  8. Failed asserting that "/path/to/directory" is readable.
  9. /path/to/DirectoryIsReadableTest.php:6
  10. FAILURES!
  11. 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.58 Usage of assertDirectoryIsWritable()

  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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/DirectoryIsWritableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) DirectoryIsWritableTest::testFailure
  8. Failed asserting that "/path/to/directory" is writable.
  9. /path/to/DirectoryIsWritableTest.php:6
  10. FAILURES!
  11. Tests: 1, Assertions: 1, 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.59 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/FileExistsTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) FileExistsTest::testFailure
  8. Failed asserting that file "/path/to/file" exists.
  9. /path/to/tests/FileExistsTest.php:8
  10. FAILURES!
  11. 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.

Example 1.60 Usage of assertFileIsReadable()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/FileIsReadableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) FileIsReadableTest::testFailure
  8. Failed asserting that "/path/to/file" is readable.
  9. /path/to/FileIsReadableTest.php:6
  10. FAILURES!
  11. 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.61 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/FileIsWritableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) FileIsWritableTest::testFailure
  8. Failed asserting that "/path/to/file" is writable.
  9. /path/to/FileIsWritableTest.php:6
  10. FAILURES!
  11. 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.62 Usage of assertIsReadable()

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

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsReadableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) IsReadableTest::testFailure
  8. Failed asserting that "/path/to/unreadable" is readable.
  9. /path/to/IsReadableTest.php:6
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertIsWritable()

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.63 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/IsWritableTest.php
  2. PHPUnit 10.0.11 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.3
  4. F
  5. Time: 00:00, Memory: 14.29 MB
  6. There was 1 failure:
  7. 1) IsWritableTest::testFailure
  8. Failed asserting that "/path/to/unwritable" is writable.
  9. /path/to/IsWritableTest.php:6
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Math

assertInfinite()

assertInfinite(mixed $actual[, string $message])

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

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

Example 1.64 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/InfiniteTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) InfiniteTest::testFailure
  8. Failed asserting that 1 is infinite.
  9. /path/to/tests/InfiniteTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

assertNan()

assertNan(mixed $actual[, string $message])

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

Example 1.65 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/NanTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) NanTest::testFailure
  8. Failed asserting that 1 is nan.
  9. /path/to/tests/NanTest.php:8
  10. FAILURES!
  11. Tests: 1, Assertions: 1, Failures: 1.

Constraints

assertThat()

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

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

More complex assertions can be formulated using the PHPUnit\Framework\Constraint classes. They can be evaluated using the assertThat() method.

This example shows how the logicalNot() and equalTo() constraints can be used, for instance, to express the same assertion as assertNotEquals():

Example 1.66 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. }

Running the test shown above yields the output shown below:

  1. ./tools/phpunit tests/BiscuitTest.php
  2. PHPUnit 10.0.16 by Sebastian Bergmann and contributors.
  3. Runtime: PHP 8.2.4
  4. F 1 / 1 (100%)
  5. Time: 00:00, Memory: 14.31 MB
  6. There was 1 failure:
  7. 1) BiscuitTest::testEquals
  8. Failed asserting that Biscuit Object #388 (
  9. 'name' => 'Ginger'
  10. ) is not equal to Biscuit Object #382 (
  11. 'name' => 'Ginger'
  12. ).
  13. /path/to/tests/BiscuitTest.php:11
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.

Table 1.1 shows the available PHPUnit\Framework\Constraint classes.

Table 1.1 Constraints

Constraint

Meaning

PHPUnit\Framework\Constraint\IsAnything anything()

Constraint that accepts any input value.

PHPUnit\Framework\Constraint\ArrayHasKey arrayHasKey(mixed $key)

Constraint that asserts that the array has a given key.

PHPUnit\Framework\Constraint\TraversableContains contains(mixed $value)

Constraint that asserts that the array or object that implements the Iterator interface contains a given value.

PHPUnit\Framework\Constraint\TraversableContainsOnly containsOnly(string $type)

Constraint that asserts that the array or object that implements the Iterator interface 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 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\DirectoryExists directoryExists()

Constraint that checks if the directory exists.

PHPUnit\Framework\Constraint\FileExists fileExists()

Constraint that checks if the file(name) exists.

PHPUnit\Framework\Constraint\IsReadable isReadable()

Constraint that checks if the file(name) is readable.

PHPUnit\Framework\Constraint\IsWritable isWritable()

Constraint that checks if the file(name) is writable.

PHPUnit\Framework\Constraint\GreaterThan greaterThan(mixed $value)

Constraint that asserts that the value is greater than a given value.

PHPUnit\Framework\Constraint\LogicalOr greaterThanOrEqual(mixed $value)

Constraint that asserts that the value is greater than or equal to a given value.

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 is false.

PHPUnit\Framework\Constraint\IsInstanceOf isInstanceOf(string $className)

Constraint that asserts that the object is an instance of a given class.

PHPUnit\Framework\Constraint\IsNull isNull()

Constraint that asserts that the value is null.

PHPUnit\Framework\Constraint\IsTrue isTrue()

Constraint that asserts that the value is true.

PHPUnit\Framework\Constraint\IsType isType(string $type)

Constraint that asserts that the value is of a specified type.

PHPUnit\Framework\Constraint\LessThan lessThan(mixed $value)

Constraint that asserts that the value is smaller than a given value.

PHPUnit\Framework\Constraint\LogicalOr lessThanOrEqual(mixed $value)

Constraint that asserts that the value 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 matches a regular expression.

PHPUnit\Framework\Constraint\StringContains stringContains(string $string, bool $case)

Constraint that asserts that the string contains a given string.

PHPUnit\Framework\Constraint\StringEndsWith stringEndsWith(string $suffix)

Constraint that asserts that the string ends with a given suffix.

PHPUnit\Framework\Constraint\StringStartsWith stringStartsWith(string $prefix)

Constraint that asserts that the string starts with a given prefix.