Symfony web test case JSON
After you do $client->request(...)
call, you can do $client->getResponse()
to get server response.
You can then assert status code and check it's contents, for example:
$client->request('GET', '/my-url');$response = $client->getResponse();$this->assertSame(200, $response->getStatusCode());$responseData = json_decode($response->getContent(), true);// etc...
There is a PHPUnit\Framework\Assert::assertJson() method since this commit You can also test 'Content-Type' of response.
$response = $client->getResponse();$this->assertTrue($response->headers->contains('Content-Type', 'application/json'));$this->assertJson($response->getContent());$responseData = json_decode($response->getContent(), true);
The willdurand/rest-extra-bundle bundle provides additional helpers to test JSON. To test equality there is already a built-in assertion for this purpose:
use Bazinga\Bundle\RestExtraBundle\Test\WebTestCase as BazingaWebTestCase;// ...$client->request('GET', '/my-url');$response = $client->getResponse();$this->assertJsonResponse($response, Response::HTTP_OK);$this->assertJsonStringEqualsJsonString($expectedJson, $response);
Note that the assertJsonStringEqualsJsonString
assertion will take in charge the normalization of both $expectedJson
and $response
strings.