Working With PHPUnit and PhpStorm

Community stalwart and Laravel aficionado Adam Wathan blogged on his PHPUnit workflow in Sublime text. I was sent a Tweet asking how you do this in PhpStorm:

Here’s the answer.

First, Adam talks about having templates you can use to generate the boilerplate code around your test quickly. PhpStorm already does this when you create a unit test, either from the “Navigate to Tests” action or if you use “New” in the project manager and select PHPUnit then PHPUnit Test.

new-test

You can edit these templates by going to the settings pane and looking under EditorFile and Code Templates and then PHPUnit Tests. The templates have some predefined variables you can use to tailor the new file exactly to your liking.

test-template-settings

You can learn more in the Updating Your Templates in PhpStorm blog post.

Next, Adam shows us how he has used snippets when he wants to create a new test to reduce the typing of the function definitions and annotation. We can do exactly the same in PhpStorm using Live Templates.

Live templates allow you to type some characters and then use the Tab key to substitute the key for a snippet of code. PhpStorm already comes with a batch of them out of the box – try typing rqr and then pressing Tab in a PHP file to see the results. We can use Live Templates to reproduce Adam’s Sublime Text snippet. Live Templates can be found under Editor then Live Templates in the Settings, and you’ll want to create this under the PHP scope.

live-template-test

Now, when we type the abbreviation test into our Test class, and press Tab, we get the function definition with annotation, with the cursor in the right place to type our test name.

live-template-usage

Finally, Adam talks about how to run just the tests in a single file, a single class, or a single method. This is relatively easy in PhpStorm if you’re using the PhpStorm test runner already.

To run all the tests in a single file, right-click the test in the Project Pane (the left-hand navigation pane), and select Run <Filename>.

To run all the tests in a single class, right-click the class name in the editor, and select Run <Class Name>.

To run the tests in a single method, right-click the method name, and select Run <Method Name>.

run-only-method-tests

I hope that covers everything. A big thank you to Adam Wathan for all he does to promote testing in the Laravel and wider PHP communities!

 — Gary and the PhpStorm Team

 

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in Cool Feature. Bookmark the permalink.
  • Daniel Keeble

    Great article.

    Is there a way to set a default configuration when using ctrl + r?

    I see that every time I run ctrl + shift + r it creates a new phpunit configuration, which is then set as the default for every ctrl + r subsequent to that.

    Ultimately I’d like to be able to individually run a method via ctrl + shift + r and the whole test suite via ctrl + r

    Cheers!

    • spabby

      Hi Daniel,

      This is not currently possible – but there are some nice keyboard shortcuts you can use to make life easier.

      – If you have a failed test, use CMD+R/Ctrl+F5 to Rerun Failed Tests
      – CTRL+R/Shift+F10 will run the currently selected configuration (as you already mentioned)
      – CTRL+Shift+R/Ctrl+Shift+F10 will ask you in place which runner to use – this is useful for quickly switching runners
      – CTRL+ALT+R/Shift+Alt+F10 gives you a more complete option on which runner to use, including creating a new run configuration

      I know this doesn’t help, but I think that being able to assign a keyboard shortcut to a given run configuration would be useful, would that solve your problem?

      Gary

      • Daniel Keeble

        Hey Gary,

        Thanks for the in-depth reply – will definitely be trying out those different shortcuts.

        I think you last suggestion makes a great deal of sense. I could definitely see it benefiting my workflow.

        Dan

  • Ruben

    The article was right up my street. While working with unittests I have stumbled upon two issues. Hopefully there are implementations or workarounds for it I don’t know about, but otherwise this might be a feature request.

    1. In our environment we work with two diffrent kinds of tests. One is based on the basic PHP Unit Test Framework. The other one is based on a database framework. For the basic framework there is indeed a template that supports the creation of this kind of test class. I hoped to create another template to set all the basics for the database testframework. This didn’t work as expected however because I didn’t have access to the same predefined variables. Additionally when creating a new file based on the template, the dialog box contained a different set of items that did not fit the unit test format.

    Is there a possibility to mark a new template as a type of unittest template with the same variables and new dialog options?

    2. Our code is based on a legacy autoloader that doesn’t support namespacing. The autoloader expects a tree structure based from multiple defined root folders where the base classes and it’s extending classes are located. Is it possible to bind the folders and/or files to matching test folders and/or files, so that the linked test can be run directly from the context menu?

    • Maxim Kolmakov

      Unfortunately, there is no way to do 1 and 2 at the moment without writing a custom plugin for PhpStorm. You may also file a feature requests at: https://youtrack.jetbrains.com/issues#newissue to track the progress.

  • https://www.cloudways.com Saquib Rizwan

    Testing always seems tricky to me, But this is a great guide. Thanks

  • Jonathan Williamson

    With regards the last part of trying to run a full test or a single test in a file as quickly as Adam does in Sublime – here’s a really quick way of doing it, using keyboard shortcuts only in Phpstorm!!

    No wasting time with the mouse! Hope it helps! :)

    https://vimeo.com/212458996