【问题标题】:Asserting POST data in a ZF2 controler test在 ZF2 控制器测试中断言 POST 数据
【发布时间】:2015-12-20 10:54:54
【问题描述】:

这是一个 PHPUnit 测试,用于实现 post DATA,用于 ZF2 控制器:

public function testEditActionPost()
    {
        $this->routeMatch->setParam('action', 'editaffaire');
        $this->routeMatch->setParam('idaffaire', '400');

        $data = array(
            'foo' => 'bar',
            'bar' => 'foo'
        );

        $this->request->setMethod('POST')
                      ->setPost($data);

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

        $this->assertEquals(200, $response->getStatusCode());
    }

但这不起作用... PHPUnit 向我发送一个错误:

1) MaintenanceTest\Controller\SitesControllerTest::testEditActionPost 传递给 Zend\Http\Request::setPost() 的参数 1 必须是一个实例 Zend\St dlib\ParametersInterface,给定数组

这是我的设置:

protected function setUp()
{
    $serviceManager = Bootstrap::getServiceManager();

    $this->controller = new SitesController();

    $this->request    = new Request();
    $this->routeMatch = new RouteMatch(array('controller' => 'index'));
    $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 editaffaireAction()
    {
        try {
            $iMaiAffaireId = $this->params('idaffaire');

            $oAffaire = $this->maiAffaireService->selectByIdOrCreate($iMaiAffaireId);

            $maiAffairesForm = new FMaiAffaireForm(
                $oAffaire
            );

            if ($this->getRequest()->isPost()) {
                $maiAffairesForm->setInputFilter($oAffaire->getInputFilter());
                $postData = $this->getRequest()->getPost();
                $maiAffairesForm->setData($postData);
                if ($maiAffairesForm->isValid()) {
                    $aData = $maiAffairesForm->getData();
                    $oAffaire->exchangeArray($aData);
                    $iMaiAffaireId = $this->maiAffaireService->save($oAffaire);
                }
            }

            $viewModel = new ViewModel([
                'oAffaire'        => $oAffaire
            ]);
            return $viewModel;
        } catch (\Exception $e) {
            throw new \Exception($e);
        }
    }

【问题讨论】:

    标签: php zend-framework2 phpunit


    【解决方案1】:

    您要传递给setPost() 的参数需要在Parameters 对象中:

    use Zend\Stdlib\Parameters;   
    //...
    $this->request->setMethod('POST')
        ->setPost(new Parameters($data));
    

    参见 Zend 2 的文档: http://framework.zend.com/manual/current/en/modules/zend.test.phpunit.html#testing-your-controllers-and-mvc-applications

    【讨论】:

    • 它可以工作,但我的表单无效,它显示“不在数组中”错误,为什么?
    • 我真的不明白......我的值在我的数组中,并在我的请求中发送!
    • 好吧,因为我不知道您的控制器代码,所以我无法帮助您解决这个问题 :-)
    • 好的,我明白了。我的元素是选择输入。如果我发送样本数据作为我的测试,这会向我发送一个错误“The input was not found in the haystack”,我将其更正为:'disable_inarray_validator' => true in my select ...
    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    相关资源
    最近更新 更多