symfony2 使用 POST 数据测试客户端请求


symfony2 test client request with POST data

我想在symfony webapp上测试Facebook的登录。

我已经创建了一个测试用户并为此用户请求了访问令牌,现在我想发送以下 POST 请求:

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()));
}

并在调用 URI 方法符号下方

/**
 * 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)
{

你应该在方法符号后面的第三个参数中使用post参数:

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

并按如下方式调用方法:

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