Prev Next

Chapter 19. Build Automation

This chapter provides an overview of build automation tools that are commonly used with PHPUnit.

Apache Ant

Apache Ant is a Java-based build tool. In theory, it is kind of like make, without make's wrinkles. Build files for Apache Ant are XML-based, calling out a target tree where various tasks get executed.

Example 19.1 shows an Apache Ant build.xml file that invokes PHPUnit using the built-in <exec> task. The build process is aborted if a test fails (failonerror="true").

Example 19.1: Apache Ant build.xml file that invokes PHPUnit

<project name="Money" default="build">
 <target name="clean">
  <delete dir="${basedir}/build"/>
 </target>

 <target name="prepare">
  <mkdir dir="${basedir}/build/logs"/>
 </target>

 <target name="phpunit">
  <exec dir="${basedir}" executable="phpunit" failonerror="true">
   <arg line="--log-xml ${basedir}/build/logs/phpunit.xml MoneyTest" />
  </exec>
 </target>

 <target name="build" depends="clean,prepare,phpunit"/>
</project>


ant
Buildfile: build.xml

clean:

prepare:
    [mkdir] Created dir: /home/sb/Money/build/logs

phpunit:
     [exec] PHPUnit 3.5.0 by Sebastian Bergmann.
     [exec] 
     [exec] ......................
     [exec] 
     [exec] Time: 0 seconds
     [exec] 
     [exec] OK (22 tests, 34 assertions)

build:

BUILD SUCCESSFUL
Total time: 0 seconds

The XML logfile for test results produced by PHPUnit (see the section called “Test Results (XML)”) is based upon the one used by the <junit> task that comes with Apache Ant.

Apache Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Apache Maven can manage a project's build, reporting and documentation from a central place of information. Maven for PHP uses the power of Maven for building, testing, and documenting PHP projects.

Phing

Phing (PHing Is Not GNU make) is a project build system based on Apache Ant. You can do anything with it that you could do with a traditional build system such as GNU make, and its use of simple XML build files and extensible PHP "task" classes make it an easy-to-use and highly flexible build framework. Features include file transformations (e.g. token replacement, XSLT transformation, Smarty template transformations), file system operations, interactive build support, SQL execution, CVS operations, tools for creating PEAR packages, and much more.

Example 19.2 shows a Phing build.xml file that invokes PHPUnit using the built-in <phpunit> task. The build process is aborted if a test fails (haltonfailure="true").

Example 19.2: Phing build.xml file that invokes PHPUnit

<project name="Money" default="build">
 <target name="clean">
  <delete dir="build"/>
 </target>

 <target name="prepare">
  <mkdir dir="build/logs"/>
 </target>

 <target name="phpunit">
  <phpunit printsummary="true" haltonfailure="true">
    <formatter todir="build/logs" type="xml"/>
    <batchtest>
      <fileset dir=".">
        <include name="*Test.php"/>
      </fileset>
    </batchtest>
  </phpunit>
 </target>

 <target name="build" depends="clean,prepare,phpunit"/>
</project>


phing
Buildfile: /home/sb/Money/build.xml

Money > clean:


Money > prepare:

    [mkdir] Created dir: /home/sb/Money/build/logs

Money > phpunit:

  [phpunit] Test: MoneyTest, Run: 22, Failures: 0, Errors: 0,
            Incomplete: 0, Skipped: 0, Time elapsed: 0.06887 s

Money > build:


BUILD FINISHED

Total time: 0.2632 seconds
Prev Next
1. Automating Tests
2. PHPUnit's Goals
3. Installing PHPUnit
4. Writing Tests for PHPUnit
Test Dependencies
Data Providers
Testing Exceptions
Testing PHP Errors
5. The Command-Line Test Runner
6. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
Global State
7. Organizing Tests
Composing a Test Suite Using the Filesystem
Composing a Test Suite Using XML Configuration
Using the TestSuite Class
8. TestCase Extensions
Testing Output
9. Database Testing
Data Sets
Flat XML Data Set
XML Data Set
CSV Data Set
Replacement Data Set
Operations
Database Testing Best Practices
10. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
11. Test Doubles
Stubs
Mock Objects
Stubbing and Mocking Web Services
Mocking the Filesystem
12. Testing Practices
During Development
During Debugging
13. Test-Driven Development
BankAccount Example
14. Code Coverage Analysis
Specifying Covered Methods
Ignoring Code Blocks
Including and Excluding Files
15. Other Uses for Tests
Agile Documentation
Cross-Team Tests
16. Skeleton Generator
Generating a Test Case Class Skeleton
Generating a Class Skeleton from a Test Case Class
17. PHPUnit and Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
18. Logging
Test Results (XML)
Test Results (TAP)
Test Results (JSON)
Code Coverage (XML)
19. Build Automation
Apache Ant
Apache Maven
Phing
20. Continuous Integration
Atlassian Bamboo
CruiseControl
phpUnderControl
21. PHPUnit API
Overview
PHPUnit_Framework_Assert
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInstanceOf()
assertInternalType()
assertLessThan()
assertLessThanOrEqual()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertTag()
assertThat()
assertTrue()
assertType()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
Package Structure
22. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Assert Classes
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
Subclass PHPUnit_Framework_TestResult
Implement PHPUnit_Framework_TestListener
New Test Runner
A. Assertions
B. Annotations
@assert
@author
@backupGlobals
@backupStaticAttributes
@covers
@dataProvider
@depends
@expectedException
@expectedExceptionCode
@expectedExceptionMessage
@group
@outputBuffering
@runTestsInSeparateProcesses
@runInSeparateProcess
@test
@testdox
@ticket
C. The XML Configuration File
PHPUnit
Test Suites
Groups
Including and Excluding Files for Code Coverage
Logging
Test Listeners
Setting PHP INI settings, Constants and Global Variables
Configuring Browsers for Selenium RC
D. Index
E. Bibliography
F. Copyright