ZF-7839: Support or document Zend_Application_Bootstrap usage in Zend_Test_PHPUnit_ControllerTestCase
Description
I would like to see the "standard" Zend Framework bootstrapping method to be supported in Zend_Test_PHPUnit_ControllerTestCase. It is confusing and not obvious on how to reuse your Zend_Application_Bootstrap when using the ControllerTestCase.
There are 2 ways that this can be done.
------ First Method ------
Document with code examples on how to use Zend_Application_Bootstrap_* in the Zend_Test_PHPUnit_ControllerTestCase manual. However one issue is that it feels a bit weird as you have to overwrite $this->_frontController manually yourself.
--- One Current Example ---
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->frontController
->registerPlugin(new Bugapp_Plugin_Initialize('development'));
}
--- Using Zend_Application_Bootstrap Example ---
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
// Create application, bootstrap, but don't RUN!
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap(); //note run() is not called!
//front controller now bootstrapped using whatever method you used (config, resource plugin...etc)
//Having $this->frontController and $this->_frontController may be hard to explain
$this->_frontController = $bootstrap->getResource('frontController');
}
As you can see this is quite simple, but as I noted in the comment it may be confusing to explain that you have to overwrite $this->_frontController not $this->frontController.
------ Second Method ------
Actually supporting Zend_Application_Bootstrap in ControllerTestCase.
public function setUp()
{
$this->bootstrap = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
if $this->bootstrap is set to an instance of Zend_Application then have the ControllerTestCase automatically call $bootstrap->bootstrap() for you. Future calls to $this->getFrontController() will retrieve the frontController from the bootstrap instead.
This is a simplified example obviously and I may have missed some details, but it seems obvious for some kind of support of the "standard" bootstrapping method. Bootstrapping can be very lengthy/complex and since the main goal is re-usability why would we want to have a completely different bootstrap method (or duplicated) for controller testing. I think this improvement will "sync" Zend_Test_PHPUnit_ControllerTestCase with the encouraged bootstrap method. This also puts to great use the separate production , development, testing ini settings document in the Zend_Application quick start. It becomes even more important with all the plugins with preDispatch details...etc. I think you get the point :).
Thoughts, suggestions, hate messages are welcomed :)
Comments
Posted by Duo Zheng (duoduo) on 2009-09-13T13:00:23.000+0000
Fixed code format
Posted by Duo Zheng (duoduo) on 2009-09-13T13:03:43.000+0000
Made adjustment to title
Posted by Duo Zheng (duoduo) on 2009-09-13T13:18:51.000+0000
Clarifying some confusing writing.
Posted by Duo Zheng (duoduo) on 2009-09-13T13:41:40.000+0000
Added additional support for importance of re-usability.
Posted by Matthew Weier O'Phinney (matthew) on 2009-10-16T11:48:52.000+0000
I think the second example makes the most sense. It's simple and concise for the end-user, and relatively easy to support. I'll take a stab at it today.
Posted by Matthew Weier O'Phinney (matthew) on 2009-10-16T13:24:50.000+0000
The second use case is now possible:
This will be available in 1.9.5, and is currently in trunk and the 1.9 release branch.