phpsymfonyfunctional-testingsymfony-http-client

symfony2 test client request with POST data


I want to test login with facebook on a symfony webapp.

I've created a test user and requested an access token for this user, now I want to send the below POST request:

public function setup()
{
    $this->access_token = 'CAAL0aG1XXXXXX...';
    $this->email = 'test....@tfbnw.net';
    $this->uid = '105.......';
    $this->formData =json_encode(
        array(
            'authResponse' => array(
                'access_token' => $this->access_token,
                'expiresIn' => 5418,
                'userID' => $this->uid
            ),
            'status' => 'connected'));
}

public function testFacebookConnection()
{
    $client = static::createClient();
    // Submit a raw JSON string in the request body
    $client->request(
        'POST',
        $client->getContainer()->get('router')->generate('oauth_connect'),
        array(),
        array(),
        array('CONTENT_TYPE' => 'application/json'),
        '{ "service":"facebook","authentication" : "'. $this->formData.'"}'
    );
    die(var_dump($client->getResponse()->getContent()));
}

and below the call URI method signuature

/**
 * Calls a URI.
 *
 * @param string $method        The request method
 * @param string $uri           The URI to fetch
 * @param array  $parameters    The Request parameters
 * @param array  $files         The files
 * @param array  $server        The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
 * @param string $content       The raw body data
 * @param bool   $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
 *
 * @return Crawler
 */
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
{

Solution

  • you should use post params in the third argument following the method signuature :

    $this->postData =
            array(
                'service' => 'facebook',
                'authentication'=> $this->formData,
                );
    

    and you call method as following:

     $client->request(
                'POST',
                $client->getContainer()->get('router')->generate('oauth_connect'),
                $this->postData,
                array(),
                array('CONTENT_TYPE' => 'application/json')
            );