PhpStorm 2017.1 Help

Testing with PHPUnit

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

On this page:

Installing PHPUnit

You can use PHPUnit in PhpStorm 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 PhpStorm 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.phar archive.

Configuring the PEAR folder with PHPUnit 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.

  1. Open the Settings dialog box, and click PHP. The PHP page opens.
  2. From the CLI Interpreter drop-down list, choose the PHP interpreter to use.
  3. Configure the PEAR folder as an include path. Click the Add button in the Include path area, and select the PEAR folder in the dialog box, that opens.
  4. Click PHPUnit under the PHP node, and on the PHPUnit page that opens choose Load from include path.

Loading PHPUnit with autoload.php from the Composer dependency manager

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.

  1. 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 Tools | Composer | Init Composer and then specify the location of an existing composer.phar file or have PhpStorm download the file. For more details, see Using Composer Dependency Manager.
      When the initialization process is completed, the autoload.php file is stored in the vendor folder.
  2. Open the Settings dialog box, and click PHPUnit under the PHP node.
  3. On the PHPUnit page that opens, choose Use custom loader and specify the location of the autoload.php in 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.

Running PHPUnit from the phpunit.phar archive

You can download phpunit.phar manually and then tell PhpStorm to load the libraries from it or have PhpStorm download phpunit.phar automatically. Depending on your preferences, do one of the following:

  • Download phpunit.phar manually:
    1. Download phpunit.phar manually.
    2. Open the Settings dialog box, and click PHPUnit under the PHP node.
    3. 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.
  • Have PhpStorm download phpunit.phar:
    1. Open the Settings dialog box, and click PHPUnit under the PHP node.
    2. On the PHPUnit page that opens, choose Path to phpunit.phar and click the Download phpunit.phar... link.
    3. In the dialog box that opens, specify the folder to store the phpunit.phar archive after download. PhpStorm 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.

PhpStorm 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 PhpStorm 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 PhpStorm. For more information, see http://www.phpunit.de/manual/current/en/installation.html and Testing with PHPUnit.

Generating a test for a PHP class defined in a separate file

  1. In the Project tool window, select the PHP class to create unit tests for, e.g. MyPHPClass as shown in the image below, and choose New | PHPUnit | PHPUnit Test on the context menu of the selection.
    generatePHPUnitTestClass
  2. In the Generate PHPUnit Test dialog box that opens, specify the following data:
    1. 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.
    2. The name of the test class. PhpStorm 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.
    3. 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.
    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 Control+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.

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

When the test is ready, navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigating Between Test and Test Subject.

Generating a test for a PHP class defined among others within a PHP file

  1. In the editor, open the file with the class to generate the test for.
  2. 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.
      generatePHPUnitTestSpecificClass
  3. In the Generate PHPUnit Test dialog box that opens, specify the following data:
    1. 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.
    2. The name of the test class. PhpStorm automatically generates the name and displays it in the Name text box of the Test Class area.
    3. 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.
    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 Control+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.

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

Generating a PHPUnit test method

Besides generating test class stubs, PhpStorm can help you populate them by generating stubs for 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 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.
    1. On the context menu, choose Generate | Override method.
    2. 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.

To attach a test to a group

  • Tag the test with the @group annotation in the format:
    /** @group <group specification> */

To enable filtering a test by its author

  • Tag the test with the @author annotation in the format:
    /** @author <author specification> */

To configure execution of a test group

  1. In the Run/Debug Configuration: PHPUnit by HTTP dialog box, open the Test groups tab.
  2. To enable appointing groups to execute, select the Include/Exclude check box.
  3. 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.

See Also

Last modified: 31 March 2017