Michaël Gallego

This is my blog. What can you expect here? Well... Zend Framework 2, Amazon AWS...






Testing a ZF2 application with multiple modules

If you are using ZF2, you likely are using modules (wait… aren’t you?). You likely also have one “tests” folder inside each module, with their own phpunit.xml.dist files. However, if you have multiple modules, it can quickly become annoying to ensure your application is passing tests, because you manually need to launch phpunit command for each of your modules.

Hopefully, as mac_nibblet told me, you can use Apache Ant to automatize this process. On OS X, the simplest way to install Ant is using Homebrew:

brew update
brew install ant

Then, at the root of your ZF2 project, create a new file called build.xml, and copy-paste the following code:

<?xml version="1.0" encoding="UTF-8"?>
<project name="my-project" default="phpunit">
    <target name="phpunit">
        <parallel threadCount="4">
            <echo message="Running unit tests with PHPUnit" />

            <exec executable="phpunit" failonerror="true">
                <arg line="-c ${basedir}/module/Application/phpunit.xml.dist" />

            <exec executable="phpunit" failonerror="true">
                <arg line="-c ${basedir}/module/User/phpunit.xml.dist" />

            <exec executable="phpunit" failonerror="true">
                <arg line="-c ${basedir}/module/Twitter/phpunit.xml.dist" />

This is a simple XML file. Each exec part allows to launch a specific test suite. The parallel tag allows to execute multiple commands… well… in parallel! This makes the process quite fast, even with a lot of test suites.

To execute the tests, simply run the ant command in the same folder of the build.xml file!

You now have no excuse not to test your application!