David Mintz wrote:
Playing around with Zend_Application and friends, I made a Bootstrap.php
which does this:
protected function _initLog() {
printf ("running %s:%s()\n",__CLASS__,__FUNCTION__);
return new Zend_Log(new Zend_Log_Writer_Firebug());
}
---
And a controller plugin:
---
class Plugin_OurTestPlugin extends Zend_Controller_Plugin_Abstract {
function preDispatch(Zend_Controller_Request_Abstract $request ) {
$front = Zend_Controller_Front::getInstance();
$bootstrap = $front->getParam('bootstrap');
printf ("\$bootstrap is a %s.\n", gettype($bootstrap));
$log = $front->getParam('bootstrap')->getResource('log');
$log->info('woo hoo, your controller plugin works');
}
}
---
In a browser, the printf statement outputs " $bootstrap is a object" and
indeed it is, but when I run phpunit tests from the command line, -- forgive
all the "echo" output, I have temporarily polluted my code with echo()s in
order to determine what's executing --
Bootstrap constructor running...
running Bootstrap:_initLog()
we are bootstrapping the application in
/opt/www/shitou/tests/ControllerTestCase.php
$bootstrap is a NULL.
Fatal error: Call to a member function getResource() on a non-object in
/opt/www/shitou/application/plugins/OurTestPlugin.php on line 14
Seems like my Bootstrap is instantiated before, not after, my base class
ControllerTestCase.php bootstraps the application. I must be doing something
weird but can't figure out what.
My test setup:
I have a phpunit.xml:
---
<phpunit bootstrap="./bootstrap.php" colors="false">
<testsuite name="MyZFProject">
<directory>./</directory>
</testsuite>
</phpunit>
---
and bootstrap.php contains:
---
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
// Include path
set_include_path(
'.'
. PATH_SEPARATOR . BASE_PATH . '/library'
. PATH_SEPARATOR . get_include_path()
);
// Define application environment
define('APPLICATION_ENV', 'testing');
require_once './ControllerTestCase.php';
echo "we are in ".__FILE__. "\n";
---
and ControllerTestCase.php looks like
---
abstract class ControllerTestCase extends
Zend_Test_PHPUnit_ControllerTestCase
{
public $application;
public $bootstrap;
public function setUp()
{
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->application->bootstrap();
echo "we are bootstrapping the application in ".__FILE__."\n";
}
}
---
This is with ZF 1.9.0a1 -- because therein is a fix for a bug that was
ruining my day.
Thanks!
I stumbled across the same issue lately.
The bootstrap object is assigned as a frontcontroller param in the run()
method of Zend_Application_Bootstrap_Bootstrap. But this method is not
executed in the test environment, only bootstrap().
I think that´s the reason for the error you get (Fatal error: Call to a
member function getResource() on a non-object...)
As a workaround I set the frontcontroller param "bootstrap" manually
within the callback method appBootstrap() of my testclasses.
public function setUp()
{
<snip>...
}
public function appBootstrap()
{
$this->application->bootstrap();
// set bootstrap param
$bootstrap = $this->application->getBootstrap();
$front = $bootstrap->getResource('FrontController');
$front->setParam('bootstrap', $bootstrap);
}