PHPUnit与Zend Framework 2模块

时间:2012-10-19 18:10:50

标签: phpunit zend-framework2

我正在努力让PHPUnit测试与ZF2一起使用。

我的目录结构如下

project
- src
  - config, data, module, public, vendor
  - init_autoloader.php
- test
  - bootstrap.php
  - SimpleTest.php

应用程序本身运行良好。

现在运行PHPUnit测试,我的bootstrap.php如下所示

putenv('ZF2=../src/vendor/zendframework/zendframework/library');
$loader = include '../src/vendor/autoload.php';
include '../src/init_autoloader.php';

这适用于ZF2相关的东西,但找不到我的模块。然后我读到我必须将以下行添加到我的bootstrap.php

Zend\Mvc\Application::init(include '../src/config/application.config.php');

但现在我收到以下错误:

PHP Fatal error:  Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (Mymodule) could not be initialized.' in /Users/_/src/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:139

不幸的是,我无法解决此问题。我做错了什么?我怎样才能做到这一点?

非常感谢。

2 个答案:

答案 0 :(得分:2)

与此同时,我能够使用set_include_path()spl_autoload_register()解决问题,如http://robertbasic.com/blog/unit-testing-zend-framework-2-modules所述。

答案 1 :(得分:0)

我也有这个问题。

我通过从我的ZF2项目的根目录运行phpunit并使用以下参数解决了这个问题:

 ./vendor/bin/phpunit
  --bootstrap ./module/Rest/test/Bootstrap.php
  ./module/Rest/test/RestTest/Controller/RestControllerTest.php

<zf2_project_root>/module/Rest/test/TestConfig.php.dist

设置为测试我的休息模块,如下所示:

<?php
return array(
    'modules' => array(
        'Rest' // <- my 'Rest' module is the one I test here.
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            '../../../config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            'module',
            'vendor',
        ),
    ),
);

当然,我的 ./ composer.json 包含对phpunit的引用,如下所示:

{
    "require" : {
        ...,
        "phpunit/phpunit" : "3.7.*",
        ...,
    }
}

注意:

phpunit也可以仅使用bootstrap类调用(即,不指定测试套件类)。另外使用 - colors 标志使phpunit输出更具可读性:

 ./vendor/bin/phpunit
  --colors
  --bootstrap ./module/Rest/test/Bootstrap.php