Testing with PHPUnit
IntelliJ IDEA supports unit testing of PHP applications through integration with the PHPUnit testing framework.
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 Managing plugins.
Downloading and installing PHPUnit
Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer Dependency Manager.
Download and install phpunit.phar manually
Download phpunit.phar from the PHPUnit Official website and save it on your computer:
If you need full coding assistance in addition to the ability of running PHPUnit tests, store phpunit.phar under the root of the project where PHPUnit will be later used.
If you only need to run PHPUnit tests and you do not need any coding assistance, you can save phpunit.phar outside the project.
Download and install phpunit.phar with Composer
From the composer.json context menu, choose . Alternatively choose from the main menu.
In the Manage Composer Dependencies Dialog that opens, select the phpunit/phpunit package from the Available Packages list, possibly using the search field.
Integrating PHPUnit with a IntelliJ IDEA project
Configure PHPUnit automatically
Store the phpunit.xml or phpunit.xml.dist configuration file under the project root.
Configure PHPUnit manually
On the Test Frameworks page that opens, click in the central pane and choose the configuration type from the list:
In local configurations, the default project PHP interpreter is used, see Default project CLI interpreters for details.
To use PHPUnit with a remote PHP interpreter, choose one of the configurations in the dialog that opens:
In the right-hand pane, choose the PHPUnit library installation type:
To use Composer autoloader, specify the path to the autoload.php file in the vendor folder. See Composer for details.
To run PHPUnit from phpunit.phar, download phpunit.phar, save the archive in the project root folder, and specify the path to it. For local configurations, you can download the archive by clicking the Download link. To use it in the current project, make sure a default PHP interpreter is defined.
When you click , IntelliJ IDEA detects and displays the PHPUnit version.
In the Test Runner area, appoint the configuration XML file to use for launching and executing scenarios.
By default, PHPUnit looks for a phpunit.xml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.
You can also type the path to a bootstrap file to have a PHP script always executed before launching tests. In the field, specify the location of the script. Type the path manually or click and select the desired folder in the dialog that opens.
Note that you can also provide an alternative configuration and bootstrap file when editing a PHPUnit run/debug configuration.
Generating a PHPUnit test for a class
Open the Create New PHP Test dialog by doing any of the following:
From the main menu, choose PHP Test | PHPUnit Test or PHP Test | from the context menu.. Then, choose either
In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose either New | PHP Test | PHPUnit Test or New| PHP Test | .
In the editor of the PHP class to be tested, position the caret inside the definition of the class. Then, choose Go To | Test from the context menu or press Ctrl+Shift+T and select Create New Test from the popup menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.
To create a test for a certain method, position the caret within the method declaration. The chosen method will be automatically selected in the Generate test methods for area of the Create New PHP Test dialog.
The Create New PHP Test dialog opens.
Provide the parameters of the generated test:
The test file template, that is, the template based on which IntelliJ IDEA will generate the test class. Make sure that either PHPUnit or PHPUnit6+ is selected in the Test file template list.
The name of the test class. IntelliJ IDEA automatically composes the name from the production class name as <production class>Test.php.
The folder for the test class file, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix, or the
directoryvalue specified in the phpunit.xml configuration file.
To specify a different folder, click next to the Directory field and choose the relevant folder.
The namespace the test class will belong to, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix.
The production class methods to generate test method stubs for. In the Generate test methods for area, select the checkboxes next to the required production class methods. To include inherited methods from parent classes, select the Show inherited methods checkbox.
IntelliJ IDEA will automatically compose the test methods' names as
test<production method>. You can customize the code templates used for generating test method stubs on the Code tab of the File and Code Templates settings page.
When the test is ready, you can navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigating between test and test subject.
Generate PHPUnit test methods
Open the required test class in the editor and position the caret anywhere inside the class definition.
Choose Generate in the context menu or press Alt+Insert. Then choose Test Method from the Generate list.
Set up the test fixture, that is, generate stubs for the code that emulates the required environment before test start and returns the original environment after the test is over:
Choose Generate in the context menu or press Alt+Insert. Then choose SetUp Method or TearDown Method from the Generate list.
For more details, see Fixtures on the PHPUnit Official website.
You can customize the code templates used for generating PHPUnit test methods on the File and Code Templates page of the Settings/Preferences dialog Ctrl+Alt+S. To quickly access this page, in the Generate list, select Edit template from the submenu of a method.
Running and debugging PHPUnit tests
You can run and debug single tests as well as tests from entire files and folders. IntelliJ IDEA creates a run/debug configuration with the default settings and a launches the tests. You can later save this configuration for further re-use.
Run or debug PHPUnit tests
Run or debug a single test
Open the test file in the editor, right-click the call of the test and choose Run '<test_name>' or Debug '<test_name>' from the context menu.
Run a selection of tests
Open the target file in the editor, right-click the desired test target, that is, a class or a method being tested, and either chooseor press Ctrl+Shift+T.
From the popup menu, select the tests to be executed. For multiple selection use Ctrl and Shift.
Press Ctrl+Shift+F10 to run the tests selection.
After a test session is over, IntelliJ IDEA automatically creates a run/debug configuration with its Test scope set to Composite. See Run/Debug Configuration: PHPUnit for details.
Save an automatically generated default configuration
Run or debug tests through a previously saved run/debug configuration
Create a custom run/debug configuration
In the Project tool window, select the file or folder with the tests to run and choose Create run configuration from the context menu. Alternatively, choose from the main menu, then click and choose PHPUnit from the list.
In the Run/Debug Configuration: PHPUnit dialog that opens, specify the scenarios to run, choose the PHP interpreter to use, and customize its behavior by specifying the options and arguments to be passed to the PHP executable.
Monitoring test results
The tab is divided into 2 main areas:
The left-hand area lets you drill down through all unit tests to see the succeeded and failed ones. You can filter tests, export results, and use the context menu commands to run specific tests or navigate to the source code.
The right-hand area displays the raw PHPUnit output.
Running PHPUnit tests automatically
You can have IntelliJ IDEA re-run tests automatically when the affected code is changed. This option is configured per run/debug configuration and can be applied to a test, a test file, a folder, or a composite selection of tests, depending on the test scope specified in this run/debug configuration.
Configure re-running tests automatically
Run the tests.
On the Test Runner tab, press the toggle button on the toolbar:
Optionally, click the button and set the time delay for launching the tests upon the changes in the code: