PHP Backend Testing (Pest)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The PHP backend of the Duplicator project utilizes the Pest testing framework to ensure the reliability and correctness of its management modules. Pest is built on top of PHPUnit, providing a more expressive and functional syntax for writing tests. Within this project, Pest is primarily used for Unit and Feature testing of the administrative logic, including database management, site settings, and machine statistics.
The testing suite is integrated into the PHP application layer (located in the /app directory), allowing developers to validate business logic independently of the Electron frontend. It leverages SQLite in-memory or temporary databases to simulate production environments during test execution.
Sources: app/composer.json:11-15, app/tests/Pest.php:1-10
Testing Architecture
The testing environment is configured via phpunit.xml and initialized through the Pest.php configuration file. The architecture focuses on isolation, where each test suite typically recreates a fresh database state to prevent side effects.
Test Initialization and Lifecycle
Pest uses "hooks" to manage the state before and after each test. Common hooks include beforeEach() to set up database schemas and afterEach() to clean up temporary files.
The diagram shows the lifecycle of a typical feature test, emphasizing the creation and destruction of the SQLite database.
Sources: app/tests/Feature/AdminStatsManagerTest.php:5-18, app/tests/Feature/AdminAideManagerTest.php:5-48
Global Helpers and Extensions
The project extends the Pest Expectation API and provides global helper functions in app/tests/Pest.php to streamline database and environment setup.
| Function | Purpose |
|---|---|
expect()->extend('toBeOne') | Custom assertion to check if a value is exactly 1. |
create_test_sqlite_database() | Generates a temporary .sqlite file and returns a PDO instance. |
configure_sqlite_conf(path) | Sets the $GLOBALS['conf'] array to point to the temporary test database. |
reset_i18n_environment() | Clears sessions and globals to test translation and locale logic. |
Sources: app/tests/Pest.php:24-65
Test Categories
Feature Testing
Feature tests validate the interaction between Managers and the database. These tests ensure that CRUD operations (Create, Read, Update, Delete) function correctly across different modules like AideManager, SiteManager, and StatsManager.
Admin Aide Management
Tests for AideManager verify the management of machine Q&A (Questions and Answers) and help content.
- Add/Update QA: Validates that questions are stored with correct order and categories.
- Machine Filtering: Ensures help content is correctly retrieved by machine name.
Sources: app/tests/Feature/AdminAideManagerTest.php:53-83
Site Settings and Statistics
Tests for SiteManager and StatsManager verify global application configurations and data aggregation.
- Setting Persistence: Checks if booleans (like
show_mailing_list) are correctly persisted in thesite_settingstable. - Stats Calculation: Aggregates counts from multiple tables (
a3,a4,photocop,news,email) to verify global dashboard statistics.
Sources: app/tests/Feature/AdminSiteManagerTest.php:24-48, app/tests/Feature/AdminStatsManagerTest.php:20-33
The sequence diagram illustrates how SiteManager interacts with the database to calculate statistics during a test run.
Sources: app/tests/Feature/AdminSiteManagerTest.php:38-48
Configuration and Dependencies
The testing environment relies on specific PHP dependencies and XML configurations to define the scope of execution.
Composer Requirements
Testing dependencies are managed in the require-dev section of the backend configuration.
- pestphp/pest: The core testing framework.
- pestphp/pest-plugin: Support for additional Pest features.
Sources: app/composer.json:11-14
PHPUnit Configuration
The phpunit.xml file defines the test suites and code coverage reporting.
- Directory: Tests are located in the
./testsdirectory. - Coverage: Includes source code from
./appand./srcfor analysis. - Bootstrap: Uses
vendor/autoload.phpto handle class loading.
Sources: phpunit.xml:1-18
Summary
The Pest-based testing system in Duplicator provides a robust framework for ensuring backend stability. By using temporary SQLite databases and structured Manager classes, the project maintains high technical accuracy for its accounting and machine management logic. Global helpers in Pest.php ensure that environment-specific details, such as database paths and i18n states, are consistently handled across the test suite.