简体   繁体   English

phpunit和http内容类型

[英]phpunit and http content-type

I have an API built in Laravel (Dingo) and it works perfectly. 我有一个用Laravel(Dingo)构建的API,它运行得很好。 However I have a problem with implementing phpunit to unit test my API 但是我在实现phpunit来测试我的API时遇到了问题

class ProductControllerTest extends TestCase
{
    public function testInsertProductCase()
    {
        $data = array(
            , "description" => "Expensive Pen"
            , "price" => 100
        );

        $server = array();                        
        $this->be($this->apiUser);
        $this->response = $this->call('POST', '/products', [], [], $server, json_encode($data));
        $this->assertTrue($this->response->isOk());
        $this->assertJson($this->response->getContent());
    }

}

meanwhile my API endpoint points to this controller function 同时我的API端点指向此控制器功能

private function store()
{

    // This always returns null
    $shortInput = Input::only("price");
    $rules = [
            "price" => ["required"]
    ];
    $validator = Validator::make($shortInput, $rules);

    // the code continues
    ...
}

However it always fail, because the API can't recognise the payload. 但是它总是失败,因为API无法识别有效负载。 Input::getContent() returns the JSON but Input::only() returns blank. Input :: getContent()返回JSON,但Input :: only()返回空白。 Further investigation this is because Input::only() only returns value if the content type of the request payload is on JSON 进一步调查这是因为如果请求有效负载的内容类型是JSON,则Input :: only()仅返回值

So ... how do I set my phpunit code above to use content-type application/json ? 那么......如何设置上面的phpunit代码以使用内容类型的application / json? I am assuming it must have something to do with $server but I don't know what 我假设它必须与$server但我不知道是什么

Edit: There are actually 2 problems with my original thinking 编辑:我原来的想法实际上有两个问题

  1. Input::getContent() works because I fill the sixth parameter but Input::only() doesn't work because i didn't fill the third parameter. Input :: getContent()有效,因为我填充了第六个参数但是Input :: only()不起作用,因为我没有填充第三个参数。 Thanks to @shaddy 感谢@shaddy
  2. How to set content-type in phpunit request header is still unanswered 如何在phpunit请求标头中设置内容类型仍然没有答案

Thanks heaps 谢谢堆

The third parameter of the call function must be the parameters you send to the controller as input parameters - in your case the data parameter. 调用函数的第三个参数必须是您作为输入参数发送到控制器的参数 - 在您的情况下是数据参数。

$response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content); $ response = $ this-> call($ method,$ uri,$ parameters,$ cookies,$ files,$ server,$ content);

Changing your code like the example below should work (you do not have to json_encode the array): 像下面的例子一样更改你的代码应该有效(你不必对数组进行json_encode):

$this->response = $this->call('POST', '/products', $data);

In Laravel 5.4 and above, you can verify the response of headers like the Content-Type like so ( docs ): 在Laravel 5.4及更高版本中,您可以验证标题的响应,如内容类型,如此( docs ):

$this->response->assertHeader('content-type', 'application/json');

or for Laravel 5.3 and below ( docs ): 或者对于Laravel 5.3及以下版本( docs ):

$this->assertEquals('application/json', $response->headers->get('Content-Type'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM