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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering CLI Path Mapping and PHPUnit Setup for PHP Backend 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.

Directory structure diagram
Directory structure 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.

CLI interpreter settings
CLI interpreter settings

Setting Up the PHPUnit Test Framework

1. Install PHPUnit

composer require --dev phpunit/phpunit

2. 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.

Debugging interface
Debugging interface
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIDockerConfigurationunit testingBackend testingPHPphpunit
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.