@uses

@uses The @uses annotation specifies code which will be executed by a test, but is not intended to be covered by the test. A good example is a value object which is necessary for testing a unit of code. /** * @covers BankAccount::deposit * @uses Money */ public function testMoneyCanBeDepositedInAccount() { // ... } This annotation is especially useful in strict coverage mode where unintentionally covered code will cause a test to fail. See the section called “Unintentionally Covere

assertStringEqualsFile()

assertStringEqualsFile() assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = '']) Reports an error identified by $message if the file specified by $expectedFile does not have $actualString as its contents. assertStringNotEqualsFile() is the inverse of this assertion and takes the same arguments. Example A.47: Usage of assertStringEqualsFile() <?php use PHPUnit\Framework\TestCase; class StringEqualsFileTest extends TestCase { public function testFailur

assertClassHasAttribute()

assertClassHasAttribute() assertClassHasAttribute(string $attributeName, string $className[, string $message = '']) Reports an error identified by $message if $className::attributeName does not exist. assertClassNotHasAttribute() is the inverse of this assertion and takes the same arguments. Example A.2: Usage of assertClassHasAttribute() <?php use PHPUnit\Framework\TestCase; class ClassHasAttributeTest extends TestCase { public function testFailure() { $this->assertCla

assertObjectHasAttribute()

assertObjectHasAttribute() assertObjectHasAttribute(string $attributeName, object $object[, string $message = '']) Reports an error identified by $message if $object->attributeName does not exist. assertObjectNotHasAttribute() is the inverse of this assertion and takes the same arguments. Example A.40: Usage of assertObjectHasAttribute() <?php use PHPUnit\Framework\TestCase; class ObjectHasAttributeTest extends TestCase { public function testFailure() { $this->assert

@runTestsInSeparateProcesses

@runTestsInSeparateProcesses Indicates that all tests in a test class should be run in a separate PHP process. use PHPUnit\Framework\TestCase; /** * @runTestsInSeparateProcesses */ class MyTest extends TestCase { // ... } Note: By default, 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 seri

assertDirectoryIsWritable()

assertDirectoryIsWritable() assertDirectoryIsWritable(string $directory[, string $message = '']) Reports an error identified by $message if the directory specified by $directory is not a directory or is not writable. assertDirectoryNotIsWritable() is the inverse of this assertion and takes the same arguments. Example A.13: Usage of assertDirectoryIsWritable() <?php use PHPUnit\Framework\TestCase; class DirectoryIsWritableTest extends TestCase { public function testFailure() {

assertIsWritable()

assertIsWritable() assertIsWritable(string $filename[, string $message = '']) Reports an error identified by $message if the file or directory specified by $filename is not writable. assertNotIsWritable() is the inverse of this assertion and takes the same arguments. Example A.32: Usage of assertIsWritable() <?php use PHPUnit\Framework\TestCase; class IsWritableTest extends TestCase { public function testFailure() { $this->assertIsWritable('/path/to/unwritable'); }

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

assertJsonFileEqualsJsonFile()

assertJsonFileEqualsJsonFile() assertJsonFileEqualsJsonFile(mixed $expectedFile, mixed $actualFile[, string $message = '']) Reports an error identified by $message if the value of $actualFile does not match the value of $expectedFile. Example A.33: Usage of assertJsonFileEqualsJsonFile() <?php use PHPUnit\Framework\TestCase; class JsonFileEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonFileEqualsJsonFile( 'path/to/fixtu

assertContainsOnlyInstancesOf()

assertContainsOnlyInstancesOf() assertContainsOnlyInstancesOf(string $classname, Traversable|array $haystack[, string $message = '']) Reports an error identified by $message if $haystack does not contain only instances of class $classname. Example A.9: Usage of assertContainsOnlyInstancesOf() <?php use PHPUnit\Framework\TestCase; class ContainsOnlyInstancesOfTest extends TestCase { public function testFailure() { $this->assertContainsOnlyInstancesOf( Foo::cl