Tuesday, July 29, 2008

PhpUnderControl for Symfony

PhpUnderControl Framework For Symfony Projects


Introduction

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

* PHPUnit
o 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
o For static code analysis
* PHPDocumentor
o 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

+

+ 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.


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 . The parameter for svn update is





* 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



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




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



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







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



The final build.xml file look like this































































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.

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




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.








d) Schedule

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








e) Listeners








f) Bootstrappers








g) Logging

Here we merge the logfiles








h) Publishers

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





















The final config file look like






































































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

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


1 comment:

Kg said...

Thanks man i have been searching for this. Thanks and Good work!!