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

13

我正在遵循官方Zend Framework 2.1版本的教程。在单元测试部分,我应该在module/Application/test中运行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');
    }
}

我不知道为什么应用程序无法初始化,希望得到任何指针。


我的问题是我在模块文件夹和文件中拥有的权限。(我在一台Ubuntu电脑上) - leticia
2个回答

35

这是一个自动加载问题,可能与config/application.config.php中的module_paths选项有关(在教程中,它是位于/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的路径。当运行phpunit时,您的cwd位于测试目录内,在那里没有(显然)任何module目录。

感谢您的回复@Ocramius。我确实尝试过这个方法,但是我得到了一个错误信息:PHP致命错误:在/var/www/test/zf2-tutorial/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php的第22行中调用未定义的方法ApplicationTest\Controller\IndexControllerTest::assertModule()。我不确定它是否解决了一个问题并引起了另一个问题。 - Mark Davidson
问题似乎已经解决了(IndexControllerTest::assertModule不存在是另一个问题)。我们可以聊一下这个。 - Ocramius
是的,目前还需要大量的工作。如果您发现了这样可怕的错误,请最终在Github上进行编辑(链接在文档侧边栏中)。 - Ocramius
1
控制器名称是您在$config['controllers']['invokables']中配置的名称(通常为此)。那个名称是您分配给控制器服务的名称。 - Ocramius
感谢@Ocramius的澄清,我认为这意味着需要在用户指南中进行另一个更新。这样它就可以与Skeleton配置中的默认设置匹配了。 - Mark Davidson
显示剩余2条评论

-1
检查根配置文件中的application.config文件,在那里我们必须在“modules”中定义应用程序。
示例代码:
'modules' => array(
        'Application',
        'FrontEnd',
              ),

2
很抱歉,那不是它。在我的config/application.config.php文件中,我有以下代码:'modules' => array( 'Application', 'Album', ), - Jake Green

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接