Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing phpForm中的PHPUnit引导_Unit Testing_Ide_Phpunit_Zend Framework2_Phpstorm - Fatal编程技术网

Unit testing phpForm中的PHPUnit引导

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/并

我正在使用Zend Framework 2,我想在PhpStorm 5.0.4中为我的所有模块运行测试。我已经设置了PhpStorm来检查
myproject/module
中的测试,它成功地找到了我的测试。问题是它没有读取每个模块中需要的配置文件(它指向一个引导文件)

以下是模块()的目录结构:

当我运行测试时,它会给我一个错误,因为在运行
UserTest.php
之前没有运行
Bootstrap.php
。所有文件都是正确的,因为如果I
cd
/myproject/module/User/tests/
并在终端内运行
phpunit
,则工作正常

我希望它在每个模块中使用配置(从而引导)。我尝试使用带有相对路径的
--configuration
选项,但无法使其工作

以下是我当前的配置:

在测试模块时,有没有关于如何运行配置文件(和引导)的指针?也就是说,模块有自己的配置文件和引导


提前感谢。

除非我遗漏了什么,否则您必须为每个模块设置测试配置。在你的情况下,你有我的项目。相反,您需要为每个模块设置一个引导文件,然后为每个模块设置配置(使用备用配置文件)。

PHP Storm 7假定您只需要一个默认引导文件,因此不会直接为每个PHPUnit测试配置启用单个引导文件

但是,zf2以每个模块为基础进行测试。因此,将默认值设置为第一个模块后,其他模块将无法工作。解决这个问题的办法是

  • 删除文件|设置| PHP | PHPUnit中的默认选项 您不必删除默认配置文件,但必须清空并取消选中默认引导文件。仅仅取消勾选是不够的

  • Go Run |编辑配置(此框还有其他快捷方式) 对于每个模块,您必须创建一个测试配置。例如,您将有一个标准的应用程序模块,然后是一个“应用程序模块测试”,可能是一个管理模块,然后是一个“管理模块测试”

  • 对于每个测试(假设标准zf2目录结构)

  • a。测试范围:目录 B目录:C:\wamp\www\PROJECT-NAME\module\module-NAME\test C选中“使用备用配置文件:” D输入C:\wamp\www\PROJECT-NAME\module\module-NAME\test\module-NAMETest\phpunit.xml.dist E在“测试运行程序选项”中,输入“-bootstrap C:\wamp\www\PROJECT-NAME\module\module-NAME\Test\module-NAMETest\bootstrap.php”

  • 对下一个模块重复上述步骤
  • 这里的问题是,只要default bootsrap字段有一个条目,phpstorm就会将其作为默认的--bootstrap选项添加到特定于案例的测试运行程序选项中。因此,无论您做什么,除了第一个/默认测试用例之外,每次都会运行错误的引导文件


    希望这有助于

    我在运行配置中使用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();