Incomplete and Skipped Tests

Incomplete Tests When you are working on a new test case class, you might want to begin by writing empty test methods such as: public function testSomething() { } to keep track of the tests that you have to write. The problem with empty test methods is that they are interpreted as a success by the PHPUnit framework. This misinterpretation leads to the test reports being useless -- you cannot see whether a test is actually successful or just not yet implemented. Calling $this->fail() in th

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

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

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

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

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 {

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

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

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