PhpStorm 2018.1 Help

Creating PHP Tests

You can write tests manually or have the test class stubs generated automatically based on the PHP classes that are subject for testing. PhpStorm provides this ability for the PHPUnit, Codeception, and PHPSpec test frameworks.

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, PhpStorm will automatically compose the name for the corresponding PHPUnit test class as <MyClass>Test.php.

PhpStorm 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 PhpStorm will create a separate file.

Generating a test for a PHP class

  1. Do any of the following:

    • On the main menu, choose File | New. Then, choose PHP Test | Test or PHP Test | <Test framework> Test from the pop-up menu.
    • 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 on the context menu of the selection.
    • In the editor of the PHP class to be tested, position the caret inside the definition of the class. Then, choose Go To | Test on the context menu or press Ctrl+Shift+T and select Create New Test from the pop-up menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.

    The Create New PHP Test dialog opens:

    ps create new php test phpunit dialog
  2. In the Create New PHP Test dialog, specify the following data:
    1. The test file template based on which the test class will be generated. PhpStorm provides the built-in file templates for generating test classes with the following supported test frameworks: PHPUnit, Codeception, and PHPSpec.
    2. 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 Ctrl+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.
    3. The name of the test class. PhpStorm 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, PhpStorm will automatically compose the name for a PHPUnit test class as <MyClass>Test.php. The test class name is displayed in the Name text box of the Test Class area.
    4. The namespace the test class will belong to. PhpStorm 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 Ctrl+Space and choose the relevant namespace from the pop-up list.
    5. The test file name. By default, the name is displayed in the Name text box and is the same as the test class name.
    6. The directory where the test class file 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 a different folder, click the Browse button and choose the relevant folder in the dialog box that opens. To use completion, press Ctrl+Space and choose the relevant folder from the pop-up list.
    7. In the Generate test methods for area, select the checkboxes next to the production class methods you want to generate test method stubs for. PhpStorm 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.

    Check, accept, or update the predefined settings and click OK to initiate the test class generation.

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.

Generating a PHPUnit test method

Besides generating test class stubs, PhpStorm can help you populate them by generating stubs for PHPUnit test methods.

  1. 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 Code | Generate, 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.
  2. Set up the test fixture, that is, have PhpStorm generate stubs for the SetUp and TearDown methods. The SetUp method emulates the required environment before the test starts. The TearDown method returns the original environment after the test is over. For more details, refer to the PHPUnit documentation.
    1. On the context menu, choose Generate | Override Methods.
    2. From the Choose methods to override dialog box that opens, choose SetUp or TearDown and click OK.
Last modified: 13 February 2018

See Also