This article provides a modern introduction to PHPUnit for a contemporary PHP development environment. We'll build a simple command-line tool that converts JSON to PHP arrays, demonstrating key PHPUnit concepts along the way.
We assume familiarity with object-oriented PHP (PHP 7 or higher). For a streamlined setup, use Homestead Improved, which pre-installs PHP 7 and simplifies the process. Some command-line usage is involved, but we'll guide you through it.
Key Concepts:
composer require phpunit/phpunit --dev
), and configuring phpunit.xml
to manage test suites and bootstrapping.PHPUnitFrameworkTestCase
, use assertions (assertEquals
, etc.) to define expected results, driving functional code development.Test-Driven Development Explained:
TDD involves writing tests to define how code should behave before writing the code itself. Assertions (assertEquals
, assertTrue
, etc.) check if the code meets expectations. A failed test indicates a need for code changes.
PHPUnit Overview:
PHPUnit provides tools (classes and executables) to simplify test writing and analysis. It generates reports showing code quality, coverage, and more.
Example Application: JSON to PHP Array Converter
We'll create a command-line tool to convert JSON files to PHP arrays. Assume a PHP 7 environment with Composer. If using Homestead Improved, ssh
into the VM (vagrant ssh
).
Project Setup:
cd Code git clone https://github.com/php-pds/skeleton converter cd converter composer require phpunit/phpunit --dev rm bin/* src/* docs/* tests/*
Front Controller (index.php in converter/public):
<?php echo "Hello world";
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="tests/autoload.php"> <testsuites> <testsuite name="converter"> <directory suffix="Test.php">tests</directory> </testsuite> </testsuites> </phpunit>
tests/autoload.php:
cd Code git clone https://github.com/php-pds/skeleton converter cd converter composer require phpunit/phpunit --dev rm bin/* src/* docs/* tests/*
composer.json (updated): (Replace template values with your project details)
<?php echo "Hello world";
Run composer dump-autoload
First Test (tests/SitePoint/Converter/ConverterTest.php):
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="tests/autoload.php"> <testsuites> <testsuite name="converter"> <directory suffix="Test.php">tests</directory> </testsuite> </testsuites> </phpunit>
Run Tests (php vendor/bin/phpunit
) (Expect failure initially)
Implement Converter Class (src/SitePoint/Converter/Converter.php):
<?php require_once __DIR__ . '/../vendor/autoload.php';
Re-run tests. (Should now pass)
The remainder of the tutorial details adding more tests, using data providers for cleaner code, and setting up code coverage reports using XDebug. The final sections include FAQs covering TDD and PHPUnit best practices. The complete code is available on Github (link provided in original text).
The above is the detailed content of Re-Introducing PHPUnit: Getting Started with TDD in PHP. For more information, please follow other related articles on the PHP Chinese website!