Testing in PHP development

For the PHP developers among us who test their code using PHPUnit, Sublime has an awesome plugin to support PHPUnit known as sublime-phpunit, written by Stuart Herbert and located at http://github.com/stuartherbert/sublime-phpunit.

Knowing about PHPUnit

If you are a PHP developer and do not know what PHPUnit is, you should learn that PHPUnit is "the standard" for unit testing in PHP projects. It combines a framework that lets us easily write and run tests with the facility to analyze the results.

We can learn more about PHPUnit at https://github.com/sebastianbergmann/phpunit/ or check the full manual at http://phpunit.de/manual/current/en/index.html.

Using PHPUnit plugin for Sublime

Before using this plugin, we must have PHPUnit installed and functional in our project and know how to use it, a topic which is not covered here. We'll then need to download and install the PHPUnit plugin using Package Control. Let's open up the command palette by pressing Ctrl + Shift + P in Windows or Linux and Command + Shift + P in OS X. Then choose Install Package and install PHPUnit package. After installing it, we can right-click on our code to see the new the PHPUnit option that has been added to our context menu, as shown in the following screenshot:

Using PHPUnit plugin for Sublime

The options in PHPUnit are disabled because we are not currently inside a PHP project. To use the PHPUnit plugin in our project, we'll need a phpunit.xml or phpunit.xml.dist file. These files contain all the PHPUnit configuration options. PHPUnit will always favor phpunit.xml over phpunit.xml.dist if both exist. After your project is properly configured, right-clicking inside a source file should show the following:

Using PHPUnit plugin for Sublime

Stuart Herbert ©

As we can see in the preceding screenshot, we have three available options in the PHPUnit menu:

  • Test This Class…: This option will run the unit tests just for this class.
  • Open Test Class: This option opens our tests in Sublime Text; if tests are already open in Sublime this will switch between test's tabs.
  • Run All Unit Tests…: This option runs all the unit tests for our code. This option just points to our phpunit.xml file.

Tip

Test file names must match the original file names; for example, if our class is called OurNamespaceOurClass.php, the plugin expects to find our tests in a file called OurNamespaceOurClassTest.php somewhere inside our project.

We can also find all the available PHPUnit commands inside the command palette by pressing Ctrl + Shift + P on Windows or Linux and Command + Shift + P on OS X, and type phpunit, as shown in the following screenshot:

Using PHPUnit plugin for Sublime

In the preceding screenshot, we can see all available commands that the PHPUnit plugin has to offer.

Helpful PHPUnit snippets

The PHPUnit plugin also includes some very helpful snippets to use while writing our unit tests:

  • phpunit-testcase: This will create a new test class for us to fill out
  • phpunit-test: This will create a new test method for us to fill out

These snippets can be used like any regular snippet, by typing their names and pressing the Tab key.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset