1. Assertions

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.

In fact, you can even use global function wrappers such as assertTrue() in
any context (including classes that extend PHPUnit\Framework\TestCase)
when you (manually) include the src/Framework/Assert/Functions.php
sourcecode file that comes with PHPUnit.

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.

Reports an error identified by $message if the absolute difference between two floats $expected and $actual is greater than $delta. If the absolute difference between two floats $expected and $actual is less than or equal to$delta, the assertion will pass.

assertThat()

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