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

Test::testMaxingOutRetriesWillThrow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

290
        $response = $fb->uploadVideo(/** @scrutinizer ignore-type */ 'me', __DIR__.'/foo.txt', [], 'foo-token', 3);
Loading history...
291
        $this->assertEquals([
292
          'video_id' => '1337',
293
          'success' => true,
294
        ], $response);
295
    }
296
297
    /**
298
     * @expectedException \Facebook\Exception\ResponseException
299
     */
300
    public function testMaxingOutRetriesWillThrow()
301
    {
302
        $client = new FakeGraphApiForResumableUpload();
303
        $client->failOnTransfer();
304
305
        $config = array_merge($this->config, [
306
          'http_client' => $client,
307
        ]);
308
        $fb = new Facebook($config);
309
        $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3);
310
    }
311
}
312