@author

@author The @author annotation is an alias for the @group annotation (see the section called “@group”) and allows to filter tests based on their authors.

@test

@test As an alternative to prefixing your test method names with test, you can use the @test annotation in a method's DocBlock to mark it as a test method. /** * @test */ public function initialBalanceShouldBe0() { $this->assertEquals(0, $this->ba->getBalance()); }

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

assertJsonStringEqualsJsonFile()

assertJsonStringEqualsJsonFile() assertJsonStringEqualsJsonFile(mixed $expectedFile, mixed $actualJson[, string $message = '']) Reports an error identified by $message if the value of $actualJson does not match the value of $expectedFile. Example A.34: Usage of assertJsonStringEqualsJsonFile() <?php use PHPUnit\Framework\TestCase; class JsonStringEqualsJsonFileTest extends TestCase { public function testFailure() { $this->assertJsonStringEqualsJsonFile( 'p

assertGreaterThan()

assertGreaterThan() assertGreaterThan(mixed $expected, mixed $actual[, string $message = '']) Reports an error identified by $message if the value of $actual is not greater than the value of $expected. assertAttributeGreaterThan() is a convenience wrapper that uses a public, protected, or private attribute of a class or object as the actual value. Example A.26: Usage of assertGreaterThan() <?php use PHPUnit\Framework\TestCase; class GreaterThanTest extends TestCase { public function t

Writing Tests for PHPUnit

Example 2.1 shows how we can write tests using PHPUnit that exercise PHP's array operations. The example introduces the basic conventions and steps for writing tests with PHPUnit: The tests for a class Class go into a class ClassTest. ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase. The tests are public methods that are named test*. Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method. Inside the test methods, assertion me

@backupStaticAttributes

@backupStaticAttributes The @backupStaticAttributes annotation can be used to back up all static property values in all declared classes before each test and restore them afterwards. It may be used at the test case class or test method level: use PHPUnit\Framework\TestCase; /** * @backupStaticAttributes enabled */ class MyTest extends TestCase { /** * @backupStaticAttributes disabled */ public function testThatInteractsWithStaticAttributes() { // ... } }

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

assertLessThan()

assertLessThan() assertLessThan(mixed $expected, mixed $actual[, string $message = '']) Reports an error identified by $message if the value of $actual is not less than the value of $expected. assertAttributeLessThan() is a convenience wrapper that uses a public, protected, or private attribute of a class or object as the actual value. Example A.36: Usage of assertLessThan() <?php use PHPUnit\Framework\TestCase; class LessThanTest extends TestCase { public function testFailure() {

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