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

PHPUNIT Zend framework how mock Zend_Registry::set to be tested against 1st datasource item only and unset it for the 2nd?

我在使用 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->_getZendHelper() 是引入 Zend_Registry::set('importantValue', '123');

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

有什么办法可以制作

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) : [];

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