Using Phing and PHPUnit with Symfony2


So recently I have been playing a lot with phing, symfony2, and Jenkins. Today I will show you how to build a very basic build script that runs phpunit. Future articles I will show you how to add to your build script to generate documentation and eventually integrate it all into Jenkins.

First we need to update our composer.json file to include phing and phpunit if it is not already there.

{
  "require-dev": {
      "phing/phing": "*",
      "phpunit/phpunit": "*"
  }
}

Now we need to run the update command with composer to install the dependencies.

php composer.phar update phing/phing phpunit/phpunit

Awesome, now that we have both phing and phpunit, we can start on the build script.

NOTE: By default these are installed in vendor/bin however, if you have changed this, they might be installed elsewhere. Composer Docs

In your project root directory, create a build.xml and fill it with

<?xml version="1.0" encoding="UTF-8"?>
<project name="Acme" default="phpunit">
    <target name="phpunit">
        <exec executable="vendor/bin/phpunit" passthru="true" checkreturn="true">
            <arg value="-c" />
            <arg value="app" />
        </exec>
    </target>
</project>

This will now run phpunit by default. Give it a try by running php vendor/bin/phing and it will run all your unit tests.

That’s all there is to it. In the next few articles I will show you some more in depth examples and configuring the Symfony2 test environment.