Unit testing Zend Framework Models with PHPUnit

phpunit-logo

A new project created by the ZF command line tool (zf) will have a default IndexControllerTest in the tests folder. The test class inherits from Zend_Test_PHPUnit_ControllerTestCaseand provides a good example of how to test a controller.

Unfortunately the tests folder doesn’t come with any example of how to test a model. I’m guessing the reason for that is ZF doesn’t need to provide any code (class to extend) to help with that. In fact PHPUnit_Framework_TestCase is enough to perform a model unit testing.

The only problem one might come across is how to setup a test class to have at minimum autoloader at place. It can be achieved by initialising Zend_Application in the setUp() method.

Obviously you can bootstrap more than just the autoloader but that depends on your particular needs. For example I usually have to bootstrap: config, defaultModuleAutoloader, autoloader and propel.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">