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

2019-07-19 15:21发布

我下面的官方Zend框架2教程2.1版本。 在单元测试部分,在这里我应该跑在模块/应用/测试我遇到了以下问题的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');
    }
}

我不知道为什么应用程序不进行初始化,我将不胜感激任何指针。

Answer 1:

这是一个自动加载的问题可能与有关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 __DIR__ . '/../module ,假设application.config.php是在config/在你的应用程序的根目录。

为了澄清:出现问题,因为./module是相对于你的路径cwd 。 您的cwd运行时, phpunit是测试目录,那里没有(明显)任何内部module目录。



Answer 2:

检查根config文件夹中的文件application.config我们在“模块”来定义的应用程序。

示例代码:

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


文章来源: Zend Framework 2 tutorial: Module (Application) could not be initialized