【问题标题】:Zend Framework 2 how to test forward in controller using phpunit?Zend Framework 2 如何使用 phpunit 在控制器中测试转发?
【发布时间】:2014-07-22 15:46:27
【问题描述】:

如何使用 PHPUnit 在控制器中测试转发?

我有两个简单的模块(A 和 B),模块 A 使用转发调用模块 B。 这是一个不起作用的简单代码:

模块A

class ModuleAController extends AbstractRestfulController 
{              
protected $em;

 public function setEntityManager(EntityManager $em)
    {
        $this->em = $em;
    }

public function getEntityManager()
{
    if (null === $this->em) {
        $this->em 
        $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    }
    return $this->em;
}

 public function getList()
{  
    $data = array('message' => 'passed by module A');

    $forward = $this->forward()->dispatch('ModuleB\Controller\ModuleB');

    $data['Message'] = $forward->getVariable('Message');
    return new JsonModel($data);
}

}

模块B

class ModuleBController extends AbstractRestfulController 
{

 public function setEntityManager(EntityManager $em)
    {
        $this->em = $em;
    }

 public function getEntityManager()
 {
    if (null === $this->em) {
        $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    }

 }
  public function getList()
 {
    $data = array('Message'=>'passed by module B');
    return new JsonModel($data);
 }
}

这是一个测试代码:

class ModuleAControllerTest extends AbstractHttpControllerTestCase
{
    protected $controller;
    protected $request;
    protected $response;
    protected $routeMatch;
    protected $event;

    protected function setUp()
    {
        $serviceManager = Bootstrap::getServiceManager();
        $this->controller = new ModuleAController();
        $this->request    = new Request();
        $this->routeMatch = new RouteMatch(array());
        $this->event      = new MvcEvent();
        $config = $serviceManager->get('Config');
        $routerConfig = isset($config['router']) ? $config['router'] : array();
        $router = HttpRouter::factory($routerConfig);

        $this->event->setRouter($router);
        $this->event->setRouteMatch($this->routeMatch);
        $this->controller->setEvent($this->event);
        $this->controller->setServiceLocator($serviceManager);
    }
    public function testModuleAControllerCanBeAccessed()
    {        

        $result   = $this->controller->dispatch($this->request);
        $response = $this->controller->getResponse();

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals(200, 1+99+100);
    }
}

这是错误信息: 有 1 个错误:

1) ModuleATest\Controller\ModuleAControllerTest::testModuleAControllerCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "forward"; no instance returned
....
Caused by
Zend\ServiceManager\Exception\ServiceNotCreatedException: Zend\Mvc\Controller\Plugin\Service\ForwardFactory requires that the application service manager has been injected; none found
....

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

有什么办法可以让这段代码工作吗??有什么想法吗??

谢谢。

【问题讨论】:

  • 尝试为forward 使用Mock 对象?
  • 你有例子吗??
  • 找到解决办法了吗?

标签: php unit-testing zend-framework phpunit forward


【解决方案1】:

我还没有为插件创建模拟。我不知道如何将新插件设置为控制器。但是mock会是这样。

PHPunit 测试文件

public function testControllerWithMock()
{
    /* Result from ModuleBController method getList() */
    $forwardResult = new JsonModel(array('Message'=>'passed by module B'));

    /* Create mock object for forward plugin */
    $forwardPluginMock = $this->getMockBuilder('\Zend\Mvc\Controller\Plugin\Forward')
        ->disableOriginalConstructor()
        ->getMock();
    $forwardPluginMock->expects($this->once())
        ->method('dispatch') /* Replace method dispatch in forward plugin */
        ->will($this->returnValue($forwardResult)); /* Dispatch method will return $forwardResult */

    /* Need register new plugin (made mock object) */
    $controller->setPluginManager(); /* ??? Set new plugin to controller */

我正在考虑如何决定。

好的,试试看。

    $controller->getPluginManager()->injectController($forwardPluginMock);

我不为控制器编写 PHPUnit 测试。因为控制器必须返回一个视图和使用Selenium 测试视图的最佳解决方案。我通常使用PHPUnitSelenium 测试来测试它。

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 2015-07-26
    • 2012-11-18
    • 1970-01-01
    • 2012-03-04
    • 2013-10-10
    • 2012-09-17
    • 2012-10-30
    相关资源
    最近更新 更多