Themodem Themodem - 9 months ago 41
JSON Question

Silex & phpunit JSON response

I am trying to write some tests for Silex using phpunit.

I have a class Symfony\Component\BrowserKit\Client that generates a Crawler object.

This object expects the results of the client to be xhtml however my api that I am trying to test returns JSON and the crawler does not allow this.

Is there a built in class in either Silex or phpunit that will work with JSON or will I have to roll my own?



There is nothing special for dealing with json, but you can use the client without using the crawler. Simply call getResponse() on the client to get the response, like this:

$client = $this->createClient();
$client->request('GET', '/');
$response = $client->getResponse();

$data = json_decode($response->getContent(), true);
$this->assertSame(array('id' => 1, 'name' => 'igorw'), $data['users'][0]);

I suggest you move this logic into a helper method on the test case and use that.