Setting Up PHPUnit for Laravel

Tutorial 1 of 5

Introduction

In this tutorial, we'll be setting up PHPUnit for Laravel, a powerful PHP framework that is recognized for its elegant syntax. PHPUnit is a widely-accepted unit testing framework for PHP which will allow you to test your code in isolation, ensuring that each unit of your code performs as expected.

By the end of this tutorial, you will be able to:

  • Install and configure PHPUnit for Laravel
  • Write and run your first simple unit test

Prerequisites:

You should have a basic understanding of PHP and Laravel. A local development environment with Laravel installed is also necessary.

Step-by-Step Guide

  1. Installation of PHPUnit

Laravel already comes with PHPUnit included in its dev dependencies. But, you can also install it globally on your machine for convenience. To install PHPUnit globally, run the following command in your terminal:

composer global require phpunit/phpunit
  1. PHPUnit Configuration for Laravel

Laravel ships with a phpunit.xml file in its root directory, which is already set up for application testing. The configuration defines how PHPUnit behaves when it is run in the test environment.

  1. Writing a Basic Test

Tests in Laravel are typically stored in the tests directory of your application. There are two subdirectories: Feature and Unit. Feature tests are tests that interact with your application from the "outside" like a user would, while Unit tests are meant to test the smallest parts of your application in isolation.

Code Examples

  1. Creating a Basic Test

Let's create a basic unit test. Run the following command:

php artisan make:test UserTest --unit

This will create a new test case in the tests/Unit directory.

The new UserTest.php file might look like this:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        $this->assertTrue(true);
    }
}

The test_example method is the test we're going to run. In this case, it's just testing that true is true.

  1. Running the Test

To run your tests, you can use the php artisan test command in your terminal:

php artisan test

You will see something like this as output:

PASS  Tests\Unit\UserTest
✓ basic test

Tests:  1 passed
Time:   0.02s

This indicates that the test was successful.

Summary

In this tutorial, we learned how to set up PHPUnit in Laravel, create a basic test, and run that test. To further your knowledge, you could learn more about automated testing and how you can integrate it into your development workflow.

Practice Exercises

  1. Exercise 1: Create a test for a Product model that checks if a product's name is a string.

  2. Exercise 2: Create a feature test that checks if a user can view a specific product's details.

Solutions:

  1. Solution to Exercise 1:
public function test_product_name_is_string()
{
    $product = new Product;
    $product->name = 'Example Product';
    $this->assertIsString($product->name);
}
  1. Solution to Exercise 2:
public function test_user_can_view_product()
{
    $product = Product::factory()->create();

    $response = $this->get('/product/'.$product->id);

    $response->assertSee($product->name);
}

Keep practicing and happy coding!