Table of Contents

Php - PhpUnit

About

Unit test Framwork

Installation

Command Line

Get the phar file for your php version. See Requirement

cd phpHome
wget https://phar.phpunit.de/phpunit.phar -OutFile phpunit.phar
wget https://phar.phpunit.de/phpunit-7.5.9.phar -OutFile phpunit.phar # for the 7.5.9
php phpunit.phar --version
cd phpHome
wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar --version

Idea/WebStorm Configuration

Idea Php Include Path

Idea Php Unit

Phpunit Default Working Dir

Management

Creating a test case

The TestClasses:

The unit test are public methods that:

class MyClassTest extends PHPUnit_Framework_TestCase
{

    public function test()
    {
        $this->assertEquals('foo', 'foo');
    }
}

Annotations to group, filter test

/**
    @group <group specification>
    @author <author specification>
*/

Run

# To run all test in the classFile  tests/myClassTest 
phpunit --bootstrap src/autoload.php tests/myClassTest 

# Or to execute all tests found declared in *Test.php sourcecode files in the tests directory.
phpunit --bootstrap src/autoload.php tests

# Or to execute all tests in a group
phpunit --bootstrap src/autoload.php --group plugins

# Or to exclude a group of test
phpunit --bootstrap src/autoload.php --exclude-group slow,internet

where:

Example of output:

PHPUnit 3.7.21 by Sebastian Bergmann.

Time: 0 seconds, Memory: 2.00Mb

OK (1 test, 1 assertion)

Others

Skipping test

$this->markTestSkipped('Skipped !' );

Testing for exception

try {
    functionThatShouldThrownAnException();
    $this->fail("Exception should be thrown");
} catch (ExceptionNotFound $e) {
  // other assertion or simply nothing
}

Documentation / Reference