Test Driven Development with Zend Framework and PHPUnit

  • Sharebar

Over the past few days I was going through the Zend Framework reference docs and I found myself pleasantly surprised with all that the latest version of this web application framework provides. My first thought was to just acknowledge the speed in which PHP as a technology has been maturing. Out of the many new features, what stood out for me was the ease with which Zend Framework and PHPUnit complement and work with each other.

Now I've worked with PHPUnit enough to know how powerful, customizable and easy to use it can be (primarily as it belongs to xUnit family of testing frameworks) but what is really heartening is to see it perfectly complement the Zend Framework (which IMHO should be the default choice for any PHP project, but that's another blog post..).

Here's what we know about PHPUnit: you can not only unit test your PHP classes but let's you create dependencies between tests, use dataProviders for expanding the sample size for test case arguments, test for exceptions and errors.

Basics aside PHPUnit also lets you organize your tests, provides for database testing, test doubles, provides for code coverage, and can even be used for agile documentation. To top it off, PHPUnit plays nicely with Selenium, Apache Ant, Apache Maven, Phing, Atlassian Bamboo, CruiseControl etc. so it should fit into a good percentage of project development environments.

I'll do myself a favor and point to the PHPUnit Documentation and get back to what this post was about.

Zend Framework App Folder StructureAs clearly depicted in the Zend Framework application project folder structure, test driven development had always been given a high priority by the authors of the framework. In fact much of the official docs and other reference material / literature stress quite strongly for the adoption of these practices and generally claim it to be the the "Zend way of PHP development".

Coming to specifics, in the Zend_Test package we find PHPUnit extended to give us Zend_Test_PHPUnit and Zend_Test_PHPUnit_Db.

Zend_Test_PHPUnit provides classes that can beĀ used to test the MVC classes of the framework so a typical situation would be that the test for a controller will extend Zend_Test_PHPUnit_ControllerTestCase. Find below an example of a controller test (courtesy the official docs):

Zend_Test_PHPUnit_Db extends the PHPUnit database extension with Zend Framework specific code such that writing database tests becomes much simpler as persistence gets pre-handled. Find below an example of a database test (courtesy the official docs):

---
Once your web application is closer to a release, Zend Framework + PHPUnit's support for functional testing on Selenium IDE comes handy as well. These are tests that broadly mimic end user behavior and are specific to testing areas such as usability, security and performance.

A typical Selenium test would extend PHPUnit_Extensions_SeleniumTestCase and would look like this:

With the plethora of features available, this blog post has hardly scratched the surface. Perhaps the purpose here is to showcase that these complimentary frameworks do indeed exist and they work seamlessly with their environments and that it is for us the PHP user group to leverage these to practice and implement TDD, refine our competence and try raise the quality benchmarks just a little bit.

2 Responses to “Test Driven Development with Zend Framework and PHPUnit”

  1. THANX for ur example
    I want to set up phpunit for my application
    which is custom PHP framework. How can i integrate both of them?
    So that i can test functionlaities like user registeration and group creation from my controllers

  2. [...] Zend Framework and PHPUnit [...]

Leave a Reply

You must be logged in to post a comment.