Book Review: Heads You Win

Setting up and running php unit tests from Eclipse - 3 / 3

In Part 1, we saw how to set up your Windows system to run phpunit.
In Part 2, we saw how we can set up Eclipse to run phpunit

Here, I want to show how we set this up to run on jenkins, our continuous integration server and use the data for sonar report.

I decided to go with a simple ant build script.  There were two requirements
  • since vendor folder was not checked in, ant should run composer install to create the vendor folder and install/update the dependencies
  • it should then run phpunit with the relevant parameters to generate a test report as well as a coverage report
The previous version of sonar php plugin ran phpunit as part of its run.  The current version requires you to have run the tests and generated the report.  It takes the test report and coverage report as properties.

Here are the relevant snippets of the build script.
  • Run composer install
    <target name="run-composer">
        <exec executable="composer">
            <arg value="install"/>

  • Run phpunit tests and generate reports
 <target name="test" description="Run phpunit" depends="run-composer">
<echo message="Running unit tests with PHPUnit" />
        <exec executable="${basedir}/vendor/bin/phpunit" failonerror="false">
            <arg value="-c"/>
            <arg value="protected/tests/phpunit.xml"/>
            <arg value="--log-junit"/>
            <arg value="report/junit.xml"/>
            <arg value="--coverage-clover"/>
            <arg value="report/code-coverage.xml"/>

Jenkins console
Jenkins Console log

The above will create a report folder and create two files - junit.xml, which will have a test report in junit format and code-coverage.xml, which will have the coverage report in clover format.   Sonar needs the reports to be in these formats - this information is not well-documented.

Configure the project for sonar by having a with the relevant entries:

# Required metadata
sonar.projectName=My Project




You will also need to specify the language explicitly


if you get an error similar to mine

ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: Language of file 'protected/components/Controller.php' can not be decided as the file matches patterns of both sonar.lang.patterns.web : **/*.html,**/*.xhtml,**/*.jsp,**/*.jspf,**/*.jsf,**/*.php,**/*.erb,**/*.rhtml and sonar.lang.patterns.php : **/*.php,**/*.php3,**/*.php4,**/*.php5,**/*.phtml,**/*.inc

Sonar report
Sonar Report