Reply to Silex & phpunit JSON

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

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

This object expects client results to be xhtml, but my api, which I am trying to verify, returns JSON and the crawler does not allow this.

Is there a built-in class in Silex or phpunit that will work with JSON, or will I have to download it myself?

Greetings

+5
source share
2 answers

There is nothing special about working with json, but you can use it without using a crawler. Just call getResponse()the client to get an answer, for example:

$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]);

.

+22

JSON Symfony 2 Browser-Kit HTTP_ACCEPT ACCEPT HTTP_CONTENT_TYPE CONTENT_TYPE. POST $data:

$client->request(
    $method = 'POST',
    $uri,
    $parameters = array(),
    $files = array(),
    $server = array(
        'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8', // for sending urlencoded data
        //or 'HTTP_CONTENT_TYPE' => 'application/json', // for sending JSON data
        'HTTP_ACCEPT'       => 'application/json',      
    ),
    $content = $data,
    $changeHistory = true
);

$response = $client->getResponse();
$response_data = json_decode($response->getContent(), true);
0

All Articles