【问题标题】:How to get Lumen response content as string?如何将流明响应内容作为字符串获取?
【发布时间】:2019-03-10 09:50:39
【问题描述】:

我正在尝试将响应内容作为字符串从具有流明响应的单元测试中获取:

class MovieQueryTest extends TestCase
{
    use DatabaseMigrations;

    public function testCanSearch()
    {
        Movie::create([
            'name' => 'Fast & Furious 8',
            'alias' => 'Fast and Furious 8',
            'year' => 2016
        ]);

        $response = $this->post('/graphql', [
            'query' => '{movies(search: "Fast & Furious"){data{name}}}'
        ]);

        $response->getContent(); // Error: Call to undefined method MovieQueryTest::getContent()
        $response->getOriginalContent(); // Error: Call to undefined method MovieQueryTest::getOriginalContent()
        $response->content; // ErrorException: Undefined property: MovieQueryTest::$content
    }
}

但我无法弄清楚如何获取响应内容。

我不想使用 Lumen TestCase->seeJson() 方法。

我只需要获取响应内容。

【问题讨论】:

    标签: php api phpunit response lumen


    【解决方案1】:

    $response 还包含一个response 字段,您需要在该字段上调用getContent(),因此您首先需要提取该字段,然后调用getContent(),因此在您的代码中将变为:

    public function testCanSearch()
        {
            Movie::create([
                'name' => 'Fast & Furious 8',
                'alias' => 'Fast and Furious 8',
                'year' => 2016
            ]);
    
            $response = $this->post('/graphql', [
                'query' => '{movies(search: "Fast & Furious"){data{name}}}'
            ]);
    
            $extractedResponse = $response->response; // Extract the response object
            $responseContent = $extractedResponse->getContent(); // Extract the content from the response object
    
            $responseContent = $response->response->getContent(); // Alternative one-liner
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多