Ric*_*ing 8 plugins phpunit unit-testing zend-framework2
TLDR:
如何为FilePostRedirectGet表单提交请求编写集成测试?
在我的应用程序中,我有一个多页面的表单,里面有一个文件元素.最初控制器代码是(伪代码 - 删除所有多页面内容):
$form = new MyForm();
if($this->getRequest()->isPost()) {
if($form->isValid($this->getRequest()->getPost()) {
//move file to new home and save post to database.
return $this->redirect()->toRoute('admin/cms/edit');
}
}
return new ViewModel(['form' => $form]);
Run Code Online (Sandbox Code Playgroud)
基本上,如果表单有效,它将保存表单详细信息并执行重定向(通常是页面的编辑版本).如果表单无效,它将仅使用相应的错误消息再次显示该表单.
这种方法的问题是,如果其中一个表单元素未通过验证,则文件输入将失去其值,用户将需要重新上载其文件.为了解决这个问题,我改为FPRG方法:
$form = new MyForm();
/* @var $prg FilePostRedirectGet */
$prg = $this->filePostRedirectGet($form, $this->url()->fromRoute($this->routeMatch, ['action' => $this->action, 'id' => $id], ['query' => ['page' => $page]]), true);
if ($prg instanceof Response) {
return $prg; // Return PRG redirect response
} elseif ($prg !== false) {
//move file to new home and save post to database.
return $this->redirect()->toRoute('admin/cms/edit');
}
return new ViewModel(['form' => $form]);
Run Code Online (Sandbox Code Playgroud)
该FilePostRedirectGet插件将发布数据保存在会话中,重定向到同一页面,验证表单,如果有效,则重定向到成功/编辑页面 - 否则只显示表单有错误.这种方法的好处是文件输入保留其值,而不管其他任何失败的元素.
问题是,如何为此请求编写集成测试?
最初,只有一个重定向(成功),所以我可以测试(模型嘲笑的东西为简洁而删除):
/**
* Redirect to next page after form submission.
*/
public function testAddActionRedirectsAfterValidPost() {
$postData = $this->_postData();
$this->dispatch('http://localhost/admin/cms/add', 'POST', $postData);
$this->assertResponseStatusCode(302);
$this->assertRedirectTo('http://localhost/admin/cms/edit/13');
return $postData;
}
Run Code Online (Sandbox Code Playgroud)
但是,由于多重重定向,如果请求成功与否,我无法使用此测试方法.
如何为此请求编写集成测试?
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |