Symfony web测试用例JSON

Gas*_*sim 14 json functional-testing symfony

如何在API中再次运行webtestcase?有关功能测试的默认指南仅提供以下命令:

$client = static::createClient();
$crawler = $client->request('GET', '/some-url');
Run Code Online (Sandbox Code Playgroud)

Crawler类是一个DOM爬虫.我检查了FrameworkBundle\Client类的引用,但是找不到允许我发出返回原始Response的请求的方法.至少这样,我将能够json_decode输出并进行我的测试.

我可以用什么来实现这个目标?

Igo*_*vić 21

你以后$client->request(...)叫,你可以做$client->getResponse()得到服务器的响应.

然后,您可以断言状态代码并检查其内容,例如:

$client->request('GET', '/my-url');
$response = $client->getResponse();
$this->assertSame(200, $response->getStatusCode());
$responseData = json_decode($response->getContent(), true);
// etc...
Run Code Online (Sandbox Code Playgroud)


Arn*_*rno 9

自此提交以来,有一种PHPUnit\Framework\Assert::assertJson()方法 您还可以测试响应标头。Content-Type

$response = $client->getResponse();
$this->assertTrue($response->headers->contains('Content-Type', 'application/json'));
$this->assertJson($response->getContent());
$responseData = json_decode($response->getContent(), true);
Run Code Online (Sandbox Code Playgroud)