(I know ZF1 is dead, but I'm trying to improve my tests so I can transition away from it.)
In a ZF1 bootstrap file, I can define resources (like database connections), and use them throughout the app. I'd like to replace some of these resources with mocks for my integration tests, but I'm not sure of the best way.
I can define a resource like this:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
public function _initMyResource() {
return new MyResource();
}
}
and then my controllers can retrieve it like this:
class MyController extends Zend_Controller_Action {
public function indexAction() {
$myResource = $this->getInvokeArg('bootstrap')->getResource('MyResource');
}
}
Then my controller test might look like:
class MyTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
protected function setUp() {
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->application->bootstrap();
}
public function testSample() {
$myResource = new MyMockResource();
// what do I do with it?
$this->dispatch('/index');
}
}
The problem is, all of the resources get bootstrapped by the setUp
method of Zend_Test_PHPUnit_ControllerTestCase
, when it calls my appBootstrap
method.
But I'd like to replace certain resources like MyResource
after setUp
, in my individual test methods. What's a good way to do this? I'd still like them to use their original values by default, but I want to be able to replace them when necessary.
Is there a more typical way to do this?