Unit testing phpForm中的PHPUnit引导
我正在使用Zend Framework 2,我想在PhpStorm 5.0.4中为我的所有模块运行测试。我已经设置了PhpStorm来检查Unit testing phpForm中的PHPUnit引导,unit-testing,ide,phpunit,zend-framework2,phpstorm,Unit Testing,Ide,Phpunit,Zend Framework2,Phpstorm,我正在使用Zend Framework 2,我想在PhpStorm 5.0.4中为我的所有模块运行测试。我已经设置了PhpStorm来检查myproject/module中的测试,它成功地找到了我的测试。问题是它没有读取每个模块中需要的配置文件(它指向一个引导文件) 以下是模块()的目录结构: 当我运行测试时,它会给我一个错误,因为在运行UserTest.php之前没有运行Bootstrap.php。所有文件都是正确的,因为如果Icd到/myproject/module/User/tests/并
myproject/module
中的测试,它成功地找到了我的测试。问题是它没有读取每个模块中需要的配置文件(它指向一个引导文件)
以下是模块()的目录结构:
当我运行测试时,它会给我一个错误,因为在运行UserTest.php
之前没有运行Bootstrap.php
。所有文件都是正确的,因为如果Icd
到/myproject/module/User/tests/
并在终端内运行phpunit
,则工作正常
我希望它在每个模块中使用配置(从而引导)。我尝试使用带有相对路径的--configuration
选项,但无法使其工作
以下是我当前的配置:
在测试模块时,有没有关于如何运行配置文件(和引导)的指针?也就是说,模块有自己的配置文件和引导
提前感谢。除非我遗漏了什么,否则您必须为每个模块设置测试配置。在你的情况下,你有我的项目。相反,您需要为每个模块设置一个引导文件,然后为每个模块设置配置(使用备用配置文件)。PHP Storm 7假定您只需要一个默认引导文件,因此不会直接为每个PHPUnit测试配置启用单个引导文件 但是,zf2以每个模块为基础进行测试。因此,将默认值设置为第一个模块后,其他模块将无法工作。解决这个问题的办法是
希望这有助于我在运行配置中使用environment variables选项来定义一个值,我可以在global bootstrap.php中使用该值来引入特定于给定模块或应用程序部分的需求
class GlobalBootstrap
{
private static $applicationSections = [
'section_a',
'section_b',
'section_c'
];
public static function init()
{
$localMethod = self::fetchLocalMethod();
if (!is_null($localMethod)) {
self::$localMethod();
} else {
throw new Exception(
__CLASS__
. '::'
. __FUNCTION__
. 'Says: No local method has been defined for this test section'
);
}
}
private static function fetchLocalMethod()
{
$section = getenv('APPLICATION_SECTION');
if (is_null($section) || !in_array($section, self::$applicationSections)) {
return null;
}
$section = preg_replace("/[^a-zA-Z]+/", "", $section);
$method = 'bootstrap' . ucfirst(strtolower($section));
return $method;
}
/**
* Section specific methods
*/
protected static function bootstrapSectiona()
{
require __DIR__ . '/../../{section_a}/module/Test/Bootstrap.php';
}
}
GlobalBootstrap::init();
可以创建任意变量和值,然后在bootstrap.php中使用:getevn(variable_NAME);这在PHPStorm中节省了大量冗长的配置,但如果您依赖许多不同的引导功能,culd可能会变得同样复杂
class GlobalBootstrap
{
private static $applicationSections = [
'section_a',
'section_b',
'section_c'
];
public static function init()
{
$localMethod = self::fetchLocalMethod();
if (!is_null($localMethod)) {
self::$localMethod();
} else {
throw new Exception(
__CLASS__
. '::'
. __FUNCTION__
. 'Says: No local method has been defined for this test section'
);
}
}
private static function fetchLocalMethod()
{
$section = getenv('APPLICATION_SECTION');
if (is_null($section) || !in_array($section, self::$applicationSections)) {
return null;
}
$section = preg_replace("/[^a-zA-Z]+/", "", $section);
$method = 'bootstrap' . ucfirst(strtolower($section));
return $method;
}
/**
* Section specific methods
*/
protected static function bootstrapSectiona()
{
require __DIR__ . '/../../{section_a}/module/Test/Bootstrap.php';
}
}
GlobalBootstrap::init();