Completed
Push — master ( 7113c4...8875d3 )
by Yassine
14s
created

tests/FacebookTest.php (3 issues)

Labels
Severity
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\Facebook;
27
use Facebook\FacebookClient;
28
use Facebook\FacebookRequest;
29
use Facebook\Authentication\AccessToken;
30
use Facebook\GraphNode\GraphEdge;
31
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload;
32
use Facebook\Tests\Fixtures\FooHttpClientInterface;
33
use Facebook\Tests\Fixtures\FooPersistentDataInterface;
34
use Facebook\Tests\Fixtures\FooUrlDetectionInterface;
35
use Facebook\HttpClients\FacebookCurlHttpClient;
0 ignored issues
show
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...
36
use Facebook\HttpClients\FacebookStreamHttpClient;
0 ignored issues
show
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\HttpClients\FacebookGuzzleHttpClient;
0 ignored issues
show
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...
38
use Facebook\PersistentData\FacebookMemoryPersistentDataHandler;
39
use Facebook\Url\FacebookUrlDetectionHandler;
40
use Facebook\FacebookResponse;
41
use Facebook\GraphNode\GraphUser;
42
use PHPUnit\Framework\Error\Error;
43
use PHPUnit\Framework\TestCase;
44
45
class FacebookTest extends TestCase
46
{
47
    protected $config = [
48
        'app_id' => '1337',
49
        'app_secret' => 'foo_secret',
50
        'default_graph_version' => 'v0.0',
51
    ];
52
53
    /**
54
     * @expectedException \Facebook\Exception\FacebookSDKException
55
     */
56
    public function testInstantiatingWithoutAppIdThrows()
57
    {
58
        // unset value so there is no fallback to test expected Exception
59
        putenv(Facebook::APP_ID_ENV_NAME.'=');
60
        $config = [
61
            'app_secret' => 'foo_secret',
62
            'default_graph_version' => 'v0.0',
63
        ];
64
        new Facebook($config);
65
    }
66
67
    /**
68
     * @expectedException \Facebook\Exception\FacebookSDKException
69
     */
70
    public function testInstantiatingWithoutAppSecretThrows()
71
    {
72
        // unset value so there is no fallback to test expected Exception
73
        putenv(Facebook::APP_SECRET_ENV_NAME.'=');
74
        $config = [
75
            'app_id' => 'foo_id',
76
            'default_graph_version' => 'v0.0',
77
        ];
78
        new Facebook($config);
79
    }
80
81
    /**
82
     * @expectedException \InvalidArgumentException
83
     */
84
    public function testInstantiatingWithoutDefaultGraphVersionThrows()
85
    {
86
        $config = [
87
            'app_id' => 'foo_id',
88
            'app_secret' => 'foo_secret',
89
        ];
90
        new Facebook($config);
91
    }
92
93
    /**
94
     * @expectedException \InvalidArgumentException
95
     */
96
    public function testSettingAnInvalidHttpClientTypeThrows()
97
    {
98
        $config = array_merge($this->config, [
99
            'http_client' => 'foo_client',
100
        ]);
101
        new Facebook($config);
102
    }
103
104
    /**
105
     * @expectedException \InvalidArgumentException
106
     */
107
    public function testSettingAnInvalidHttpClientClassThrows()
108
    {
109
        $config = array_merge($this->config, [
110
            'http_client' => new \stdClass(),
111
        ]);
112
        new Facebook($config);
113
    }
114
    /**
115
     * @expectedException \InvalidArgumentException
116
     */
117
    public function testSettingAnInvalidPersistentDataHandlerThrows()
118
    {
119
        $config = array_merge($this->config, [
120
            'persistent_data_handler' => 'foo_handler',
121
        ]);
122
        new Facebook($config);
123
    }
124
125
    public function testPersistentDataHandlerCanBeForced()
126
    {
127
        $config = array_merge($this->config, [
128
            'persistent_data_handler' => 'memory'
129
        ]);
130
        $fb = new Facebook($config);
131
        $this->assertInstanceOf(
132
            FacebookMemoryPersistentDataHandler::class,
133
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
134
        );
135
    }
136
137
    /**
138
     * @expectedException Error
139
     */
140
    public function testSettingAnInvalidUrlHandlerThrows()
141
    {
142
        $config = array_merge($this->config, [
143
            'url_detection_handler' => 'foo_handler',
144
        ]);
145
        new Facebook($config);
146
    }
147
148
    public function testTheUrlHandlerWillDefaultToTheFacebookImplementation()
149
    {
150
        $fb = new Facebook($this->config);
151
        $this->assertInstanceOf(FacebookUrlDetectionHandler::class, $fb->getUrlDetectionHandler());
152
    }
153
154 View Code Duplication
    public function testAnAccessTokenCanBeSetAsAString()
155
    {
156
        $fb = new Facebook($this->config);
157
        $fb->setDefaultAccessToken('foo_token');
158
        $accessToken = $fb->getDefaultAccessToken();
159
160
        $this->assertInstanceOf(AccessToken::class, $accessToken);
161
        $this->assertEquals('foo_token', (string)$accessToken);
162
    }
163
164 View Code Duplication
    public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity()
165
    {
166
        $fb = new Facebook($this->config);
167
        $fb->setDefaultAccessToken(new AccessToken('bar_token'));
168
        $accessToken = $fb->getDefaultAccessToken();
169
170
        $this->assertInstanceOf(AccessToken::class, $accessToken);
171
        $this->assertEquals('bar_token', (string)$accessToken);
172
    }
173
174
    /**
175
     * @expectedException \InvalidArgumentException
176
     */
177
    public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows()
178
    {
179
        $config = array_merge($this->config, [
180
            'default_access_token' => 123,
181
        ]);
182
        new Facebook($config);
183
    }
184
185
    public function testCreatingANewRequestWillDefaultToTheProperConfig()
186
    {
187
        $config = array_merge($this->config, [
188
            'default_access_token' => 'foo_token',
189
            'enable_beta_mode' => true,
190
            'default_graph_version' => 'v1337',
191
        ]);
192
        $fb = new Facebook($config);
193
194
        $request = $fb->request('FOO_VERB', '/foo');
195
        $this->assertEquals('1337', $request->getApp()->getId());
196
        $this->assertEquals('foo_secret', $request->getApp()->getSecret());
197
        $this->assertEquals('foo_token', (string)$request->getAccessToken());
198
        $this->assertEquals('v1337', $request->getGraphVersion());
199
        $this->assertEquals(
200
            FacebookClient::BASE_GRAPH_URL_BETA,
201
            $fb->getClient()->getBaseGraphUrl()
202
        );
203
    }
204
205
    public function testCreatingANewBatchRequestWillDefaultToTheProperConfig()
206
    {
207
        $config = array_merge($this->config, [
208
            'default_access_token' => 'foo_token',
209
            'enable_beta_mode' => true,
210
            'default_graph_version' => 'v1337',
211
        ]);
212
        $fb = new Facebook($config);
213
214
        $batchRequest = $fb->newBatchRequest();
215
        $this->assertEquals('1337', $batchRequest->getApp()->getId());
216
        $this->assertEquals('foo_secret', $batchRequest->getApp()->getSecret());
217
        $this->assertEquals('foo_token', (string)$batchRequest->getAccessToken());
218
        $this->assertEquals('v1337', $batchRequest->getGraphVersion());
219
        $this->assertEquals(
220
            FacebookClient::BASE_GRAPH_URL_BETA,
221
            $fb->getClient()->getBaseGraphUrl()
222
        );
223
        $this->assertInstanceOf('Facebook\FacebookBatchRequest', $batchRequest);
224
        $this->assertEquals(0, count($batchRequest->getRequests()));
225
    }
226
227
    public function testCanInjectCustomHandlers()
228
    {
229
        $config = array_merge($this->config, [
230
            'http_client' => new FooHttpClientInterface(),
231
            'persistent_data_handler' => new FooPersistentDataInterface(),
232
            'url_detection_handler' => new FooUrlDetectionInterface(),
233
        ]);
234
        $fb = new Facebook($config);
235
236
        $this->assertInstanceOf(
237
            FooHttpClientInterface::class,
238
            $fb->getClient()->getHttpClient()
239
        );
240
        $this->assertInstanceOf(
241
            FooPersistentDataInterface::class,
242
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
243
        );
244
        $this->assertInstanceOf(
245
            FooUrlDetectionInterface::class,
246
            $fb->getRedirectLoginHelper()->getUrlDetectionHandler()
247
        );
248
    }
249
250
    public function testPaginationReturnsProperResponse()
251
    {
252
        $config = array_merge($this->config, [
253
            'http_client' => new FooHttpClientInterface(),
254
        ]);
255
        $fb = new Facebook($config);
256
257
        $request = new FacebookRequest($fb->getApp(), 'foo_token', 'GET');
258
        $graphEdge = new GraphEdge(
259
            $request,
260
            [],
261
            [
262
                'paging' => [
263
                    'cursors' => [
264
                        'after' => 'bar_after_cursor',
265
                        'before' => 'bar_before_cursor',
266
                    ],
267
                    'previous' => 'previous_url',
268
                    'next' => 'next_url',
269
                ]
270
            ],
271
            '/1337/photos',
272
            GraphUser::class
273
        );
274
275
        $nextPage = $fb->next($graphEdge);
276
        $this->assertInstanceOf(GraphEdge::class, $nextPage);
277
        $this->assertInstanceOf(GraphUser::class, $nextPage[0]);
278
        $this->assertEquals('Foo', $nextPage[0]['name']);
279
280
        $lastResponse = $fb->getLastResponse();
281
        $this->assertInstanceOf(FacebookResponse::class, $lastResponse);
282
        $this->assertEquals(1337, $lastResponse->getHttpStatusCode());
283
    }
284
285
    public function testCanGetSuccessfulTransferWithMaxTries()
286
    {
287
        $config = array_merge($this->config, [
288
          'http_client' => new FakeGraphApiForResumableUpload(),
289
        ]);
290
        $fb = new Facebook($config);
291
        $response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3);
292
        $this->assertEquals([
293
          'video_id' => '1337',
294
          'success' => true,
295
        ], $response);
296
    }
297
298
    /**
299
     * @expectedException \Facebook\Exception\FacebookResponseException
300
     */
301
    public function testMaxingOutRetriesWillThrow()
302
    {
303
        $client = new FakeGraphApiForResumableUpload();
304
        $client->failOnTransfer();
305
306
        $config = array_merge($this->config, [
307
          'http_client' => $client,
308
        ]);
309
        $fb = new Facebook($config);
310
        $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3);
311
    }
312
}
313