Php 在zf2中测试restfull控制器时如何设置原始post内容?

Php 在zf2中测试restfull控制器时如何设置原始post内容?,php,unit-testing,zend-framework2,phpunit,Php,Unit Testing,Zend Framework2,Phpunit,我有一个这样的测试方法: public function testTaskPut() { $this->dispatch('/task/67', 'put'); $this->assertResponseStatusCode(200); $this->assertModuleName('Task'); $this->assertControllerName('Task\Controller\Task'); $this->ass

我有一个这样的测试方法:

public function testTaskPut()
{
    $this->dispatch('/task/67', 'put');
    $this->assertResponseStatusCode(200);
    $this->assertModuleName('Task');
    $this->assertControllerName('Task\Controller\Task');
    $this->assertControllerClass('TaskController');
    $this->assertMatchedRouteName('task');
}
现在我想设置原始帖子内容:

{"content":"example content"}
怎么做

我可以做如下事情:

$request = new Request();
$request->setContent(json_encode(array(
   'content' => 'example content'
)));

但是如何在dispatch()中使用
$request

我还没有测试过,但是ZF test controller类已经有一个用于分派的请求对象,因此您应该能够执行以下操作:

public function testTaskPut()
{
    $request = $this->getRequest();
    $request->setContent(json_encode(array(
       'content' => 'example content'
    )));

    $this->dispatch('/task/67', 'put');
    $this->assertResponseStatusCode(200);
    $this->assertModuleName('Task');
    $this->assertControllerName('Task\Controller\Task');
    $this->assertControllerClass('TaskController');
    $this->assertMatchedRouteName('task');
}

然后,当您调用
dispatch()
时,应该使用您的数据。

不幸的是,
dispatch()
没有使用我的数据:(