Hello, I've just subscribed to the group, and am fairly new to PHP and
ZF (although I have a long history in software development on various
platforms).
I'm in the middle of setting myself up to do TDD on a ZF-based app.
I've got Model testing in pretty good shape, but I've found myself a bit
stalled on testing my action controllers, and haven't found much via
Googling.
I've come up with an approach that I think will work, but I thought I'd
run it by the knowledgeable folks here first. Also, my apologies if
there's a better place to post this -- redirection gratefully accepted.
Here's my analysis of the situation (I'm using Smarty, but this could
apply to other rendering engines):
The basic task of an action controller's action method (generic,
simplistic):
- Validate its inputs, if any (top-level, context-free validation)
- If any invalid, render an appropriate page with a helpful error
message
- Instantiate the model, pass inputs to the appropriate method, get
the result of the method
- If invalid, render an appropriate page with a helpful error message
- If valid, set view variables and render the appropriate page
(Note that all 3 "render" actions have the same form: set some variables
and render a template. I'd guess that this could be extended in the
same vein for more sophisticated rendering situations.)
For unit testing, we don't really want to deal with the results of
rendering; we're testing the controller, not the view, and certainly not
the rendering engine. What we really want is to test the logic as
outlined above.
Thus, here's a pattern for a "testable action method":
- For each action, pull out the "guts" into a separate method, which
returns a pair (template name, array of view variables)
- The action method just calls the guts method, sets the variables into
the view, and renders the template
- This allows a unit test to just call the guts method and make
assertions on the returned template and variables.
Any good words appreciated,
--
Don Dwiggins
Advanced Publishing Technology