Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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
Zend framework PHPUNIT Zend framework如何将模拟Zend_Registry::设置为仅针对第一个数据源项进行测试,并为第二个数据源项取消设置?_Zend Framework_Phpunit - Fatal编程技术网

Zend framework PHPUNIT Zend framework如何将模拟Zend_Registry::设置为仅针对第一个数据源项进行测试,并为第二个数据源项取消设置?

Zend framework PHPUNIT Zend framework如何将模拟Zend_Registry::设置为仅针对第一个数据源项进行测试,并为第二个数据源项取消设置?,zend-framework,phpunit,Zend Framework,Phpunit,我在用PHPUNIT和Zend框架创建这个UT时遇到了一个问题,我很新,这是我的第二个UT 这是要测试的功能: 这是UT的功能: protected function _functionToTest( $view ) { if($this->domagick){ $view->details = array(         'important value' => Zend_Registry::get('importantval

我在用PHPUNIT和Zend框架创建这个UT时遇到了一个问题,我很新,这是我的第二个UT

这是要测试的功能:

这是UT的功能:

 protected function _functionToTest( $view ) {
    if($this->domagick){
        $view->details = array(
                   'important value' => Zend_Registry::get('importantvalue')
                   'Some String' => $this->_getZendHelper()
        );
    }
    }
注意,$this->\u getZendHelper()是引入Zend_Registry::set('importantValue','123')的函数

第二个断言失败了,我应该创建两个单独的测试吗

有什么办法可以使你满意吗

Zend_Registry::set('importantValue', '123');
针对第一个数据提供程序(data1)运行 然后在第二次运行时,禁用它?因为它应该为Null,并且它正在创建一个数组而不是Null

还是我应该以一种特殊的方式来嘲笑它

我的测试代码:

   /** @dataProvider functionToTestDataProvider
   */

    public function testfunctionToTest($message, $siteMagicSettings, $expected)
    {

// this is the value that is I cant understand how to "reset"
// for the second dataprovider test which expects null

        Zend_Registry::set('importantValue', '123');

        $myMockForDebug = $this->getMockBuilder('CLASS_Name_Jack')
            ->disableOriginalConstructor()
            ->setMethods(array('_getZendHelper, _functionToTest'))
            ->getMock();

        $myMockForDebug = $this->getPublicClass($myMockForDebug);
        $myMockForDebug->_siteMagicSettings = $siteMagicSettings;

        $myMockForDebug->expects($this->any())
            ->method('_getZendHelper')
            ->will($this->returnValue('hello world'));

        $zendFrameworkControllerFrontEndMockVariable= $this->getMockBuilder('Zend_Controller_Front')
            ->disableOriginalConstructor()
            ->setMethods(
                array(
                    'getMagicalContext';
                )
            )
            ->getMock();

        $myMockForDebug->_zendControllerFront = $zendFrameworkControllerFrontEndMock;
        $view = $myMockForDebug->_zendControllerFront;

        $myMockForDebug->_functionToTest($view);

        $this->assertEquals($expected , $view->details);
        $this->assertNull($expected , $view->details);


    }
我的数据提供者:

    public function functionToTestDataProvider()
    {
        return [
            'data1'=> [
                    'message' => 'Do magick is true so it should create expected',
                    'siteMagicSettings' => (object) array(
                        'values' => (object) array(
                            'doMagick'=> 'true'
                        )
                    ),
                    'expected' => array(
                            'important value' => '123',
                            'Some String' => 'hello world'
                    ),
                ],
            'data2'=> [
                    'message' => 'Do magick is turned off',
                    'siteMagicSettings' => (object) array(
                        'values' => (object) array(
                            'doMagick'=> 'false'
                        )
                    ),
                    'expected' => null
                ]
            ];
    }
当前,当我运行测试时,我得到1个失败:

"data2" ('Do magick is turned off', stdClass, NULL)      
Array (...) does not match expected type "NULL".

在所有的评论之后,我将尝试对此作出回答。 从我的观点来看,主要问题是您的测试方法与Zend组件耦合太多,它们不需要测试,它们已经由框架进行了测试

这就是我重构代码的方法

protected function _functionToTest($carData, $helperString) {
    return [
        'important value' => $carData
        'Some String' => $helperString
    ];
}

// ..and somewhere
$helperString = $this->_getZendHelper();
$view->details = $this->domagick ? $this->_functionToTest($this->car, $helperString) : [];

您的函数实际上只是创建一个数组。

您是否考虑过将Zend_注册表中的值作为函数依赖项传递?此更改将允许您忘记Zend_注册表,在测试中使用您希望的任何数据集,并使代码更易于理解。@SergioRinaudo您能在回答中告诉我您将如何做到这一点吗?那太好了…或者有一个参考链接。实际上,我不知道“_functionToTest”的代码,但主要的概念是添加“importantValue”作为参数。我还将从参数中删除Zend_View实例,并返回一个数组,该数组稍后将分配给$View。实际上,建议将“\u functionToTest”与Zend\u注册表以及Zend\u视图分离。如果可以,发布“_functionToTest”实际代码。@SergioRinaudo要测试的函数就在那里,它说“这就是被测试的函数”。我真的很感谢你的帮助,我在这方面学到了很多。更新了代码,使其更易于解释,缺少了$this->_getZendHelper()是要测试的函数的一部分这一事实,正是它带来了Zend_Registry::set('importantValue',123')的值;