PHP Unit Testing: A Simple Guide to Testing Your PHP Code

15 Min | September 26, 2024

In software development, ensuring the quality and reliability of your code is crucial, and unit testing plays a vital role in achieving that. Specifically for PHP applications, unit testing helps you catch errors early, validate your business logic, and maintain code stability as your project evolves.

But what exactly is unit testing, and how can you implement it effectively in your PHP projects? This article will guide you through the essentials of PHP unit testing, exploring the tools, techniques, and best practices that will elevate your coding process and help you deliver robust applications.

What is Unit Testing?

Unit testing is a critical practice in software development. In unit testing, individual components of an application, known as units, are tested independently to ensure they function correctly. A unit typically refers to the most minor code that performs a specific function, such as a single function or method.

By isolating these units, developers can verify that each application part behaves as intended, catching potential errors early in development. This testing is often automated, allowing developers to run and re-run tests as they modify their code quickly.

Unit testing is essential because it helps maintain code quality, reduces bugs, and makes it easier to update or refactor code without breaking existing functionality.

What is PHPUnit?

PHPUnit is a robust and widely used testing framework designed specifically for PHP. It allows developers to perform unit testing by providing a structured and efficient way to test individual code units. As an implementation of the xUnit architecture, PHPUnit is tailored to suit the needs of PHP developers, making it straightforward to set up and begin testing.

With PHPUnit, you can create automated tests that help ensure your code behaves as expected, detect issues early, and maintain high code quality throughout development. Whether you’re building small projects or large-scale applications, PHPUnit is an essential tool in the PHP developer’s toolkit.

PHPUnit Installation

To start with PHPUnit, you have two primary installation options: globally on your server or locally within a specific project. Installing PHPUnit locally on a per-project basis is recommended for most development scenarios. You can do it using Composer, a PHP dependency manager.

  1. Create a New Project:

Open your terminal and run the following commands to set up a new project directory:

$ mkdir my-phpunit-project

$ cd my-phpunit-project

$ composer init

The first command creates a directory named my-phpunit-project, and the second command navigates into it. The third command starts an interactive setup for Composer.

  1. Follow the Interactive Setup:

During the Composer initialization process, you’ll be prompted to enter details about your project. You can accept the default values or provide specific information such as project description, author name, and license. When prompted about dependencies, you can skip them initially since PHPUnit will be added as a development dependency.

  1. Add PHPUnit as a Development Dependency:

When asked if you want to define your development dependencies (require-dev), press Enter to accept. Type phpunit/phpunit to specify PHPUnit as a dev dependency:

Would you like to define your dev dependencies (require-dev) interactively [yes]? yes

Then enter:

phpunit/phpunit

  1. Complete the Setup:

Continue the setup, and Composer will generate a composer.json file for your project. The file should look something like this:

{

    “name”: “your-username/my-phpunit-project”,

    “require-dev”: {

        “phpunit/phpunit”: “^9.5”

    },

    “autoload”: {

        “psr-4”: {

            “YourNamespace\\”: “src/”

        }

    },

    “authors”: [

        {

            “name”: “Your Name”,

            “email”: “[email protected]

        }

    ],

    “require”: {}

}

  1. Install PHPUnit:

Run the following command to install PHPUnit and its dependencies:

$ composer install

This will download PHPUnit and set it up in your project’s vendor directory.

By following these steps, you’ll have PHPUnit installed and ready to use to test your PHP code. 

How to Write Tests in PHPUnit?

Writing tests in PHPUnit is straightforward once you become familiar with the conventions. Here’s a step-by-step guide to help you get started:

  1. Create a Test Class: To test a PHP class, you must create a corresponding test class. The convention is to name the test class by appending the Test to the name of the class being tested. For instance, if you have a Product class, your test class should be named ProductTest.
  2. Extend PHPUnit\Framework\TestCase: Your test class should extend the PHPUnit\Framework\TestCase class. This inheritance gives your test class the necessary methods and functionality to perform the tests.

use PHPUnit\Framework\TestCase;

class ProductTest extends TestCase

{

    // Test methods will go here

}

  1. Write Test Methods: Inside your test class, you will define public methods that test the specific functionality of the class. Each test method should start with the prefix test. For example, if you want to test a method named calculatePrice in the Product class, your test method should be named testCalculatePrice.

public function testCalculatePrice()

{

    // Test logic will go here

}

  1. Use Assertions: Within your test methods, use PHPUnit’s assertion methods to check if the actual output of your code matches the expected result. Common assertions include assertEquals, assertTrue, and assertFalse. For instance:

public function testCalculatePrice()

{

    $product = new Product();

    $result = $product->calculatePrice();

    $this->assertEquals(100, $result);

}

  1. Organize Your Tests: It’s a good practice to keep your test files in a dedicated directory, typically named tests, while placing your application code in a src directory. This separation helps maintain a clean project structure.

/src

    Product.php

/tests

    ProductTest.php

Following these conventions, you can write practical tests that ensure your PHP code behaves as expected and remains robust against future changes.

PHPUnit Testing Example

To illustrate how to use PHPUnit for testing, let’s consider a simple User class with methods we’ll test using PHPUnit. Below is an example of the User class and the corresponding test class.

Sample User Class

<?php

namespace MyApp;

use InvalidArgumentException;

class User

{

    public int $age;

    public array $favoriteMovies = [];

    public string $name;

    /**

     * @param int $age

     * @param string $name

     */

    public function __construct(int $age, string $name)

    {

        $this->age = $age;

        $this->name = $name;

    }

    public function tellName(): string

    {

        return “My name is ” . $this->name . “.”;

    }

    public function tellAge(): string

    {

        return “I am ” . $this->age . ” years old.”;

    }

    public function addFavoriteMovie(string $movie): bool

    {

        $this->favoriteMovies[] = $movie;

        return true;

    }

    public function removeFavoriteMovie(string $movie): bool

    {

        if (!in_array($movie, $this->favoriteMovies)) {

            throw new InvalidArgumentException(“Unknown movie: ” . $movie);

        }

        unset($this->favoriteMovies[array_search($movie, $this->favoriteMovies)]);

        return true;

    }

}

UserTest Class

Create a UserTest class in your tests directory. Below is how you can set up various tests for the User class:

<?php

namespace MyApp;

use PHPUnit\Framework\TestCase;

final class UserTest extends TestCase

{

    public function testClassConstructor()

    {

        $user = new User(30, ‘Alice’);

        $this->assertSame(‘Alice’, $user->name);

        $this->assertSame(30, $user->age);

        $this->assertEmpty($user->favoriteMovies);

    }

    public function testTellName()

    {

        $user = new User(30, ‘Alice’);

        $this->assertIsString($user->tellName());

        $this->assertStringContainsStringIgnoringCase(‘Alice’, $user->tellName());

    }

    public function testTellAge()

    {

        $user = new User(30, ‘Alice’);

        $this->assertIsString($user->tellAge());

        $this->assertStringContainsStringIgnoringCase(’30’, $user->tellAge());

    }

    public function testAddFavoriteMovie()

    {

        $user = new User(30, ‘Alice’);

        $this->assertTrue($user->addFavoriteMovie(‘Inception’));

        $this->assertContains(‘Inception’, $user->favoriteMovies);

        $this->assertCount(1, $user->favoriteMovies);

    }

    public function testRemoveFavoriteMovie()

    {

        $user = new User(30, ‘Alice’);

        $user->addFavoriteMovie(‘Inception’);

        $this->assertTrue($user->removeFavoriteMovie(‘Inception’));

        $this->assertNotContains(‘Inception’, $user->favoriteMovies);

        $this->assertEmpty($user->favoriteMovies);

    }

}

Running the Tests

You can run all the tests in your tests directory using the following command:

$ ./vendor/bin/phpunit –verbose tests

To run a specific test file:

$ ./vendor/bin/phpunit –verbose tests/UserTest.php

Explanation

  • testClassConstructor(): Verifies that the constructor correctly initializes the properties.
  • testTellName(): Checks if the tellName method returns a string containing the user’s name.
  • testTellAge(): Ensures the tellAge method returns a string that includes the user’s age.
  • testAddFavoriteMovie(): Tests if adding a movie correctly updates the favorite movies list.
  • testRemoveFavoriteMovie(): Validates that removing a movie updates the list and handles errors correctly.

This example demonstrates how to write and run tests in PHPUnit to ensure your User class behaves as expected.

Other Types of Testing in PHP

In addition to unit testing, various other types of testing can help ensure the quality and reliability of your PHP applications. Here’s a look at some of the most common types:

Integration Tests

Integration tests focus on how different modules or components of your application work together. Unlike unit tests, which test individual code units in isolation, integration tests check the interactions between components to ensure they function as expected when combined. These tests can involve external dependencies, such as databases or third-party services.

Example:

<?php

declare(strict_types=1);

namespace TheSoftwareHouse\HowToTest\Tests\Integration\Domain;

use PHPUnit\Framework\TestCase;

use TheSoftwareHouse\HowToTest\Domain\Order;

use TheSoftwareHouse\HowToTest\Domain\Product;

use TheSoftwareHouse\HowToTest\Domain\Shop;

class OrderTest extends TestCase

{

    public function testShouldAddProductsWithSuccess(): void

    {

        // Given

        $shop = new Shop(“My shop”, new Product(“Apple”, 5), new Product(“Chocolate”, 7), new Product(“Watermelon”, 20));

        $order = new Order($shop);

        $chocolate = new Product(‘Chocolate’, 2);

        // When

        $order->addProduct($chocolate);

        // Then

        self::assertSame($chocolate, $order->getProductByName(‘Chocolate’));

        self::assertSame(5, $shop->getProductByName(‘Chocolate’)->getQuantity());

    }

}

Integration tests are slower than unit tests but provide more comprehensive coverage of the interactions between components.

Acceptance Tests

Acceptance tests validate that the application meets the specified requirements. They are often written in a natural language format that non-technical stakeholders can understand, using tools like Behat or PHPSpec. They focus on the application’s overall behavior from an end-user perspective.

Example with Behat:

Feature: Making an order

  In order to buy products

  As a customer

  I should be able to make an order

  Scenario: Make an order for a one product

    Given there are 7 “Chocolate” products in “My shop” shop

    When I add 3 “Chocolate” products to my order

    Then the overall number of products in my order should be 3

    And “My shop” shop should have 4 “Chocolate” products left

Acceptance tests help ensure the application meets user requirements and behaves as expected in real-world scenarios.

Mutation Tests 

Mutation testing improves the quality of your unit tests by introducing small changes (mutations) to the source code to check if the tests can detect the modifications. The goal is to ensure your tests are robust and can catch errors.

Example Concept:

  1. Change a condition in the code (e.g., from if ($quantity < 0) to if ($quantity <= 0)).
  2. Run your tests to see if any fail.
  3. If tests pass, your tests might not be robust enough.

Mutation testing helps identify weaknesses in your test suite by ensuring it can catch intentional faults.

Static Code Analysis

Static code analysis involves examining the code without executing it. Before runtime, tools like PHPStan, PHP Mess Detector, and EasyCodingStandard analyze the codebase to identify potential issues, such as coding standard violations or potential bugs.

Example:

vendor/bin/phpstan analyse src –level=max

Static code analysis can help catch issues early in development and enforce coding standards.

Architecture Testing

Architecture testing ensures the application’s architecture adheres to the defined rules and boundaries between layers. Tools like Deptrac can enforce architectural constraints and prevent unwanted dependencies between application layers.

Example Depfile:

paths:

  – ./src

exclude_files:

  – ‘#.*test.*#’

layers:

  – name: Domain

    collectors:

      – type: className

        regex: ^TheSoftwareHouse\\HowToTest\\Domain\\*

  – name: Application

    collectors:

      – type: className

        regex: ^TheSoftwareHouse\\HowToTest\\Application\\*

  – name: Infrastructure

    collectors:

      – type: className

        regex: ^TheSoftwareHouse\\HowToTest\\Infrastructure\\*

  – name: Utils

    collectors:

      – type: className

        regex: ^TheSoftwareHouse\\HowToTest\\Utils\\*

ruleset:

  Infrastructure:

    – Application

    – Utils

    – Domain

  Application:

    – Domain

    – Utils

  Domain:

    – Utils

Architecture tests help maintain clean architecture and enforce design principles.

Manual Testing

Manual testing involves manually checking the application to ensure it meets functional requirements. It includes testing new features before code reviews, checking edge cases, and using tools like Postman for API testing.

Tips:

  • Verify that the new functionality meets the requirements.
  • Test various paths, not just the happy path.
  • Use Postman for API endpoints.

Performance Testing

Performance testing assesses how well the application handles load and stress. Tools like k6 allow you to simulate virtual users and measure the application’s performance under various conditions.

Example k6 Script:

import http from ‘k6/http’;

import { check, sleep } from ‘k6’;

export default function () {

  const res = http.get(‘https://example.com’);

  check(res, {

    ‘is status 200’: (r) => r.status === 200,

  });

  sleep(1);

}

Performance testing ensures the application can handle the expected load and perform efficiently under pressure.

Each type of testing has its strengths and focuses on different aspects of application quality. Combining these testing strategies will provide a more comprehensive evaluation of your PHP application.

Conclusion

Effective testing is crucial for maintaining high-quality PHP applications. By understanding and implementing various types of testing—unit tests, integration tests, acceptance tests, mutation tests, static code analysis, architecture testing, and manual testing—you can ensure that your code is reliable, maintainable, and free of critical issues.

Unit tests are foundational, offering quick feedback and helping to catch bugs early by focusing on individual components of your code. Integration tests build on this by verifying interactions between components, while acceptance tests validate that the application meets user requirements and business logic. Mutation testing and static code analysis enhance code quality by identifying potential vulnerabilities and inconsistencies. Architecture testing ensures adherence to design principles, and manual testing provides a final layer of assurance by simulating real-world usage.

Ultimately, a thoughtful and structured testing strategy not only helps identify and resolve issues but also contributes to a more efficient and effective development lifecycle, leading to better software and a more satisfied user base.

The Author

Anmol is a dedicated technical content writer known for her practical approach. She believes in experiencing processes firsthand before translating them into insightful content. Additionally, she is good at WordPress development and skills of digital forensics and cybersecurity. Beyond her professional endeavors, she enjoys playing sports games, particularly table tennis and badminton, valuing the balance between mental and physical agility.

Scroll to Top