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"/>
        </exec>
    </target>

  • 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"/>
        </exec>
    </target>


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 sonar-project.properties with the relevant entries:

# Required metadata
sonar.projectKey=my.package:myProject
sonar.projectName=My Project
sonar.projectVersion=1.0-SNAPSHOT

sonar.sources=src
sonar.exclusions=lib/**

sonar.php.coverage.reportPath=report/code-coverage.xml

sonar.php.tests.reportPath=report/junit.xml


You will also need to specify the language explicitly

sonar.language=php


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

Comments

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

Automating a cordova ios build