@preserveGlobalState

@preserveGlobalState When a test is run in a separate process, PHPUnit will attempt to preserve the global state from the parent process by serializing all globals in the parent process and unserializing them in the child process. This can cause problems if the parent process contains globals that are not serializable. To fix this, you can prevent PHPUnit from preserving global state with the @preserveGlobalState annotation. use PHPUnit\Framework\TestCase; class MyTest extends TestCase {

@codeCoverageIgnore*

@codeCoverageIgnore* The @codeCoverageIgnore, @codeCoverageIgnoreStart and @codeCoverageIgnoreEnd annotations can be used to exclude lines of code from the coverage analysis. For usage see the section called “Ignoring Code Blocks”.

@coversNothing

@coversNothing The @coversNothing annotation can be used in the test code to specify that no code coverage information will be recorded for the annotated test case. This can be used for integration testing. See Example 11.3 for an example. The annotation can be used on the class and the method level and will override any @covers tags.

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

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

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