phpunit Logo

Introduction

Laravel phpunit implementation

Phpunit test structure

Phpunit test structure

Unit tests ( PWD: tests -> Unit -> ExamplesTest.php )

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

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

Integration tests ( PWD: tests -> Feature -> ExampleTest.php )

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');
        $response->assertOk();
    }
}

Run tests

php artisan test

Phpunit run tests

Generate report

vendor/bin/phpunit --coverage-html storage/coverage-report/

Phpunit run tests

Case we want to expose the coverage to public

Requirements to run test and generate report / php.init updates

How to check if xdebug module is loaded:

    php -m

How to install xdebug from mac:

    pecl install xdebug

How to install xdebug:

    apt-get -y install php-xdebug

How to know where is the php.init file:

    php --ini | grep "Loaded Configuration File"

Edit php.ini file: nano /opt/homebrew/etc/php/{version}/php.ini

    zend_extension="xdebug.so"
    xdebug.mode=coverage

Change the title and breadcrumbs of the report ( Usefully case the project is expose with a proxy reverse )