PhpStorm 2018.2 Help

Testing with PHPUnit

PhpStorm supports unit testing of PHP applications through integration with the PHPUnit testing framework.

Before you start

Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.

Downloading and installing PHPUnit

To 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.

To download and install phpunit.phar with Composer

  1. On the context menu of composer.json, choose Composer | Manage Dependencies. Alternatively choose Tools | Composer | Manage Dependencies from the main menu.

  2. In the Manage Composer Dependencies Dialog that opens, select the phpunit/phpunit package from the Available Packages list, possibly using the search field. The list shows all the available packages; the packages that are already installed are marked with a checkmark.

    Choose the relevant version from the Version to install list.

  3. If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters field, type the additional command line parameters.

  4. Click Install.

Integrating PHPUnit with a PhpStorm project

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), expand the Languages and Frameworks node and select Test Frameworks under PHP.

    On the Test Frameworks page that opens, click Add icon in the central pane and choose the configuration type from the list:

    ps_settings_php_test_frameworks.png
    • 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:

      ps_settings_php_test_frameworks_phpunit_choose_php_interpreter.png
  2. 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.

      When you click icons actions refresh svg, PhpStorm detects and displays the PHPUnit version.

    • To run PHPUnit from PEAR, configure PEAR as an include path.

      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.

  3. 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 browseButton.png and select the desired folder in the dialog that opens.

Generating a PHPUnit test for a class

  1. Open the Create New PHP Test dialog by doing any of the following:

    • On the main menu, choose File | New. Then, choose PHP Test | PHPUnit Test from the context menu.

    • In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose New | PHP Test | PHPUnit 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 pop-up 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.

  2. The Create New PHP Test dialog opens.

    create new php test phpunit dialog

    Provide the parameters of the generated test:

    • The test file template, that is, the template based on which PhpStorm will generate the test class. Make sure that PHPunit is selected in the Test file template list.

    • The fully qualified name of the class to be tested, this name will be used to propose the Test Class Name. By default, the Name box displays the name of the class on which the test generation was invoked. To use completion, press Ctrl+Space.

    • The name of the test class. PhpStorm automatically composes the name from the production class name as <production class>Test.php. The test class name is displayed in the Name field of the Test Class area.

    • The folder for the test class. By default, it is the folder that is marked as a test sources root. If no such folder is specified, the folder containing the production class is proposed instead.

      To specify a different folder, click browseButton next to the Directory field and choose the relevant folder.

    • 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.

      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.

  3. Check, accept, or update the predefined settings and click OK to initiate the test 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

  1. Open the required test class in the editor, position the cursor anywhere inside the class definition, and choose Generate on the context menu. 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 code that emulates the required environment before test start and returns the original environment after the test is over:

    On the context menu, choose Generate | Override Methods, then choose SetUp or TearDown in the Choose methods to override dialog that opens.

    For more details, see Fixtures on the PHPUnit Official website.

Running and debugging PHPUnit tests

You can run and debug single tests as well as tests from entire files and folders. PhpStorm creates a run/debug configuration with the default settings and a launches the tests. You can later save this configuration for further re-use.

To run or debug PHPUnit tests

  • In the Project tool window, select the file or folder to run your tests from and choose Run '<file or folder>' or Debug '<file or folder>' on the context menu of the selection:
    ps_test_frameworks_phpunit_launch_tests.png

    PhpStorm generates a default run configuration and starts a run/debug test session with it.

To 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>' on the context menu.

To run a selection of tests

  1. Open the target file in the editor, right-click the desired test target, that is, a class or a method being tested, and either choose Go To | Test or press Ctrl+Shift+T.

  2. From the pop-up menu, select the tests to be executed. For multiple selection use Ctrl ( for macOS) and Shift keys.

    ps run tests selection
  3. Press Ctrl+Shift+F10 to run the tests selection.

After a test session is over, PhpStorm automatically creates a run/debug configuration with its Test scope set to Composite. See Run/Debug Configuration: PHPUnit for details.

To save an automatically generated default configuration

  • After a test session is over, choose Save <default_test_configuration_name> on the context menu of the file or folder.

To run or debug tests through a previously saved run/debug configuration

  • Choose the required PHPUnit configuration from the list on the toolbar and click artwork icons run play or icons actions startDebugger dark svg.

To create a custom run/debug configuration

  1. In the Project tool window, select the file or folder with the tests to run and choose Create run configuration on the context menu. Alternatively, choose Run | Edit Configurations on the main menu, then click Add icon and choose PHPUnit from the list.

  2. In the Run/Debug Configuration: PHPUnit dialog that opens, specify the scenarios to run and customize the behavior of the current PHP interpreter by specifying the options and arguments to be passed to the PHP executable file.

Monitoring test results

PhpStorm shows the tests execution results in the Test Runner tab of the Run Tool Window. The tab is divided into 2 main areas. In the left-hand area you can drill down through all unit tests to see the succeeded and failed ones. You can also 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:

ps_test_result_phpunit.png

Running PHPUnit tests automatically

You can have PhpStorm 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.

To configure re-running tests automatically

  1. Run the tests.

  2. On the Test Runner tab, press the ps_icon_phpunit_run_test_automatically.png toggle button on the toolbar:

    ps_phpunit_enable-auto-test.png
  3. Optionally, set the time delay for launching the tests upon the changes in the code:

    ps_phpunit_set-auto-test-delay.png
Last modified: 10 October 2018

See Also