我想在Zend Framework 3中测试特定的控制器动作。因为我使用ZfcUser ( https://github.com/ZF-Commons/ZfcUser )和Bjyauthorize ( https://github.com/bjyoungblood/BjyAuthorize ),我需要嘲笑一些看法助手。 例如,我需要模拟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,并且失败了异常:
Exceptions raised: Exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'A plugin by the name "isAllowed" was not found in the plugin manager Zend\View\HelperPluginManager' in ../vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:131我怎样才能以一种让测试用例( testViewAction / $this->dispatch() )通过的方式将我的isAllowed模拟注入到视图帮助器管理器中。
I want to test a specific controller action in Zend Framework 3. Because I use ZfcUser (https://github.com/ZF-Commons/ZfcUser) and Bjyauthorize (https://github.com/bjyoungblood/BjyAuthorize) I need to mock some view helpers. For example I need to mock isAllowed view helper and let it return true always:
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'); } }In my view.phtml (which will be rendered by opening/dispatching /myuri uri) I call the view helper $this->isAllowed('my-resource').
But I got response code 500 with failing exception when executing testViewAction():
Exceptions raised: Exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'A plugin by the name "isAllowed" was not found in the plugin manager Zend\View\HelperPluginManager' in ../vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:131How can I inject my isAllowed mock to the view helper manager in a way, that let the test case (testViewAction / $this->dispatch()) pass.
最满意答案
正如前面的回答中所述,我们需要覆盖应用程序对象中ViewHelperManager中的ViewHelperManager 。 以下代码显示了如何实现这一点:
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); }As stated in the previous answer already, we need to override the ViewHelper within the ViewHelperManager in the application object. The following code shows how this could be achieved:
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); }更多推荐
发布评论