Zend framework2 使用Zend Framework 2在使用put方法时如何将参数附加到请求对象?
我正在尝试使用phpunit测试REST更新方法。我创建一个请求对象,匹配一条路由,并将其分派给控制器:Zend framework2 使用Zend Framework 2在使用put方法时如何将参数附加到请求对象?,zend-framework2,Zend Framework2,我正在尝试使用phpunit测试REST更新方法。我创建一个请求对象,匹配一条路由,并将其分派给控制器: $this->routeMatch->setParam('id', '1'); $this->request->setMethod(Request::METHOD_PUT); $result = $this->controller->dispatch($this->request); 一切正常,但我遇到的问题是如何通过此请求传递数据?我尝试过通过p
$this->routeMatch->setParam('id', '1');
$this->request->setMethod(Request::METHOD_PUT);
$result = $this->controller->dispatch($this->request);
一切正常,但我遇到的问题是如何通过此请求传递数据?我尝试过通过post或get方法附加,但没有成功:
$this->request->getQuery()->set('description', 'foo'); //Nope
$this->request->getPost()->set('description', 'foo'); //Nope
我没有看到->getPut()方法。。。如何向使用put方法的请求对象添加数据?我没有看到任何参数通过我的控制器,除了匹配的路由id
更新:
//Create mocks:
$statusMock=new Status();
$statusMock->exchangeArray($testData);
$statusTableMock = $this->getMockBuilder('Status\Model\StatusTable')
->disableOriginalConstructor()
->getMock();
$statusTableMock->expects($this->any())
->method("getStatus")
->will($this->returnValue($statusMock));
$statusTableMock->expects($this->once())
->method('updateStatus')
->will($this->returnValue(array()));
//pass mocks to service manager, will use mocks instead of actual model
$serviceManager = Bootstrap::getServiceManager();
$serviceManager->setAllowOverride(true);
$serviceManager->setService('Status\Model\StatusTable', $statusTableMock);
//set route and request object
$this->routeMatch->setParam('id', '1');
$this->request->setMethod(Request::METHOD_PUT);
//try to attach data
$this->request->getQuery()->set('description', 'foo'); //NOPE!
$this->request->getPost()->set('title', 'bar'); //NOPE!
//Send and test
$result = $this->controller->dispatch($this->request);
$this->assertResponseStatusCode(200);
更新2
这项工作:
$ curl -i -H "Accept: application/json" -X PUT -d "title=something&description=something else" http://mysite.com
那么如何使用Zend\Http\Request对象实现这一点呢?不太熟悉zf2中的RESTful内容,但我认为对于PUT,它读取请求正文,因此应该使用
setContent()
确保还将内容类型设置为application/json。我为此挂了几个小时的电话:
$this->request->setMethod(Request::METHOD_PUT)
->setContent("title=something&description=something else");