Continuous Integration for PHP – phpUnderControl & CruiseControl

Did you know that you can automate unit tests (which is the PHP worlds equalient of compilation checks 🙂 ).

Set up your development team using local checkouts of the project and have them do local PHPUnit tests, check their changes in and then get Continuous Integration checks done on a central server using phpUnderControl that emails the team with success/fail reports, it’s a good way to work.

phpUnderControl is an addon application for the continuous integration tool CruiseControl, which integrates some of the best PHP development tools. This project aims to make your first steps with CruiseControl and PHP as easy as possible. Therefore phpUnderControl comes with a command line tool that performs all modifications to an existing CruiseControl installation.

Integrated tools

  • Testing and software metrics – PHPUnit is the most popular xUnit implementation for PHP that provides a framework for automated software tests. Except the pure test automation PHPUnit contains a rich set of features like Code Coverage, Project Mess Detection and Software Metrics. To visualize the generated XML reports phpUnderControl comes with a set of XSL stylesheets that prepare the output for CruiseControl.
  • Documentation – phpUnderControl uses the most common documentation tool for PHP projects, PhpDocumentor, to generate an up to date documentation of the software on every build cycle. Therefore the developers will always get the latest API documentation of their project. Additionally phpUnderControl extracts the documentation violations found by the PhpDocumentor and visualizes these as an additional quality report in the user interface and the project time line of documentation violations.
  • Coding Standards – With the package PHP_CodeSniffer the PEAR project gave PHP developers a very useful tool to detect coding standard violations in a project. Since version 1.0.0RC3 it has native support for the Checkstyle XML format that can be visualized by CruiseControl. PHP_CodeSniffer comes with a variety of pre defined coding standards like PEAR and ZEND but due to its modular structure you can easily implement a custom rule set or extend one of the pre defined sets. This development tool assures that the whole project code will remain clean and consistent.

Go and check it out today 🙂

Bootstrap Zend_Translate

This entry is part [part not set] of 4 in the series Working with Zend_Translate and Poedit

A recurring problem for site developers is implementing a solid way to create and maintain multilingual sites, this article series is my feeble attempt to guide you through how to quickly implement the Zend_Translate in an Zend Framework 1.9.x site.

The procedures and best practices for this is unfortunately like training a dog, everyone has a different way of doing it and an opinion, so the methods and code I show here are taken out of applications that are running in production so if you have a better way of doing it please feel free to comment!.

I usually use poedit a gettext editor which is available for most platforms to create my translation files, and after some initial configuration of the catalog paths so it can see your source files please see Part 1 of this article series.

The bootstrap below looks for the language specific gettext .mo files in /application/languages/ for example /application/languages/sv_SE.mo

Bootstrap.php

protected function _initTranslate() {
// We use the Swedish locale as an example
$locale = new Zend_Locale('sv_SE');
Zend_Registry::set('Zend_Locale', $locale);

// Create Session block and save the locale
$session = new Zend_Session_Namespace('session');
$langLocale = isset($session->lang) ? $session->lang : $locale;

// Set up and load the translations (all of them!)
$translate = new Zend_Translate('gettext', APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', $langLocale,
array('disableNotices' => true));

//$translate->setLocale($langLocale); // Use this if you only want to load the translation matching current locale, experiment.

// Save it for later
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}

Now when you use statements like

translate('Contact Admin'); ?>
in your layout.phtml or view.phtml files it will be picked up by poedit and you will be presented with a string “Contact Admin” to translate, in my case i’ll just enter “Kontakta Administratören”.

There is some debate on what to put in the translate strings as identifiers, I personaly prefer the actual term to translate in a base language, in this case English instead of some convoluted “IDS0001” type strings.

Poedit will keep track of changes, i.e if I would change the “Contact Admin” to “Contact us” it will tell you on synchronization that “Contact Admin” disappeared and a new translation is required for “Contact us”. It’s quite easy to send those strings to your translators.

Thats it for today.

Generate Doctrine models/classes that extend a custom record class

When using using Doctrine 1.2.1 and Zend Framework 1.9.x to generate classes from Yaml/db each Base class (which includes the table definition) extends the Doctrine_Record class.

I need to find a way of telling Doctrine to extend my own Base class, or find a different solution to a master/slave db setup using Doctrine.

Example generated model:

abstract class My_Base_User extends Doctrine_Record
{

However I need it to be automatically generated as:

abstract class My_Base_User extends My_Record
{

And the answer is (drumroll)

Typical, as soon as I ask the question I manage to find the answer. I’m recording it here in case anyone else has the same issue.

You can pass in the parameter ‘baseClassName’ into the generateModels* methods and Doctrine will use that as the Base record class.

Examples:

Doctrine_Core::generateModelsFromDb('models', array('master'), array('generateTableClasses' => true, 'baseClassName' => 'My_Record'));

or using Cli:

$options['generate_models_options'] = array(
'pearStyle'             => true,
'baseClassPrefix'       => 'My_',
'baseClassName'         => 'My_Record',
'classPrefix'           => '',
'classPrefixFiles'      => false,
'generateTableClasses'  => true,
);
$cli = new Doctrine_Cli($options);

Object-relational mapping with Doctrine, Flash Builder, and PHP

Richard Bates @ Zend Developer Zone wrote a good article on my favorite ORM Doctrine integration in Zend;

Rich Internet applications built with Adobe Flex and Flash Builder have been steadily gaining a foothold in enterprise development for quite some time. As the platform has grown and evolved, PHP has also made amazing progress toward becoming a mature, powerful object-oriented language with rapid adoption and dozens of frameworks and design pattern implementations. As PHP continues to prosper, developers are able to borrow more and more of the things Java has got right, taking one check after another from the “Java-only” column. One outstanding example of this is object-relational mapping (ORM). A few different PHP ORM implementations are available, and all of them have positive attributes. However, after some experimentation, I’ve found that Doctrine is my favorite.

Read the complete story;

Object-relational mapping with Doctrine, Flash Builder, and PHP.

Automate Db Model Creation with Zend_CodeGenerator_Php_Class

Joey Rivera wrote a very interesting and time saving article about the Zend_CodeGenerator complete with a easy to use code snippet that will create the model and DbTable files and classes for you, as it picks the information directly from your database, it’s no way those pesky typos and cut and paste mistakes creep in when you have to do 10 (or 300) almost identical files.

Worth checking out; Joey’s Blog

Console ProgressBar and Text

The query was; How to set the Text Label on the progressbar in console mode.
The answer by Michael “Ray” Rehbein: You want to set the ‘elements’ option of the adapter.

http://framework.zend.com/manual/en/zend.progressbar.html#zend.progressbar.adapter.console


$pbAdapter = new Zend_ProgressBar_Adapter_Console(
array('elements'=>
array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT,
Zend_ProgressBar_Adapter_Console::ELEMENT_BAR,
Zend_ProgressBar_Adapter_Console::ELEMENT_ETA,
Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)
)
);

$progressBar = new Zend_ProgressBar($pbAdapter, 0, 10);

for ($i = 0; $i < 10; $i++) {
sleep(1);
$progressBar->update($i, "Iteration: {$i}"); } $progressBar->finish();
}

Quite nifty.

ZamfBrowser 1.0

When doing AMF projects especially in AIR it’s good to see what gets returned from your Zend_AMF services, here is the solution; ZamfBrowser allows developers to unit test ZendAMF Service classes via an Adobe AIR application. Implementation requires a simple edit to the ZendAMF gateway file that allows ZamfBrowser to introspect your server set up and provide access for all classes and methods registered with the Zend_Amf_Server object. For version 1.0 the ZamfBrowser AIR source code is still closed, but it is intended to go Open Source as soon as the source code documentation is complete and a couple of features are implemented that are still in the pipeline.

Go and get it: http://www.zamfbrowser.org/