2012-10-19 122 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