Early Access Program Features Releases

Using Behat in PhpStorm

behat_logo_newBehat is a BDD (behavior driven development) framework for PHP. With BDD, you write human-readable stories that describe the behavior of your application. These stories can then be auto-tested against your application.

In this blog post we’ll look into how to install, configure and use Behat right from the PhpStorm IDE. Please have a look at the full tutorial on using Behat in PhpStorm.

Behat support is available in PhpStorm 8 EAP. The following Behat-related features have been implemented:

  • Behat installation and configuration helpers
  • Run Configuration for Behat (similar to PHPUnit’s)
  • Behat Intellisense including:
    • Resolve from step in Gherkin file to Context file
    • Find Usages from Context file to usages in Gherkin files
    • Keyword and step completion
    • Generation for undefined steps

Prerequisites

To make use of Behat, we first have to install it on our system. Read more about Behat installation in official documentation. Please note that PHP interpreter should have mbstring extension installed (or just compiled with –enable-mbstring option) to execute Behat tests.

Installing Behat via Composer

Composer package for Behat is available. A dependency can be added to composer.json using Composer Support in PhpStorm.

Use the Composer | Add dependency… context menu in your project and install behat/behat package:

behat_tutorial_1

Installing Behat via Phar package

You can either download behat.phar package from Behat GitHub repository manually, or automatically install it from Settings | PHP | Behat | Download behat.phar from…

behat_tutorial_2

Run Configuration for Behat

Behat run configurations can be set in the same way as PHPUnit’s. The usual testing workflow (running with run action, UI for running tests and displaying results) is supported.

Setting Behat run configuration via Edit Configurations… action

1. New Run Configuration for Behat can be created either in Run | Edit configurations… menu, or with the same action from the toolbar.

2. Add a new configuration with the + button and select Behat.

3. Provide the path to the directory that contains test scenarios (/Users/user/PhpStormProjects/Behat/features in our case). Test scope can be defined as a DirectoryFileScenario or Defined in configuration file.

behat_tutorial_6

4. If you haven’t configured the path to Behat directory or behat.phar file before, PhpStorm will offer you to do so with the Fix button.

behat_tutorial_7

Behat configuration can be always changed in Settings | PHP | Behat. You can also use Behat via Remote Interpreter.

Setting Behat run configuration via file/folder context action

Depending on your workflow, in order to create run configuration on the fly you can just invoke the context action Create Run Configuration | Folder name (with Behat icon), or create and run it straight away with Run | Folder name (with Behat icon) for folders in Project Tool Window. The same actions are available in Run | Run menu (Alt + Shift + F10 / Ctrl + Alt + R):

behat_tutorial_10

It works the same way for a context action inside the *.feature file in the editor.

Running Behat tests and evaluating results

As soon as a run configuration is created (or you are running/creating test scenarios for folder/file from the context menu ), your tests will run and results will be displayed in the test runner tab. From this tab you can navigate to the source code of the test, and perform other actions.

behat_tutorial_12

Behat Intellisense

Resolve from step in Gherkin file to Context file

Press and hold Ctrl / Cmd hotkey and hover over a step in Gherkin file: the relevant method from the Context file will be highlighted. Click on the displayed link to navigate to the declaration. You can also navigate to the method declaration in the Context file with Ctrl+B / Cmd+B when the editor caret is placed at the step in Gherkin file.

behat_tutorial_13

Find Usages from Context file to usages in Gherkin files

From any Context file you can find all the usages of a method in all *.feature files. Just place the caret at the method name and press Alt+F7 (or select Find Usages from the context menu). You will see all usages of the desired method in project files:

behat_tutorial_15

Keyword and step completion

Any keywords and steps are available with the code completion action (Ctrl+Space) in *.feature files.

behat_tutorial_16

Generation for undefined steps

If any of steps in your *.feature file are undefined, PhpStorm will detect this, and will immediately highlight the step as undefined step reference:

behat_tutorial_17

Press Alt+Enter (or click the light bulb) to get a list of relevant quick-fixes, and select Create Step Definition. You will be prompted to choose the step definition file from your project or create a new one:

behat_tutorial_18

Have a look at the full tutorial on using Behat in PhpStorm.

If you have any bugs to report and/or feature requests to add related to Behat functionality, please do so in our Issue Tracker. Also, feel free to comment on this blog post or write a message in our forums.

Develop with pleasure!
– JetBrains PhpStorm Team

image description