Rojakcoder

Of Linux, Programming, and Singaporean Ramblings

Zend Framework Testing with PHPUnit - Installation

This post describes how to set up automated testing with Zend Test and PHPUnit. The latter is a unit testing package for PHP applications done in PHP. I had searched for posts on how to do this but there weren’t any easy-to-understand or straightforward examples to follow, so I decided to write one myself. This post will thus be written in a way that newbies are able to get a test working straight away by following the examples in this article. After reviewing the length of the post, I decided to break this article into several smaller ones that are much easier to digest. It’ll be easier for you to get to specific information with smaller articles rather than a large one as well.

I hope this post will help some newbie down the road. Bear in mind that I’m no expert on this - only enough to get it working (and maybe to screw up, which I hope doesn’t happen.) So if I make any mistakes, please point them out and I will make amendments where necessary. If you have something to ask or simply want to share, please comment at the end of the post. Hope you find the following useful!

I’ll be using Zend Test to create automated tests for an application created on the Zend Framework. I assume that you already know how to create a working application. (Although the sample is available for download at the end of the article.) This article will introduce the installation part of the testing framework. Subsequent articles will illustrate with an actual application along with the sample code for download.

At the heart of Zend Test is basically PHPUnit - the open source unit testing application. However, this package does not come with the Zend Framework (ZF) download.

The first step then is to get PHPUnit on your computer (where your ZF application resides). The general way to do this is to use PEAR (PHP Extension and Application Repository). There is another way though - simple copy and paste. I’ll explain both ways below.

Method 1 - Install Through PEAR

PEAR is a sort of package installer for PHP. How you install it on your system depends on what operating system you are using. Unfortunately, this is the one part that I cannot guide you along as I don’t know what your platform is. Your best bet is the official documentation. What I’ll do here though, is explain how to do it on openSUSE 10.3.

Installing PEAR on openSUSE

Start YaST > Software > Software Management.

Search for pear. The results should show a package with the name php5-pear. Select that one and continue installation.

Voila! PEAR is now installed on your system.

To test if PEAR is really installed, open up a terminal/console/command line window, and type pear. A list of options will be displayed on screen. If instead you get a ‘command not found’ error, you probably did something wrong. Revisit your steps and the documentation to solve it.

Installing PHPUnit

After you’ve gotten PEAR to install, getting PHPUnit is a breeze. You can read the official documentation with all the explanation here, but if you just want to install, below are the commands:

pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit

To test if PHPUnit is installed correctly, open up a terminal/console/command line window and type phpunit. Similarly, if you get a list of options, you’ve installed it correctly. If not, you will most likely have gotten a ‘command not found’ error. Make sure you did the preceding steps correctly.

Method 2 - Copy and Paste PHPUnit

If you don’t like to muck around with your system settings much (like me), you will love this method. Personally I find it much more straightforward and cleaner compared to using PEAR.

The first step is to download a copy of PHPUnit. You can get it from http://pear.phpunit.de/get/. As of this writing, the latest version is 3.3.9. (Windows users, you need to get a program to extract the archive file. You can try 7-zip.)

Once you’ve extracted the contents of the archive file, you should see two files and a directory. One is the signature file (package.sig) and the other is a XML file (package.xml). What we are interested in is the directory (in my case it is PHPUnit-3.3.9). Go into this directory and you will see another two files and a directory. The files have to do with PEAR so we can ignore them. What you want is the directory (named PHPUnit). Copy this entire directory into the /library directory and you are done!

Testing the Framework

So how do you know if you’ve got the test unit working? Test it of course! (pun intended)

A typical ZF application will have the following directory structure at minimum, with the ZF library files residing below the root library directory.

- application/
- library/
|- Zend/
- public/

If you followed method 2 above, you will have

- application/
- library/
|- PHPUnit/
|- Zend/
- public/

Create another root directory named tests. Within this directory create a file named AllTests.php with the following code:

define('APPLICATION_PATH', realpath(dirname(dirname(__FILE__))));
set_include_path(APPLICATION_PATH . '/library' . PATH_SEPARATOR . get_include_path();
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
class AllTests {
        public static function run()
            {
                        PHPUnit_TextUI_TestRunner::run(new PHPUnit_Framework_TestSuite()
                                    , array());
                            }
}
AllTests::run();

So eventually your directory structure looks like this:

- application/
- library/
|- PHPUnit/ (only if you followed method 2)
|- Zend/
- public/
- tests/
|- AllTests.php

Then open up a command line console and go to this directory. Run the command:

php -f AllTests.php

If all goes well, you will see the output PHPUnit 3.3.9 by Sebastian Bergmann. and some stuff about 0 tests and 0 assertions. Woohoo!

comments powered by Disqus

Related contents

zend (3)
php (4)