Last Updated: February 25, 2016
·
2.121K
· kraeuschen

Unittest Restful Api with Symfony 2

For sending an JSON Request in unittesting your symfony 2 controller/routes use HTTPACCEPT instead of ACCEPT and HTTPCONTENTTYPE insted of CONTENTTYPE.

/**
 * @param string $method
 * @param string $uri
 * @return \Symfony\Component\BrowserKit\Response
 */
protected function sendJsonRequest($method, $uri)
{
    $server = array(
        'HTTP_CONTENT_TYPE' => 'application/json',
        'HTTP_ACCEPT'       => 'application/json',
    );

    $client = static::createClient();
    $client->request($method, $uri, array(), array(), $server);
    return $client->getResponse();
}