Passed
Pull Request — master (#888)
by Tobias
01:50
created

testAFacebookBatchRequestWillProperlyBatchFiles()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
namespace Facebook\Tests;
25
26
use Facebook\Exception\SDKException;
27
use Facebook\Application;
28
use Facebook\Request;
29
use Facebook\BatchRequest;
30
use Facebook\Client;
31
use Facebook\FileUpload\File;
32
use Facebook\FileUpload\Video;
33
// These are needed when you uncomment the HTTP clients below.
34
use Facebook\HttpClients\FacebookCurlHttpClient;
0 ignored issues
show
Bug introduced by
The type Facebook\HttpClients\FacebookCurlHttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Facebook\HttpClients\FacebookGuzzleHttpClient;
0 ignored issues
show
Bug introduced by
The type Facebook\HttpClients\FacebookGuzzleHttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Facebook\HttpClients\FacebookStreamHttpClient;
0 ignored issues
show
Bug introduced by
The type Facebook\HttpClients\FacebookStreamHttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use Facebook\Tests\Fixtures\MyFooBatchHttpClient;
38
use Facebook\Tests\Fixtures\MyFooHttpClient;
39
use Facebook\Response;
40
use Facebook\BatchResponse;
41
use Facebook\GraphNode\GraphNode;
42
use Http\Client\HttpClient;
43
use PHPUnit\Framework\TestCase;
44
45
class ClientTest extends TestCase
46
{
47
    /**
48
     * @var Application
49
     */
50
    public $fbApp;
51
52
    /**
53
     * @var Client
54
     */
55
    public $fbClient;
56
57
    /**
58
     * @var Application
59
     */
60
    public static $testFacebookApp;
61
62
    /**
63
     * @var Client
64
     */
65
    public static $testFacebookClient;
66
67
    protected function setUp()
68
    {
69
        $this->fbApp = new Application('id', 'shhhh!');
70
        $this->fbClient = new Client(new MyFooHttpClient());
71
    }
72
73
    public function testACustomHttpClientCanBeInjected()
74
    {
75
        $handler = new MyFooHttpClient();
76
        $client = new Client($handler);
77
        $httpClient = $client->getHttpClient();
78
79
        $this->assertInstanceOf(MyFooHttpClient::class, $httpClient);
80
    }
81
82
    public function testTheHttpClientWillFallbackToDefault()
83
    {
84
        $client = new Client();
85
        $httpClient = $client->getHttpClient();
86
87
        $this->assertInstanceOf(HttpClient::class, $httpClient);
88
    }
89
90
    public function testBetaModeCanBeDisabledOrEnabledViaConstructor()
91
    {
92
        $client = new Client(null, false);
93
        $url = $client->getBaseGraphUrl();
94
        $this->assertEquals(Client::BASE_GRAPH_URL, $url);
95
96
        $client = new Client(null, true);
97
        $url = $client->getBaseGraphUrl();
98
        $this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url);
99
    }
100
101
    public function testBetaModeCanBeDisabledOrEnabledViaMethod()
102
    {
103
        $client = new Client();
104
        $client->enableBetaMode(false);
105
        $url = $client->getBaseGraphUrl();
106
        $this->assertEquals(Client::BASE_GRAPH_URL, $url);
107
108
        $client->enableBetaMode(true);
109
        $url = $client->getBaseGraphUrl();
110
        $this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url);
111
    }
112
113
    public function testGraphVideoUrlCanBeSet()
114
    {
115
        $client = new Client();
116
        $client->enableBetaMode(false);
117
        $url = $client->getBaseGraphUrl($postToVideoUrl = true);
118
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url);
119
120
        $client->enableBetaMode(true);
121
        $url = $client->getBaseGraphUrl($postToVideoUrl = true);
122
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL_BETA, $url);
123
    }
124
125
    public function testAFacebookRequestEntityCanBeUsedToSendARequestToGraph()
126
    {
127
        $fbRequest = new Request($this->fbApp, 'token', 'GET', '/foo');
128
        $response = $this->fbClient->sendRequest($fbRequest);
129
130
        $this->assertInstanceOf(Response::class, $response);
131
        $this->assertEquals(200, $response->getHttpStatusCode());
132
        $this->assertEquals('{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}', $response->getBody());
133
    }
134
135
    public function testAFacebookBatchRequestEntityCanBeUsedToSendABatchRequestToGraph()
136
    {
137
        $fbRequests = [
138
            new Request($this->fbApp, 'token', 'GET', '/foo'),
139
            new Request($this->fbApp, 'token', 'POST', '/bar'),
140
        ];
141
        $fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests);
142
143
        $fbBatchClient = new Client(new MyFooBatchHttpClient());
144
        $response = $fbBatchClient->sendBatchRequest($fbBatchRequest);
145
146
        $this->assertInstanceOf(BatchResponse::class, $response);
147
        $this->assertEquals('GET', $response[0]->getRequest()->getMethod());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        $this->assertEquals('GET', $response[0]->/** @scrutinizer ignore-call */ getRequest()->getMethod());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
        $this->assertEquals('POST', $response[1]->getRequest()->getMethod());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        $this->assertEquals('POST', $response[1]->/** @scrutinizer ignore-call */ getRequest()->getMethod());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
    }
150
151
    public function testAFacebookBatchRequestWillProperlyBatchFiles()
152
    {
153
        $fbRequests = [
154
            new Request($this->fbApp, 'token', 'POST', '/photo', [
155
                'message' => 'foobar',
156
                'source' => new File(__DIR__ . '/foo.txt'),
157
            ]),
158
            new Request($this->fbApp, 'token', 'POST', '/video', [
159
                'message' => 'foobar',
160
                'source' => new Video(__DIR__ . '/foo.txt'),
161
            ]),
162
        ];
163
        $fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests);
164
        $fbBatchRequest->prepareRequestsForBatch();
165
166
        list($url, $method, $headers, $body) = $this->fbClient->prepareRequestMessage($fbBatchRequest);
167
168
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url);
169
        $this->assertEquals('POST', $method);
170
        $this->assertContains('multipart/form-data; boundary=', $headers['Content-Type']);
171
        $this->assertContains('Content-Disposition: form-data; name="batch"', $body);
172
        $this->assertContains('Content-Disposition: form-data; name="include_headers"', $body);
173
        $this->assertContains('"name":0,"attached_files":', $body);
174
        $this->assertContains('"name":1,"attached_files":', $body);
175
        $this->assertContains('"; filename="foo.txt"', $body);
176
    }
177
178
    public function testARequestOfParamsWillBeUrlEncoded()
179
    {
180
        $fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['foo' => 'bar']);
181
        $response = $this->fbClient->sendRequest($fbRequest);
182
183
        $headersSent = $response->getRequest()->getHeaders();
184
185
        $this->assertEquals('application/x-www-form-urlencoded', $headersSent['Content-Type']);
186
    }
187
188
    public function testARequestWithFilesWillBeMultipart()
189
    {
190
        $myFile = new File(__DIR__ . '/foo.txt');
191
        $fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['file' => $myFile]);
192
        $response = $this->fbClient->sendRequest($fbRequest);
193
194
        $headersSent = $response->getRequest()->getHeaders();
195
196
        $this->assertContains('multipart/form-data; boundary=', $headersSent['Content-Type']);
197
    }
198
199
    /**
200
     * @expectedException \Facebook\Exception\SDKException
201
     */
202
    public function testAFacebookRequestValidatesTheAccessTokenWhenOneIsNotProvided()
203
    {
204
        $fbRequest = new Request($this->fbApp, null, 'GET', '/foo');
205
        $this->fbClient->sendRequest($fbRequest);
206
    }
207
208
    /**
209
     * @group integration
210
     */
211
    public function testCanCreateATestUserAndGetTheProfileAndThenDeleteTheTestUser()
212
    {
213
        $this->initializeTestApp();
214
215
        // Create a test user
216
        $testUserPath = '/' . FacebookTestCredentials::$appId . '/accounts/test-users';
0 ignored issues
show
Bug introduced by
The type Facebook\Tests\FacebookTestCredentials was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
217
        $params = [
218
            'installed' => true,
219
            'name' => 'Foo Phpunit User',
220
            'locale' => 'en_US',
221
            'permissions' => implode(',', ['read_stream', 'user_photos']),
222
        ];
223
224
        $request = new Request(
225
            static::$testFacebookApp,
226
            static::$testFacebookApp->getAccessToken(),
227
            'POST',
228
            $testUserPath,
229
            $params
230
        );
231
        $response = static::$testFacebookClient->sendRequest($request)->getGraphNode();
232
233
        $testUserId = $response->getField('id');
234
        $testUserAccessToken = $response->getField('access_token');
235
236
        // Get the test user's profile
237
        $request = new Request(
238
            static::$testFacebookApp,
239
            $testUserAccessToken,
240
            'GET',
241
            '/me'
242
        );
243
        $graphNode = static::$testFacebookClient->sendRequest($request)->getGraphNode();
244
245
        $this->assertInstanceOf(GraphNode::class, $graphNode);
246
        $this->assertNotNull($graphNode->getField('id'));
247
        $this->assertEquals('Foo Phpunit User', $graphNode->getField('name'));
248
249
        // Delete test user
250
        $request = new Request(
251
            static::$testFacebookApp,
252
            static::$testFacebookApp->getAccessToken(),
253
            'DELETE',
254
            '/' . $testUserId
255
        );
256
        $graphNode = static::$testFacebookClient->sendRequest($request)->getGraphNode();
257
258
        $this->assertTrue($graphNode->getField('success'));
259
    }
260
261
    public function initializeTestApp()
262
    {
263
        if (!file_exists(__DIR__ . '/FacebookTestCredentials.php')) {
264
            throw new SDKException(
265
                'You must create a FacebookTestCredentials.php file from FacebookTestCredentials.php.dist'
266
            );
267
        }
268
269
        if (!strlen(FacebookTestCredentials::$appId) ||
270
            !strlen(FacebookTestCredentials::$appSecret)
271
        ) {
272
            throw new SDKException(
273
                'You must fill out FacebookTestCredentials.php'
274
            );
275
        }
276
        static::$testFacebookApp = new Application(
277
            FacebookTestCredentials::$appId,
278
            FacebookTestCredentials::$appSecret
279
        );
280
281
        static::$testFacebookClient = new Client();
282
    }
283
}
284