testing php applications

PHPUnit Installation And Configuration

PHPUnit is the standard xUnit-style testing framework in the PHP ecosystem. Install it as a development dependency, run the project-local binary, and keep configuration in the repository so developers and CI execute the same suite.

Install The Project Tool

Use Composer so the project records a compatible PHPUnit version. Run vendor/bin/phpunit, not an unrelated global binary. The compatible PHPUnit major depends on the PHP versions supported by the project, so check the current PHPUnit documentation when choosing a constraint.

Keep Configuration Reviewable

A phpunit.xml or phpunit.xml.dist file can define bootstrap loading, test suites, cache directory, coverage source directories, and strictness options. Keep environment-specific secrets out of committed configuration.

composer require --dev phpunit/phpunit
vendor/bin/phpunit
vendor/bin/phpunit --testsuite unit
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
    <testsuites>
        <testsuite name="unit">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="integration">
            <directory>tests/Integration</directory>
        </testsuite>
    </testsuites>
</phpunit>

The configuration keeps test discovery predictable. A real project may add coverage source directories and stricter options as its suite grows.

Common Mistakes

  • Using a global PHPUnit version that differs from CI.
  • Committing secrets in XML environment entries.
  • Mixing slow integration tests into every fast feedback command without a deliberate suite strategy.
  • Ignoring the supported PHP range when selecting PHPUnit.

What To Practise

  • Install PHPUnit as a project dependency.
  • Run the local binary and a named suite.
  • Recognise the role of PHPUnit configuration.

Practice

Practice: Configure A Small PHPUnit Project

Configure PHPUnit in a Composer practice project and run one passing test through the project-local binary.

Requirements

  • Install PHPUnit as a development dependency.
  • Use the project-local binary.
  • Name unit and integration suites.
  • Keep test bootstrap and secrets separate.

Create tests/Unit/SmokeTest.php, add the XML configuration from the lesson, and run the full suite plus the named unit suite.

Show solution
PHP example
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class SmokeTest extends TestCase
{
    public function testPhpunitRunsProjectTests(): void
    {
        $this->assertSame(4, 2 + 2);
    }
}

Then run:

composer require --dev phpunit/phpunit
vendor/bin/phpunit
vendor/bin/phpunit --testsuite unit
vendor/bin/phpunit --testsuite integration

Bootstrap Composer's generated autoloader in XML. Keep fast tests under tests/Unit, real boundary tests under tests/Integration, and credentials outside committed XML. Use the PHPUnit major compatible with the project runtime rather than copying a version constraint from another repository.