如何将 Lumen 响应内容获取为字符串?

How to get Lumen response content as string?

我正在尝试的是从带有 Lumen 响应的单元测试中获取响应内容作为字符串:

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() 方法。

我只需要获取响应内容即可

$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

}