-
Notifications
You must be signed in to change notification settings - Fork 0
Configuring PHPUnit for PhpStorm
This HOWTO applies to the PhpStorm IDE running under both Linux and Windows.
There are 3 things you will need to have installed and configured in order to create unit tests for your PHP projects:
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();
?>
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.
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
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.
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.
To install and configure XDebug on Windows, follow these steps after using the XDebug wizard as described above:
- Download the recommended .dll and place it in your PHP install
folder. If you used Chocolatey, this should be
C:\php
. - Make a note of the configuration setting the XDebug wizard provides.
- 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.
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.
- 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. - 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.
Now that all the components are installed, you can configure the project to use them. In PhpStorm:
- Go to Settings -> Project Settings -> PHP
- 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.
- Expand the PHP node in Project Settings and select PHPUnit
- 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:
- Open the Run menu and select Edit Configurations...
- Create a new PHPUnit configuration
- Give the configuration a meaningful Name
- 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.
- 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:
- Creating PHPUnit Tests in the PhpStorm documentation.
- Writing Tests for PHPUnit in the PHPUnit manual.
For more information about unit testing in PhpStorm, see the Testing topic in the online help.