Coding standards
Security in ResourceSpace
Developer reference
Database

Automated Testing

Available from v11.1

Automated testing helps us verify that the application behaves as expected and gives us confidence when making changes.

We use PestPHP as our testing framework. Tests should be considered part of the development process, not an afterthought: new features, bug fixes and changes to existing behaviour should be covered by appropriate automated tests where practical.

This guide explains how we approach automated testing in ResourceSpace, including how tests are structured, how to run them and the conventions or patterns we expect developers to follow.

Environment setup

Your development/test run environment will require PHP 8.4+ for all dependency criteria to be met. Composer might complain about additional requirements (e.g. sockets, pcntl).

Ensure your development dependencies are installed:

composer install
pnpm install --frozen-lockfile
pnpm exec playwright install --with-deps chromium

Then you can run all tests (unit, integration and E2E) with:

composer test:full

Writing tests

Browser (E2E) testing

The browser test runner supports two optional command line flags for skipping the normal test environment setup and teardown steps. The flags are passed to the browser test runner after Composer's argument separator (--).

  • Use --nosetup to skip `composer test:env:provision` - useful when the test environment has already been provisioned and you want to rerun browser tests without recreating it;
composer test:browser -- --nosetup
  • Use --noteardown to skip `composer test:env:cleanup` - leaves the test environment in place after the test run. Default behaviour for failed tests, however can be useful during development when combined with --nosetup for a fast write code-test loop. For obvious reasons, when combining these two flags, always run the test with --noteardown first then with both of them.
composer test:browser -- --noteardown

Hooks

PestPHP provides hooks that allow setup and clean-up code to run automatically at different points during a test run. Hooks are useful for preparing test data, configuring the test environment and resetting any state changed by tests.

The main hooks are:

  • beforeEach() and afterEach() which run before or after every test in the current test file, and;
  • beforeAll() and afterAll() which run once before or after all tests in the file.

Hooks can also be scoped to a group of tests by defining them inside a describe() block.

Example - configure environment for all browser (e2e) tests in a file:

beforeAll(fn() => setTestConfiguration(<<<'PHP'
    $upload_then_edit = false;
    $example = 'value';
PHP));

afterAll(fn() => setTestConfiguration(''));

Example - configure environment only for one browser (e2e) test:

afterAll(fn() => setTestConfiguration(''));

test('example description', function (): void {
    setTestConfiguration('$show_resource_type = true;');
    ...
});