PHP:
Writing tests

How to:

Native PHP – PHPUnit

A widely-used tool for testing in PHP is PHPUnit. Install it via Composer:

composer require --dev phpunit/phpunit ^9

Writing a simple test:

Create a CalculatorTest.php file in a tests directory:

use PHPUnit\Framework\TestCase;

// Assuming you have a Calculator class that adds numbers
class CalculatorTest extends TestCase
{
    public function testAdd()
    {
        $calculator = new Calculator();
        $this->assertEquals(4, $calculator->add(2, 2));
    }
}

Run the tests with:

./vendor/bin/phpunit tests

Sample output:

PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:00.005, Memory: 6.00 MB

OK (1 test, 1 assertion)

Third-party Libraries – Mockery

For complex testing, including mocking objects, Mockery is a popular choice.

composer require --dev mockery/mockery

Integrating Mockery with PHPUnit:

use PHPUnit\Framework\TestCase;
use Mockery as m;

class ServiceTest extends TestCase
{
    public function tearDown(): void
    {
        m::close();
    }

    public function testServiceCallsExternalService()
    {
        $externalServiceMock = m::mock(ExternalService::class);
        $externalServiceMock->shouldReceive('process')->once()->andReturn('mocked result');

        $service = new Service($externalServiceMock);
        $result = $service->execute();

        $this->assertEquals('mocked result', $result);
    }
}

To run, use the same PHPUnit command as above. Mockery allows for expressive and flexible mock objects, facilitating testing of complex interactions within your application.