Integrate PHPStorm, Behat, and Vagrant

Behat tests can be initiated right from PHPStorm, saving a lot of hassle, as well as simplifying access to tests for less technical users.  Behat v3.3 does come pre-installed via composer and is found at /usr/local/bin/behat.  If you wish to use your own version of Behat, you’ll need that path later.  At Bryn Mawr, we have behat as part of our repository, so we use that path instead of the default /usr/local/bin/behat one.

Here’s some things you’ll need to have before starting:

  • PHPStorm open and licensed
  • RV project up and running
  • Behat path in the vagrant box
  • Behat.yml path
  • A Behat feature to run

PHPStorm Setup (File->Settings)

  1. Press the button for Project Root and press OK
  2. Settings Changes (File-Settings)Editor > Code Style
    1. Click Manage and import PHPStorm-Drupal.xml from [Project]/buildFiles/configs folder
    2. Version Control
      1. Remove the Project Root folder, add public_html
  3. Build Execution & Deployment > Deployment
    1. Add a new connection > Local or mounted folder
    2. Set Project file folder to point to [Project]/public_html
    3. Set Web server URL to http://website.vbox.local
    4. Change Deployment path to “.” under the mapping tab
  4. Language and Frameworks > PHP
    1. Change PHP level to 5.3 (RV uses PHP 5.3 by default.  Change this if you decide to upgrade to a different version)
    2. Add CLI Interpreter
      1. Remote > Vagrant
      2. Change name to “Rampant Vagrant”
    3. Select the CLI interpreter to “Rampant Vagrant”
    4. Add a path mapping for [Project]/public_html to /vagrant/public_html
    5. Behat Configuration
      1. Click to add a new Behat instance
      2. Select “Rampant Vagrant”
      3. Set path to be the appropriate behat path
      4. Set default runner path to the path of your behat.yml file
  5. Close Settings

You should now be able to run behat tests in the vagrant box right from PHPStorm.

Happy Testing!

Behat, A Quick Introduction

Perhaps the single-most helpful thing I learned about while at the DrupalCampPA was Behat.  Behat is a behavioral driven testing framework that lays out tests in English that are then translated to PHP and other commands.  A simple test might look like the following:

Feature: Listing command
  In order to change the structure of the folder I am currently in
  As a UNIX user
  I need to be able see the currently available files and folders there

  Scenario: Listing two files in a directory
    Given I am in a directory "test"
    And I have a file named "foo"
    And I have a file named "bar"
    When I run "ls"
    Then I should get:
      """
      bar
      foo
      "

The basic structure of a behat test is to define the overall feature in a storytelling context:

  1. Feature to be tested
  2. In order to do X
  3. As a user USERTYPE
  4. I should be able to perform ACTION

The rest of the test consists of steps that a user takes (thus the behavior-driven name), which is written in “regular” language.

But what does it do for me?

As websites become more complex, it is increasingly difficult to test all features manually.  Behat tests provide an automated way of ensuring that features work as intended with each upgrade.  It’s a great safety net to the deployment process.