PhpStorm 2024.2 Help

PHPSpec

With PhpStorm, you can practice behaviour-driven development by running specifications using the PHPSpec toolset.

Before you start

Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configure local PHP interpreters and Configure remote PHP interpreters.

Install PHPSpec with Composer

Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.

  1. Inside composer.json, add the phpspec/phpspec dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

Learn more about PHPSpec installation from PHPSpec Official website.

Clicking the Settings button next to the package record in the composer.json editor gutter will take you to the corresponding Settings page where you can configure PHPSpec manually.

Gutter icon for phpspec settings in composer.json

Integrate PHPSpec with a PhpStorm project

If you use a local PHP interpreter, PhpStorm performs initial Codeception configuration automatically. In the case of remote PHP interpreters, manual Codeception configuration is required.

Configure PHPSpec automatically

  1. Store the phpspec.yml or phpspec.yml.dist configuration file under the project root.

  2. Install PHPSpec with Composer.

PhpStorm detects the installed PHPSpec executable (or executables, if the project has Composer-managed subprojects with a specified PHPSpec dependency) and creates a test framework configuration per each installed PHPSpec executable on the Test Frameworks page.

PHPSpec configurations

If the phpspec.yml or phpspec.yml.dist configuration file is detected under the project (or subproject) root automatically or specified explicitly during manual configuration, PhpStorm also creates a respective PHPSpec run/debug configuration.

PHPSpec run/debug configurations

Configure PHPSpec manually

  1. In the Settings dialog (Ctrl+Alt+S) , go to PHP | Test Frameworks.

    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. For more information, refer to Default project CLI interpreters.

    • To use PHPSpec with a remote PHP interpreter, choose one of the configurations in the dialog that opens:

      ps_settings_php_test_frameworks_phpspec_choose_php_interpreter.png
  2. In the Path to PHPSpec executable field, specify the location of phpspec. PHPSpec does not necessarily have to be installed under the current project root.

    Click the Reload button next to the Path to PHPSpec directory or phar file field. PhpStorm detects the version of PHPSpec and displays it below the field.

    If no path to PHPSpec is specified for a Local interpreter, PhpStorm does not provide full support of PHPSpec, for example, it does not show suggestions for code completion and does not resolve references.

  3. In the Test Runner area, appoint the configuration YML file to use for launching and executing scenarios.

    By default, PHPSpec looks for a phpspec.yml or phpspec.yml.dist configuration file in the project root folder. You can appoint a custom configuration file.

    • Clear the Default configuration file checkbox to have PHPSpec use the phpspec.yml or phpspec.yml.dist configuration file from the project root folder. If no such file is found, test execution fails, therefore it may be more reliable to specify the configuration file explicitly.

    • Select the Default configuration file checkbox to specify your own YML configuration file. This file will be later used as default in all PHPSpec run/debug configurations.

      In the field, specify the location of the configuration file to use. Type the path manually or click the Browse button and choose the file in the dialog that opens.

    In local configurations, the Prefix ('spec_prefix'): read-only field shows the namespace prefix for specifications. PhpStorm detects spec_prefix from the configuration file specified in the Default Configuration File field. The default value is spec. For more information, refer to PHPSpec Configuration: PSR-4 and PHPSpec Configuration: Spec and Source Location.

Generate a PHPSpec test for a class

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

    • Go to File | New. Then, choose PHP Test | PHPSpec Spec 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 | PHPSpec Spec.

    • In the editor of the PHP class to be tested, place the caret at the definition of the class. Then, press Alt+Enter 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, place the caret within the method declaration. The chosen method will be automatically selected from the list of methods in the Create New PHP Test dialog.

  2. In the Create New PHP Test dialog that opens, provide the parameters of the generated test.

    the Create new phpspec test dialog
    • Test file template: the template based on which PhpStorm will generate the test class. Make sure that PHPSpec is selected from the list.

    • Name: the name of the test class. PhpStorm automatically composes the name from the production class name as <production class>Spec.php.

    • Directory: 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 src_path value specified in the phpspec.yml configuration file. Note that for the automatic folder suggestion to work correctly, the test suite and the production class namespaces have to match.

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

    • Namespace: 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, or the spec_prefix and namespace values specified in the phpspec.yml configuration file.

    • Member: the list of production class methods to generate test method stubs for. 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.

After the test is created, you can navigate back to the production class by choosing Navigate | Go to Test Subject. For more information, refer to Navigate between a test and its test subject.

Run and debug PHPSpec tests

For more information about writing PHPSpec specifications, refer to the PHPSpec Documentation. To run or debug your tests, do one of the following:

Run or debug PHPSpec 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>' from the context menu of the selection:

    ps_test_frameworks_phpspec_launch_tests.png

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

Save an automatically generated default configuration

  • After a test session is over, choose Save Configuration from the context menu of the respective run/debug configuration in the run widget.

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

  • Choose the required PHPSpec configuration from the list on the toolbar and click the Run button or the Debug button.

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 from the context menu. Alternatively, go to Run | Edit Configurations in the main menu, then click Add icon and choose PHPSpec from the list.

  2. In the PHPSpec 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.

Monitor test results

PhpStorm shows the tests execution results in the Test Runner tab of the Run tool window.

ps_test_result_phpspec.png

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 PHPSpec output.

Last modified: 17 June 2024