Chapter 13. PHPUnit and PhingPhing (PHing Is Not GNU make) [7] is a project-build system based on Apache Ant.[8] In the context of PHP, you do not need to build and compile your sources; the intention of Phing is to ease the packaging, deployment, and testing of applications. For these tasks, Phing provides numerous out-of-the-box operation modules ("tasks") and an easy-to-use, object-oriented model for adding your own custom tasks.
Phing can be installed using the PEAR Installer, as shown in the following command line: pear install http://phing.info/pear/phing-2.1.0-pear.tgz Phing uses simple XML build files that specify a target tree where various tasks are executed. One out-of-the-box task that comes with Phing is the <phpunit2> task that runs test cases using the PHPUnit framework. It is a functional port of Apache Ant's JUnit task. Example 15 shows a Phing build.xml file that specifies a <project> named BankAccount. The project's default <target> is called test. Using the <phpunit2> task, this target runs all test cases that can be found in source files that match the *Test.php condition. This is done by using a <batchtest> element that collects the included files from any number of nested <fileset> elements. In this example, the tests declared in the class BankAccountTest in the source file BankAccountTest.php will be run. Example 15. Phing build.xml file for the BankAccount tests<?xml version="1.0"?> <project name="BankAccount" basedir="." default="test"> <target name="test"> <phpunit2 haltonfailure="true" printsummary="true"> <batchtest> <fileset dir="."> <include name="*Test.php"/> </fileset> </batchtest> </phpunit2> </target> </project> Invoking Phing in the directory that contains build.xml (Example 15), BankAccount.php (Example 12), and BankAccountTest.php (Example 10) will run the tests by executing the project's default target, tests: phing Buildfile: /home/sb/build.xml BankAccount > test: [phpunit2] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 0.00067 sec BUILD FINISHED Total time: 0.0960 seconds Table 3 shows the parameters that can be used to configure the <phpunit2> task.
The following example shows the <phpunit2> task's output when a test fails: phing Buildfile: /home/sb/build.xml BankAccount > test: [phpunit2] Tests run: 4, Failures: 1, Errors: 0, Time elapsed: 0.00067 sec Execution of target "test" failed for the following reason: /home/sb/build.xml:5:37: One or more tests failed BUILD FAILED /home/sb/build.xml:5:37: One or more tests failed Total time: 0.0968 seconds |