I have a library I’ve made for use with Zend Framework. In it, I have Company/Controller/Action.php which all application controllers extend. It’s pretty simple and just injects some things into the controllers (like Doctrine’s entity manager).
I’m trying to get unit testing to work and have stumbled across an issue where I’m unable to get the bootstrap from within the Company_Controller_Action class (which extends Zend_Controller_Action):
function preDispatch()
{
$bootstrap = $this->getInvokeArg('bootstrap');
}
$bootstrap is null at this point. Anyone have any ideas why? I’ve verified that my bootstrap is being called and I can fetch the front controller, but I can’t get the bootstrap (through either getInvokeArg or the front controller).
This works in the normal production and development environments. The testing section of my application.ini just inherits from the production one, so it should be the same.
This is what I’m doing in my setUp():
public function setUp()
{
$this->bootstrap = new Zend_Application(
'testing',
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
My Bootstrap.php:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
My phpunit.xml:
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".php">../application/Entities</directory>
<directory suffix=".php">../application/modules/default/views</directory>
<file>../application/Bootstrap.php</file>
<file>../application/modules/default/controllers/ErrorController.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
<log type="testdox" target="./log/testdox.html" />
</logging>
</phpunit>
Update: My $bootstrap is no longer null using the above configuration.
This was a bug if I remember some few weeks ago which I saw in Zend bug trackers.
Here is the fix which has worked for many:
Initialize Front controller in your Bootstrap.php:
EDIT:
I did a quick Google for the issue. I got a similar workaround (almost the same as I mentioned above) and also this answer on stackoverflow might help you, according to this answer it can be called via action helpers.