This guide shows how to test what you build on the framework:
Nothing needs mocking. A test starts the real application, with the real modules, and handles a request without any web server. One request takes about a millisecond.
| Folder | For |
|---|---|
tests/Feature/ |
tests that start the application and send it requests |
tests/Unit/ |
tests of one class on its own |
Both use the namespace App\Tests\, which loads from tests/.
vendor/bin/phpunit tests/Feature/DeskTest.php # one file
vendor/bin/phpunit --filter inbox # tests whose name contains "inbox"
composer test # everything
Extend App\Tests\Support\TestCase, and write a small helper that starts an
application for each test:
final class DeskTest extends TestCase
{
/** @param array<string, mixed> $config */
private function app(array $config = []): Application
{
$config['app']['debug'] ??= false;
return $this->shippedApplication($config)->boot();
}
}
shippedApplication() loads everything in modules/. The array you pass is
settings, in the same shape as the files in config/: passing
['Desk' => ['maintenance' => true]] is the same as a
config/Desk.php that returns ['maintenance' => true].
For every test it also:
system/ and no test inherits another test’s 429;add_hook() calls when the test ends, so one test’s listeners
cannot reach the next.Set
app.debugtofalsewhen the test is about what a visitor sees. In debug mode an error shows the framework’s diagnostic page instead of your error template.
$response = $this->app()->handle(Request::create('GET', '/contact'));
self::assertSame(200, $response->status());
self::assertStringContainsString('<h1>Contact us</h1>', $response->body());
Request::create() takes the method, the path, and options:
| Option | Example |
|---|---|
query |
['page' => 2], or write it in the path: /api/v1/messages?page=2 |
body |
an array is sent as a form; a string is sent as it is |
headers |
['Accept' => 'application/json', 'Content-Type' => 'application/json'] |
cookies |
['session' => $id] |
files |
['avatar' => new UploadedFile(...)] |
server |
['SCRIPT_NAME' => '/framework/index.php'] to test the application in a subfolder, with the path /framework/contact |
For JSON, send the body as a string with the content type, and read the answer back as data:
$response = $app->handle(Request::create('POST', '/api/v1/customers', [
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
'body' => '{"name":"Ada","email":"ada@example.test"}',
]));
self::assertInstanceOf(JsonResponse::class, $response);
self::assertSame(201, $response->status());
self::assertSame('Ada', $response->data()['data']['name']);
One application can handle several requests in a row, and sessions last as long as it does. So a test can act like a browser, as long as it carries the cookies itself.
This test sends the contact form and checks that the “thank you” message appears once:
private function cookie(Response $response, string $name): ?string
{
foreach ($response->cookies() as $cookie) {
if ($cookie->name === $name) {
return $cookie->value;
}
}
return null;
}
public function test_a_sent_message_redirects_and_flashes_once(): void
{
$app = $this->app();
$token = (string) $this->cookie($app->handle(Request::create('GET', '/contact')), Csrf::COOKIE);
$sent = $app->handle(Request::create('POST', '/contact', [
'cookies' => [Csrf::COOKIE => $token],
'body' => ['_token' => $token, 'email' => 'ada@example.test', 'body' => 'Hello'],
]));
$session = (string) $this->cookie($sent, SessionManager::DEFAULT_COOKIE);
$cookies = [Csrf::COOKIE => $token, SessionManager::DEFAULT_COOKIE => $session];
$next = $app->handle(Request::create('GET', '/contact', ['cookies' => $cookies]));
$after = $app->handle(Request::create('GET', '/contact', ['cookies' => $cookies]));
self::assertSame(303, $sent->status());
self::assertSame('/contact', $sent->header('Location'));
self::assertStringContainsString('Thank you. We will reply soon.', $next->body());
self::assertStringNotContainsString('Thank you.', $after->body());
}
What it does:
XSRF-TOKEN cookie._token field.Logging in works the same way: send your login route the token and the
details, keep the session cookie, and send it with later requests. An API
client instead sends 'headers' => ['Authorization' => 'Bearer <token>'].
A POST without a matching token is a 403. That is worth one test per
form.
With no database configured, repositories keep data in memory belonging to that
one application, so every app() starts empty. Put test data in through your own
repository methods:
$messages = $this->app()->container()->get(MessageRepository::class);
$messages->receive(['email' => 'a@example.test', 'body' => 'one']);
To test real SQL, use an in-memory SQLite database, which is equally fresh for each application, and create the tables with your migrations:
$app = $this->app(['database' => ['connections' => ['default' => ['dsn' => 'sqlite::memory:']]]]);
$db = $app->container()->get(ConnectionManager::class)->connection();
$this->migrate($app); // every module's migrations, as `php laika migrate` runs them
The test then has the same tables as production, made by the same files. See Migrations and seeders.
Listen inside the test, after the application has started:
$heard = [];
add_hook('note.added', static function (Note $note) use (&$heard): void {
$heard[] = $note->body();
});
// ... do the thing ...
self::assertSame(['Call Ada'], $heard);
add_filter() does the same for a value, which lets a test change what another
module would see.
The default queue store is sync, so a job runs inside push() and its effects
are there as soon as the handler returns.
To check what was queued rather than what it did, listen to job.queued:
$queued = [];
add_hook('job.queued', static function (mixed $envelope, Job $job) use (&$queued): void {
$queued[] = $job;
});
To queue jobs without running them, start the application with
['queue' => ['store' => 'memory']].
A command is an ordinary class, so the simplest test calls it:
private function console(): Output
{
$stream = \fopen('php://memory', 'w');
self::assertIsResource($stream);
return new Output($stream);
}
$status = $app->container()->get(AddNote::class)($this->console(), 'Buy milk');
console() gives the command somewhere to print that is not the test runner’s
screen.
To test the command line itself — how arguments are read, --help, the exit
code for a typo — run it through the console kernel, the way php laika does:
/** @return array{int, string} the exit code, and everything printed */
private function command(Application $app, string ...$arguments): array
{
$container = $app->container();
$stream = \fopen('php://memory', 'r+');
self::assertIsResource($stream);
$kernel = new ConsoleKernel(
$container->get(CommandRegistry::class),
$container->get(CommandDispatcher::class),
$container->get(HookEngine::class),
$container->get(ErrorHandler::class),
new Output($stream),
);
$status = $kernel->handle(ExecutionContext::cli(\array_values(['laika', ...$arguments])));
\rewind($stream);
return [$status, (string) \stream_get_contents($stream)];
}
[$status, $output] = $this->command($this->app(), 'notes:add', '');
self::assertSame(ConsoleKernel::USAGE, $status); // 2
The exit codes are ConsoleKernel::SUCCESS (0), FAILURE (1), USAGE (2) and
UNKNOWN (127).
ConsoleKernelandCommandRegistryare internal classes. Using them in a test is fine; using them in a module is not, because internal classes change without notice.
composer check looks at engine/ and tests/. Point the same tools at your
module:
vendor/bin/phpstan analyse modules/Desk
vendor/bin/php-cs-fixer fix --dry-run --diff --path-mode=override modules/Desk
One framework test already reads your modules:
DocumentationTest::test_no_module_uses_an_internal_class fails when anything in
modules/ uses a class that STABILITY.md marks Internal. If your module seems
to need one, that is worth raising as a question about the framework rather than
working around.
TestCase marks run(), output(),
count(), name(), status(), size() and others as final. Naming your
helper after one is a fatal error the moment the test file loads, with a
message about overriding a final method. Pick another name, as command()
above does.DefaultPagesSliceTest, which checks that only Shared ships and that / is
the default home page. Once modules/ holds your modules, those tests describe
a different application. Adapt or delete them; they are not about your module.app.debug left on, a
test that expects your 500 page gets the framework’s diagnostic page.| What you see | Why | Fix |
|---|---|---|
| “Cannot override final method” when the file loads | A helper is named after a PHPUnit method. | Rename it; see Traps. |
403 in a form test |
The CSRF cookie or the _token field is missing. |
Fetch the page first and send the cookie back, as above. |
no such table in a test |
The migrations have not run for this application. | Call $this->migrate($app) after starting it. |
| Data from one test appears in another | Both used the same application. | Start a fresh application per test; in-memory storage goes with it. |
| The framework’s own tests fail after you add a module | They describe a fresh install. | See Traps. |
More in Troubleshooting.