PHP Cheat Sheet

phpunit ./tests/unit

Search and run all "*Test.php" files in "./tests/unit" directory or subdirectory.

<?php

use PHPUnit\Framework\TestCase;

final class SimpleTest extends TestCase
{

    public function testAddition(): void
    {
        $this->assertSame(2, 1 + 1);
    }

}

The test file must contain class matching the file name. A class method which name starts with "test" defines a test.

public function testHeaderRendering(): void
{
    $this->expectOutputString('<h1>NSWI142</h1>');
    print('<h1>NSWI142</h1>');
}

Working with output.

$addOperation = new class implements Operation {
    public function execute($left, $right) {
        return $left + $right;
    }
};

Example using anonymous class to implement (mock) an interface for a test.