Zend Framework 2教程:模块(应用程序)无法初始化

时间:2013-02-08 08:38:17

标签: zend-framework2

我正在关注2.1版的官方Zend Framework 2教程。在Unit Testing部分,我应该在模块/应用程序/测试中运行phpunit,我遇到了以下问题:

user@xubuntu1210:~/Desktop/zf2-tutorial/module/Application/test$ phpunit
PHPUnit 3.7.13 by Sebastian Bergmann.

Configuration read from /home/user/Desktop/zf2-tutorial/module/Application/test/phpunit.xml.dist

E

Time: 0 seconds, Memory: 4.00Mb

There was 1 error:

1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Zend\ModuleManager\Exception\RuntimeException: Module (Application) could not be initialized.

/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:140
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:81
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:204
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:100
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:239
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:146
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:173
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:193
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:236
/home/user/Desktop/zf2-tutorial/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php:20

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

我从教程中复制了IndexControllerTest.php的内容。

<?php

namespace ApplicationTest\Controller;

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class IndexControllerTest extends AbstractHttpControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(
            include '/home/user/Desktop/zf2-tutorial/config/application.config.php'
        );
        parent::setUp();
    }

    public function testIndexActionCanBeAccessed()
    {
        $this->dispatch('/'); // this is line 20
        $this->assertResponseStatusCode(200);

        $this->assertModule('application');
        $this->assertControllerName('application_index');
        $this->assertControllerClass('IndexController');
        $this->assertMatchedRouteName('home');
    }
}

我不知道为什么应用程序不会初始化,我会很感激任何指针。

2 个答案:

答案 0 :(得分:32)

这是一个自动加载问题,可能与module_paths中的config/application.config.php选项有关(在教程中,它是/path/to/application/config/test/application.config.php中的{}}。

application.config.php可能如下所示:

return array(
    'modules' => array(
        'Application',
        'Album',
    ),

    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

要解决您的问题,请将值./module更改为绝对路径,例如__DIR__ . '/../module,假设您的应用中的application.config.php位于config/中根目录。

澄清:问题出现是因为./module是相对于cwd的路径。运行cwdphpunit位于测试目录内,其中(显然)没有任何module目录。

答案 1 :(得分:-1)

检查Root配置文件夹中的application.config文件,我们必须在'modules'中定义应用程序。

示例代码:

'modules' => array(
        'Application',
        'FrontEnd',
              ),