Skip to main content
Version: Next
PlatformNextopen-dxp/opendxpGitHub

Writing Tests

On this page you write one test and let it grow. It starts as a single check on the children of a page and ends as one test that covers documents and data objects alike. Along the way you learn where each piece of test code belongs. You also learn how tests stay independent of each other.

The page assumes a package that is set up as Testing describes. The examples use the factories of OpenDXP. Factories and Stories explains them in detail.

Put the test in its place​

A test that needs the application lives under tests/Feature. A test that needs nothing but PHP lives under tests/Unit. Unit tests get no test case, so they start no kernel and stay fast.

Every subject has a directory, and every file holds the tests of one subject. The file name names the subject and ends in Test.php. The children of a document go into tests/Feature/Document/ChildrenTest.php.

Write the test​

use OpenDxp\Test\Factory\DocumentPageFactory;

it('lists only the published children', function () {
$parent = DocumentPageFactory::createOne();
DocumentPageFactory::new()
->withParent($parent)
->create();
DocumentPageFactory::new()
->withParent($parent)
->unpublished()
->create();

$children = $parent->getChildren()->getDocuments();

expect($children)->toHaveCount(1);
});

The name is a sentence that says what is guaranteed. Pest puts it in front of it, so it reads it lists only the published children. Keep the sentence short and factual, and make sure it matches what the test checks.

The body has three blocks, separated by an empty line. The first prepares, the second acts, and the third checks. The first block starts right after function () {, without an empty line. A test has exactly one action. If you need a second action, you need a second test.

Two things stay out of a test:

  • A check before the action. If the parent had no children to begin with, the result would show it.
  • A check of PHP itself or of a plain setter and getter. A test checks what OpenDXP does.

Check more than one thing​

A test may check several results of its one action. Chain the checks with ->and():

it('lists only the published children', function () {
$parent = DocumentPageFactory::createOne();
$published = DocumentPageFactory::new()
->withParent($parent)
->create();
DocumentPageFactory::new()
->withParent($parent)
->unpublished()
->create();

$children = $parent->getChildren()->getDocuments();

expect($children)
->toHaveCount(1)
->and($children[0]->getId())
->toBe($published->getId());
});

A single check stays on one line. From two checks on, every call of the chain goes on a line of its own. The same holds for a factory chain that has more than one call.

A getter of the subject can go straight into the chain. Pest calls it on the value and checks the result. This is how the suite of OpenDXP checks a link document:

expect($link)
->toBeInstanceOf(Link::class)
->getInternal()
->toBe($target->getId());

Use the expectations of Pest, not the assertions of PHPUnit. If a model is loaded from the database, load it once into a variable and check that variable. Do not load it again in every line of the chain.

An exception is checked with ->throws() when the whole test is the action that throws:

it('refuses a value longer than its column', function () {
$object = UnittestFactory::new()
->unsaved()
->create(['input' => str_repeat('x', 500)]);

$object->save();
})->throws(ValidationException::class, 'Value in field [ input ] is longer than 190 characters');

In every other case, wrap the action in a closure:

expect(fn () => $this->handler->save($key, 'test'))
->toThrow(InvalidArgumentException::class, 'contains reserved characters');

Refresh an element​

OpenDxp\Test\Elements::refresh() loads an element the way the next request would. It clears the runtime cache of the process first. An element that the refreshed element loads in turn, like its parent or a related object, comes from the database as well:

use OpenDxp\Test\Elements;

$product->setName('Sneaker');
$product->save();

expect(Elements::refresh($product)->getName())->toBe('Sneaker');

The expectations of OpenDXP​

OpenDXP brings expectations for checks that Pest cannot make on its own. Test API shows how you register them.

toCarryField() checks that a data object carries the value it was given. Each field type compares values in its own way. The expectation therefore asks the field definition and does not compare the values directly:

expect(Elements::refresh($object))->toCarryField('consent', new Consent(true));

toCarryLocalizedField() does the same for one language of a localized field:

expect(Elements::refresh($object))
->toCarryLocalizedField('linput', $germanValue, 'de')
->toCarryLocalizedField('linput', $englishValue, 'en');

The redirect expectations check the response to a request. toRedirectTo() checks the location. A location that starts with a slash is compared with the path and the query only, so the test does not depend on the host. toBeAnsweredBy() names the redirect that answered, and toBeAnsweredWithoutRedirect() checks that none did:

expect($response)
->toRedirectTo('/new-page')
->toBeAnsweredBy($redirect);

Test API lists all of them.

Cover data objects with the same test​

Data objects have children too, and an unpublished object is left out in the same way as an unpublished document. You could copy the test and swap the factory. But two tests for the same behaviour drift apart over time. Write one test with a dataset instead:

use OpenDxp\Test\Factory\DocumentPageFactory;
use OpenDxp\Tests\Factory\UnittestFactory;

dataset('publishable elements', [
'a document' => DocumentPageFactory::class,
'an object' => UnittestFactory::class,
]);

it('lists only the published children', function (string $factory) {
$parent = $factory::createOne();
$factory::new()
->withParent($parent)
->create();
$factory::new()
->withParent($parent)
->unpublished()
->create();

$children = $parent->getChildren()->getDocuments();

expect($children)->toHaveCount(1);
})->with('publishable elements');

UnittestFactory builds objects of a class in the suite of OpenDXP. In your package, the factory of one of your own classes takes its place.

Pest runs the test once per entry and shows the key in the name of the test. Give every entry a key that reads as a case, such as a document or an object.

A dataset changes the data of a test, never its course. If the test needs an if, a match or a method name that depends on the dataset, the cases are different behaviours. They get tests of their own.

A dataset passes an object as a closure. Pest calls the closure after beforeEach. The closure can therefore use what beforeEach put on $this, and the parameter of the test keeps its type:

dataset('descendants', [
'the child' => fn () => $this->child,
'the grandchild' => fn () => $this->grandchild,
]);

it('gives a brick value of the parent to every descendant', function (Inheritance $descendant) {
// ...
})->with('descendants');

Define a dataset where it is used. A named dataset is only visible in that scope.

Used byWhere it goes
one testinline, with ->with([...])
several tests of one filedataset('name', ...) in that file
the tests of one directoryDatasets.php in that directory
the whole suitea file under tests/Datasets/

Share the setup​

Once several tests in the file need the same parent, move it into beforeEach. beforeEach puts the state it prepares on $this:

beforeEach(function () {
$this->parent = DocumentPageFactory::createOne();
});

it('leaves out an unpublished child', function () {
DocumentPageFactory::new()
->withParent($this->parent)
->unpublished()
->create();

$children = $this->parent->getChildren();

expect($children)->toBeEmpty();
});

Wrap tests in describe() only when a group of them shares a setup or a dataset that the rest of the file does not need.

Helpers, values and expectations of your own​

Sooner or later a test needs a small piece of code more than once. A function that keeps no state goes into the test file, below the use lines. Once a second file needs it, it moves to tests/Helpers/, into a file named after its subject in lowercase. A file name in PascalCase would promise a class. There is never a second helper for the same thing.

The suite of OpenDXP has a helper that turns a list of elements into their ids:

// tests/Helpers/element.php

function elementIds(array $elements): array
{
return array_map(
static fn (ElementInterface $element): int => $element->getId(),
array_values($elements),
);
}

A helper does real work. It never wraps a single getter, and it holds no expectation. A check that repeats is an expectation of your own. It goes into tests/Expectations/, its name follows the pattern of Pest (toBe..., toHave..., toCarry...), and it ends with return $this.

A function never returns an array whose keys describe a shape. Such a shape becomes a class. Put it under tests/Value/ as a final readonly class:

final readonly class SearchAnswer
{
/**
* @param list<string> $paths
*/
public function __construct(
public array $paths,
public int $total,
) {
}
}

A double for a service of the framework is a named class under tests/Application/. Its name says which slot it fills, like MockTranslator or MockResponseFactory. It is never an anonymous class in a test or a helper.

This table sums up where test code goes:

WhatWhere
State that beforeEach shares with the tests of a file$this
Behaviour that the tests of several directories shareA method of their test case, reached through $this
A helper without state for one test fileA function in that file, below the use lines
A helper without state for several test filesA function in tests/Helpers/<subject>.php
Something that builds a model and saves itA factory
A check that repeatsAn expectation in tests/Expectations/
Something other packages need for their tests tooA class with one task in the package itself, like OpenDxp\Test
A double for a service of the frameworkA named class in tests/Application/, like MockTranslator

The layout of a suite​

The directories of a suite follow from the table above:

tests/
Pest.php assigns the test cases and registers the expectations
Feature/ tests that boot the application, one directory per subject
Unit/ tests that boot nothing
Application/ the application of a bundle: kernel, configuration, templates, controllers, services
Factory/ factories for the models of this package
Story/ fixed worlds with fixed values
TestCase/ the test cases of this suite, not final
Datasets/ datasets for the whole suite
Helpers/ functions, no classes
Expectations/ expectations of this suite
Value/ value classes of this suite, final readonly
Fixtures/ data files and definitions

Leave out the directories you do not need.

Pest loads tests/Pest.php, everything under tests/Helpers/ and tests/Expectations/, every file named Datasets.php and everything under a directory Datasets/ on its own. You never include any of them by hand.

A test case of your own is not final, because Pest extends it for every test file. tests/Pest.php assigns it to directories. Two assignments with different test cases must not overlap, or Pest refuses to start. List the directories of the default test case one by one, one per line:

pest()->extend(TestCase::class)->in(
'Feature/Document',
'Feature/Element',
);
pest()->extend(SchemaTestCase::class)->in('Feature/Schema');

Format the code​

Every file of a suite should look as if one person wrote it. These habits get you there:

  • A chain that builds or checks something puts every call on a line of its own, once it has two calls. A single check such as expect($count)->toBe(2) stays on one line.
  • A chain that only fetches a value stays on one line: $page->getRootDocument()->getId(). Inside a longer expression it goes into sprintf() rather than a concatenation with ..
  • An array with two or more entries puts every entry on a line of its own. A dataset entry may be a tuple of plain values on one line: 'a parent id of zero' => [0, 1].
  • Arguments go one per line once one of them is not a plain value, or once the line gets longer than 120 characters.
  • A boolean or null whose meaning you cannot see at the call gets its name: getChildren(includingUnpublished: true).
  • A comment explains a reason that the code cannot show. It never repeats the code.

Keep tests apart​

Every test runs in a transaction that is rolled back. The test case also resets the static state of OpenDXP, as Static state describes. A test can therefore change a system setting and does not need to clean up:

use OpenDxp\Test\Factory\AssetImageFactory;
use OpenDxp\Test\Settings;

it('applies a system setting a test overrides', function () {
$asset = AssetImageFactory::createOne();
Settings::override([
'assets' => [
'frontend_prefixes' => [
'source' => 'https://cdn.example.test',
],
],
]);

$path = $asset->getFullPath();

expect($path)->toBe(sprintf('https://cdn.example.test%s', $asset->getRealFullPath()));
});

A test restores any other static or global state it changes, in afterEach() or ->after():

it('writes no version while versioning is off', function () {
$object = UnittestFactory::createOne();
Version::disable();

$object->save();

expect(versionCountOf($object))->toBe(1);
})->after(fn () => Version::enable());

Tests that cannot be rolled back​

A test that changes the schema or needs a full text index runs without the rollback, as Tests without rollback describes.

Tests that need another configuration​

The configuration of the test application holds only what all tests need. A test that needs a different configuration runs in its own environment. Another configuration shows how.

Tests that need a service​

A test against an external service, such as Redis, reads the address of the service from an environment variable. If the variable is missing, the test skips itself. External services lists the variables:

function redisDsn(): string
{
$dsn = getenv('OPENDXP_TEST_REDIS_DSN');
if (!is_string($dsn) || $dsn === '') {
test()->markTestSkipped('OPENDXP_TEST_REDIS_DSN names no Redis server.');
}

return $dsn;
}

A condition that is only known after beforeEach goes into ->skip() as a closure: ->skip(fn () => ..., 'reason').

Out of scope​

A bundle does not test its own migrations or its own installer. No test only checks that the test application installs. The application is installed before every run, so a broken installation fails there already. only(), todo(), dd() and ray() never reach the repository.

Next​

Factories and Stories shows how the factories you used here work. It covers pages with bricks, data objects of your own classes, and how you write a factory and a story yourself.