在PHPUnit测试中,在Zend Framework 3中模拟视图助手(Mock view helper in Zend Framework 3 in PHPUnit test)

编程入门 行业动态 更新时间:2024-10-23 14:33:24
在PHPUnit测试中,在Zend Framework 3中模拟视图助手(Mock view helper in Zend Framework 3 in PHPUnit test)

我想在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:131

How 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); }

更多推荐

本文发布于:2023-07-23 00:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   助手   测试中   Framework   PHPUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!