Mastering CLI Path Mapping and PHPUnit Setup for PHP Backend Projects
This guide walks through configuring CLI interpreter path mappings, setting up Docker container paths, installing PHPUnit, creating a comprehensive phpunit.xml configuration, organizing test directories, writing a bootstrap script, and generating test reports, providing a complete backend testing workflow for PHP projects.
Directory Structure Overview
The project follows a conventional layout where source code, configuration files, and test resources are organized into separate directories, as illustrated in the diagram.
CLI Interpreter Path Mapping
When configuring the CLI interpreter, only local path mappings need to be defined. Both Remote Path and Container Path should be left unset, as they are unnecessary and can cause confusion.
Path mappings: configure the local directory that the interpreter should use.
Remote Path: do not configure.
Docker Container Path: do not configure.
Setting Up the PHPUnit Test Framework
1. Install PHPUnit
composer require --dev phpunit/phpunit2. Create the PHPUnit configuration file (phpunit.xml)
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" cacheResult="false">
<testsuites>
<testsuite name="tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>3. Organize test code
Place all test classes under the tests/ directory. Each test file should end with Test.php so that PHPUnit can discover them automatically.
4. Bootstrap script (tests/bootstrap.php)
<?php
/**
* @desc bootstrap.php description
* @author Tinywan (ShaoBo Wan)
* @date 2021/11/9 18:00
*/
use Webman\Bootstrap;
use Webman\Config;
require_once __DIR__ . '/../vendor/autoload.php';
Config::load(config_path(), ['route', 'container']);
if ($timezone = config('app.default_timezone')) {
date_default_timezone_set($timezone);
}
foreach (config('autoload.files', []) as $file) {
include_once $file;
}
foreach (config('bootstrap', []) as $class_name) {
/** @var Bootstrap $class_name */
$class_name::start(null);
}5. Build directory for test reports
After running the tests, PHPUnit generates reports in the build/ directory, which can be inspected for test results and coverage information.
Running and Debugging Tests
With the configuration in place, execute vendor/bin/phpunit to run all tests and produce a report. The following image shows the debugging interface used to inspect test execution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
