This feature is supported in the Ultimate edition only.
The following is only valid when PHP Plugin is installed and enabled!
IntelliJ IDEA
supports unit testing of PHP applications through integration with the
PHPUnit
tool.
On this page:
Before you start
Make sure the PHP plugin is installed and enabled. The plugin is not bundled with IntelliJ IDEA, but it can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
Installing PHPUnit
You can use PHPUnit in IntelliJ IDEA in the following ways:
-
Install PHPUnit and run it from PEAR that is configured as include path. The advantage of this approach is that once installed,
PHPUnit can be re-used in other IntelliJ IDEA projects.
The PHPUnit installation procedure depends on the operating system you use and your system settings. Please, refer to the PHPUnit installation instructions
for information on installing and configuring this tool.
- Install PHPUnit using the Composer Dependency Manager and run this instance.
-
Without installing PHPUnit. Just run it from the
phpunit.phararchive.
The PHPUnit installation procedure depends on the operating system you use and your system settings.
Please, refer to the PHPUnit installation instructions
for information on installing and configuring this tool.
- Open the Settings dialog box, and click PHP. The PHP page opens.
- From the CLI Interpreter drop-down list, choose the PHP interpreter to use.
-
Configure the
PEARfolder as an include path. Click the Add button in the Include path area, and select thePEARfolder in the dialog box, that opens. - Click PHPUnit under the PHP node, and on the PHPUnit page that opens choose Load from include path.
The Composer Dependency Manager
can download PHPUNit and add it to the current project.
The tool loads packages using the autoload.php file in the vendor folder.
-
Enable Composer in your project. Do one of the following:
-
Download
composer.phar, configure Composer as a command line tool, and initialize it in your project in the command line mode. -
Set up Composer in your project through the user interface: choose and then
specify the location of an existing
composer.pharfile or have IntelliJ IDEA download the file. For more details, see Using Composer Dependency Manager.
When the initialization process is completed, theautoload.phpfile is stored in thevendorfolder.
-
Download
- Open the Settings dialog box, and click PHPUnit under the PHP node.
-
On the PHPUnit page that opens, choose Use custom loader and specify the location of the
autoload.phpin the Path to script text box below.
When you start a testing session for the first time, the autoload.php file starts, installs and runs PHPUnit.
You can download phpunit.phar manually and then tell IntelliJ IDEA to load the libraries from it or have IntelliJ IDEA download phpunit.phar automatically.
Depending on your preferences, do one of the following:
-
Download
phpunit.pharmanually:-
Download phpunit.phar
manually.
- Open the Settings dialog box, and click PHPUnit under the PHP node.
- On the PHPUnit page that opens, choose Path to phpunit.phar and specify the location of the archive in the Path to phpunit.phar text box below.
-
Download phpunit.phar
-
Have IntelliJ IDEA download
phpunit.phar:- Open the Settings dialog box, and click PHPUnit under the PHP node.
- On the PHPUnit page that opens, choose Path to phpunit.phar and click the Download phpunit.phar... link.
-
In the dialog box that opens, specify the folder to store the
phpunit.phararchive after download. IntelliJ IDEA starts the download. After the process is completed successfully the file is stored in the chosen folder and the path to this folder is displayed in the Path to phpunit.phar text box below.
Generating PHPUnit tests
You can write unit tests manually or have test stubs generated automatically based on the PHP classes that are subject for testing.
A Test class is a PHP class with the name <production class>Test.php.
For example, if the class to test is Myclass.php, the tests for it will be in MyclassTest.php.
IntelliJ IDEA also generates tests for classes defined among others within a PHP file. If several production classes are defined in one single file, for each generated test class IntelliJ IDEA will create a separate file.
Test classes are inherited from PHPUnit_Framework_TestCase.
The data for generating PHP test classes are specified in the Generate PHPUnit Test dialog box.
Make sure, the PHPUnit
tool is installed on your machine
and enabled in IntelliJ IDEA. For more information, see http://www.phpunit.de/manual/current/en/installation.html
and Testing with PHPUnit.
-
In the Project tool window, select the PHP class to create unit tests for, e.g.
MyPHPClassas shown in the image below, and choose New | PHPUnit | PHPUnit Test on the context menu of the selection.
-
In the Generate PHPUnit Test dialog box that opens, specify the following data:
- The fully qualified name of the class to be tested in the Class to test area. The specified name will be used to propose the Test Class Name. To use completion, press Control+Space and choose the relevant production class from the pop-up list. By default, the Name text box displays the name of the class on which the test generation was invoked.
-
The name of the test class. IntelliJ IDEA automatically composes the name from the production class name as follows:
<production class>Test.php. The test class name is displayed in the Name text box of the Test Class area. - The directory where the file with the test class will be stored. By default, it is the directory where the production class is stored. The default value is displayed in the Directory text box of the Test Class area. To specify another folder, click the Browse button and choose the relevant folder in the dialog box that opens. To use completion, press Control+Space and choose the relevant folder from the pop-up list.
- The namespace the test class will belong to. IntelliJ IDEA completes the namespace automatically based on the directory and displays the generated value in the Namespace text box. When the directory is changed, the namespace is changed accordingly. To use completion, press Control+Space and choose the relevant namespace from the pop-up list.
- The test file name. By default, the name is displayed in the Name text box and is the same as the test class name.
Check, accept, or update the predefined settings and click OK to initiate the test generation.
Alternatively, you select the PHP production class in question, choose New | PHPUnit | PHPUnit Test, and fill in the data manually.
When the test is ready, navigate back to the production class by choosing . For details, see Navigating Between Test and Test Subject.
- In the editor, open the file with the class to generate the test for.
-
Do one of the following:
- On the main menu, choose File | New | PHPUnit | PHPUnit Test.
-
On the context menu, choose Go To | Test on the context menu, then choose Create New Test in the pop-up list.

-
In the Generate PHPUnit Test dialog box that opens, specify the following data:
- The fully qualified name of the class to be tested in the Class to test area. To use completion, press Control+Space and choose the relevant production class from the pop-up list. The specified name will be used to propose the Test Class Name.
- The name of the test class. IntelliJ IDEA automatically generates the name and displays it in the Name text box of the Test Class area.
- The directory where the file with the test class will be stored. By default, it is the directory where the production class is stored. The default value is displayed in the Directory text box of the Test Class area. To specify another folder, click the Browse button and choose the relevant folder in the dialog box that opens. To use completion, press Control+Space and choose the relevant folder from the pop-up list.
- The namespace the test class will belong to. IntelliJ IDEA completes the namespace automatically based on the directory and displays the generated value in the Namespace text box. When the directory is changed, the namespace is changed accordingly. To use completion, press Control+Space and choose the relevant namespace from the pop-up list.
- The test file name. By default, the name is displayed in the Name text box and is the same as the test class name.
Check, accept, or update the predefined settings and click OK to initiate the test generation.
Besides generating test class stubs, IntelliJ IDEA can help you populate them by generating stubs for test methods.
-
Open the required test class in the editor, position the cursor anywhere inside the class definition, and do one of the following:
- On the main menu, choose , then choose PHPUnit Test Method from the Generate pop-up list.
- On the context menu, choose Generate, then choose PHPUnit Test Method from the Generate pop-up list.
-
Set up the test fixture, that is, have IntelliJ IDEA generate stubs for the code that emulates the required environment before test start and returns the original environment after the test is over.
For more details, see http://phpunit.de/manual/3.7/en/fixtures.html
.
- On the context menu, choose Generate | Override method.
- From the Choose methods to override dialog box that opens, choose SetUp or TearDown and click OK.
Grouping tests
Grouping tests
is helpful, for example,
to distinguish between tests to run in a production environment from those to run in your development environment.
You just need to create two groups and then include or exclude them depending on the current environment.
-
Tag the test with the @group
annotation in the format:
/** @group <group specification> */
-
Tag the test with the @author
annotation in the format:
/** @author <author specification> */
- In the Run/Debug Configuration: PHPUnit by HTTP dialog box, open the Test groups tab.
- To enable appointing groups to execute, select the Include/Exclude check box.
-
In the Group Name list, select the relevant group and do one of the following:
- To have the tests in the selected group executed, select the Include check box.
- To have the tests in the selected group skipped, select the Exclude check box.