在我的ZF2應用程序中,我遵循處理配置的「標準」方法。主要有:PHPUnit是否忽略Zend Framework 2中的config/autoload/...文件?
/config/application.config.php
- 默認應用項目範圍的設置/config/autoload/global.php
- 默認應用項目範圍的設置/config/autoload/local.php
- 環境的具體應用項目範圍的設置/config/autoload/MODULENAME.local.php
- 環境特定模塊特定設置 個
/module/MODULENAME/config/module.config.php
- 默認模塊的具體設置
現在,當我在一個模塊測試文件夾開始PHPUnit
,我既不能使用來自global.php
從local
S中的值也不值(雖然配置文件包括在內 - 我已經檢查過了)。
例如:PHPUnit調用我的自定義視圖助手ContentForEnvironment
,它包含此代碼:$currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];
。
/path/to/project/module/Application/test# phpunit
PHPUnit 3.7.13 by Sebastian Bergmann.
Configuration read from /path/to/project/module/Application/test/phpunit.xml
E
Time: 0 seconds, Memory: 8.75Mb
There was 1 error:
1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Undefined index: environment
/path/to/project/vendor/MyNamespace/library/MyNamespace/View/Helper/ContentForEnvironment.php:32
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:400
/path/to/project/module/Application/view/layout/layout.phtml:25
/path/to/project/module/Application/view/layout/layout.phtml:25
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:507
/path/to/project/vendor/zendframework/zendframework/library/Zend/View/View.php:205
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:126
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:136
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472
/path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:332
/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:285
/path/to/project/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:255
/path/to/project/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php:46
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
的environment
選項被設置在global.php
和local.php
和作品,當我運行在瀏覽器中appllication。
當我在默認模塊特定配置文件(module.config.php
)中設置該選項時,可讀取設置值。
這裏怎麼回事?
編輯
/module/Application/test/Bootstrap.php
<?php
namespace ApplicationTest;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;
error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);
class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;
public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/phpunit.config.php')) {
$testConfig = include __DIR__ . '/phpunit.config.php';
} else {
$testConfig = include __DIR__ . '/phpunit.config.php.dist';
}
$zf2ModulePaths = array();
if (isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath))) {
$zf2ModulePaths[] = $path;
}
}
}
$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');
static::initAutoloader();
// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => array(
'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
),
);
$config = ArrayUtils::merge($baseConfig, $testConfig);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
static::$config = $config;
}
public static function getServiceManager()
{
return static::$serviceManager;
}
public static function getConfig()
{
return static::$config;
}
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));
if (!$zf2Path) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
}
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
));
}
protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) return false;
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
/module/Application/test/phpunit.config.php
<?php
return array(
'modules' => array(
'Application',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'../../../config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'module',
'vendor',
),
),
);
/module/Application/test/phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="Application Unit Tests">
<directory>./ApplicationTest</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>/path/to/project/module/Application/src/Application/</directory>
<!--
<exclude>
<directory suffix=".phtml">/path/to/project/module/Application/</directory>
<directory suffix=".php">/path/to/project/module/Application/test/</directory>
</exclude>
-->
</whitelist>
</filter>
</phpunit>
編輯
改變了/module/Application/test/Bootstrap.php
但錯誤仍然存在。
它與測試的引導配置有關。請提供。 – netiul 2013-05-07 10:06:57
感謝您的評論。我剛剛添加了Bootstrap和配置。 – automatix 2013-05-08 11:07:40