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

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

assertXmlStringEqualsXmlString()

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 A.53: Usage of assertXmlStringEqualsXmlString() <?php use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlStringTest extends TestC

Database Testing

Many beginner and intermediate unit testing examples in any programming language suggest that it is perfectly easy to test your application's logic with simple tests. For database-centric applications this is far away from the reality. Start using Wordpress, TYPO3 or Symfony with Doctrine or Propel, for example, and you will easily experience considerable problems with PHPUnit: just because the database is so tightly coupled to these libraries. Make sure you have the PHP extension pdo and da

Extending PHPUnit

PHPUnit can be extended in various ways to make the writing of tests easier and customize the feedback you get from running tests. Here are common starting points to extend PHPUnit. Subclass PHPUnit\Framework\TestCase Write custom assertions and utility methods in an abstract subclass of PHPUnit\Framework\TestCase and derive your test case classes from that class. This is one of the easiest ways to extend PHPUnit. Write custom assertions When writing custom assertions it is the best pract

Fixtures

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test. In Example 2.1, the fixture was simply the array that is stored in the $stack variable. Most of the time, though, the fixture will be more complex than a simple array, and the amount of code needed to set it up will grow accordingly. The actual content of the test

assertXmlFileEqualsXmlFile()

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 A.51: Usage of assertXmlFileEqualsXmlFile() <?php use PHPUnit\Framework\TestCase; class XmlFileEqualsXmlFileTest extends TestCase { public

assertThat()

assertThat() More complex assertions can be formulated using the PHPUnit_Framework_Constraint classes. They can be evaluated using the assertThat() method. Example A.49 shows how the logicalNot() and equalTo() constraints can be used to express the same assertion as assertNotEquals(). assertThat(mixed $value, PHPUnit_Framework_Constraint $constraint[, $message = '']) Reports an error identified by $message if the $value does not match the $constraint. Example A.49: Usage of assertThat() <

assertXmlStringEqualsXmlFile()

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 A.52: Usage of assertXmlStringEqualsXmlFile() <?php use PHPUnit\Framework\TestCase; class XmlStringEqualsXmlFileTest extends TestCase {

assertStringStartsWith()

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