Skip to content
AgileLogics
5 min read

Testing with PHPUnit

Prerequisites: Sending HTTP requests

PHPUnit is the standard PHP testing framework — a test method is any method starting with test inside a class extending TestCase.

use PHPUnit\Framework\TestCase;

class CalculatorTest extends TestCase
{
    public function testAddsTwoNumbers(): void
    {
        $calc = new Calculator();
        $this->assertEquals(5, $calc->add(2, 3));
    }

    public function testDivisionByZeroThrows(): void
    {
        $calc = new Calculator();
        $this->expectException(DivisionByZeroError::class);
        $calc->divide(1, 0);
    }
}
./vendor/bin/phpunit

Directly parallel to Java's JUnit and C#'s xUnit — Composer installs it as a dev dependency, and it discovers every *Test.php file automatically.