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!

Configuring Remote Xdebug and PHPStorm

Rampant Vagrant comes with XDebug installed and enabled in the guest OS by default with the IDE key of “vagrant”.  With a tradeoff of performance, we are able to remotely debug our code in a tool such as PHPStorm.  We’ll be configuring PHPStorm and Firefox to connect to a debugging session on the vagrant box.

First, get an instance of the RV project up and running.  Also, open up PHPStorm on the host machine.  I’m using the latest version of PHPStorm, 2016.3.2 as of writing this.

Save a new PHP file called debugTest.php within the public_html folder of the project with the following content:

<?php
/**
 * Test debugging functionality.
 */

echo "Beginning of Program";
$test = array(
    "a",
    "b",
    "c",
);
unset($test[2]);
echo "End of Program";

Within PHPStorm, Click on File->Settings and select the PHP servers. I configured mine in this way:

I added a new server and named it “Vagrant Server”.  The Host is the hostname of our vagrant box, which for this project is “website.vbox.local”, and we’re using port 80. You’ll want to also map the absolute path on the server, using “/vagrant/public_html” to map to your project’s public_html folder.

After saving the server configuration, select the Run menu and then Edit Configurations.  Create a new PHP Remote Debug configuration with the following settings:

The Server name should match the same one we just created.  Set the Ide key to ‘vagrant’.  Then click Run->Debug ‘Remote Xdebug’.

Open debugTest.php within the IDE and add some breakpoints by either selecting a line and pressing Ctrl+F8 or clicking just to the right of a line number.  A breakpoint is noted by a red circle on the left side.

In the Firefox browser, install the extension EasiestXDebug.  After installing the extension, click on its options and set the Ide key to match ‘vagrant’.  Click the button in the browser to enable debugging.

 

 

Visit http://websiter.vbox.local/debugTest.php in the browser.  The php file can now be stepped through.  If you make a breakpoint at each line, you’ll note the temp array start with 3 elements and then loses the last array element as the unset command executes.