Skip to content

Testing and Quality

mab056 edited this page Feb 15, 2026 · 3 revisions

Testing and Quality

Test Strategy

The repository uses three complementary layers:

  1. Unit tests (tests/Unit) with Brain\Monkey and Mockery
  2. Integration tests (tests/Integration) with WordPress Test Suite
  3. E2E tests (tests/e2e) with Playwright + @wordpress/env

Current test file counts:

  • Unit test files: 31
  • Integration test files: 24
  • PHP test files total: 55
  • E2E spec files: 5

Source files:

  • src/ PHP files: 31

Current test counts:

  • Unit tests: 574 (1336 assertions)
  • Integration tests: 322 (655 single-site / 684 multisite assertions)
  • E2E scenarios: 46 (138 local runs across 3 viewports; desktop-only in CI)

PHPUnit Layout

Configuration:

  • phpunit.xml.dist
  • Test bootstrap: tests/bootstrap.php

Suites:

  • unit
  • integration

Bootstrap behavior:

  • Always defines ABSPATH for source guards
  • Loads WP Test Suite only when integration suite is detected
  • Loads plugin through muplugins_loaded in integration mode
  • Supports WP_TESTS_MULTISITE env var for multisite mode

Unit Testing (Brain\Monkey)

Focus:

  • Isolated business logic
  • Hook and WP function interaction contracts
  • Architectural constraints (no singleton/static/final patterns)

Command:

  • composer test:unit

Integration Testing (WP Test Suite)

Focus:

  • Real WordPress APIs (options, transients, cron, admin pages)
  • DB-level and hook-level behavior
  • End-to-end flow inside WP runtime (excluding browser)

Setup:

  • composer install-wp-tests

Command:

  • composer test:integration

Multisite Testing

The bootstrap supports the WP_TESTS_MULTISITE environment variable:

// tests/bootstrap.php
if ( getenv( 'WP_TESTS_MULTISITE' ) ) {
    define( 'WP_TESTS_MULTISITE', true );
}

When active, the WordPress Test Suite loads WordPress as a multisite network.

Commands:

Command Description
composer test:integration:multisite Integration tests in multisite mode
composer test:coverage:multisite Multisite integration coverage (clover XML)
composer test:coverage 3 suites: unit + integration + multisite

Dedicated tests in tests/Integration/Core/UninstallerTest.php (3 multisite tests):

if ( ! is_multisite() ) {
    $this->markTestSkipped( 'Requires multisite.' );
}

Coverage: the is_multisite() branch in Uninstaller::uninstall() is 100% covered only by combining single-site (the else branch) + multisite (the if branch). The separate clover XML reports (clover-integration.xml + clover-multisite.xml) are automatically merged by Codecov.

For more details see Multisite Support.

E2E Testing (Playwright)

Tech stack:

  • @playwright/test
  • @wordpress/env (Docker WordPress)

Viewport projects:

  • Local: desktop, tablet, mobile
  • CI: desktop only

Key config:

  • workers: 1
  • timeout: 30s local / 60s CI
  • retries: 1 local / 2 CI

Commands:

  • npm run env:start
  • bash bin/e2e-setup.sh
  • npm run test:e2e
  • npm run env:stop

Pattern Enforcement Tests

Every test class MUST include two pattern enforcement tests:

/**
 * Verifies that the class is NOT final
 */
public function test_class_is_not_final() {
    $reflection = new \ReflectionClass( MyClass::class );
    $this->assertFalse( $reflection->isFinal(), 'Class should NOT be final' );
}

/**
 * Verifies that NO static methods exist (excluding magic methods)
 */
public function test_no_static_methods() {
    $reflection = new \ReflectionClass( MyClass::class );
    $methods    = $reflection->getMethods( \ReflectionMethod::IS_STATIC );

    $static_methods = array_filter( $methods, function( $method ) {
        return strpos( $method->getName(), '__' ) !== 0;
    } );

    $this->assertEmpty( $static_methods, 'Class should have NO static methods' );
}

Notes:

  • The strpos($name, '__') !== 0 filter excludes magic methods (__callStatic, etc.) from the check
  • This requirement is non-negotiable and verified in code review
  • Pattern enforcement tests are present in all 55 PHP test classes

Static Analysis and Coding Standards

PHPCS:

  • Config: .phpcs.xml.dist
  • Standards: WordPress + WordPress-Extra + WordPress-Docs + PHPCompatibilityWP
  • Command: composer phpcs

PHPStan:

  • Level: 6
  • Config: phpstan.neon
  • WordPress extension: szepeviktor/phpstan-wordpress
  • Command: composer analyse

Coverage and Quality Gates

Coverage commands:

  • composer test:coverage — runs 3 suites: unit + integration + multisite
  • composer test:coverage:unit
  • composer test:coverage:integration
  • composer test:coverage:multisite

Clover XML output:

  • coverage/clover-unit.xml
  • coverage/clover-integration.xml
  • coverage/clover-multisite.xml

Codecov configuration:

  • codecov.yml
  • Separate flags: unit, integration
  • Single-site and multisite combined by Codecov with carryforward: true
  • Project target: 95%

Local Matrix Script

Script: bin/test-matrix.sh

Capabilities:

  • PHP matrix 7.4 to 8.5
  • Runs PHPCS + PHPStan + PHPUnit
  • Optional E2E execution
  • Supports sequential or parallel modes

Useful modes:

  • composer test:matrix
  • bin/test-matrix.sh --parallel
  • bin/test-matrix.sh --php 8.3
  • bin/test-matrix.sh --tests-only
  • bin/test-matrix.sh --e2e-only

Clone this wiki locally