2012-10-19 123 views
3

我在努力获得PHPUnit测试以使用ZF2。带有Zend Framework 2模块的PHPUnit

我的目录结构如下所示

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 

不幸的是,我没能解决这个问题。我做错了什么?我该如何做这项工作?

非常感谢。

+0

看看这个,它为我工作。 http://devblog.x2k.co.uk/getting-phpunit-working-with-a-zend-framework-2-mvc-application/ – vascowhite

+0

他们使用Zend \ Mvc \ Application :: init()和我一样,所以它不适合我。 – str

+0

我很高兴看到你的工作。如果您有兴趣,请点击此处查看我的工作设置https://github.com/vascowhite/ZendMinimumApplication/tree/master/test。 – vascowhite

回答

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的也可以用引导类只引用(即指定内部消除测试套件类)。还使用--colors标志使得PHPUnit的输出可读性更强:

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