【问题标题】:Zend Framework Testing controller Zend_Controller_Exception: No default module defined for this applicationZend Framework Testing 控制器 Zend_Controller_Exception:没有为此应用程序定义默认模块
【发布时间】:2011-04-05 11:50:22
【问题描述】:

我尝试为控制器编写测试。我使用 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();
    }   
}

最好的问候,奥列格。

【问题讨论】:

    标签: zend-framework testing phpunit


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      $this->getFrontController()->setControllerDirectory(APPLICATION_PATH . '/controllers');

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

      $this->front->setControllerDirectory('../application/controllers');

      【讨论】:

        【解决方案3】:

        $this-&gt;front-&gt; 不起作用,因为前端控制器属性是 $this-&gt;frontController-&gt;$this-&gt;getFrontController()-&gt;

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

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

        您无需在Zend_Application 实例上调用bootstrap(),因为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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多