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

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

Other Uses for Tests

Once you get used to writing automated tests, you will likely discover more uses for tests. Here are some examples. Agile Documentation Typically, in a project that is developed using an agile process, such as Extreme Programming, the documentation cannot keep up with the frequent changes to the project's design and code. Extreme Programming demands collective code ownership, so all developers need to know how the entire system works. If you are disciplined enough to consequently use "sp

assertRegExp()

assertRegExp() assertRegExp(string $pattern, string $string[, string $message = '']) Reports an error identified by $message if $string does not match the regular expression $pattern. assertNotRegExp() is the inverse of this assertion and takes the same arguments. Example A.41: Usage of assertRegExp() <?php use PHPUnit\Framework\TestCase; class RegExpTest extends TestCase { public function testFailure() { $this->assertRegExp('/foo/', 'bar'); } } ?> phpunit RegExp

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

Testing Practices

You can always write more tests. However, you will quickly find that only a fraction of the tests you can imagine are actually useful. What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail. Another way to think of it is in cost/benefit terms. You want to write tests that will pay you back with information. --Erich Gamma During Development When you need to make a change to the internal structure

assertEquals()

assertEquals() assertEquals(mixed $expected, mixed $actual[, string $message = '']) Reports an error identified by $message if the two variables $expected and $actual are not equal. assertNotEquals() is the inverse of this assertion and takes the same arguments. assertAttributeEquals() and assertAttributeNotEquals() are convenience wrappers that use a public, protected, or private attribute of a class or object as the actual value. Example A.16: Usage of assertEquals() <?php use PHPUnit\Fr

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

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