Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/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 PHPUnit测试中Zend Framework 3中的模拟视图帮助器_Unit Testing_Mocking_Zend Framework3_View Helpers_Zend Servicemanager - Fatal编程技术网

Unit testing PHPUnit测试中Zend Framework 3中的模拟视图帮助器

Unit testing PHPUnit测试中Zend Framework 3中的模拟视图帮助器,unit-testing,mocking,zend-framework3,view-helpers,zend-servicemanager,Unit Testing,Mocking,Zend Framework3,View Helpers,Zend Servicemanager,我想在Zend Framework 3中测试一个特定的控制器操作。因为我使用了ZfcUser()和bjyaauthorize(),所以我需要模拟一些视图帮助程序。例如,我需要模拟isAllowed查看帮助程序,并让它始终返回true: class MyTest extends AbstractControllerTestCase { public function setUp() { $this->setApplicationConfig(include '

我想在Zend Framework 3中测试一个特定的控制器操作。因为我使用了
ZfcUser
()和
bjyaauthorize
(),所以我需要模拟一些视图帮助程序。例如,我需要模拟
isAllowed
查看帮助程序,并让它始终返回true:

class MyTest extends AbstractControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(include 'config/application.config.php');
        $bootstrap      = \Zend\Mvc\Application::init(include 'config/application.config.php');
        $serviceManager = $bootstrap->getServiceManager();

        $viewHelperManager = $serviceManager->get('ViewHelperManager');

        $mock = $this->getMockBuilder(IsAllowed::class)->disableOriginalConstructor()->getMock();
        $mock->expects($this->any())->method('__invoke')->willReturn(true);

        $viewHelperManager->setService('isAllowed', $mock);

        $this->getApplication()->getServiceManager()->setAllowOverride(true);
        $this->getApplication()->getServiceManager()->setService('ViewHelperManager', $viewHelperManager);
    }

    public function testViewAction()
    {
        $this->dispatch('/myuri');
        $resp = $this->getResponse();
        $this->assertResponseStatusCode(200);
        #$this->assertModuleName('MyModule');
        #$this->assertMatchedRouteName('mymodule/view');
    }
}
在我的
view.phtml
(将通过打开/分派
/myuri
uri来呈现)中,我调用视图助手
$this->isAllowed('my-resource')

但在执行
testViewAction()
时,我得到了响应代码500,但异常失败:


如何将我的
以某种方式注入到视图帮助器管理器中,让测试用例(
testViewAction
/
$this->dispatch()
)通过。

ViewHelperManager是service manager的另一个实例。而且它不允许覆盖。您可以在“setService”方法之前尝试“setAllowOverride”吗?

如前一个答案所述,我们需要在应用程序对象的
ViewHelperManager中重写ViewHelper。以下代码显示了如何实现这一点:

public function setUp()
{
    $this->setApplicationConfig(include 'config/application.config.php');
    $bootstrap = \Zend\Mvc\Application::init(include 'config/application.config.php');
    $serviceManager = $bootstrap->getServiceManager();

    // mock isAllowed View Helper of Bjyauthorize
    $mock = $this->getMockBuilder(IsAllowed::class)->disableOriginalConstructor()->getMock();
    $mock->expects($this->any())->method('__invoke')->willReturn(true);

    // inject the mock into the ViewHelperManager of the application
    $this->getApplication()->getServiceManager()->get('ViewHelperManager')->setAllowOverride(true);
    $this->getApplication()->getServiceManager()->get('ViewHelperManager')->setService('isAllowed', $mock);
}
public function setUp()
{
    $this->setApplicationConfig(include 'config/application.config.php');
    $bootstrap = \Zend\Mvc\Application::init(include 'config/application.config.php');
    $serviceManager = $bootstrap->getServiceManager();

    // mock isAllowed View Helper of Bjyauthorize
    $mock = $this->getMockBuilder(IsAllowed::class)->disableOriginalConstructor()->getMock();
    $mock->expects($this->any())->method('__invoke')->willReturn(true);

    // inject the mock into the ViewHelperManager of the application
    $this->getApplication()->getServiceManager()->get('ViewHelperManager')->setAllowOverride(true);
    $this->getApplication()->getServiceManager()->get('ViewHelperManager')->setService('isAllowed', $mock);
}