PHPUnit 4 in PhpStorm 8 EAP

Posted on by

A couple of months ago, PHPUnit 4.0 was released (as well as 4.1 a few days ago). This latest incarnation of the popular unit testing framework for PHP adds a couple of new features and bugfixes. In this blog post, we’ll look at some of these new features and see how we can use PhpStorm 8 EAP to work with them.

Here is a list of notable PHPUnit’s new features:

Support for HHVM

Test Proxies for more effective integration testing

Improvement of @covers and addition of @uses for better code coverage analysis

Fine grained control over the checks performed in strict mode

Simplified stubbing of return values using the new willReturn*() syntax

Support for the @before, @after, @beforeClass and @afterClass annotations

Support for Patch Coverage through phpcov command-line tool

Let’s dive into some of these!

Test Proxies

Stubbing and mocking has been available in PHPUnit for a couple of versions. Using stubs and mocks, we can generate a “fake” object that has all the methods the original object has but does not execute code. This allows us to check whether our test calls into the object or not, without having to care about the dependency implementation. In some cases, however, it may make sense to have a combination of the actual object and a mock on which we can validate some assertions. That is exactly what Test Proxies allow us to do!

In the following example, we’re testing using the actual BankAccount class. But by creating a Test Proxy, we can also verify that specific methods have been called:

The PHPUnit documentation has much more information on stubs and mocks and when and why we can use them.

New test fixture annotations

For every unit test, we can set up some test prerequisites before running it, and clean them up after it has run. For example, we could create an array or a series of mocks that are required for a test to run and clean them up after the test runs.

PHPUnit 4.0 comes with some new annotations: @before, @after, @beforeClass and @afterClass. Methods annotated with any of these run right before and after each individual test, or right before and after running a test class.

Wasn’t this already there with the setUp(), tearDown(), setupBeforeClass() and teardownAfterClass() methods? Yes it was! But now we can have multiple, which makes setting up multiple fixtures much cleaner.

These new annotations are also supported by PhpStorm 8 EAP.

The @requires OS annotation

When we write code that targets multiple operating systems, chances are that some tests will only have to be run on a subset of these. Using the @requires OS annotation, we can now do this! For example, we could write a test that only runs on Linux. PhpStorm 8 EAP will clearly show that this test is being ignored when run on Windows:

A list of some common operating system names can be found here.

Curious to play with the new PHPUnit 4 features? Download PhpStorm 8 EAP and start writing your unit tests! Your feedback is welcome through the issue tracker or by posting your comments below or in our forums!

Develop with pleasure!

– JetBrains PhpStorm Team