Skip to content

Configuring PHPUnit for PhpStorm

Shawn South edited this page Mar 19, 2014 · 2 revisions

This HOWTO applies to the PhpStorm IDE running under both Linux and Windows.

Requirements

There are 3 things you will need to have installed and configured in order to create unit tests for your PHP projects:

  1. PHP
  2. A debugger (we will be using XDebug)
  3. PHPUnit

PHP

This tutorial was developed with PHP 5.3. See the next sections for installation instructions on your platform. Once PHP is installed, you will want to know the current configuration. You can obtain this information by running the following at the command-line:

$ php -i

or creating a .php file with the following content in your web server's directory and then viewing it in a browser:

<?php
phpinfo();
?>

Installing PHP on Linux

PHP is likely already installed on your Linux computer. You can check by doing any of the following:

  • Searching in your package manager to see if the php package is installed.
  • Typing which php at the command-line.

Installing PHP on Windows

If you don't want to install a complete PHP/web server solution I recommend using the Chocolatey package management system to install PHP on Windows. Once Chocolatey is installed, issue the following command:

cinst php

XDebug

Strictly speaking, you can still run tests without a debugger, but it will be much more difficult to troubleshoot failed tests without one. In addition to XDebug, PhpStorm also supports the Zend Debugger, but this tutorial focuses on the former.

First, determine the version of XDebug you need: Visit http://www.xdebug.org/wizard.php, paste the output of the PHP configuration you obtained in the previous section (from either method) into the textbox and follow the directions.

Installing XDebug on Linux

Again, it is likely you already have XDebug installed. The wizard in the previous step will tell you whether or not this is so - and provide directions if it is not.

Installing XDebug on Windows

To install and configure XDebug on Windows, follow these steps after using the XDebug wizard as described above:

  1. Download the recommended .dll and place it in your PHP install folder. If you used Chocolatey, this should be C:\php.
  2. Make a note of the configuration setting the XDebug wizard provides.
  3. Edit the C:\php\php.ini file and add or modify the following lines, using the value(s) provided by the wizard:
[XDebug]
zend_extension="php_xdebug.dll"
xdebug.remote_enable=1
xdebug.remote_port=""
xdebug.profiler_enable=1
xdebug.profiler_output_dir="C:\Windows\tmp>"

For more detailed information, see Configuring XDebug in the PhpStorm online help.

PHPUnit

The PhpStorm documentation describes several ways to install PHPUnit. This tutorial describes what I consider to be the easiest, fastest and most portable method. It also has the benefit of including PHPUnit in your source code repository where it can be available to the entire project team and remain at a stable version until the team is ready to update it.

  1. Download the Long Term Support (LTS) release, version 3.7, from http://phpunit.de/. WARNING: The PhpStorm documentation recommends letting Settings download the .phar file, but this downloads version 4 which is NOT compatible with version 7.1.3 of PhpStorm.
  2. Place the phpunit-lts.phar file in your project folder and add it to source control.

If you would prefer not to maintain a copy of PHPUnit in the project repository, use one of the other methods and modify the instructions in the next section accordingly.

Configuring PhpStorm

Now that all the components are installed, you can configure the project to use them. In PhpStorm:

  1. Go to Settings -> Project Settings -> PHP
  2. Set the PHP language level to 5.3 and the Interpreter to the desired PHP installation.
    • You may need to configure a new Interpreter with the appropriate PHP path, etc.
  3. Expand the PHP node in Project Settings and select PHPUnit
  4. Select Path to phpunit.phar and enter the full path to the phpunit-lts.phar file you downloaded in the previous section.

At this point, XDebug and PHPUnit should be set up and working. The final step is to define a build configuration that will run the unit tests you will create in your project:

  1. Open the Run menu and select Edit Configurations...
  2. Create a new PHPUnit configuration
    1. Give the configuration a meaningful Name
    2. Specify a Test scope:
      • Directory runs all the tests found in files in the specified folder.
      • Class runs all the tests in the specified PHP class.
      • Method runs a single test method.
    3. Click OK

You should now be able to select the new configuration when you run the project. This will open the Test Runner tab and run the specified unit tests.

For guidance on writing unit tests, see:

For more information about unit testing in PhpStorm, see the Testing topic in the online help.