Приглашаем посетить
Андреев (andreev.lit-info.ru)

Chapter 13.  PHPUnit and Phing

Previous
Table of Contents
Next

Chapter 13. PHPUnit and Phing

Phing (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.

[7] http://www.phing.info/wiki/

[8] http://ant.apache.org/

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.

Table 3. Attributes for the <phpunit2> element

Name

Type

Description

Default

haltonerror

Boolean

Stops the build process if an error occurs during the test run.

false

haltonfailure

Boolean

Stops the build process if a test fails. Errors are considered failures as well.

false

printsummary

Boolean

Prints one-line statistics for each test case.

false


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


Previous
Table of Contents
Next