Archive for the ‘Environment’ Category

h1

Zend_PHPUnit_Fixtures uploaded to GitHub

December 4, 2008

Well I’ve been working on this project (Zend PHPUnit Fixtures) along with a few collegues over the past few months, It was my intension to submit it to Zend some point in the new year but thought I’d put it out in the wild for others to pick at.

Well I use TDD (PHPUnit is my friend) on a daily basis within ZendFramework and soon found it cumbersome to create test data (fixtures) ending up in huge test cases with more test data setup than actual assertions, this matched up with the fact that I hate integrating DB into my tests (mainly because the tables would still contain old test data & fail depending on whether the data was added in previous tests or not) So I decided to do some digging, the best solution seemed to be to create a fixture handling system that plugs into ZendFramework, leaving myself and others with more time actually testing and less time building up test data and removing fixtures from a database (which can be near impossible when it comes to automated testing).

With this realisation I decided to create my own which I’ve reversed engineered from CakePHP. When I have the time (hopefully over the holidays) I’ll go into how the mini framework can be used. For now I’ll just describe the features or each class and what they are for:-

PHPUnit_Fixtures

Basic fixture handler, used for creating test data that does not interact with a DB. With this object we are able to create basic fixtures that we can use for dummy data with our test cases. Each piece of test data can have an alias (‘ALIAS’) with the aliases name as the value, doing so will allow us to use the PHPUnit_Fixtures::find($aliasName) which will retrieve the desired fixture.

PHPUnit_Fixtures_DB

Has the same functionality as PHPUnit_Fixtures but used specifically for DB centric tests, DB test data will be added to our ‘_test’ DB, and cleaned up (truncated) on each test case, to make sure that we have the expected data.

PHPUnit_Fixtures_DynamicDB

Has the same functionality as PHPUnit_Fixture_DB, with the added functionality of being able to create tables setup my MySQL Workbench. With an child object of this class we are able to specify retrieving all schema or a specific on (denoted by the schema table name).

DevelopmentHandler

Used to handle our development environments, there are times when we want to quickly place test data on our staging DB for functionality testing and the such like, this class along with one of our PHPUnit_Fixtures, will easily allow us to populate this environment with the data we have been using for our unit tests, making it quicker to migrate test data from one place to another.

You can find the project at GitHub, If anyones interested in adding to this project or have any comments/questions drop me a line.

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

h1

Getting ZendDebugger to work on Centos5.1

October 21, 2008

Well I had to setup a Zend Studio for Eclipse on Centos today, me thinking that it would be a blissful journey, abruptly followed by the realization that Centos wasn’t going to play nice. First thing after a clean install I ran yum update to make sure the system was up to date.

I tried to install ZendFramework 1.7, ZendStudio6.1 & PHPUnit3 and installed as I have before but came against a brick wall, PHPUnit would not install, due to PHP-XML being missing.
yum install php-xml
Which installs php-xml5.1.6, okay, I know I need 5.2 at least but for now I want to make sure PHPUnit will install.
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit

Once PHPUnit was installed I ran the test cases, ZS complains with the follow:
Error launching 'AllTests.php
The session counld not be startd.
In order to generate debug inormation, please make sure that the debugger is properly configured as a php.ini directive

I run phpunit from shell to get:
Failed loading /usr/lib/php/modules/ZendDebugger.so: /usr/lib/php/modules/ZendDebugger.so: undefined symbol: zend_memory_usage

Okay so ZendDebugger isn’t working, php -v shows me:
PHP 5.1.6 (cli) (built: Sep 20 2007 10:16:10)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies

So I need a new version of PHP, PHP5.2.6 will do, yum update php tells me that 5.1.6 is that latest version, so I need to find a repository that has one.

cd /etc/yum.repos.d
wget http://dev.centos.org/centos/5/CentOS-Testing.repo
vim
CentOS-Testing.repo

Edit the line below.
enabled=0

to
enabled=1

your file should now look like this:
[c5-testing]
name=CentOS-5 Testing
baseurl=http://dev.centos.org/centos/$releasever/testing/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing
# CentOS-Testing:
# !!!! CAUTION !!!!
# This repository is a proving grounds for packages on their way to CentOSPlus and CentOS Extras.
# They may or may not replace core CentOS packages, and are not guaranteed to function properly.
# These packages build and install, but are waiting for feedback from testers as to
# functionality and stability. Packages in this repository will come and go during the
# development period, so it should not be left enabled or used on production systems without due
# consideration.

Now run the following

yum install php

You should get something simular to this….
=============================================================================
Package Arch Version Repository Size
=============================================================================
Updating:
php i386 5.2.6-2.el5s2 c5-testing 1.2 M
php-cli i386 5.2.6-2.el5s2 c5-testing 2.4 M
php-common i386 5.2.6-2.el5s2 c5-testing 230 k
Updating for dependencies:
php-ldap i386 5.2.6-2.el5s2 c5-testing 31 k
php-xml i386 5.2.6-2.el5s2 c5-testing 97 k
Transaction Summary

Now I ran the test and got the following error.
Failed loading /usr/lib/php/modules/ZendDebugger.so: /usr/lib/php/modules/ZendDebugger.so: cannot restore segment prot after reloc: Permission denied

Okay, that was totally unexpected, so after a little searching with my best friend (google) I found the following command
/usr/sbin/setenforce 0
Ran the tests again….
phpunit AllTests.php
PHPUnit 3.3.2 by Sebastian Bergmann.
.......

Time: 0 seconds

OK (7 tests, 0 assertions)

Perfect & running within ZS gives me the below results, job done.

One thing to make note of; when I restarted the box, SELinux had been reactivated so I disabled SELinux by doing the following (this is not ideal, i’ll have to look allowing doing this a better way).

vim  /etc/sysconfig/selinux
and changing
SELINUXTYPE=enforcing
to
SELINUXTYPE=disabled
Doing this disabled SELinux so that we I wouldn’t get the permissions error. Alternatively I could of placed the setenforce command in a startup script (bashrc or something simular).

h1

Installing ZendStudio 6.1

October 20, 2008

Okay, I’ve done this so many times & helped a few people do the same that I thought It would be a good idea to actually document the process. I’ll explain the basic steps to getting ZendStudio working with PHP5.2.6 & PHPUnit3, this setup will allow you to use the same library files as the actual system instead of the environment prepacked with ZS6.1 (I prefer this way as running tests in ZS6.1 should produce the same results when run via CLI.

Needed:
PHP5.2.6
ZF latest version
ZendStudio for Eclipse 6.1
PEAR

Make sure that php is atleast at version 5.2 (php -v will tell you the version), update if needed. Download Zend Framework and place in your php include path (typically /usr/share/php or /usr/share/pear).

If pear is not installed we’ll need install it using the following for Debian based systems (remember you’ll need to be root to install via apt or yum).
apt-get install php-pear
and
yum install php-pear
for RHEL based systems.

cd /opt &&
wget http://framework.zend.com/releases/ZendFramework-1.7.0PR/ZendFramework-1.7.0PR.zip &&
unzip ZendFramework-1.7.0PR.zip &&
cd ZendFramework-1.7.0PR/library/ &&
mv Zend INCLUDE_PATH

Now we have ZF within our include path we need to setup PHPUnit & ZendStudio.
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit

We need to download ZendStudio from here & running the file
./ZendStudioForEclipse-6_1_0.bin Follow the instructions & wait for the installation to complete.

Now we’ll need to customise ZS so that it will work with using the systems PHP & PHPUnit instead of the ones packed with ZS.

We’ll need to setup ZendDebugger to work with our systems php.ini file, so we will need to copy ZendDebugger from ZS and place in with the other modules.
cp /PATH/TO/ZEND/plugins/org.zend.php.debug.debugger.linux.x86_5.2.14.v20080602/resources/php5/ZendDebugger.so /usr/lib/php5/

Here we need to add the below text into our php.ini file, ideally at the bottom, so it is easy to find.
[Zend]
zend_extension=/usr/lib/php5/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1
zend_debugger.expose_remotely=always
zend_debugger.connector_port=10000

Once installed and started, select Window, Preferences & you’ll be given a window simular to below.

Click Add, which will bring up the dialog box below.

Enter the following, the path to the systems php executable & your php.ini file as entered in the above screenshot and click Finish.

The dialog box will close and leave you with Figure 1.1, select the newly added settings and click Default. We have now setup ZS to work with our systems PHP version instead of the one packed with ZS.

We now need to add a our PHP include path to ZS so that we have all our library files available for ZS.

Click New, which will display the below dialog box, this is where we need to set our current PHP path, the screenshot below displays an example.

Once the include path has been added we click OK and OK again, now when ever we create a new project, we will just remove the predefined paths and just include our one.

Well that’s it, now we should be able to run tests within ZendStudio & the shell, knowing that our results come from the same environment..