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

发布于 2024-09-16 14:14:57 字数 4469 浏览 3 评论 0原文

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

最好的问候,Oleg。

I try to write test for controller. I use OS Windows, zend framework and my libraries are in C:/library which is added to the include_path of php.ini.
When I run test testLoginAction I get an error No default module define for the application. But I don't use modules at all. Do you know how to solve this problem?

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();
    }   

}

My 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>

My bootstrap.php in tests/application:

// 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';

My Base class for tests:

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();
    }   
}

Best regards, Oleg.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浊酒尽余欢 2024-09-23 14:14:57

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

Make sure your application.ini does not contain configuration like resources.frontController.moduledirectory = APPLICATION_PATH "/modules" or resources.frontController.params.prefixDefaultModule = 1 or resources.modules[] =

冷了相思 2024-09-23 14:14:57

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

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

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

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

You need to modify your setUp() method from the ControllerTestCase class, after parent::setUp() line:

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

and remove the following line from your appBoostrap method:

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

溺渁∝ 2024-09-23 14:14:57

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

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

$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');
    }
}

完成。

资源

引导您的测试用例

$this->front-> won't work because the front controller property is $this->frontController-> or $this->getFrontController()->.

Even so, your tests probably won't work anyways because of the way you're bootstrapping. Are you configuring your front controller from an ini configuration file? If yes, then you don't need to configure the frontController in your tests, you need to configure your bootstrap by way of Zend_Application.

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

You won't need to call bootstrap() on your Zend_Application instance because Zend_Test_PHPUnit_ControllerTestCase will do that when you dispatch a request.

So your base class might loook like this:

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();
    }
}

And your controller test, extending your base class, will look like this:

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');
    }
}

Done.

Resources

Bootstraping your TestCase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文