Essentials

Testing

Tempest is built with testing in mind. It ships with convenient utilities that make it easy to test application code without boilerplate.

Overview

Tempest uses PHPUnit for testing and provides an integration through the Tempest\Framework\Testing\IntegrationTest test case. This class boots the framework with configuration suitable for testing, and provides access to multiple utilities.

Testing utilities specific to components are documented in their respective chapters. For instance, testing the router is described in the routing documentation.

Running tests

If you created a Tempest application through the recommended installation process, you already have access to tests/IntegrationTestCase, which your application tests can inherit from.

In this case, you may use the composer phpunit command to run your test suite.

composer phpunit

Creating new test files

By default, PHPUnit is configured to look for test files that end in *Test.php in the root tests directory. You may create a such a file and make it extend IntegrationTestCase.

tests/HomeControllerTest.php
use Tests\IntegrationTestCase;

final class HomeControllerTest extends IntegrationTestCase
{
    public function test_index(): void
    {
        $this->http
            ->get('/')
            ->assertOk();
    }
}

Changing the location of tests

The phpunit.xml file contains a <testsuite> element that configures the directory in which PHPUnit looks for test files. This may be changed to follow any rule of your convenience.

For instance, you may colocate test files and their corresponding class by changing the suffix attribute in phpunit.xml to the following:

<testsuites>
	<testsuite name="Tests">
- 		<directory suffix="Test.php">./tests</directory>
+ 		<directory suffix="Test.php">./app</directory>
	</testsuite>
</testsuites>

Using Pest as a test runner

Pest is a test runner built on top of PHPUnit. It provides a functional way of writing tests similar to JavaScript testing frameworks like Vitest, and features an elegant console reporter.

Pest is framework-agnostic, so you may use it in place of PHPUnit if that is your preference. The installation process consists of removing the dependency on phpunit/phpunit in favor of pestphp/pest.

composer remove phpunit/phpunit
composer require pestphp/pest --dev --with-all-dependencies

The next step is to create a tests/Pest.php file, which will instruct Pest how to run tests. You may read more about this file in the dedicated documentation.

tests/Pest.php
pest()
    ->extend(Tests\IntegrationTestCase::class)
    ->in(__DIR__);

You may now run ./vendor/bin/pest to run your test suite. You might also want to replace the phpunit script in composer.json by one that uses Pest.