Zend Framework Testing控制器Zend_Controller_Exception:没有为此应用程序定义的默认模块

时间:2010-08-29 18:30:33

标签: zend-framework testing phpunit

我尝试为控制器编写测试。我使用OS Windows,zend框架和我的库在C:/library中,它被添加到php.ini的include_path中。 当我运行测试testLoginAction时,我收到错误没有为应用程序定义默认模块。但我根本不使用模块。你知道如何解决这个问题吗?

IndexControllerTest.php

class Controller_IndexControllerTest extends ControllerTestCase 
{
    public function testIsEverythingOK()
    {
        $this->assertTrue(true);
    }

    public function testLoginAction()
    {
            $this->dispatch('/login/index');
            $this->assertModule('default');
            $this->assertController('login');
            $this->assertAction('index');
    }
}

ControllerTestCase.php

require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $_application;

    public function setUp()
    {
        $this->bootstap = array($this, 'appBootstrap');     
        parent::setUp();
    }

    public function appBootstrap()
    {
    //       require dirname(__FILE__) . '/bootstrap.php';


        $this->front->setControllerDirectory('../application/controllers');     
        $this->_application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini');
        $this->_application->bootstrap();
    }   

}

我的phpunit.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./application/bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    stopOnFailure="true"
    syntaxCheck="true">

    <!-- запускаем все тесты из корневой директории -->
    <testsuite name="Main Test Suite">
        <directory>./</directory>
    </testsuite>

        <filter>            <!-- смотрим лишь на следующие директории -->
        <whitelist>
                  <directory suffix=".php">../application</directory>
 <!--               <directory suffix=".php">../library</directory>-->
            <exclude>
                <directory suffix=".phtml">../application</directory>
                <directory>../application/forms</directory>
                <directory>../application/models</directory>                    
                <directory>../library</directory>                    
                <file>../application/Bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <!-- логирование и создание отчета -->
        <log type="coverage-html" target="./report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
    </logging>
</phpunit>

我在测试/应用程序中的bootstrap.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(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';

我的测试基类:

require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $_application;

    public function setUp()
    {
        $this->bootstap = array($this, 'appBootstrap');     
        parent::setUp();
    }

    public function appBootstrap()
    {
//       require dirname(__FILE__) . '/bootstrap.php';


        $this->front->setControllerDirectory('../application/controllers');     
        $this->_application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini');
        $this->_application->bootstrap();
    }   
}

最好的问候,奥列格。

3 个答案:

答案 0 :(得分:0)

确保您的application.ini不包含resources.frontController.moduledirectory = APPLICATION_PATH "/modules"resources.frontController.params.prefixDefaultModule = 1resources.modules[] =

等配置

答案 1 :(得分:0)

您需要在parent :: setUp()行之后修改ControllerTestCase类中的setUp()方法:

$ this-&gt; getFrontController() - &gt; setControllerDirectory(APPLICATION_PATH。'/ controllers');

并从appBoostrap方法中删除以下行:

$这 - &GT;前置式&GT; setControllerDirectory(” ../应用/控制器);

答案 2 :(得分:0)

$this->front->无效,因为前端控制器属性为$this->frontController->$this->getFrontController()->

即便如此,由于你的引导方式,你的测试可能无论如何都行不通。您是从ini配置文件配置前端控制器吗?如果是,那么您不需要在测试中配置frontController,您需要通过Zend_Application配置引导程序。

$this->bootstrap = new Zend_Application(
   'testing',
   APPLICATION_PATH . '/configs/application.ini'
);

您不需要在bootstrap()个实例上致电Zend_Application,因为Zend_Test_PHPUnit_ControllerTestCase会在发送请求时执行此操作。

所以你的基类可能会像这样:

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        // Assign and instantiate in one step:
        $this->bootstrap = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );
        parent::setUp();
    }
}

您的控制器测试,扩展您的基类,将如下所示:

class Controller_IndexControllerTest extends ControllerTestCase 
{
    public function testIsEverythingOK()
    {
        $this->assertTrue(true);
    }

    public function testLoginAction()
    {
            $this->dispatch('/login/index');
            $this->assertModule('default');
            $this->assertController('login');
            $this->assertAction('index');
    }
}

完成。

资源

Bootstraping your TestCase