Ideas for testing Zend Framework apps

Tags:

Here are some ideas I was thinking of for making testing Zend Framework (and Doctrine) based apps nice and easy.

PHP testing

Naturally, you’d first off use PHPUnit and Matthew’s work on Zend Framework request testing to write unit tests for your models, controllers and library code.

Testing library code should be relatively simple, since most of it probably works more like “traditional” code: It does not output html, does not access the DB and things like that.

Testing models and controllers is a more complex thing. Models quite likely will access the DB, so how to make testing them simpler?

First, you could use the in-memory SQLite adapter with PDO (new PDO(‘sqlite::memory:’)) to create a database that won’t mess with anything external. Then, if you’re using a library like Doctrine, you can let it generate the database for you based on the models. You would also want to write some fixtures with some dummy data that you can use in your tests.

Since Doctrine’s models should work most of the time, you may not want to spend time writing tests for them, but if you provide any custom methods in your models, you might want to make sure they do what they are supposed to.

Controller testing can be the most difficult part, and this is where you may need to perform some refactoring if your design is not suitable for testing. For example, you may have a lot of logic in your controllers, which might be better off in your models or library code. Testing the whole idea of getting out the correct output or redirects may be difficult, but the Zend Framework test case stuff Matthew Weier O’Phinney has been working on should help a lot there.

Other testing

In addition to testing the stuff that happens on the server, you may want to test what’s actually going on on the client side of things.

For this, there’s JsUnit, which is essentially similar as PHPUnit is for PHP but just for JavaScript. Also, there’s things like Selenium, which can automatically test a site in a browser to make sure they work correctly. I haven’t tried either of these though.