PHPUnit setup in PHPStorm 10.x

Setup a project specific testing environment

Posted by on March 01, 2016 PHPUnit setup in PHPStorm 10.x

PHPUnit is a well-known unit testing framework based on xUnit architecture for PHP developed by Sebastian Bergmann.

I show you how to setup a project specific testing environment with PHPUnit in PhpStorm 10.x within five steps. This testing environment allows you to test http and console applications.

1. Install PHPUnit with composer

Execute composer command

If you use composer as phar archive, then use php composer.phar.

If you use composer runtime, then use composer

Execute following command to install PHPUnit based in your PHP version.

Check PHP Version

CLI: php -v.

In browser: phpinfo();

PHP 5.6 and greater

$ composer require phpunit/phpunit

PHP 5.6 and lower

$ composer require phpunit/phpunit:~4.8

2. Create the PHPUnit config file

Create phpunit.xml in your project root folder.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="Package Test Suite">
            <directory suffix=".php">./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

3. Create test folder

Create the folder tests in your project root folder and register tests to autoloader in your composer.json.

...
  "autoload-dev": {
    "psr-4": {
      "App\\Tests\\": "tests"
    }
  }
...

Run composer dumpautoload to avoid autoload conflicts.

4. Create a new test with PhpStorm

5. Setup PHPUnit in PHPStorm for your current project

Remote PHP Intepreter

Setup remote PHP interpreter