【问题标题】:PHPUnit with a Zend Framework 2 module带有 Zend Framework 2 模块的 PHPUnit
【发布时间】:2012-10-10 09:49:36
【问题描述】:

我正在努力让 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

很遗憾,我无法解决此问题。我做错了什么?我怎样才能做到这一点?

非常感谢。

【问题讨论】:

标签: phpunit zend-framework2


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我也有这个问题。

    我通过使用以下参数从我的 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
    

    已设置为测试我的 Rest 模块,如下所示:

    <?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 也可以仅使用引导程序类调用(即,不指定测试套件类)。还使用 --colors 标志使 phpunit 输出更具可读性:

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

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2012-11-18
      相关资源
      最近更新 更多