Getting Started with PHPUnit

Installation

To begin, you need to install PHPUnit. The recommended way to install PHPUnit is via Composer, a dependency manager for PHP.

  1. Install Composer: If you haven't installed Composer yet, you can do so by following the instructions on the Composer website.
  1. Create a new project directory:
   mkdir php-unit-testing
   cd php-unit-testing
  1. Initialize Composer:
   composer init
  1. Require PHPUnit:
   composer require --dev phpunit/phpunit

This command adds PHPUnit as a development dependency to your project.

Directory Structure

A common directory structure for a PHP project with tests might look like this:

php-unit-testing/
├── composer.json
├── vendor/
└── tests/
    └── ExampleTest.php

Writing Your First Test

Let's create a simple class and write a unit test for it.

  1. Create a class in a file named Calculator.php:
<?php
class Calculator {
    public function add($a, $b) {
        return $a + $b;
    }

    public function subtract($a, $b) {
        return $a - $b;
    }
}
  1. Create a test case in tests/CalculatorTest.php:
<?php
use PHPUnit\Framework\TestCase;

require '../Calculator.php'; // Adjust the path as necessary

class CalculatorTest extends TestCase {
    protected $calculator;

    protected function setUp(): void {
        $this->calculator = new Calculator();
    }

    public function testAdd() {
        $this->assertEquals(4, $this->calculator->add(2, 2));
        $this->assertEquals(0, $this->calculator->add(-1, 1));
    }

    public function testSubtract() {
        $this->assertEquals(0, $this->calculator->subtract(2, 2));
        $this->assertEquals(-2, $this->calculator->subtract(2, 4));
    }
}

Running Your Tests

To run your tests, execute the following command in your project root:

./vendor/bin/phpunit tests/CalculatorTest.php

You should see output similar to this:

PHPUnit 9.x.x by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 00:00.123, Memory: 10.00 MB

OK (2 tests, 2 assertions)

Best Practices for Writing Tests

  1. Use Descriptive Names: Name your test methods according to the functionality they test. This makes it easier to understand what each test does.
  1. Keep Tests Independent: Each test should be able to run independently of others. Avoid shared state between tests.
  1. Use Data Providers: For tests that require multiple sets of data, consider using data providers to keep your tests clean and organized.
  1. Test Edge Cases: Always consider edge cases and error handling in your tests to ensure robustness.
  1. Run Tests Regularly: Integrate running tests into your development workflow to catch issues early.

Summary of PHPUnit Features

FeatureDescription
AssertionsMethods to check expected outcomes (e.g., assertEquals)
Test FixturesSetup and teardown methods for preparing test environments
Mock ObjectsSimulate dependencies to isolate the unit being tested
Data ProvidersSupply multiple sets of data to a single test method
Code CoverageMeasure how much of your code is tested

Conclusion

Unit testing with PHPUnit is a powerful way to ensure the reliability of your PHP applications. By following the steps outlined in this tutorial, you can set up a testing environment, write your first tests, and adopt best practices for effective testing.

Learn more with useful resources: