Getting PHPUnit working with a Zend Framework 2 MVC application

Now that I’ve got a little MVC application up and running in Zend Framework 2 (see my previous post) I decided it was time to try and get PHPUnit to play with it. While I don’t think I’m alone in wanting to use ZF2 & PHPUnit together it’s certainly something not many people are talking about.

Here I will show how I bootstrapped PHPUnit with the Zend Framework.

First thing I did was create a tests folder in the root of my project folder:

Next up I created a phpunit.xml configuration inside the tests directory containing the following XML:

Nothing really special in there, the important part is that I’ve told phpunit to use the Bootstrap.php file to get things set up.

So next up I needed to create the Boostrap.php file. I came to the conclusion that I could probably use the same init_autoloader.php file that default/index.php in the ZF2 Skeleton Application uses to get the autoloader up and running.

So I created the following content in the Bootstrap.php

This seemed to do the trick and I could autoload classes from the Zend Framework, however I still couldn’t autoload my classes from my MVC application module.

I knew I some how needed to pull in the MVC configuration from the the application and after stepping through the code a bit I came to the conclusion I needed to add the following code:

This gets the application config loaded from config/application.config.php and then loads the config for the modules listed in there. So now my whole Bootloader.php looks like this:

Now my classes in my modules would easily autoload, marvelous!


I have since realised that this setup is down in the init() method of Zend\Mvc\Application therefore the Bootstrap.php file can be reduced down to:

Which is much neater and also calls the Application bootstrap() method which I’m sure will be required later on.

Next up was to write a test. I decided the directory structure in my tests folder would mirror my code tree so I created module/Album/src/Album/Model/ and put a new file in there called AlbumTest.php

I wrote the following test code:

I won’t go into detail about what that test does, that can all be looked up in the PHPUnit documentation. One thing I did do through was make Album::setInputFilter throw AlbumException instead of Exception, the reason for this is PHPUnit 3.6 doesn’t like checking for Exception classes, apparently this is fixed in 3.7.

So anyway, not when I cd into my tests directory and type phpunit I get the following output:


Next up time to work out how to use PHPUnit with classes that access the database & controller classes.


7 thoughts on “Getting PHPUnit working with a Zend Framework 2 MVC application

  1. Pingback: Getting PHPUnit working with a Zend Framework 2 MVC application … - Web 2.0 BLOG | Web 2.0 BLOG

  2. Pingback: PHPUnit mit einer Zend Framework 2 Anwendung nutzen - Zend Framework Magazin

  3. Great. Thank you very much for this tuto.
    It works very well with rc3.

    Just a little finger error 🙂
    In ‘phpunit.xml’, it is : “./” without the ‘;’


  4. Hi !

    Nice post thank you for the guide 🙂

    Found a second typo: “So now my whole Bootloader.php looks like this:”

    Should be “Bootstrap.php” 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *