这是我的单元测试的创建功能:Zendframework 2中的Restful控制器 - 它的参数是如何工作的?
public function testCreate() {
$this->routeMatch->setMatchedRouteName('restful');
$this->request->setMethod('POST')
->setContent('name=A');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals(403, $response->getStatusCode());
$this->assertArrayHasKey('id', $result);
}
这是我的函数:
public function create($data) {
if (empty($data)) {
$this->response->setStatusCode(400);
return;
}
for ($i = 0; $i < count(self::$ideas); $i++) {
if (self::$ideas[$i]['name'] == $data['name']) {
$this->response->setStatusCode(404);
return;
}
}
//@todo: secure the API
self::$index++;
$tmpArray = array('id'=>self::$index, 'name'=>$data['name']);
$this->response->setStatusCode(403);
}
但似乎$的数据始终是空白。在编写单元测试部分我错了吗? 当我尝试使用卷曲POST与-d
时,$数据具有我通过curl
发布的值。我很困惑这里有什么不对?
感谢您的阅读,并期待您的答案:)
回答
我已经与我的成功的单元测试http://pastebin.com/fwFe0Mi3
上前欲了解更多信息,我用this module实现宁静的控制器
你能否粘贴你的路线配置? –
@DanielM我使用#scaraveous模块:https://github.com/scaraveos/ZF2-Restful-Module-Skeleton – nXqd