Php - PhpUnit

Card Puncher Data Processing

About

Unit test Framwork

Installation

Command Line

Get the phar file for your php version. See Requirement

  • Powershell
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
  • bash
cd phpHome
wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar --version

Idea/WebStorm Configuration

  • Load from include_path. You need to set it for Idea. See below or the Pear webpage.

Idea Php Include Path

  • Path to phpunit.phar: the location of the phpunit.phar must be given.

Idea Php Unit

  • Custom Working Directory. Generally, if the project include others technology, the php code is located in a child folder. You can set the default in the default.

Phpunit Default Working Dir

Management

Creating a test case

The TestClasses:

  • have the following name pattern classNameTest.php. For example, if the class to test is Myclass.php, the tests for it will be in MyclassTest.php.
  • are inherited from PHPUnit_Framework_TestCase.

The unit test are public methods that:

  • have the following prefix: test.
  • or have the @test annotation in a method's docblock
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:

  • phpunit is the runner
  • --bootstrap src/autoload.php is a bootstrap script commonly used to set up load the classes that are to be tested before execution. Example: autoload.php. .

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

  • A runtime test on an exception that should be thrown
try {
    functionThatShouldThrownAnException();
    $this->fail("Exception should be thrown");
} catch (ExceptionNotFound $e) {
  // other assertion or simply nothing
}

Documentation / Reference





Discover More
Phpinfo Mbstring Enabled
Dokuwiki - Dev environment Configuration and Installation

This is the installation and configuration of the dokuwiki development platform on Windows that we use. slow. Docker with the last WSL2 is really, really slow if you don't work in WSL two environment...
Card Puncher Data Processing
How to capture the output with the php output buffer

A basic explanation of the output buffer mechanism in php
Card Puncher Data Processing
How to resolve: Test code or tested code did not (only) close its own output buffers ?

When running phpunit test case, you may get this warning message The solution is that generally that you needs to end up your test with a output buffer level of 1. Write at the end of your risky test,...
Pear Ini Windows Permissions
Php - PEAR (PHP Extension and Application Repository) - Package Manager

PEAR is a framework and distribution system for reusable PHP components. See The PEAR package manager can: install packages create new packages, manage a registry of installed packages, check...



Share this page:
Follow us:
Task Runner