@requires

@requires The @requires annotation can be used to skip tests when common preconditions, like the PHP Version or installed extensions, are not met. A complete list of possibilities and examples can be found at Table 7.3

The XML Configuration File

PHPUnit The attributes of the <phpunit> element can be used to configure PHPUnit's core functionality. <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd" backupGlobals="true" backupStaticAttributes="false" <!--bootstrap="/path/to/bootstrap.php"--> cacheTokens="false" colors="false" convertErrorsToExceptions="true"

The Command-Line Test Runner

The PHPUnit command-line test runner can be invoked through the phpunit command. The following code shows how to run tests with the PHPUnit command-line test runner: phpunit ArrayTest PHPUnit 5.6.0 by Sebastian Bergmann and contributors. .. Time: 0 seconds OK (2 tests, 2 assertions) When invoked as shown above, the PHPUnit command-line test runner will look for a ArrayTest.php sourcefile in the current working directory, load it, and expect to find a ArrayTest test case class. It will th

@beforeClass

@beforeClass The @beforeClass annotation can be used to specify static methods that should be called before any test methods in a test class are run to set up shared fixtures. use PHPUnit\Framework\TestCase; class MyTest extends TestCase { /** * @beforeClass */ public static function setUpSomeSharedFixtures() { // ... } /** * @beforeClass */ public static function setUpSomeOtherSharedFixtures() { // ... } }

assertTrue()

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 A.50: Usage of assertTrue() <?php use PHPUnit\Framework\TestCase; class TrueTest extends TestCase { public function testFailure() { $this->assertTrue(false); } } ?> phpunit TrueTest PHPUnit 5.6.0 by Sebastian Bergmann and contributors. F Time: 0 sec

assertJsonStringEqualsJsonFile()

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 A.34: Usage of assertJsonStringEqualsJsonFile() <?php use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonStringEqualsJsonFile( 'p

assertArrayHasKey()

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 A.1: Usage of assertArrayHasKey() <?php use PHPUnit\Framework\TestCase; class ArrayHasKeyTest extends TestCase { public function testFailure() { $this->assertArrayHasKey('foo', ['bar' => 'baz']); } } ?> phpun

assertNan()

assertNan() assertNan(mixed $variable[, string $message = '']) Reports an error identified by $message if $variable is not NAN. Example A.38: Usage of assertNan() <?php use PHPUnit\Framework\TestCase; class NanTest extends TestCase { public function testFailure() { $this->assertNan(1); } } ?> phpunit NanTest PHPUnit 5.6.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) NanTest::testFailure Failed asserting tha

@expectedExceptionMessageRegExp

@expectedExceptionMessageRegExp The expected message can also be specified as a regular expression using the @expectedExceptionMessageRegExp annotation. This is helpful for situations where a substring is not adequate for matching a given message. use PHPUnit\Framework\TestCase; class MyTest extends TestCase { /** * @expectedException MyException * @expectedExceptionMessageRegExp /Argument \d+ can not be an? \w+/ */ public function testExceptionHasRi

Code Coverage Analysis

In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage. --Wikipedia In this chapter you will learn all about PHPUnit's code coverage functionality that provides an insight into what parts of the production code are executed when the t