Testing Zend Framework controllers in isolation

What I do is I have my controllers fetch all their dependencies from the bootstrap and/or front controller. The most common example is to pull the db resource from the bootstrap:

// in controller
$db = $this->getInvokeArg('bootstrap')->getResource('db');

But I also take it a step further. For example, if I’m using data mappers, I have the action controller check the front controller for the data mapper I need:

// in controller
$postsMapper = $this->getInvokeArg('posts_mapper');

I then update your unit test to inject the posts mapper with a stub:

// in unit test
$this->frontController->setParam('posts_mapper', $stubPostsMapper);

However, that invoke arg won’t exist in production, so I wrap that call in an if statement a la “lazy loading” style:

if (null === ($postsMapper = $this->getInvokeArg('posts_mapper'))) {
$postsMapper = new Default_Model_Mapper_Posts();
}

What this does is it allows me to stub in my stub posts mapper in my unit tests while letting the controller lazy-load the real one in production.

An alternative is to use Zend_Registry, but I find this to be a bit cleaner without the static calls.


Hector Virgen

Acceptance Test-Driven Development

Giorgio Sironi writes an interesting TDD story; I am halfway through reading Growing object-oriented software, guided by tests, a book that teaches Test-Driven Development in a Java environment. A review will come soon, since the process described in this work is really language-agnostic and interesting also for php developers.

However, the book’s authors introduce a very productive practice, which consists in a double cycle of TDD:

* a longer cycle, where you write acceptance (aka end-to-end) tests, deriving them from the user stories or formal requirements, and make them pass;

* a shorter cycle contained in the first, which happens in the phase when an acceptance test is red: you write unit tests and make them pass until the related acceptance test does not fail anymore.

Read complete story @ PHP Zone.

Talk: PHP Best Practices – Matthew Weier O’Phinney and Lorna Jane Mitchell

Writing maintainable code is an art that takes effort and practice to master.
Part of that art is learning what tools and strategies will assist you in that effort. In this tutorial, we will cover a variety of practices and tools that can make your life, and the lives of your team members, easier as you develop your applications. Among them, we will provide overviews of:

  • Version Control
  • Coding Standards
  • Unit Testing basics
  • QA tools: phpcs, phploc, phpmd, continuous integration, and more
  • Team Collaboration tools, such as Skype, IRC, issue trackers, and more

via Talk: PHP Best Practices – Joind.in.

PHP Continuous Integration with Atlassian Bamboo

Mike Willbanks writes; Continuous integration is all the rage these days; you are unit testing your code are you not? During some consulting in January with the help of Sebastian Bergmann, from thePHP.cc, we setup continuous integration utilizing Atlassian Bamboo and received training on PHPUnit.

Using Atlassian Bamboo for continuous integration will take you a bit to setup, however, I have found it to be an invaluable tool when utilizing the Atlassian stack (JIRA, Confluence, Crucible, Bamboo and Crowd).

Overview

This posting assumes the following:

  • You have Atlassian Bamboo setup
  • Ant is available on the system
  • You already have PHPUnit setup for your project
  • You have selected a coding standard

This posting will go over the following:

  • Getting Started
  • PHP Depend
  • PHP Code Browser
  • PHP Code Sniffer
  • PHP Copy/Paste Detector
  • PHP Mess Detector
  • PHPUnit

Read the rest over at Mike Willbanks blog.

Getting Started with Zend_Test

Matthew Turland has written a very nice article on Unit Testing using Zend_Test, Zend_Test_PHPUnit_DatabaseTestCase, Zend_Test_PHPUnit_ControllerTestCase where he uses a few interesting solutions. Definitely worth a read.

“I worked on a project recently where we used Zend Framework. As part of that project, I was tasked with writing unit tests. So, I went to the “tests” directory generated for me by the zf CLI utility to get started…”

via Getting Started with Zend_Test | Blue Parabola, LLC.

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 🙂