I'm working on a web application with Zend Framework v1.11.0, but I'm failing to set up a correct test environment for controller tests. I know that there are already a lot of questions about setting up Zend test, however, after hours of research, none of these fixed my problem. So here is my code:
Test Bootstrap:
<?php
error_reporting(E_ALL | E_STRICT);
defined('APPLICATION_PATH') || define('APPLICATION_PATH',realpath(dirname(__FILE__).'/../../application'));
define('APPLICATION_ENV', 'testing');
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library'), get_include_path())));
require_once ('Zend/Application.php');
require_once ('ControllerTestCase.php');
ControllerTestCase:
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
public $application;
public 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();
}
}
Simple Test:
class IndexControllerTest extends ControllerTestCase {
public function testDefaultShouldInvokeIndexAction()
{
$this->dispatch('/');
$this->assertModule('default');
$this->assertController('index');
$this->assertAction('index');
}
}
Everytime I run this test, PHPUnit throws a fatal error message not finding a class in my library that is used in my IndexController. Therefore the reason must be something related to the Autoloader, I thought. After debugging I found out, that the test runs through the regular Bootstrap, trough the Auth plugin and so on and that my libraries are included. So I have no idea what PHPUnit's problem might be. In browser everything works fine and non-controller tests in the commandline (e.g. global settings) work, too.
I'd be very grateful for any hint what i need to do to get my controller tests working!
edit: my directory structure looks like this (I separated my classes into different libraries for certain reasons)
project
- library1
- library2
- project_name
->application
->library3
->library4
->tests
->>application
->>>ControllerTestCase.php
->>>Bootstrap.php
->>Controller
->>>IndexControllerTest.php
my include paths look actually like:
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library3'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library4'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../../library1'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../../library2'), get_include_path())));
In my own
ControllerTestCase
class I set the bootstrap object in the constructor instead of thesetUp
function. So for you that would mean to change thisinto
and removing the
appBootstrap
method. That's really the only difference I see between your code and my own code. I that does not fix the problem, it's likely your path settings are off.