Completed
Pull Request — master (#594)
by Andreas
06:19 queued 03:09
created

FacebookTest::testCanGetSuccessfulTransferWithMaxTries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 2
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 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\Facebook;
27
use Facebook\FacebookClient;
28
use Facebook\FacebookRequest;
29
use Facebook\Authentication\AccessToken;
30
use Facebook\GraphNodes\GraphEdge;
31
use Facebook\Tests\FakeGraphApi\FakeGraphApiForResumableUpload;
32
33
class FacebookTest extends \PHPUnit_Framework_TestCase
34
{
35
    protected $config = [
36
        'app_id' => '1337',
37
        'app_secret' => 'foo_secret',
38
    ];
39
40
    /**
41
     * @expectedException \Facebook\Exceptions\FacebookSDKException
42
     */
43
    public function testInstantiatingWithoutAppIdThrows()
44
    {
45
        // unset value so there is no fallback to test expected Exception
46
        putenv(Facebook::APP_ID_ENV_NAME.'=');
47
        $config = [
48
            'app_secret' => 'foo_secret',
49
        ];
50
        new Facebook($config);
51
    }
52
53
    /**
54
     * @expectedException \Facebook\Exceptions\FacebookSDKException
55
     */
56
    public function testInstantiatingWithoutAppSecretThrows()
57
    {
58
        // unset value so there is no fallback to test expected Exception
59
        putenv(Facebook::APP_SECRET_ENV_NAME.'=');
60
        $config = [
61
            'app_id' => 'foo_id',
62
        ];
63
        new Facebook($config);
64
    }
65
66
    /**
67
     * @expectedException \InvalidArgumentException
68
     */
69
    public function testSettingAnInvalidHttpClientHandlerThrows()
70
    {
71
        $config = array_merge($this->config, [
72
            'http_client_handler' => 'foo_handler',
73
        ]);
74
        new Facebook($config);
75
    }
76
77
    public function testCurlHttpClientHandlerCanBeForced()
78
    {
79
        if (!extension_loaded('curl')) {
80
            $this->markTestSkipped('cURL must be installed to test cURL client handler.');
81
        }
82
        $config = array_merge($this->config, [
83
            'http_client_handler' => 'curl'
84
        ]);
85
        $fb = new Facebook($config);
86
        $this->assertInstanceOf(
87
            'Facebook\HttpClients\FacebookCurlHttpClient',
88
            $fb->getClient()->getHttpClientHandler()
89
        );
90
    }
91
92 View Code Duplication
    public function testStreamHttpClientHandlerCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $config = array_merge($this->config, [
95
            'http_client_handler' => 'stream'
96
        ]);
97
        $fb = new Facebook($config);
98
        $this->assertInstanceOf(
99
            'Facebook\HttpClients\FacebookStreamHttpClient',
100
            $fb->getClient()->getHttpClientHandler()
101
        );
102
    }
103
104 View Code Duplication
    public function testGuzzleHttpClientHandlerCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $config = array_merge($this->config, [
107
            'http_client_handler' => 'guzzle'
108
        ]);
109
        $fb = new Facebook($config);
110
        $this->assertInstanceOf(
111
            'Facebook\HttpClients\FacebookGuzzleHttpClient',
112
            $fb->getClient()->getHttpClientHandler()
113
        );
114
    }
115
116
    /**
117
     * @expectedException \InvalidArgumentException
118
     */
119
    public function testSettingAnInvalidPersistentDataHandlerThrows()
120
    {
121
        $config = array_merge($this->config, [
122
            'persistent_data_handler' => 'foo_handler',
123
        ]);
124
        new Facebook($config);
125
    }
126
127 View Code Duplication
    public function testPersistentDataHandlerCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $config = array_merge($this->config, [
130
            'persistent_data_handler' => 'memory'
131
        ]);
132
        $fb = new Facebook($config);
133
        $this->assertInstanceOf(
134
            'Facebook\PersistentData\FacebookMemoryPersistentDataHandler',
135
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
136
        );
137
    }
138
139
    public function testSettingAnInvalidUrlHandlerThrows()
140
    {
141
        $expectedException = (PHP_MAJOR_VERSION > 5 && class_exists('TypeError'))
142
            ? 'TypeError'
143
            : 'PHPUnit_Framework_Error';
144
145
        $this->setExpectedException($expectedException);
146
147
        $config = array_merge($this->config, [
148
            'url_detection_handler' => 'foo_handler',
149
        ]);
150
        new Facebook($config);
151
    }
152
153
    public function testTheUrlHandlerWillDefaultToTheFacebookImplementation()
154
    {
155
        $fb = new Facebook($this->config);
156
        $this->assertInstanceOf('Facebook\Url\FacebookUrlDetectionHandler', $fb->getUrlDetectionHandler());
157
    }
158
159 View Code Duplication
    public function testAnAccessTokenCanBeSetAsAString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $fb = new Facebook($this->config);
162
        $fb->setDefaultAccessToken('foo_token');
163
        $accessToken = $fb->getDefaultAccessToken();
164
165
        $this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken);
166
        $this->assertEquals('foo_token', (string)$accessToken);
167
    }
168
169 View Code Duplication
    public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $fb = new Facebook($this->config);
172
        $fb->setDefaultAccessToken(new AccessToken('bar_token'));
173
        $accessToken = $fb->getDefaultAccessToken();
174
175
        $this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken);
176
        $this->assertEquals('bar_token', (string)$accessToken);
177
    }
178
179
    /**
180
     * @expectedException \InvalidArgumentException
181
     */
182
    public function testSettingAnInvalidPseudoRandomStringGeneratorThrows()
183
    {
184
        $config = array_merge($this->config, [
185
            'pseudo_random_string_generator' => 'foo_generator',
186
        ]);
187
        new Facebook($config);
188
    }
189
190 View Code Duplication
    public function testMcryptCsprgCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        if (!function_exists('mcrypt_create_iv')) {
193
            $this->markTestSkipped(
194
                'Mcrypt must be installed to test mcrypt_create_iv().'
195
            );
196
        }
197
198
        $config = array_merge($this->config, [
199
            'persistent_data_handler' => 'memory', // To keep session errors from happening
200
            'pseudo_random_string_generator' => 'mcrypt'
201
        ]);
202
        $fb = new Facebook($config);
203
        $this->assertInstanceOf(
204
            'Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator',
205
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
206
        );
207
    }
208
209 View Code Duplication
    public function testOpenSslCsprgCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211
        if (!function_exists('openssl_random_pseudo_bytes')) {
212
            $this->markTestSkipped(
213
                'The OpenSSL extension must be enabled to test openssl_random_pseudo_bytes().'
214
            );
215
        }
216
217
        $config = array_merge($this->config, [
218
            'persistent_data_handler' => 'memory', // To keep session errors from happening
219
            'pseudo_random_string_generator' => 'openssl'
220
        ]);
221
        $fb = new Facebook($config);
222
        $this->assertInstanceOf(
223
            'Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator',
224
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
225
        );
226
    }
227
228 View Code Duplication
    public function testUrandomCsprgCanBeForced()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
    {
230
        if (ini_get('open_basedir')) {
231
            $this->markTestSkipped(
232
                'Cannot test /dev/urandom generator due to open_basedir constraint.'
233
            );
234
        }
235
236
        if (!is_readable('/dev/urandom')) {
237
            $this->markTestSkipped(
238
                '/dev/urandom not found or is not readable.'
239
            );
240
        }
241
242
        $config = array_merge($this->config, [
243
            'persistent_data_handler' => 'memory', // To keep session errors from happening
244
            'pseudo_random_string_generator' => 'urandom'
245
        ]);
246
        $fb = new Facebook($config);
247
        $this->assertInstanceOf(
248
            'Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator',
249
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
250
        );
251
    }
252
253
    /**
254
     * @expectedException \InvalidArgumentException
255
     */
256
    public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows()
257
    {
258
        $config = array_merge($this->config, [
259
            'default_access_token' => 123,
260
        ]);
261
        new Facebook($config);
262
    }
263
264
    public function testCreatingANewRequestWillDefaultToTheProperConfig()
265
    {
266
        $config = array_merge($this->config, [
267
            'default_access_token' => 'foo_token',
268
            'enable_beta_mode' => true,
269
            'default_graph_version' => 'v1337',
270
        ]);
271
        $fb = new Facebook($config);
272
273
        $request = $fb->request('FOO_VERB', '/foo');
274
        $this->assertEquals('1337', $request->getApp()->getId());
275
        $this->assertEquals('foo_secret', $request->getApp()->getSecret());
276
        $this->assertEquals('foo_token', (string)$request->getAccessToken());
277
        $this->assertEquals('v1337', $request->getGraphVersion());
278
        $this->assertEquals(
279
            FacebookClient::BASE_GRAPH_URL_BETA,
280
            $fb->getClient()->getBaseGraphUrl()
281
        );
282
    }
283
284
    public function testCanInjectCustomHandlers()
285
    {
286
        $config = array_merge($this->config, [
287
            'http_client_handler' => new FooClientInterface(),
288
            'persistent_data_handler' => new FooPersistentDataInterface(),
289
            'url_detection_handler' => new FooUrlDetectionInterface(),
290
            'pseudo_random_string_generator' => new FooBarPseudoRandomStringGenerator(),
291
        ]);
292
        $fb = new Facebook($config);
293
294
        $this->assertInstanceOf(
295
            'Facebook\Tests\FooClientInterface',
296
            $fb->getClient()->getHttpClientHandler()
297
        );
298
        $this->assertInstanceOf(
299
            'Facebook\Tests\FooPersistentDataInterface',
300
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
301
        );
302
        $this->assertInstanceOf(
303
            'Facebook\Tests\FooUrlDetectionInterface',
304
            $fb->getRedirectLoginHelper()->getUrlDetectionHandler()
305
        );
306
        $this->assertInstanceOf(
307
            'Facebook\Tests\FooBarPseudoRandomStringGenerator',
308
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
309
        );
310
    }
311
312
    public function testPaginationReturnsProperResponse()
313
    {
314
        $config = array_merge($this->config, [
315
            'http_client_handler' => new FooClientInterface(),
316
        ]);
317
        $fb = new Facebook($config);
318
319
        $request = new FacebookRequest($fb->getApp(), 'foo_token', 'GET');
320
        $graphEdge = new GraphEdge(
321
            $request,
322
            [],
323
            [
324
                'paging' => [
325
                    'cursors' => [
326
                        'after' => 'bar_after_cursor',
327
                        'before' => 'bar_before_cursor',
328
                    ],
329
                    'previous' => 'previous_url',
330
                    'next' => 'next_url',
331
                ]
332
            ],
333
            '/1337/photos',
334
            '\Facebook\GraphNodes\GraphUser'
335
        );
336
337
        $nextPage = $fb->next($graphEdge);
338
        $this->assertInstanceOf('Facebook\GraphNodes\GraphEdge', $nextPage);
339
        $this->assertInstanceOf('Facebook\GraphNodes\GraphUser', $nextPage[0]);
340
        $this->assertEquals('Foo', $nextPage[0]['name']);
341
342
        $lastResponse = $fb->getLastResponse();
343
        $this->assertInstanceOf('Facebook\FacebookResponse', $lastResponse);
344
        $this->assertEquals(1337, $lastResponse->getHttpStatusCode());
345
    }
346
347
    public function testCanGetSuccessfulTransferWithMaxTries()
348
    {
349
        $config = array_merge($this->config, [
350
          'http_client_handler' => new FakeGraphApiForResumableUpload(),
351
        ]);
352
        $fb = new Facebook($config);
353
        $response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3);
354
        $this->assertEquals([
355
          'video_id' => '1337',
356
          'success' => true,
357
        ], $response);
358
    }
359
360
    /**
361
     * @expectedException \Facebook\Exceptions\FacebookResponseException
362
     */
363
    public function testMaxingOutRetriesWillThrow()
364
    {
365
        $client = new FakeGraphApiForResumableUpload();
366
        $client->failOnTransfer();
367
368
        $config = array_merge($this->config, [
369
          'http_client_handler' => $client,
370
        ]);
371
        $fb = new Facebook($config);
372
        $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3);
373
    }
374
}
375