In this chapter we will expand our existing build.xml script for Ant to run unit tests using PHPUnit and then create a Jenkins job for our PHP project.
Instead of putting this information into the build.xml script for Ant, we follow the concept of Separation of Concerns and use PHPUnit's configuration file feature to configure the tests we want to run, the logfiles (JUnit XML for test results and Clover XML for code coverage information) and reports (code coverage report in HTML format) we want generated, and other settings such as the bootstrap script (our autoloader).
Example 3-1 shows the phpunit.xml.dist configuration file for our project.
Example 3-1. phpunit.xml.dist configuration file for PHPUnit
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="tests/autoload.php" backupGlobals="false" backupStaticAttributes="false" strict="true" verbose="true"> <testsuite name="BankAccount"> <directory suffix="Test.php">tests/unit</directory> </testsuite> <logging> <log type="coverage-clover" target="build/logs/clover.xml"/> <log type="coverage-html" target="build/coverage" title="BankAccount"/> <log type="junit" target="build/logs/junit.xml"/> </logging> <filter> <whitelist addUncoveredFilesFromWhitelist="true"> <directory suffix=".php">src</directory> <exclude> <file>src/autoload.php</file> </exclude> </whitelist> </filter> </phpunit>
Invoking phpunit
without any
parameters in the project directory (where the phpunit.xml.dist configuration file resides)
will result in PHPUnit running the tests the way we set it up in the
configuration.
We can now add a target to our build.xml script that invokes PHPUnit to run our unit tests. While we are at it, we add two other targets that handle cleaning up build artifacts and preparing directories for build artifacts, respectively:
The clean
target is
responsible for cleaning up (deleting) any artifacts that may have
been produced by a previous build. This target is a dependency of
prepare
but invoking it manually is
also useful sometimes.
The prepare
target invokes
the clean
target and then creates
the directories to which PHPUnit writes its logfiles and reports and
(by means of the phpab
target)
generates the autoloader code.
The phpunit
target invokes
PHPUnit.
Example 3-2 shows
how these three new targets are implemented. The implementation of the
phpab
task remains the same as shown in
Chapter 1 and is not repeated here.
Example 3-2. build.xml script that invokes PHPUnit
<?xml version="1.0" encoding="UTF-8"?> <project name="BankAccount" default="build"> <target name="build" depends="prepare,phpunit"/> <target name="clean" description="Cleanup build artifacts"> <delete dir="${basedir}/build/coverage"/> <delete dir="${basedir}/build/logs"/> </target> <target name="prepare" depends="clean,phpab" description="Prepare for build"> <mkdir dir="${basedir}/build/coverage"/> <mkdir dir="${basedir}/build/logs"/> </target> <target name="phpab" description="Generate autoloader scripts"> <!-- ... --> </target> <target name="phpunit" description="Run unit tests with PHPUnit"> <exec executable="phpunit" failonerror="true"/> </target> </project>
Example 3-3 shows the output of running the build.xml script with Ant.
Example 3-3. Running the build.xml script
ant
Buildfile: /home/sb/bankaccount/build.xml
clean:
[delete] Deleting directory /home/sb/bankaccount/build/coverage
[delete] Deleting directory /home/sb/bankaccount/build/logs
phpab:
[exec] Autoload file '/home/sb/bankaccount/src/autoload.php' generated.
[exec]
[exec] Autoload file '/home/sb/bankaccount/tests/autoload.php' generated.
[exec]
prepare:
[mkdir] Created dir: /home/sb/bankaccount/build/coverage
[mkdir] Created dir: /home/sb/bankaccount/build/logs
phpunit:
[exec] PHPUnit 3.5.15 by Sebastian Bergmann.
[exec]
[exec] .......................................
[exec]
[exec] Time: 0 seconds, Memory: 9.50Mb
[exec]
[exec] OK (39 tests, 69 assertions)
[exec]
[exec] Writing code coverage data to XML file, this may take a moment.
[exec]
[exec] Generating code coverage report, this may take a moment.
[exec]
build:
BUILD SUCCESSFUL
Total time: 2 seconds
We should make sure that the PHP code does not contain an syntax
errors before we try to run the unit tests. The code below implements a
target that uses Apache Ant's <apply>
task to invoke PHP's lint checker
(php -l
) for each source code file in
the src and tests directories. The <apply>
task works like the <exec>
task but can operate on multiple
files when provided with a <fileset>
.
<target name="lint"> <apply executable="php" failonerror="true"> <arg value="-l" /> <fileset dir="${basedir}/src"> <include name="**/*.php" /> </fileset> <fileset dir="${basedir}/tests"> <include name="**/*.php" /> </fileset> </apply> </target>
Now we have everything in place to create a job for our PHP project in Jenkins.