简体   繁体   中英

Zend Framework 2 tutorial: Module (Application) could not be initialized

I am following the official Zend Framework 2 tutorial for version 2.1. In the Unit Testing section, where I am supposed to run phpunit in module/Application/test I am running into the following problem:

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.

I copied the contents of IndexControllerTest.php from the tutorial.

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

I have no clue why the application won't initialize and I would appreciate any pointers.

This is an autoloading problem that may be related with the module_paths option in config/application.config.php (in the tutorial, it's the one you have in /path/to/application/config/test/application.config.php ).

That application.config.php probably looks like following:

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

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

To fix your problem, change the value ./module into an absolute path, for example __DIR__ . '/../module __DIR__ . '/../module , assuming the application.config.php is in config/ in your application's root dir.

To clarify: the problem occurs because ./module is a path relative to your cwd . Your cwd when running phpunit is inside the test dir, where there isn't (obviously) any module dir.

Check the application.config file in Root config folder where we have to define the applications in 'modules'.

Sample code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM