在 ZF2 控制器测试中断言 POST 数据
Asserting POST data in a ZF2 controler test
这是针对 ZF2 控制器的 post DATA 实施的 PHPUnit 测试:
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
Argument 1 passed to Zend\Http\Request::setPost() must be an instance
of Zend\St dlib\ParametersInterface, array given
这是我的设置:
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);
}
}
要传递给 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
这是针对 ZF2 控制器的 post DATA 实施的 PHPUnit 测试:
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 Argument 1 passed to Zend\Http\Request::setPost() must be an instance of Zend\St dlib\ParametersInterface, array given
这是我的设置:
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);
}
}
要传递给 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