According to this mailing list discussion, the recommended way to access the application resources in a Zend MVC controller is:
$this->getInvokeArg('bootstrap')->getResource('foo');
This works in production (when browsing to the corresponding Web page). However, when testing a controller action containing this code with Zend_Test_PHPUnit_ControllerTestCase
, I get:
PHP Fatal error: Call to a member function getResource() on a non-object in .../application/controllers/IndexController.php on line 12
Until introducing that getInvokeArg
thing, the tests ran just fine. The question is, how can I make the “recommended” way of accessing resources work in the test harness?
Just checked: $this->getFrontController()->getParam('bootstrap')->getResource('foo')
doesn't work either.
UPDATE: I do call the application bootstrap with phpunit --bootstrap ./scripts/application_bootstrap.php ...
and I know it executes fine.
And there I have:
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
I ran into the same issue when using a controller plugin that needed the bootstrap.
Basically I created an abstract class and inherited from it.
abstract class My_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $application;
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
return parent::setUp();
}
public function appBootstrap()
{
$this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/config/app.ini');
$this->application->bootstrap();
$bootstrap = $this->application->getBootstrap();
$front = $bootstrap->getResource('FrontController');
$front->setParam('bootstrap', $bootstrap);
}
}
Then you use as follows:
class MyControllerTest extends My_ControllerTestCase
{
}
I also logged a request to have this functionality become apart of ZF
[ZF-7373]: (http://framework.zend.com/issues/browse/ZF-7373) -
Leaving a comment would help to highlight this for inclusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With