Posts Tagged ‘PHPUnderControl on Centos5.1’

h1

Setting up PHPUnderControl on Centos5.1

October 24, 2008

So for the past few days I’ve been tinkering around with PHPUnderControl, for all kinds of reasons I decided to install it on centos5.1 & thought I’d docuement it for myself & others, I know I’ll definately be installing it again, some time soon.

Needed files
JDK
CruiseControl
PHPUnderControl
XDebug

Make sure that there are no JDK alternatives already install, remove if there is
chmod 755 &&
run ./jdk-6u10-linux-i586-rpm.bin

which will uncompress the RPM for your, now run the below to install it.
rpm -Uvh jdk-6u10-linux-i586.rpm

We’ll also need to modify php.ini so that the memory limit is increased the below should do.
memory_limit = 256M

Still having php.ini open, we will also need to modify the date.timezone property, as not doing so, will produce errors. Set it to your locale, the below works for me.
date.timezone = Europe/London

Once this is done, we will need to setup our JAVA_HOME environment, to initialise this at login, place in .bashrc as java is installed in /usr/bin as default.
export JAVA_HOME=/usr

we then unpack cruisecontrol & run in the background
unzip cruisecontrol-bin-2.8-dev.zip -d /opt &&
cd /opt &&
ln -s /opt/cruisecontrol-bin-2.4.1 /opt/cruisecontrol

We’ll need to configure ant so that it knows where to find our projects & setup build times etc, the below config file will do for now:
<?xml version="1.0"?>
<cruisecontrol>
<project name="PROJECTNAME" buildafterfailed="false">
<listeners>
<currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
</listeners>
<modificationset quietperiod="30">
<filesystem folder="projects/${project.name}"/>
</modificationset>
<schedule interval="300">
<ant anthome="apache-ant-1.7.0" buildfile="projects/${project.name}/build.xml"/>
</schedule>
<log dir="logs/${project.name}">
<merge dir="projects/${project.name}/build/logs/"/>
</log>
<publishers>
<currentbuildstatuspublisher file="logs/${project.name}/buildstatus.txt"/>
<artifactspublisher dir="projects/${project.name}/build/api" dest="logs/${project.name}" subdirectory="api"/>
<artifactspublisher dir="projects/${project.name}/build/coverage" dest="logs/${project.name}" subdirectory="coverage"/>
<execute command="phpuc graph logs/${project.name}"/>
<execute command="phpuc graph logs/${project.name} artifacts/${project.name}" />
</publishers>
</project>
</cruisecontrol>

We’ll have to retrieve the project from SVN manually, so do the follow:
cd /opt/cruisecontrol &&
mkdir -p projects/PROJECTNAME/build/logs &&
cd projects/PROJECTNAME &&
svn co pathto.svn.repo/svn source

We’ll need PEAR 1.6.1 to be able to install phpundercontrol, so we’ll do an update. which I found here
wget http://www.smudge-it.co.uk/pub/yum/centos/4/i386/php-pear-1.6.1-2.noarch.rpm
rpm -Uvh php-pear-1.6.1-2.noarch.rpm

we now need to install phpundercontrol
pear config-set preferred_state beta
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear install --alldeps phpunit/phpUnderControl

The above command will install PHPUnderControl along with all its dependancies. All we need to do is the follow:
phpuc install /opt/cruisecontrol

Which will install PHPUnderControl into our CruiseControl file. Now we’ll start adding the bells & whistles.

So lets get PHPDocumentor to automatically build  or API docs for us.

<project name='PROJECTNAME' default='build' basedir='.'>
....
<target name="php-documentor">
<exec executable="phpdoc" dir="${basedir}/source" logerror="on">
<arg line="--title '${ant.project.name}' -ue on -t ${basedir}/build/api -d source -tb '/usr/share/pear/data/phpUnderControl/data/phpdoc' -o HTML:Phpuc:phpuc"/>
</exec>
</target>
.....
</project>

Now we want to create the documents directory & run ant (from within our project directory) to test that our docs get created.

mkdir -p build/api &&
../../apache-ant-1.7.0/bin/ant php-documentor

Once everything has run without a hitch, we’ll need XDebug so that PHPUnit will work under phpundercontrol (note that XDebug conflicts with ZendDebugger), type the command below if you don’t already have gcc & php-devel they’ll be needed to compile XDebug.
yum install php-devel gcc

Now we can compile XDebug.
pecl install xdebug

All we need now is to do add XDebug to PHP is
echo zend_extension=/usr/lib/php/modules/xdebug.so >>/etc/php.ini

Now lets get PHPUnit to do our testing for us, we’ll need to configure build.xml file for this.
<project name='PROJECTNAME' default='build' basedir='.'>
....
<target name="phpunit">
<exec executable="phpunit" dir="${basedir}/source" failonerror="on">
<arg line=" --log-xml ${basedir}/build/logs/phpunit.xml --log-pmd ${basedir}/build/logs/phpunit.pmd.xml  --log-metrics ${basedir}/build/logs/phpunit.metrics.xml --coverage-xml  ${basedir}/build/logs/phpunit.coverage.xml --coverage-html ${basedir}/build/coverage ProjectTests test/AllTests.php"/>
</exec>
</target>
.....
</project>

The above will setup PHPUnit to run our tests AllTests.php from the test directory and call our tests ProjectTests (all of which can be changed). AllTests.php contains a list of all the test suites within the project. The log related flags builds our logs, which PHPUnderControl will use for statistics, the coverage flags are used for code coverage & are useful to determine how much of the project has been tested.

We now create our logs directory & make sure our setup is correct.
mkdir -p build/logs &&
../../apache-ant-1.7.0/bin/ant phpunit

Again, once all is well, we’ll move on to the next step, CodeSniffer.
<project name='PROJECTNAME' default='build' basedir='.'>
....
<target name="php-codesniffer">
<exec executable="phpcs" dir="${basedir}/source" output="${basedir}/build/logs/checkstyle.xml" error="/tmp/checkstyle.error.log">
<arg line="--report=checkstyle --standard=PEAR source"/>
</exec>
</target>
.....
</project>

The above configuration will check the code against the PEAR (please refer to CodeSniffer for other standards),  Once we have saved the changes, we run ant again to make sure everything is working as intended.
../../apache-ant-1.7.0/bin/ant php-codesniffer

We have now setup PHPUnderControl & our build.xml file should look similar to below:
<?xml version="1.0" encoding="UTF-8"?>
<project name="PROJECTNAME" default="build" basedir=".">
<target name="build" depends="php-documentor,php-codesniffer,phpunit"/>
<target name="php-documentor">
<exec executable="phpdoc" dir="${basedir}/source" logerror="on">
<arg line="--title '${ant.project.name}' -ue on -t ${basedir}/build/api -d src -tb '/usr/share/pear/data/phpUnderControl/data/phpdoc' -o HTML:Phpuc:phpuc"/>
</exec>
</target>
<target name="php-codesniffer">
<exec executable="phpcs" dir="${basedir}/source" output="${basedir}/build/logs/checkstyle.xml" error="/tmp/checkstyle.error.log">
<arg line="--report=checkstyle --standard=Zend src"/>
</exec>
</target>
<target name="phpunit">
<exec executable="phpunit" dir="${basedir}/source" failonerror="on">
<arg line=" --log-xml ${basedir}/build/logs/phpunit.xml --log-pmd ${basedir}/build/logs/phpunit.pmd.xml  --log-metrics ${basedir}/build/logs/phpunit.metrics.xml --coverage-xml  ${basedir}/build/logs/phpunit.coverage.xml --coverage-html ${basedir}/build/coverage ProjectTest test/AllTests.php"/>
</exec>
</target>
</project>

As one last addage, we’ll setup cruisecontrol to send out emails on success, failure & always (not the spam potential here).

<email mailhost="smtp.emailserver.com"
username="username"
password="password"
returnaddress="y@me.com"
buildresultsurl="http://localhost:8080/buildresults/${project.name}"
skipusers="true"
spamwhilebroken="true">
<always address="always@me.com"/>
<failure address="fail@me.com"/>
<success address="success@me.com"/>
</email>

Note that the SMTP server, login credentials & the address attribute are always valid, otherwise there will be no easy way to determine the mails are getting sent out,

And the config.xml file should look simular to the below:
<?xml version="1.0"?>
<cruisecontrol>
<project name="PROJECTNAME" buildafterfailed="false">
<listeners>
<currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
</listeners>
<modificationset quietperiod="30">
<!-- touch any file in project to trigger a build -->
<filesystem folder="projects/${project.name}"/>
</modificationset>
<schedule interval="300">
<ant anthome="apache-ant-1.7.0" buildfile="projects/${project.name}/build.xml"/>
</schedule>
<log dir="logs/${project.name}">
<merge dir="projects/${project.name}/build/logs/"/>
</log>
<publishers>
<currentbuildstatuspublisher file="logs/${project.name}/buildstatus.txt"/>
<artifactspublisher dir="projects/${project.name}/build/api" dest="logs/${project.name}" subdirectory="api"/>
<artifactspublisher dir="projects/${project.name}/build/coverage" dest="logs/${project.name}" subdirectory="coverage"/>
<execute command="phpuc graph logs/${project.name}"/>
<execute command="phpuc graph logs/${project.name} artifacts/${project.name}" />
<email mailhost="smtp.emailserver.com"
username="username"
password="password"
returnaddress="y@me.com"
buildresultsurl="http://localhost:8080/buildresults/${project.name}"
skipusers="true"
spamwhilebroken="true">
<always address="always@me.com"/>
<failure address="fail@me.com"/>
<success address="success@me.com"/>
</email>
</publishers>
</project>
</cruisecontrol>

Finally we tie everything together by editing PHPUnderControls build.xml file
<target name="build" depends="phpunit,php-documentor,php-codesniffer" />

We now have PHPUnderControl working with our project, we can further customise CruiseControl so that it will run on bootup & listen on a specific host & port. To do this we need to run the following
./cruisecontrol -host testing.myhostname.com -webport 8000

Now if we want to automatically start CruiseControl we could place it in rc.local to do so we do the following:
vim /etc/rc.local
Appending the below command to the file.
/opt/cruisecontrol/cruisecontrol.sh -host testing.myhostname.com -webport 8000&

We should now have PHPUnderControl fully functional & running ready to retrieve our project from SVN and test all of its test cases periodically.

Advertisements