Completed
Pull Request — master (#629)
by
unknown
03:06
created

FacebookTest::testLoadPolyfills()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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\Fixtures\FakeGraphApiForResumableUpload;
32
use Facebook\Tests\Fixtures\FooBarPseudoRandomStringGenerator;
33
use Facebook\Tests\Fixtures\FooClientInterface;
34
use Facebook\Tests\Fixtures\FooPersistentDataInterface;
35
use Facebook\Tests\Fixtures\FooUrlDetectionInterface;
36
37
class FacebookTest extends \PHPUnit_Framework_TestCase
38
{
39
    protected $config = [
40
        'app_id' => '1337',
41
        'app_secret' => 'foo_secret',
42
    ];
43
44
    /**
45
     * @expectedException \Facebook\Exceptions\FacebookSDKException
46
     */
47
    public function testInstantiatingWithoutAppIdThrows()
48
    {
49
        // unset value so there is no fallback to test expected Exception
50
        putenv(Facebook::APP_ID_ENV_NAME.'=');
51
        $config = [
52
            'app_secret' => 'foo_secret',
53
        ];
54
        new Facebook($config);
55
    }
56
57
    /**
58
     * @expectedException \Facebook\Exceptions\FacebookSDKException
59
     */
60
    public function testInstantiatingWithoutAppSecretThrows()
61
    {
62
        // unset value so there is no fallback to test expected Exception
63
        putenv(Facebook::APP_SECRET_ENV_NAME.'=');
64
        $config = [
65
            'app_id' => 'foo_id',
66
        ];
67
        new Facebook($config);
68
    }
69
70
    /**
71
     * @expectedException \InvalidArgumentException
72
     */
73
    public function testSettingAnInvalidHttpClientHandlerThrows()
74
    {
75
        $config = array_merge($this->config, [
76
            'http_client_handler' => 'foo_handler',
77
        ]);
78
        new Facebook($config);
79
    }
80
81
    public function testCurlHttpClientHandlerCanBeForced()
82
    {
83
        if (!extension_loaded('curl')) {
84
            $this->markTestSkipped('cURL must be installed to test cURL client handler.');
85
        }
86
        $config = array_merge($this->config, [
87
            'http_client_handler' => 'curl'
88
        ]);
89
        $fb = new Facebook($config);
90
        $this->assertInstanceOf(
91
            'Facebook\HttpClients\FacebookCurlHttpClient',
92
            $fb->getClient()->getHttpClientHandler()
93
        );
94
    }
95
96 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...
97
    {
98
        $config = array_merge($this->config, [
99
            'http_client_handler' => 'stream'
100
        ]);
101
        $fb = new Facebook($config);
102
        $this->assertInstanceOf(
103
            'Facebook\HttpClients\FacebookStreamHttpClient',
104
            $fb->getClient()->getHttpClientHandler()
105
        );
106
    }
107
108 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...
109
    {
110
        $config = array_merge($this->config, [
111
            'http_client_handler' => 'guzzle'
112
        ]);
113
        $fb = new Facebook($config);
114
        $this->assertInstanceOf(
115
            'Facebook\HttpClients\FacebookGuzzleHttpClient',
116
            $fb->getClient()->getHttpClientHandler()
117
        );
118
    }
119
120
    /**
121
     * @expectedException \InvalidArgumentException
122
     */
123
    public function testSettingAnInvalidPersistentDataHandlerThrows()
124
    {
125
        $config = array_merge($this->config, [
126
            'persistent_data_handler' => 'foo_handler',
127
        ]);
128
        new Facebook($config);
129
    }
130
131 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...
132
    {
133
        $config = array_merge($this->config, [
134
            'persistent_data_handler' => 'memory'
135
        ]);
136
        $fb = new Facebook($config);
137
        $this->assertInstanceOf(
138
            'Facebook\PersistentData\FacebookMemoryPersistentDataHandler',
139
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
140
        );
141
    }
142
143
    public function testSettingAnInvalidUrlHandlerThrows()
144
    {
145
        $expectedException = (PHP_MAJOR_VERSION > 5 && class_exists('TypeError'))
146
            ? 'TypeError'
147
            : 'PHPUnit_Framework_Error';
148
149
        $this->setExpectedException($expectedException);
150
151
        $config = array_merge($this->config, [
152
            'url_detection_handler' => 'foo_handler',
153
        ]);
154
        new Facebook($config);
155
    }
156
157
    public function testTheUrlHandlerWillDefaultToTheFacebookImplementation()
158
    {
159
        $fb = new Facebook($this->config);
160
        $this->assertInstanceOf('Facebook\Url\FacebookUrlDetectionHandler', $fb->getUrlDetectionHandler());
161
    }
162
163 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...
164
    {
165
        $fb = new Facebook($this->config);
166
        $fb->setDefaultAccessToken('foo_token');
167
        $accessToken = $fb->getDefaultAccessToken();
168
169
        $this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken);
170
        $this->assertEquals('foo_token', (string)$accessToken);
171
    }
172
173 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...
174
    {
175
        $fb = new Facebook($this->config);
176
        $fb->setDefaultAccessToken(new AccessToken('bar_token'));
177
        $accessToken = $fb->getDefaultAccessToken();
178
179
        $this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken);
180
        $this->assertEquals('bar_token', (string)$accessToken);
181
    }
182
183
    /**
184
     * @expectedException \InvalidArgumentException
185
     */
186
    public function testSettingAnInvalidPseudoRandomStringGeneratorThrows()
187
    {
188
        $config = array_merge($this->config, [
189
            'pseudo_random_string_generator' => 'foo_generator',
190
        ]);
191
        new Facebook($config);
192
    }
193
194 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...
195
    {
196
        if (!function_exists('mcrypt_create_iv')) {
197
            $this->markTestSkipped(
198
                'Mcrypt must be installed to test mcrypt_create_iv().'
199
            );
200
        }
201
202
        $config = array_merge($this->config, [
203
            'persistent_data_handler' => 'memory', // To keep session errors from happening
204
            'pseudo_random_string_generator' => 'mcrypt'
205
        ]);
206
        $fb = new Facebook($config);
207
        $this->assertInstanceOf(
208
            'Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator',
209
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
210
        );
211
    }
212
213 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...
214
    {
215
        if (!function_exists('openssl_random_pseudo_bytes')) {
216
            $this->markTestSkipped(
217
                'The OpenSSL extension must be enabled to test openssl_random_pseudo_bytes().'
218
            );
219
        }
220
221
        $config = array_merge($this->config, [
222
            'persistent_data_handler' => 'memory', // To keep session errors from happening
223
            'pseudo_random_string_generator' => 'openssl'
224
        ]);
225
        $fb = new Facebook($config);
226
        $this->assertInstanceOf(
227
            'Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator',
228
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
229
        );
230
    }
231
232 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...
233
    {
234
        if (ini_get('open_basedir')) {
235
            $this->markTestSkipped(
236
                'Cannot test /dev/urandom generator due to open_basedir constraint.'
237
            );
238
        }
239
240
        if (!is_readable('/dev/urandom')) {
241
            $this->markTestSkipped(
242
                '/dev/urandom not found or is not readable.'
243
            );
244
        }
245
246
        $config = array_merge($this->config, [
247
            'persistent_data_handler' => 'memory', // To keep session errors from happening
248
            'pseudo_random_string_generator' => 'urandom'
249
        ]);
250
        $fb = new Facebook($config);
251
        $this->assertInstanceOf(
252
            'Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator',
253
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
254
        );
255
    }
256
257
    /**
258
     * @expectedException \InvalidArgumentException
259
     */
260
    public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows()
261
    {
262
        $config = array_merge($this->config, [
263
            'default_access_token' => 123,
264
        ]);
265
        new Facebook($config);
266
    }
267
268
    public function testCreatingANewRequestWillDefaultToTheProperConfig()
269
    {
270
        $config = array_merge($this->config, [
271
            'default_access_token' => 'foo_token',
272
            'enable_beta_mode' => true,
273
            'default_graph_version' => 'v1337',
274
        ]);
275
        $fb = new Facebook($config);
276
277
        $request = $fb->request('FOO_VERB', '/foo');
278
        $this->assertEquals('1337', $request->getApp()->getId());
279
        $this->assertEquals('foo_secret', $request->getApp()->getSecret());
280
        $this->assertEquals('foo_token', (string)$request->getAccessToken());
281
        $this->assertEquals('v1337', $request->getGraphVersion());
282
        $this->assertEquals(
283
            FacebookClient::BASE_GRAPH_URL_BETA,
284
            $fb->getClient()->getBaseGraphUrl()
285
        );
286
    }
287
288
    public function testCanInjectCustomHandlers()
289
    {
290
        $config = array_merge($this->config, [
291
            'http_client_handler' => new FooClientInterface(),
292
            'persistent_data_handler' => new FooPersistentDataInterface(),
293
            'url_detection_handler' => new FooUrlDetectionInterface(),
294
            'pseudo_random_string_generator' => new FooBarPseudoRandomStringGenerator(),
295
        ]);
296
        $fb = new Facebook($config);
297
298
        $this->assertInstanceOf(
299
            'Facebook\Tests\Fixtures\FooClientInterface',
300
            $fb->getClient()->getHttpClientHandler()
301
        );
302
        $this->assertInstanceOf(
303
            'Facebook\Tests\Fixtures\FooPersistentDataInterface',
304
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
305
        );
306
        $this->assertInstanceOf(
307
            'Facebook\Tests\Fixtures\FooUrlDetectionInterface',
308
            $fb->getRedirectLoginHelper()->getUrlDetectionHandler()
309
        );
310
        $this->assertInstanceOf(
311
            'Facebook\Tests\Fixtures\FooBarPseudoRandomStringGenerator',
312
            $fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator()
313
        );
314
    }
315
316
    public function testPaginationReturnsProperResponse()
317
    {
318
        $config = array_merge($this->config, [
319
            'http_client_handler' => new FooClientInterface(),
320
        ]);
321
        $fb = new Facebook($config);
322
323
        $request = new FacebookRequest($fb->getApp(), 'foo_token', 'GET');
324
        $graphEdge = new GraphEdge(
325
            $request,
326
            [],
327
            [
328
                'paging' => [
329
                    'cursors' => [
330
                        'after' => 'bar_after_cursor',
331
                        'before' => 'bar_before_cursor',
332
                    ],
333
                    'previous' => 'previous_url',
334
                    'next' => 'next_url',
335
                ]
336
            ],
337
            '/1337/photos',
338
            '\Facebook\GraphNodes\GraphUser'
339
        );
340
341
        $nextPage = $fb->next($graphEdge);
342
        $this->assertInstanceOf('Facebook\GraphNodes\GraphEdge', $nextPage);
343
        $this->assertInstanceOf('Facebook\GraphNodes\GraphUser', $nextPage[0]);
344
        $this->assertEquals('Foo', $nextPage[0]['name']);
345
346
        $lastResponse = $fb->getLastResponse();
347
        $this->assertInstanceOf('Facebook\FacebookResponse', $lastResponse);
348
        $this->assertEquals(1337, $lastResponse->getHttpStatusCode());
349
    }
350
351
    public function testCanGetSuccessfulTransferWithMaxTries()
352
    {
353
        $config = array_merge($this->config, [
354
          'http_client_handler' => new FakeGraphApiForResumableUpload(),
355
        ]);
356
        $fb = new Facebook($config);
357
        $response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3);
358
        $this->assertEquals([
359
          'video_id' => '1337',
360
          'success' => true,
361
        ], $response);
362
    }
363
364
    /**
365
     * @expectedException \Facebook\Exceptions\FacebookResponseException
366
     */
367
    public function testMaxingOutRetriesWillThrow()
368
    {
369
        $client = new FakeGraphApiForResumableUpload();
370
        $client->failOnTransfer();
371
372
        $config = array_merge($this->config, [
373
          'http_client_handler' => $client,
374
        ]);
375
        $fb = new Facebook($config);
376
        $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3);
377
    }
378
}
379