PHPUnit Plugin For Sublime Text 2

Posted by Stuart Herbert on February 4th, 2012 in Toolbox.

Sublime Text 2 is a new cross-platform text editor that I’ve recently switched to. It’s still in public beta, but already offers better performance (and battery life!) and a better look (fonts that render properly!) than Java-based IDEs such as Netbeans.

One thing it didn’t have was support for PHPUnit, so I’ve made a plugin. It’s available to install via Package Control.

You Need A phpunit.xml or phpunit.xml.dist File

To use this plugin, your project needs to contain either a phpunit.xml or a phpunit.xml.dist file. This file contains all the configuration that needs to be passed to PHPUnit. The plugin searches upwards from your code, and will favour a phpunit.xml file over a phpunit.xml.dist file if it finds both.

If you don’t have one, you need to go and create one now.

How To Use

If you have your code open in a Sublime Text 2 window, right-click inside the window to see what your options are:

  • Test This Class – click this option to run just the unit tests for this class.

    This option appears if the PHPUnit plugin can find your unit tests. It takes the name of your class, and uses the standard PSR-0 transformation to figure out what the name of your test file should be.

    For example, if your class is called ‘Phix_Project\CommandLineLib\CommandParser.php’, the PHPUnit plugin will search for a file ‘CommandLineParserTest.php’ that’s in a folder called ‘Phix_Project/CommandLineLib’.

  • Open Test Class – click this option to open up your tests in Sublime Text 2. If you already have the tests open, this will switch tabs to your tests.

    Again, this option only appears if the PHPUnit plugin can find your unit tests.

  • Run All Unit Tests – click this option to run all the unit tests for your code.

    This option just points PHPUnit at your phpunit.xml or phpunit.xml.dist file.

If you have your tests open in a Sublime Text 2 window, right-click inside the window to see what your options are:

  • Run These Tests – run these unit tests, using the phpunit.xml or phpunit.xml.dist file that the PHPUnit plugin has found.
  • Open Class Being Tested – open the class that these tests are for in Sublime Text 2. If you already have the class open, this will switch tags to your code.
  • Run All Unit Tests – click this option to run all the unit tests for your code.

If you’re someone who prefers keyboard over mouse, then you’ll probably want to run the PHPUnit plugin commands from Sublime Text 2′s Command Palette:

You get the same commands that appear on the right-click menu … the right commands will appear for the file that you’re currently editing, just as you’d expect.

Finally, you can also right-click on your phpunit.xml (or phpunit.xml.dist) file in the Project Sidebar, and run your unit tests using that specific config file.

Helpful Snippets

Like TextMate before it, Sublime Text 2 also has a handy snippets feature, where it can insert a pre-crafted block of text (or, in our case, PHP code) to speed up your coding. I’m collecting most PHP-related snippets in my Additional PHP Snippets plugin (hat-tip to Rob Allen for the inspiration for this), but the PHPUnit plugin includes a few PHPUnit-related snippets to help.

  • phpunit-testcase – will create a new test class for you to fill out.

    I find this handy mostly so that I don’t have to remember which class my test class has to extend :)

  • phpunit-test – will create a new test method for you to fill out.

To use any snippet, type its name and then press the <TAB> key. Sublime Text 2 will insert the snippet, and then you can use the <TAB> key to cycle through any placeholders that you need to edit.

If you find snippets useful, please don’t forget to check out my Additional PHP Snippets plugin too!

Feedback, Contributions Welcome

I’d love to hear how you get on with the plugin, and any ideas you might have for additional features. And pull requests via GitHub are most welcome too!

6 Comments

  1. Jayanti says:
    February 18th, 2012 at 3:24 pm

    This article help me a lot to understand PHPUnit.

  2. Pablo Nunez says:
    April 25th, 2012 at 6:04 pm

    It worked for me, without any extra configuration… but the only thing is that when I am on a class and then Right-Click (to get the contextuals menus) Sublime hangs for at least 10 seconds, so I had to disable the phpunit plugin for now :(

    Good work!

  3. Thomas McGregor says:
    June 9th, 2012 at 12:25 pm

    Annoyingly I’ve had to do the same as Pablo Nunez and disable it as it hangs when trying to access the contextual menu in a Class

  4. rubdottocom says:
    July 4th, 2012 at 8:12 am

    Hi Stuart, this is an amazing plugin that improve my productivity when working in TDD context, thank you very much.

    Currently I’m evaluating the possibility to execute unit tests remotely doing something like this: http://aaronsaray.com/blog/2012/01/03/running-phpunit-on-remote-system-from-command-line/

    I’m a newbie with phpunit still and I’m wondering if would be possible to invoke remote tests through your plugin.

    I don’t know if I’m asking for a nonsense :-) could you suggest something about this scenario?

    Thank you.

  5. Saša MLadenović says:
    August 28th, 2012 at 10:44 pm

    Any chance you integrate PHPUnit Skeleton in this plugin:
    http://www.phpunit.de/manual/current/en/skeleton-generator.html

    SOmething similar that is done in Netbeans – you right click on a class file and choose: “Create test for this class”

  6. Kramar, Petr says:
    September 4th, 2012 at 1:57 pm

    Hello,

    great job, thank you Stuart.


    [2], [3] yes, ST2 hangs when you have missing phpunit.xml file -http://www.phpunit.de/manual/current/en/appendixes.configuration.html

    For some small projects is usually enough minimal configuration like this:

    ./tests/