Generate PHP tests
You can have the test class stubs generated automatically based on the PHP classes that are subject for testing. IntelliJ IDEA provides this ability for the PHPUnit, Codeception, and PHPSpec test frameworks. If you are using Behat framework, you have to create tests manually.
A Test class is a PHP class with its name derived from the production class name. For example, if the class to test is MyClass.php, IntelliJ IDEA will automatically compose the name for the corresponding PHPUnit test class as <MyClass>Test.php.
IntelliJ IDEA can generate tests for the classes that are defined in separate files as well as for the classes that are defined within a single PHP file. In the latter case, for each generated test class IntelliJ IDEA will create a separate file.
Do any of the following:
From the main menu, select PHP Test | Test or PHP Test | <Test framework> Test from the popup menu.. Then, choose
If you choose <Test framework> Test, the framework to generate the test for will be selected automatically in the Create New Test dialog. You can switch to a different framework prior to generating the test class.
In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose New | PHP Test | Test or PHP Test | <Test framework> Test from the selection context menu.
In the editor of the PHP class to be tested, position the caret at the class definition. Then, press Alt+Enter and select Create New PHP 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 methods list of the Create New PHP Test dialog.
The Create New PHP Test dialog opens:
In the Create New PHP Test dialog, specify the following data:
The test file template based on which the test class will be generated. IntelliJ IDEA provides the built-in file templates for generating test classes with the following supported test frameworks: PHPUnit, Codeception, and PHPSpec.
The name of the test class. IntelliJ IDEA automatically composes the name from the production class name according to the naming conventions of the chosen test framework. For example, if the class to test is MyClass.php, IntelliJ IDEA will automatically compose the name for a PHPUnit test class as <MyClass>Test.php.
The folder for the test class, 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 value provided in the configuration file of the corresponding test framework. To use path completion, press Ctrl+Space and choose the path from the list.
The namespace the test class will belong to. IntelliJ IDEA can complete the namespace automatically based on the configured PSR package prefixes and the values specified in the framework-specific configuration file. To use namespace completion, press Ctrl+Space and choose the relevant namespace from the list.
Select the checkboxes next to the production class methods you want to generate test method stubs for. 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.
After creating the test, you can navigate back to the production class by choosing Navigate between test and test subject.. For details, see