bwaine/facebook-test-user-client

此包的最新版本(dev-master)没有可用的许可证信息。

用于与 Facebook 测试用户 API 交互的 Guzzle 客户端

dev-master 2013-04-12 15:34 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:49:12 UTC


README

#Facebook 测试用户客户端

此工具为 Facebook 测试用户 API 提供了包装器。使用它,您可以读取、创建、更新和删除您应用程序的测试用户。您还可以在测试用户之间创建友谊。

虽然使用它需要了解底层 API(特别是传递给 CreateUser 方法的可选参数以及创建友谊所需的工作流程),但预计此客户端将用于您自己的测试框架以创建更高级别的交互。

##安装

客户端通过 composer 安装。

将以下内容添加到您的 composer.json 文件中。

{
    "require": {
        "bwaine/FacebookTestUserClient": "dev-master"
    }
}

运行 php composer.phar installphp composer.phar update

##使用方法

通过创建客户端实例并使用它创建 '命令' 来执行对 Facebook 测试用户 API 的操作。然后执行命令,并将响应自动解析为类似于 PHP 数组的格式。

<?php 

// include the clients namespace and alias it to Client.
use Bwaine\FacebookTestUserClient\Client;

// Create a client object
$client = Client::factory();

// Get the command you wish to use.

// In this example we use the client to obtain an 'app access_token'. This is
// then used in subsequent requests.  
// When getting a command the name of the command plus any arguments are passed
// to the clients 'getCommand' method. 

$authCommand = $this->object->getCommand('ObtainAppAccessToken', 
                array(
                 "client_id" => $yourAppId,
                 "client_secret" => $yourAppId)
                );

// Execute the command and save the response in a variable.

try {

    $response = $authCommand->execute();

} catch (\Exception $e) {
    // Bad / failed requests throw an exception. Handle this here or let it 
    // bubble up to your top level error handler. 
    throw $e;
}

##命令

所有命令在调用客户端的 'getCommand' 方法时都需要传递参数。下面将概述这些参数。

注意:如果命令需要 app_access_token,您必须通过使用 ObtainAppAccessToken 命令来获取一个。

###ObtainAppAccessToken

此方法允许您通过交换您的应用 ID 和密钥来获取应用访问令牌。令牌用于后续请求,代表您的应用程序创建和操作测试用户。

参数

| Name          | Explanation                                               | Required | 
| client_id     | Your application ID (obtained from the developer app)     | Y        |
| client_secret | Your application secret (obtained from the developer app) | Y        | 

响应

| Name          | Explanation                                                 | 
| access_token  | Used to make further calls to the Api on behalf of your app | 
        $appId = "YOUR ID GOES HERE";
        $appSecret = "YOUR SECRET GOES HERE";

        $client = Client::factory();

        $authCommand = $client->getCommand('ObtainAppAccessToken', array(
            "client_id" => $appId,
            "client_secret" => $appSecret));

        try {
            $response = $authCommand->execute();
        } catch (\Exception $e) {
            // Handle failed requests
        }
        
        // Save for future requests!
        $token = $response['access_token']

###CreateUser

此方法根据您的指定创建测试用户。

参数

| Name         | Explanation                                              | Required | 
| appId        | Your application ID (obtained from the developer app)    | Y        |
| access_token | An app access_token (see ObtainAppAccessToken)           | Y        |
| name         | The users name                                           | n        |
| permissions  | Comma seperated list of permissions the user has granted | n        |
| installed    | True / False - Has the user installed your app           | n        |
| locale       | The users locale                                         | n        |

响应

| Name         | Explanation                                                       | 
| id           | The users id                                                      | 
| access_token | A user access_token used to make Open Graph requests for the user | 
| login_url    | A url to allowing you to login as the user                        | 
| email        | The users email address                                           | 
| password     | The users password                                                | 
        
        // Access token from previous request. See Above.
        $token = $response['access_token'];
        $appId = "YOUR ID GOES HERE";

        $client = Client::factory();

        $command = $client>getCommand('CreateUser', array(
            "appId" => $this->appId,
            "access_token" => token,
            ));

        try {
            $user = $command->execute();
        } catch (\Exception $e) {
            // Handle failure here
        }

        $id = $user['id'];

###DeleteUser

此方法删除先前创建的测试用户。

参数

| Name         | Explanation                                              | Required | 
| testUserId   | An ID of a test user associated with your application    | Y        |
| access_token | An app access_token (see ObtainAppAccessToken)           | Y        |

响应

| Name         | Explanation            | 
| result       | Success (true / false) | 
        
        // Access token from previous request. See Above.
        $token = $response['access_token'];
        $appId = "YOUR ID GOES HERE";

        $client = Client::factory();

        // Delete the user.
        $deleteCommand = $client->getCommand('DeleteUser', array(
            'testUserId' => $testUserId,
            'access_token' => $token
                ));

        try {
            $response = $deleteCommand->execute();
        } catch (\Exception $e) {
            // Handle failure here.
        }
        
        $status = $response['result'];

###GetUsers

获取与您的应用程序关联的所有测试用户。包括访问令牌和其他数据。

参数

| Name         | Explanation                                              | Required | 
| appId        | Your application ID (obtained from the developer app)    | Y        |
| access_token | An app access_token (see ObtainAppAccessToken)           | Y        |

响应

| Name         | Explanation        | 
| users        | And array of users | 

响应 (['users'] - )

| Name         | Explanation                                              | 
| id           | An ID of a test user associated with your application    | 
| access_token | An app access_token (see ObtainAppAccessToken)           |
| login_url    | A url to allowing you to login as the user               | 
        
        // Access token from previous request. See Above.
        $token = $response['access_token'];
        $appId = "YOUR ID GOES HERE";

        $client = Client::factory();

        $command = $client->getCommand('GetUsers', array(
            "appId" => $appId,
            "access_token" => $token));

        try {
            $response = $command->execute();
        } catch (\Exception $e) {
            // Handle failure here.
        }

        foreach ($response['users'] as $user) {
            // Do things with users.
            $id = $user['id']; 
        }

###ConnectFriend

此命令允许您向另一个测试用户发送好友请求。它还用于代表测试用户回复请求。

首先,使用 user1 的 access_token 从用户1 向用户2 发送好友请求。然后使用相同的命令(用户 ID 顺序相反)和用户2 的 access_token 接受请求。以下是一个示例。

参数

| Name         | Explanation                                              | Required | 
| user1        | A test user id associated to the application             | Y        |
| user2        | A test user id associated to the application             | Y        |
| access_token | user1's access token.                                    | Y        |

注意:创建测试用户之间的友谊需要两个命令。在第一个请求中,首先传递 user1 的 ID,然后使用 user1 的 access_token。在第二个请求中,首先传递 user2 的 ID,然后使用 user2 的 access_token。

响应

| Name         | Explanation           | 
| result       | Success: true / false | 
        // Missing Code. Two users are created using the CreateUser command.
        // The result of each command is assigned to $user1 and $user2.

        $client = Client::factory();

        $connectCommand1 = $client->getCommand('ConnectFriend', array(
            "user1" => $user1['id'],
            "user2" => $user2['id'],
            "access_token" => $user1['access_token']
                ));

        $connectCommand2 = $client->getCommand('ConnectFriend', array(
            "user1" => $user2['id'],
            "user2" => $user1['id'],
            "access_token" => $user2['access_token']
                ));

        try {
            $resp1 = $connectCommand1->execute();
            $resp2 = $connectCommand2->execute();
        } catch (\Exception $e) {
            // Handle failure here.
        }
        
        $success = ($resp1['result'] && $resp2['result']);

##测试

此扩展包含一些集成风格测试。它们会对 Facebook API 进行实际调用,因此需要 appid 和 app 密钥。

要运行测试,请将 phpunit.dist.xml 复制到 phpunit.xml,然后将在所示位置插入您的 app ID 和密钥。

cp phpunit.dist.xml phpunit.xml
nano phpunit.xml // Edit file
phpunit

注意:测试是破坏性的。每次测试后,应用程序的测试用户都会被销毁。如果您在应用程序中有测试用户并需要保留它们,请不要对此套件运行您的应用程序。