Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,059 of 9,059 pages
About the help in other languages
All help pages

DB-less PHPUnit tests

Write fast PHPUnit tests for extension code that need no database - the bootstrap, loading your classes, testing services, validators, datatype string round trips and template operators, and running only the tests of a…

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/creators/app-creators/db-less-phpunit-tests.md

On this page

DB-less PHPUnit tests

This page is for application creators who write unit tests that run anywhere in seconds. Exponential's phpunit.xml loads tests/bootstrap.php, which loads Composer's autoloader and the test toolkit but does not boot the kernel; tests that need nothing else run without a database (PHPUnit).

Contents

  1. What to test without a database
  2. A test class
  3. Loading your classes
  4. Running
  5. Mapping tests to files

What to test without a database

CodeExample assertions
Services with pure logica report's totals, an import's parsing
Validatorsaccepted and refused inputs, error texts
Datatype string formsfromString( toString( x ) ) gives x
Template operatorsmodify() sets the expected value
Settings parsingdefaults when a key is missing
Security helpersescaping, allow lists, redirect checks

Keep database access out of these classes, or behind an interface you can replace in the test.

A test class

<?php
// extension/expdemo/tests/expDemoColorValidatorTest.php
class expDemoColorValidatorTest extends PHPUnit\Framework\TestCase
{
    public function testAcceptsHexColors()
    {
        $this->assertTrue( expDemoColorValidator::isValid( '#a1b2c3' ) );
    }

    public function testRefusesOtherText()
    {
        $this->assertFalse( expDemoColorValidator::isValid( 'red; background: url(x)' ) );
    }
}

Loading your classes

Classes in the extension autoload map are found once php bin/php/ezpgenerateautoloads.php -e has run. A test that runs in a separate checkout can require_once the class file when it is not autoloadable, as the kernel's own help tests do (tests/tests/kernel/classes/help/).

Running

vendor/bin/phpunit extension/expdemo/tests/
vendor/bin/phpunit --filter testRefusesOtherText extension/expdemo/tests/

Run it in your extension's CI on every push and pull request (Continuous integration for extensions).

Mapping tests to files

The kernel keeps tests/test-map.json, which maps source files to the tests that cover them, so a change runs only its own tests before a push. Do the same in a large extension: a small map file and a script that picks the tests of the changed files.

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x