Introducing PHPSpec in PhpStorm 2016.3

Posted on by Gary Hockin

You asked, and we delivered! Brand new in PhpStorm 2016.3 is extended support for the behavior-driven design framework PHPSpec.

PHPSpec has been gaining more and more traction, and with the releases of version 3 in July took a huge leap forward regarding functionality and developer experience. We’ve had a lot of support on YouTrack (JetBrains patented issue tracking system) asking for PHPSpec support in PhpStorm, and now it’s here.

PHPSpec is, at its heart, a unit testing framework like PHPUnit with the focus shifted from defining input and output to defining behaviors. It’s a very different way to work, but an interesting concept, and one I’ve been using more and more when working on PHP projects. PhpStorm 2016.3 introduces numerous improvements which mean that PhpStorm understands the magic that allows PHPSpec to work.

2016-3-phpspec-1

Because PHPSpec’s spec (test) files act as a proxy around the original class, PhpStorm has never before been able to resolve the methods of the class you’re testing. Because PhpStorm now understands PHPSpec’s internal wiring, you get full code completion on your classes, complete with the ability to navigate right to the implementation as you would in any other class. This is very useful.

You can see from the above screenshot that PHPSpec allows you to chain calls from the methods of the class you’re testing to the expected return of that method. While this makes for very readable tests, it’s internal magic means that PhpStorm was never able to provide code completion for the expected result (what PHPSpec calls a matcher), as it would assume the return to be whatever the method all returned. The new PHPSpec plugin understands what comes after a class method call could be a matcher, and gives you code completion making writing your examples that much easier.

2016-3-phpspec-2

Finally, we’ve added a PHPSpec runner in a similar way to the PHPUnit runner that’s been widely used for many versions of PhpStorm. You can add the run configuration for PHPSpec in a similar way to PHPUnit — by selecting Edit Configurations from the Run drop-down menu, and then adding a new PHPSpec configuration. You’ll need to configure the location of your PHPSpec executable from the PHPSpec configuration pane under Languages & Frameworks.

2016-3-phpspec-4Once set, you can quickly run your test suite using the green Run arrow as you would PHPUnit. At the moment the test runner is not interactive, and so you’ll need to run PHPSpec from the terminal if you want to take advantage of the code generation features of PHPSpec which are, after all, one of the most useful parts of PHPSpec. Expect improvements around this and describing classes to come in the future.

We’d love to hear what you think of the new PHPSpec integrations in PhpStorm 2016.3. Drop me a Tweet @GeeH and let me know what you think.

– Gary and the PhpStorm Team

Subscribe

Subscribe for updates