<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Gardner - PHP Developer &#187; phing</title>
	<atom:link href="http://www.davegardner.me.uk/blog/tag/phing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davegardner.me.uk/blog</link>
	<description>Just behind the bleeding edge of PHP.</description>
	<lastBuildDate>Fri, 02 Jul 2010 08:25:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setting up continuous integration for PHP using Hudson and Phing</title>
		<link>http://www.davegardner.me.uk/blog/2009/11/09/continuous-integration-for-php-using-hudson-and-phing/</link>
		<comments>http://www.davegardner.me.uk/blog/2009/11/09/continuous-integration-for-php-using-hudson-and-phing/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 13:44:44 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Dev Environment]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[hudson]]></category>
		<category><![CDATA[phing]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://www.davegardner.me.uk/blog/?p=5</guid>
		<description><![CDATA[In this, my first post, I'm going to write about the benefits of Unit Testing and how Continuous Integration (CI) can be used to get the best out of Unit Testing. This will include details of how I setup a CI system using Hudson CI server, Phing build tool combined with various other analysis tools (including PHP Unit).]]></description>
			<content:encoded><![CDATA[<p>In this, my first post, I&#8217;m going to write about the benefits of <strong>Unit Testing</strong> and how <strong>Continuous Integration</strong> (CI) can be used to get the best out of Unit Testing. This will include details of <a href="#php-ci-setup">how I setup a CI system</a> using <strong>Hudson</strong> CI server, <strong>Phing</strong> build tool combined with various other analysis tools (including <strong>PHP Unit</strong>).</p>
<p>One of the best explanations of Unit Testing I&#8217;ve read was <a href="http://stackoverflow.com/questions/67299/is-unit-testing-worth-the-effort/69263#69263" target="_blank">posted by benzado on Stack Overflow.</a></p>
<blockquote><p>Unit testing is a lot like going to the gym. You know it is good for you, all the arguments make sense, so you start working out. There&#8217;s an initial rush, which is great, but after a few days you start to wonder if it is worth the trouble.</p></blockquote>
<p>The difficulty with Unit Testing is <strong>keeping it up</strong>. It is very easy to slip into poor habits and before you know it there&#8217;s a huge chunk of code with no tests. Possibly a huge, badly designed chunk of code, that didn&#8217;t benefit from having tests written before it was coded. Before you know what&#8217;s going on, you end up with a project that you really <em>can&#8217;t</em> write tests for, because retrofitting the tests is near impossible.</p>
<p>For me, there are two critical reasons for Unit Testing:</p>
<ol>
<li><strong>Enforcing good design</strong><br />
To be able to write tests, you need to be able to zero in on a “unit” of code, isolating it from all the rest of your 1,000,000 lines of web application. Writing Unit Tests forces you to design systems that have loose coupling because otherwise it is impossible to test.</li>
<li><strong>Allowing changes to be made in confidence</strong><br />
Without Unit Tests, you get to the point where no one really wants to make any changes to the code. This is especially true in a commercial environment, where many people have worked on the code, including some key team member who has since left. Unit Tests allow you to make changes to one part of the code and be <em>pretty convinced</em> you haven&#8217;t messed up something else.</li>
</ol>
<h2>Continuous integration</h2>
<p>Martin Fowler <a href="http://martinfowler.com/articles/continuousIntegration.html" target="_blank">describes the process of Continuation Integration</a> in detail. He suggests:</p>
<blockquote><p>Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily &#8211; leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly. This article is a quick overview of Continuous Integration summarizing the technique and its current usage.</p></blockquote>
<p>The key idea behind CI is to do what is most painful often, namely “building” everyone&#8217;s code from source and making sure it all works.</p>
<p>A CI system usually consists of the following key elements:</p>
<div id="attachment_10" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-10" title="Continuous integration" src="http://www.davegardner.me.uk/blog/wp-content/uploads/2009/11/ci-300x247.jpg" alt="Continuous integration" width="300" height="247" /><p class="wp-caption-text">Continuous integration</p></div>
<ul>
<li>Developers commit code</li>
<li>CI server detects changes</li>
<li>CI server checksout code, runs tests, analyses code</li>
<li>CI server feeds back to development team</li>
</ul>
<p>If you want to find out more about CI, I recommend the excellent book <a href="#">Continuous Integration: Improving Software Quality and Reducing Risk</a>. There is an excerpt published on <a href="http://www.javaworld.com/javaworld/jw-06-2007/jw-06-awci.html" target="_blank">JavaWorld</a> which covers a lot of the key advantages. In particular, it highlights:</p>
<blockquote><p>1. Reduce risks<br />
2. Reduce repetitive manual processes<br />
3. Generate deployable software at any time and at any place<br />
4. Enable better project visibility<br />
5. Establish greater confidence in the software product from the development team</p></blockquote>
<p>CI gets the most out of Unit Tests by <strong>forcing them to be run after every change</strong>. Not only that, but with a good CI setup, developers instantly know if they haven&#8217;t written enough tests. If avoids the situtation where Joe Bloggs has added in a huge chunk of code with zero tests.</p>
<h2 id="php-ci-setup">Setting up CI for a PHP project</h2>
<p>To get my environment setup, I consulted the following blog posts which are worth a read:</p>
<ol>
<li><a href="http://blog.jepamedia.org/2009/10/28/continuous-integration-for-php-with-hudson/" target="_blank">http://blog.jepamedia.org/2009/10/28/continuous-integration-for-php-with-hudson/</a></li>
<li><a href="http://toptopic.wordpress.com/2009/02/26/php-and-hudson/" target="_blank">http://toptopic.wordpress.com/2009/02/26/php-and-hudson/</a></li>
</ol>
<p>I&#8217;m assuming you&#8217;re using a CentOS 5 server (or I guess RHEL5). If not, you may still find various parts of this useful.</p>
<h3>1. Install JDK</h3>
<p>EPEL provide a set of CentOS packages, including a package for openJDK. This is the easiest way of installing Java.</p>
<p>Firstly, setup <a href="http://fedoraproject.org/wiki/EPEL/FAQ#howtouse" target="_blank">EPEL</a>:</p>
<pre class="code">wget -O /etc/yum.repos.d/hudson.repo http://hudson-ci.org/redhat/hudson.repo</pre>
<p>Next install <a href="http://openjdk.java.net/install/" target="_blank">OpenJDK</a>:</p>
<pre class="code">yum install java-1.6.0-openjdk</pre>
<h3>2. Install Hudson</h3>
<p>Download and install the CentOS RPM for <a href="http://hudson-ci.org/redhat/" target="_blank">Hudson</a>:</p>
<pre class="code">wget -O /etc/yum.repos.d/hudson.repo http://hudson-ci.org/redhat/hudson.repo
rpm --import http://hudson-ci.org/redhat/hudson-ci.org.key
yum install hudson</pre>
<p>Now Hudson is installed, we can start using the standard CentOS “service” command.</p>
<pre class="code">service hudson start</pre>
<p>We can check Hudson is working by pointing the browser at port 8080 (the default Hudson port). Hudson will work “out of the box”  and give you a web interface immediately. This is the primary reason I decided to go with Hudson over the other possibilities, eg: CruiseControl and phpUnderControl. Although I didn&#8217;t do an exhaustive analysis before I decided on Hudson, it just <em>seemed</em> right to me.</p>
<p>To get the graphing engine working for Hudson, you may need to install x.</p>
<pre class="code">yum groupinstall base-x</pre>
<h3>3. Install phing</h3>
<p><a href="http://phing.info/trac/" target="_blank">Phing</a> is a PHP project build system or build tool based on Apache Ant. A build tool ensures that the process of creating your working web application from source code happens in a structured and repeatable way. This helps reduce the possibility of errors caused by simply uploading files via FTP or some other simple method.</p>
<p>Make sure PEAR is installed for PHP (this is the easiest way of installing phing):</p>
<pre class="code">yum install php-pear</pre>
<p>Then install the PEAR phing package:</p>
<pre class="code">pear channel-discover pear.phing.info
pear install phing/phing</pre>
<h3>4. Setup SVN</h3>
<p>If you haven&#8217;t got a <a href="http://subversion.tigris.org/" target="_blank">Subversion</a> repository, you&#8217;re going to need one (or some other SCM tool like CVS, GIT or Mercurial).</p>
<pre class="code">yum install mod_dav_svn</pre>
<p>The simplest setup involves creating a repo in /var/www/svn/&lt;my repo&gt;</p>
<pre class="code">mkdir -v /var/www/svn/test
svnadmin create --fs-type fsfs /var/www/svn/test
chown –R apache:apache /var/www/svn/test</pre>
<p>Setup Apache by pretty much uncommenting the lines in /etc/httpd/conf.d/subversion.conf. Once Apache restarted, you&#8217;ll be able to get to it via /repos/test, assuming you&#8217;re using the default settings (sets up SVN on /repos). I haven&#8217;t gone into the details of getting SVN up and running; there are lots of resources out there that will help you do this.</p>
<h3>5. Install PHP tools</h3>
<h5><a href="http://www.phpdoc.org/" target="_blank">PHPDocumentor</a> – to generate documentation automatically from code</h5>
<pre class="code">pear install PhpDocumentor</pre>
<h5><a href="http://github.com/sebastianbergmann/phpcpd" target="_blank">PHP CPD</a> – “copy and paste detector” for PHP</h5>
<p>This requires PHP 5.2. At time of writing, this wasn&#8217;t standard with CentOS 5, but is part of the CentOS “test” repo. This can be setup by creating a yum repo file, eg: /etc/yum.repos.d/centos-test.repo and populating with:</p>
<pre class="code">[c5-testing]
name=CentOS-5 Testing
baseurl=http://dev.centos.org/centos/5/testing/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing</pre>
<p>Then you can do:</p>
<pre class="code">yum update php</pre>
<p>You may also need to upgrade pear; if the install of phpcpd fails (below). To do this, try:</p>
<pre class="code">pear upgrade pear</pre>
<p>or, if this wants to be forced, and you think it&#8217;s a good idea (I did):</p>
<pre class="code">pear upgrade --force pear</pre>
<p>Finally we can install phpcpd!</p>
<pre class="code">pear channel-discover pear.phpunit.de
pear install phpunit/phpcpd</pre>
<h5><a href="http://www.pdepend.org/news.html" target="_blank">PHP Depend</a> &#8211; help analyse quality of codebase</h5>
<p>Note you may have update PHP to include the DOM module (first line below).</p>
<pre class="code">yum install php-dom
pear channel-discover pear.pdepend.org
pear install pdepend/PHP_Depend-beta</pre>
<h5><a href="http://pear.php.net/package/PHP_CodeSniffer" target="_blank">PHP Code Sniffer</a> &#8211; analyse code for adherence to style/standards</h5>
<pre class="code">pear install PHP_CodeSniffer-1.2.0</pre>
<h5><a href="http://pear.php.net/package/PHP_CodeSniffer" target="_blank">PHP Unit</a> &#8211; unit test framework for PHP</h5>
<pre class="code">pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit</pre>
<p>To make PHP Unit work, we need <a href="http://xdebug.org/" target="_blank">XDebug</a> installed, the PHP profiler.</p>
<pre class="code">yum install php-devel gcc
pecl install xdebug</pre>
<h3>6. Install Hudson plugins</h3>
<p>Use the web interface to install the following plugins (Manage Hudson -&gt; Plugins).</p>
<ul>
<li>Checkstyle</li>
<li>Clover</li>
<li>DRY</li>
<li>Green Balls (handy because it shows successful builds as green circles rather than blue)</li>
<li>JDepend</li>
<li>xUnit (will handle the output of PHPUnit test results XML)</li>
</ul>
<h3>7. Setup the phing build script</h3>
<p>The Phing build script defines what steps will be taken to “build” the application.</p>
<p>Hudson itself works by placing our code into a project workspace. It will checkout the code from subversion and place it into the following location, where “Test” is the name of our project.</p>
<pre class="code">/var/lib/hudson/jobs/Test/workspace/</pre>
<p>We can then use the Phing build script to carry out a number of processes on this code. When we talk about “building”, what we will actually do is place the code where we need it so it can actually run the website (we&#8217;ll keep this within the workspace) plus we run tests etc&#8230;</p>
<p>We&#8217;ll keep the build script in the subversion repository, so effectively it will be updated from SVN each build. For this approach to work, the following XML needs to be stored in a file named build.xml, stored in the project root folder (within trunk), eg: /trunk/build.xml</p>
<pre class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 &lt;project name="test" basedir="." default="app"&gt;
    &lt;property name="builddir" value="${ws}/build" /&gt;

    &lt;target name="clean"&gt;
        &lt;echo msg="Clean..." /&gt;
        &lt;delete dir="${builddir}" /&gt;
    &lt;/target&gt;

    &lt;target name="prepare"&gt;
        &lt;echo msg="Prepare..." /&gt;
        &lt;mkdir dir="${builddir}" /&gt;
        &lt;mkdir dir="${builddir}/logs" /&gt;
        &lt;mkdir dir="${builddir}/logs/coverage" /&gt;
        &lt;mkdir dir="${builddir}/docs" /&gt;
        &lt;mkdir dir="${builddir}/app" /&gt;
    &lt;/target&gt;

    &lt;!-- Deploy app --&gt;
    &lt;target name="app"&gt;
        &lt;echo msg="We do nothing yet!" /&gt;
    &lt;/target&gt;

    &lt;!-- PHP API Documentation --&gt;
    &lt;target name="phpdoc"&gt;
        &lt;echo msg="PHP Documentor..." /&gt;
        &lt;phpdoc title="API Documentation"
                destdir="${builddir}/docs"
                sourcecode="yes"
                defaultpackagename="MHTest"
                output="HTML:Smarty:PHP"&gt;
            &lt;fileset dir="./app"&gt;
                &lt;include name="**/*.php" /&gt;
            &lt;/fileset&gt;
        &lt;/phpdoc&gt;
    &lt;/target&gt;

    &lt;!-- PHP copy/paste analysis --&gt;
    &lt;target name="phpcpd"&gt;
        &lt;echo msg="PHP Copy/Paste..." /&gt;
        &lt;exec command="phpcpd --log-pmd=${builddir}/logs/pmd.xml source" escape="false" /&gt;
    &lt;/target&gt;

    &lt;!-- PHP dependency checker --&gt;
    &lt;target name="pdepend"&gt;
        &lt;echo msg="PHP Depend..." /&gt;
        &lt;exec command="pdepend --jdepend-xml=${builddir}/logs/jdepend.xml ${ws}/source" escape="false" /&gt;
    &lt;/target&gt;

    &lt;!-- PHP CodeSniffer --&gt;
    &lt;target name="phpcs"&gt;
        &lt;echo msg="PHP CodeSniffer..." /&gt;
        &lt;exec command="phpcs --standard=ZEND --report=checkstyle ${ws}/source &gt; ${builddir}/logs/checkstyle.xml" escape="false" /&gt;
    &lt;/target&gt;

    &lt;!-- Unit Tests &amp; coverage analysis --&gt;
    &lt;target name="phpunit"&gt;
        &lt;echo msg="PHP Unit..." /&gt;
        &lt;exec command="phpunit --log-junit ${builddir}/logs/phpunit.xml --log-pmd ${builddir}/logs/phpunit.pmd.xml --coverage-clover ${builddir}/logs/coverage/clover.xml --coverage-html ${builddir}/logs/coverage/ ${ws}/source/tests"/&gt;
    &lt;/target&gt;
&lt;/project&gt;</pre>
<h3>8. Setup Hudson</h3>
<p>The first step is to create a new job.</p>
<ul>
<li>From the Hudson homepage, click New Job.</li>
<li>Enter a Job name, for example “Dave&#8217;s Product Build” and choose “Build a free-style software project”. Click OK.</li>
</ul>
<p>Now you need to configure the job; the configuration form should be displayed immidiately after adding.</p>
<p>Under <strong>Source Code Management</strong> choose <strong>Subversion</strong> and enter:</p>
<ul>
<li>Repository URL: http://www.myrepo.com/path/to/repo</li>
<li>Local module directory: source</li>
<li>Check &#8220;Use update&#8221; which speeds up checkout</li>
</ul>
<p>Under <strong>Build Triggers</strong> select <strong>Poll SCM</strong> and enter the following schedule:</p>
<pre class="code">5 * * * *
10 * * * *
15 * * * *
20 * * * *
25 * * * *
30 * * * *
35 * * * *
40 * * * *
45 * * * *
50 * * * *
55 * * * *</pre>
<p>Note that this will poll for changes to the repository every 5 minutes and rebuild if any changes are detected.</p>
<p>Under <strong>Build</strong> click the button to <strong>Add build step</strong> and choose <strong>Execute shell</strong>, enter the command:</p>
<pre class="code">phing -f $WORKSPACE/source/build.xml prepare app phpdoc phpcs phpunit -Dws=$WORKSPACE</pre>
<p>Under <strong>Post-build Actions</strong> choose:</p>
<ul>
<li>Check Publish Javadoc and then enter:<br />
Javadoc directory = build/docs/</li>
<li>Check Publish testing tools result report and then click Add and pick PHP Unit, enter:<br />
+ PHPUnit Pattern = build/logs/phpunit.xml</li>
<li>Check Publish Clover Coverage Report and enter:<br />
+ Clover report directory = build/logs/coverage<br />
+ Clover report file name = clover.xml</li>
<li>Check Publish duplicate code analysis results and enter:<br />
+ Duplicate code results = build/logs/phpunit.pmd-cpd.xml</li>
<li>Check Publish Checkstyle analysis results and enter:<br />
+ Checkstyle results = build/logs/checkstyle.xml</li>
</ul>
<p>Finally, click <strong>Build Now</strong> to test it all works.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davegardner.me.uk/blog/2009/11/09/continuous-integration-for-php-using-hudson-and-phing/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
