Chapter 2: Writing Unit Tests with PestPHP

Chapter 2: Writing Unit Tests with PestPHP

In this chapter, we will delve into the world of unit testing using PestPHP, an elegant and enjoyable PHP testing framework. Unit testing is a crucial aspect of software development that involves testing individual components or units of code in isolation to ensure they function as expected. PestPHP provides a clean and expressive syntax for writing tests, making the process both efficient and enjoyable.

Getting Started with Unit Testing

Before we dive into writing tests, let's ensure that PestPHP is properly set up in our project. If you haven't done this yet, refer to Chapter 1: PestPHP Introduction Tutorial: A Step-by-Step Guide for Beginners.

Structuring Your Tests

PestPHP encourages a straightforward and organized directory structure for your tests. By default, it assumes that your test files reside in the tests directory. Inside this directory, you can organize your tests based on the components they are testing.

For example :

Here, we've created a Unit directory to house our unit tests. It's good practice to separate different types of tests, such as unit tests and feature tests, to maintain clarity in your project's test suite.

Writing Your First Unit Test

Now, let's write a simple unit test using PestPHP. In your EnumTest.php file inside the tests/Unit directory:

it('verify if enum returns correct value', function () {
    $enum = UserStatusEnum::class;

    expect($enum::ACTIVE->value)->toBe(1);
});

In this example, we've created a basic unit test using PestPHP's it function. It is testing whether the value of the ACTIVE enum from the UserStatusEnum class is 1.

Leveraging PestPHP's Assertions

PestPHP provides a variety of expressive assertions to validate the behavior of your code. Here are some examples:

<?php

it('can make assertions', function () {
    // Check if a variable is true
    expect(true)->toBeTrue();

    // Check if a variable is false
    expect(false)->toBeFalse();

    // Check if a variable is null
    expect(null)->toBeNull();

    // Check if a variable is an instance of a class
    expect($object)->toBeInstanceOf(SomeClass::class);

    // Check if a string contains a specific substring
    expect('Hello, World!')->toContain('Hello');
});

These assertions cover a wide range of scenarios and help ensure the correctness of your code. for more Architecture Testing | Pest - The elegant PHP Testing Framework (pestphp.com)

Running Your Tests

To execute your unit tests, run the following command in your terminal:

./vendor/bin/pest

PestPHP will automatically discover and execute your tests, providing clear and concise output.

Conclusion

In this chapter, we've explored the basics of writing unit tests with PestPHP. By structuring your tests logically and leveraging PestPHP's expressive syntax, you can create a robust suite of tests that validate the behavior of your code. As you continue your journey into unit testing, explore additional features provided by PestPHP and apply these principles to enhance the reliability of your software.

Did you find this article valuable?

Support Kasenda's Blog by becoming a sponsor. Any amount is appreciated!