Installing PHPUnit

Requirements PHPUnit 5.6 requires PHP 5.6; using the latest version of PHP is highly recommended. PHPUnit requires the dom and json extensions, which are normally enabled by default. PHPUnit also requires the pcre, reflection, and spl extensions. These standard extensions are enabled by default and cannot be disabled without patching PHP's build system and/or C sources. The code coverage report feature requires the Xdebug (2.2.1 or later) and tokenizer extensions. Generating XML reports

assertContainsOnly()

assertContainsOnly() assertContainsOnly(string $type, Iterator|array $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. assertAttributeContainsOnly() and assertAttributeNotContainsOnly() are convenience wrappers th

Logging

PHPUnit can produce several types of logfiles. Test Results (XML) The XML logfile for test results produced by PHPUnit is based upon the one used by the JUnit task for Apache Ant. The following example shows the XML logfile generated for the tests in ArrayTest: <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="ArrayTest" file="/home/sb/ArrayTest.php" tests="2" assertions="2" failures="0" er

Test Doubles

Gerard Meszaros introduces the concept of Test Doubles in [Meszaros2007] like this: Sometimes it is just plain hard to test the system under test (SUT) because it depends on other components that cannot be used in the test environment. This could be because they aren't available, they will not return the results needed for the test or because executing them would have undesirable side effects. In other cases, our test strategy requires us to have more control or visibility of the interna

Organizing Tests

One of the goals of PHPUnit is that tests should be composable: we want to be able to run any number or combination of tests together, for instance all tests for the whole project, or the tests for all classes of a component that is part of the project, or just the tests for a single class. PHPUnit supports different ways of organizing tests and composing them into a test suite. This chapter shows the most commonly used approaches. Composing a Test Suite Using the Filesystem Probably the

assertFileExists()

assertFileExists() assertFileExists(string $filename[, string $message = '']) Reports an error identified by $message if the file specified by $filename does not exist. assertFileNotExists() is the inverse of this assertion and takes the same arguments. Example A.23: Usage of assertFileExists() <?php use PHPUnit\Framework\TestCase; class FileExistsTest extends TestCase { public function testFailure() { $this->assertFileExists('/path/to/file'); } } ?> phpunit File

@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

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

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

assertEqualXMLStructure()

assertEqualXMLStructure() assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = false, string $message = '']) Reports an error identified by $message if the XML Structure of the DOMElement in $actualElement is not equal to the XML structure of the DOMElement in $expectedElement. Example A.15: Usage of assertEqualXMLStructure() <?php use PHPUnit\Framework\TestCase; class EqualXMLStructureTest extends TestCase { public function testF