Continuous Integration requires a fully automated and reproducible build as well as the use of a version control system to be effective. This book makes the assumption that the reader is already familiar with a version control system such as Git.
In this chapter we discuss build automation, the practice of automating (scripting) the various tasks that software developers need to perform in their daily routine. These tasks usually include the compilation of source code into binary code and the running of automated tests as well as the packaging and possibly even the deployment of the resulting binaries (PEAR packages or PHAR archives, for instance).
Although PHP is an interpreted language and does not use an explicit compilation step it is common to perform code generation or code transformation tasks during a build nowadays. Scaffolding code generated by a framework or code generated by an object-relational mapping tool as well as autoloader code are common use cases for such generated code.
PHP allows registering a callback which is automatically invoked when a class or interface is about to be used but has not been declared yet. phpab is a tool that automatically generates the code for such a callback by analysing the project's sourcecode.
A wide variety of build automation tools exists. I have worked with development teams that were content with using simple shell scripts to automate their builds. I have also seen GNU make, Apache Ant, Phing, Pake, Rake, and custom solutions being successfully used to automate the build of PHP projects. My personal preference is Apache Ant and that is what we will use in the examples in this book.
It poses no problem if you happen to prefer another build automation tool than Apache Ant. You will still be able to use Jenkins to continuously integrate your PHP project.
Throughout this book we will use an example project that is hosted at http://github.com/thePHPcc/bankaccount. This is what the project structure looks like:
. ├── build │ ├── phpcs.xml │ ├── phpmd.xml │ ├── src_autoload.php.in │ └── tests_autoload.php.in ├── build.xml ├── phpunit.xml.dist ├── src │ ├── autoload.php │ └── ... └── tests ├── autoload.php └── ...
The build directory contains the XML configuration files for PHP_CodeSniffer (phpcs.xml) and PHPMD (phpmd.xml) as well as customized templates for phpab, the PHP Autoload Builder.
PHP_CodeSniffer and PHPMD are static analysis tools for PHP code and are covered in Chapter 4 where we discuss Continuous Inspection.
build.xml is the Apache Ant build script that is used to build the project.
phpunit.xml.dist is the PHPUnit configuration file for the project.
If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and PHPUnit's --configuration switch is not used, the configuration for PHPUnit will be automatically read from that file.
src/autoload.php and tests/autoload.php contain the autoloader code for the application and its test suite, respectively.