PhpUnderControl Framework For Symfony Projects

Just another WordPress.com weblog

  • November 2017
    M T W T F S S
    « Jul    
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  

Posted by anoopvdk on July 29, 2008

Introduction

phpUnderControl is an extension for Cruise Control that integrates several PHP development tools, such as

  • PHPUnit
    • Which contains a rich set of features like Unit Testing and Functional Testing, Code Coverage, Project Mess Detection and Software Metrics etc,
  • PHP_Code Sniffer
    • For static code analysis
  • PHPDocumentor
    • For API documentation generation. It comes with a powerful command-line tool that can, among other things, automatically create Cruise Control’s XML configuration files for your project.

Initial Setups for phpUnder Control

Prerequisites

  1. JDK Version 1.5 or above
  2. Symfony Framework

Setting Up JDK

  1. Download & extract Latest JDK from SUN Java to /usr/java (Standard path)
  2. Setup the environment (Linux)
    1. Edit /etc/profile and add the below to make it available global
    2. export JAVA_HOME=/usr/java/jdk1.6.0_06
    3. export PATH=$PATH:$JAVA_HOME/bin
    4. save and exit.
    5. A restart or #source /etc/profile would require to make the changes effective.
    6. Check the version details Execute the command “java -version”. Should see something like
java version “1.6.0_06”
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

Setting symfony and plugins

  1. Install the symfony framework.
Further information on installing symfony framework can be obtained from http://www.symfony-project.org/installation/1_1
  1. Install the sgtest4cruise plugin.
    1. Download plugin from symfony plugins for cruise control.
    2. Put this file in the plugins directory of your application.
    3. Add the plugin to “enabled_modules” in settings.yml of your application.
    4. Modify the following variables to the needs of your project in app.yml of your application:
  • app_test4cruise_lime_file: path to the lime.php file in Symfony
  • app_test4cruise_sg_lime_file: path to the extended library
  • app_test4cruise_log: path to generated log file

Setting CruiseControl CodeBase

As mentioned earlier phpUnderControl is extension of Cruise Control, so we want to set up Cruise Control first.

  1. Just download cruise control 2.7.2 (or recent version) archives from “sourceforge.net”.
  2. Unzip the contents into an arbitrary folder (CCHOME)

Install phpUnderControl

The preferred way to install phpUnderControl is PEAR installation. The following steps will helps to install phpunderControl on to your system

  1. pear config-set preferred_state beta
  2. pear channel-discover components.ez.no
  3. pear channel-discover pear.phpunit.de
  4. pear install –alldeps phpunit/phpUnderControl

Patch your phpUnderControl with cruise control

Simple ! Execute the following command from terminal window.

  • phpUnderControl/bin/phpuc(.php|.bat) install /path/to/cruise control

Folder Structure

Now you should create a well defined folder structure for the phpundercontrol framework. First of all we need to

  1. create a new folder with your project name in” ./cruise control/project/ “ path. After that we can use svn checkout for getting the latest version of the application and put it under “/path/to/cruisecontrol/project/project name/ “ folder with a new folder name as source. Else we can also create symbolic link with same name(source) and pointed to your actual application path (Only if the website is hosted locally on that particular server).

Now the time to create your log files, it is better to create all log files under a common folder. So create a folder with name build under “/path/to/cruise control/project/project name/ “ cruisecontrol latest

Create separate folders for log doc etc under build folder

Now your framework’s folder structure looks like below

+ cruisecontrol

+ projects

+ <Your project name>

+ source

+ build

+ logs

+ doc

+ coverage

Now your phpundercontrol Framework is ready. Now you can create a build file for your application under project name folder and edit config file for phpundercontrol under cruise control folder. Now the folder structure like

+ cruisecontrol

config.xml

+ projects

+ Your project name

build.xml

+ source

+ build

+ logs

+ doc

+ coverage

Create Build.xml

This section describes how to configure your project using “ant” build method

At first the file should contain the following lines.

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”project name” basedir=”/path/to/cruisecontrol/projects/project name/”>

</project>

Working Copy Target

We need a working copy of our application on which the build process is executed . Its much preferred to create a symbolic link to the actual path of your application if both application and phpUnderControl are running on same server. Otherwise go for svn checkout option, For that create a new target method under <project>. The parameter for svn update is

<target name=”checkout”>

<exec executable=”svn” dir=”${basedir}/source”>

<arg line=”up” />

</exec>

</target>

* Please note that we need to do an initial checkout before starting an svn update.


Unit Test Target

Here we use lime test for running the tests rather than phpunit because symfony framework does not support phpunit. Unit test command are as follows

<target name=”unitTest” >

<exec dir=”${project.dir}” executable=”php” failonerror=”true”>

<arg line=”symfony sg-test4cruise ${test.app.name}” />

</exec>

</target>

Before that mention {project.dir} and {test.app.name}. Here {project.dir} is ${basedir}/source and {test.app.name} is depends on your application.

PHPDocumentor Target

We have to add new target Phpdocumentor for generate documentation of our php files on each build. The commands are as follows

<target name=”phpDocumentor”>

<exec executable=”phpdoc” dir=”${application.dir}”>

<arg line=”-i om/,map/ -d src/ -t ${log.dir}/doc” />

</exec>

</target>

Where

-i –ignore : file(s) that will be ignored, multiple separated by ‘,’. Wildcards * and ? are ok

-d –directory : name of a directory(s) to parse directory1,directory2

-t –target : path where to save the generated files

{log.dir} : ${basedir}/build/

{application.dir}: Depends on your application

CodeSniffer Target

PHP_CodeSniffer is a PHP5 script that tokenises and “sniffs” PHP and JavaScript code to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers. The build commands for this

<target name=”phpCodeSniffer”>

<exec executable=”phpcs” dir = “$${project.dir}” output= “${log.dir}/log/ CodesnifferResult.xml”/> <arg line=”–report=checkstyle –standard=Symfony –ignore= what are the files you want to exclude from code sniffs activity”/>

</exec>

</target>

Deleting the Existing Log files

If you want delete all log files before run the build then you need to create a new target ‘init’for that

<target name=”init”>

<delete>

<fileset dir=”${log.dir}/” includes=”**/*.*”/>

</delete>

</target>

Finishing the ant build file

Finally we have to combine the four independent targets into a single target. So create we create a new target ‘all’ that depends on the other four. And the commands

<target name=”all” depends=”init, phpCodeSniffer, phpDocumentor, unitTest” />

</project>

The final build.xml file look like this

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”project name” basedir=”/path/to/cruisecontrol/projects/project name/”>

<property name=”base.dir” value=”source”/>

<property name=”log.dir” value=”build” />

<property name=”application.dir” value=”${base.dir}/to your application working patht”/>

<property name=”test.app.name” value=”Applicatin name” />

<property name=”project.name” value=”projectname”/>

<target name=”all” depends=”init, phpCodeSniffer, phpDocumentor, unitTest” />

<target name=”init”>

<delete>

<fileset dir=”${log.dir}/” includes=”**/*.*”/>

</delete>

</target>

<target name=”unitTest” >

<exec dir=”${project.dir}” executable=”php” failonerror=”true”>

<arg line=”symfony sg-test4cruise ${test.app.name}” />

</exec>

</target>

<target name=”phpDocumentor”>

<exec executable=”phpdoc” dir=”${application.dir}”>

<arg line=”-i om/,map/ -d src/ -t ${log.dir}/doc” />

</exec>

</target>

<target name=”phpCodeSniffer”>

<exec executable=”phpcs” dir = “$${project.dir}” output= “${log.dir}/log/ CodesnifferResult.xml”/> <arg line=”–report=checkstyle –standard=Symfony –ignore= what are the files you want to exclude from code sniffs activity”/>

</exec>

</target>

</project>

Cruisecontrol Config File

CruiseControl have one configuration file config.xml that holds all projects. This file can be found the CruiseControl main directory. The main steps are

a) First of all we need to add project element for our projet.

<project name=”phpundercontrol.org” buildafterfailed=”false”> the buildafterfailed can be used to force CruiseControl that it retries to build a project, after a failed build without changes to the project sources. This option can be useful if the build process is based on an external resource, like a database server that could timeout during the build process.

b) Including required plugins

if we use svn checkout for getting the application then we must load corresponding plugin to the config.xml file

<plugin name=”svn”

classname=”net.sourceforge.cruisecontrol.sourcecontrols.SVN” />

c) Modificationset

It describe when will a new build happen, We can configure it in two methods first one by defining time intervells and second one is when there was a source.

<modificationset quietperiod=”60″>

<svn localWorkingCopy=”projects/${project.name}/source/”/>

</modificationset>

d) Schedule

schedule element defines the number seconds to wait between two builds. The default schedule interval is 300 seconds(five minutes).

<schedule interval=”6000″>

<ant anthome=”apache-ant-1.7.0″ buildfile=”projects/${project.name}/build.xml”/>

</schedule>

e) Listeners

<listeners>

<currentbuildstatuslistener file=”logs/${project.name}/status.txt”/>

</listeners>

f) Bootstrappers

<bootstrappers>

<antbootstrapper anthome=”apache-ant-1.7.0″ buildfile=”projects/${project.name}/build.xml” />

</bootstrappers>

g) Logging

Here we merge the logfiles

<log>

<merge dir=”projects/${project.name}/build/”/>

</log>

h) Publishers

By using this we publish our results and send build status to the corresponding team members

<execute command=”phpuc graph logs/${project.name} artifacts/${project.name}” />

<artifactspublisher dir=”projects/${project.name}/build/doc” dest=”artifacts/${project.name}” subdirectory=”doc”/>

<artifactspublisher dir=”projects/${project.name}/build/coverage-report” dest=”artifacts/${project.name}” subdirectory=”coverage”/>

<email mailhost=”host”

returnaddress=”Daily-build@qburst.com”

buildresultsurl=”${build.url}/buildresults/${project.name}”

skipusers=”false”>

<!–Add the SVN commit users ID and e-mail address–>

<map alias=”PL” address=”pl@urcomapany.com”/>

<always address=”me@urcomapany .com” />

</email>

</publishers>

The final config file look like

<cruisecontrol>

<plugin name=”svn” classname=”net.sourceforge.cruisecontrol.sourcecontrols.SVN”/>

<project name=”project name”>

<listeners>

<currentbuildstatuslistener file=”logs/${project.name}/status.txt”/>

</listeners>

<bootstrappers>

<antbootstrapper anthome=”apache-ant-1.7.0″ buildfile=”projects/${project.name}/build.xml” />

</bootstrappers>

<modificationset quietperiod=”10″ requiremodification=”false”>

<svn filesystem folder=”projects/${project.name}”/>

</modificationset>

<schedule interval=”6000″>

<ant anthome=”apache-ant-1.7.0″ buildfile=”projects/${project.name}/build.xml”/>

</schedule>

<log>

<merge dir=”projects/${project.name}/build/log”/>

<merge dir=”projects/${project.name}/source/log”/>

</log>

<publishers>

<execute command=”phpuc graph logs/${project.name} artifacts/${project.name}” />

<artifactspublisher dir=”projects/${project.name}/build/doc” dest=”artifacts/${project.name}” subdirectory=”doc”/>

<artifactspublisher dir=”projects/${project.name}/build/coverage-report” dest=”artifacts/${project.name}” subdirectory=”coverage”/>

<email mailhost=”host”

returnaddress=”Daily-build@qburst.com”

buildresultsurl=”${build.url}/buildresults/${project.name}”

skipusers=”false”>

<!–Add the SVN commit users ID and e-mail address–>

<map alias=”PL” address=”pl@urcomapany.com”/>

<always address=”me@urcomapany .com” />

</email>

</publishers>

</project>

</cruisecontrol>

Notes :

1) By defult we can see the result using URL http://localhost:8080. But we can config this URL by adding a extra property in the config file <property name=”build.url” value=”http://your URL:5001″ />

If we enter different port other than 8080, then you should also change the jmxport -webport -rmiport in cruisecontrol.sh/cruisecontrol.bat file

2) You can make sure that file names coverage and doc are same in the main.jsp file

3) You can change the styles of the phpundercontrol page by editing the jsp files in the cruisecontrol/webapps/cruisecontrol/

Reference

http://www.phpundercontrol.org

Symfony plugins for Cruisecontrol

Advertisements

Posted in PHP, Software Testing, Uncategorized | Tagged: , , , , | 19 Comments »