I'm currently working on a project to implement Zend Framework controller and model testing and specification. I'm looking for opinions on whether this is feasible - the main problem I have is that directly instantiating objects within controllers makes it impossible for any testing suite from isolating the controller action from it's carrying objects. Without requiring some perverse parameter acrobatics or requiring configuration heavy injectors like Pico or Phemto a simple solution (I think) is to enable a simplified object loader of a few dozen lines to be added for use.
This could be added either as a separate class Zend_Factory, or appended to Zend_Loader (which seems appropriate). I've copied the current sample code and unit tests below. In use, it would require a
simple call of:
Zend_Factory::create('Zend_Mail');
Then a unit test or BDD spec could mock this object result (before the create() call of course) using:
Zend_Factory::replaceClass('Zend_Mail', new ZMail_Mock);
require_once 'Zend/Registry.php'; /** * Abstract object instantiation with minimal setup and allow for replacement * of created object return values by preceding unit tests or
BDD specifications * */ class Zend_Factory {