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

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

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

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

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

assertStringMatchesFormatFile()

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 A.43: Usage of assertStringMatchesFormatFile() <?php use PHPUnit\Framework\TestCase; class StringMatchesFormatFileTest extends TestCase { public function test

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('

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() <