Failed Conditions
Pull Request — master (#1145)
by
unknown
02:10
created

Test::testSettingAnInvalidHttpClientTypeThrows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
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\PersistentData\InMemoryPersistentDataHandler;
35
use Facebook\Tests\Fixtures\MyFooHttpClient;
36
use Facebook\Url\UrlDetectionHandler;
37
use Facebook\Response;
38
use Facebook\GraphNode\GraphUser;
39
use Http\Factory\Guzzle\RequestFactory;
40
use Http\Factory\Guzzle\StreamFactory;
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(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $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(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $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(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
90
    }
91
92
    /**
93
     * @expectedException \InvalidArgumentException
94
     */
95
    public function testSettingAnInvalidPersistentDataHandlerThrows()
96
    {
97
        $config = array_merge($this->config, [
98
            'persistent_data_handler' => 'foo_handler',
99
        ]);
100
        new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
101
    }
102
103
    public function testPersistentDataHandlerCanBeForced()
104
    {
105
        $config = array_merge($this->config, [
106
            'persistent_data_handler' => 'memory'
107
        ]);
108
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
109
        $this->assertInstanceOf(
110
            InMemoryPersistentDataHandler::class,
111
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
112
        );
113
    }
114
115
    /**
116
     * @expectedException Error
117
     */
118
    public function testSettingAnInvalidUrlHandlerThrows()
119
    {
120
        $config = array_merge($this->config, [
121
            'url_detection_handler' => 'foo_handler',
122
        ]);
123
        new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
124
    }
125
126
    public function testTheUrlHandlerWillDefaultToTheImplementation()
127
    {
128
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config);
129
        $this->assertInstanceOf(UrlDetectionHandler::class, $fb->getUrlDetectionHandler());
130
    }
131
132
    public function testAnAccessTokenCanBeSetAsAString()
133
    {
134
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config);
135
        $fb->setDefaultAccessToken('foo_token');
136
        $accessToken = $fb->getDefaultAccessToken();
137
138
        $this->assertInstanceOf(AccessToken::class, $accessToken);
139
        $this->assertEquals('foo_token', (string)$accessToken);
140
    }
141
142
    public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity()
143
    {
144
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config);
145
        $fb->setDefaultAccessToken(new AccessToken('bar_token'));
146
        $accessToken = $fb->getDefaultAccessToken();
147
148
        $this->assertInstanceOf(AccessToken::class, $accessToken);
149
        $this->assertEquals('bar_token', (string)$accessToken);
150
    }
151
152
    /**
153
     * @expectedException \InvalidArgumentException
154
     */
155
    public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows()
156
    {
157
        $config = array_merge($this->config, [
158
            'default_access_token' => 123,
159
        ]);
160
        new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
161
    }
162
163
    public function testCreatingANewRequestWillDefaultToTheProperConfig()
164
    {
165
        $config = array_merge($this->config, [
166
            'default_access_token' => 'foo_token',
167
            'enable_beta_mode' => true,
168
            'default_graph_version' => 'v1337',
169
        ]);
170
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
171
172
        $request = $fb->request('FOO_VERB', '/foo');
173
        $this->assertEquals('1337', $request->getApplication()->getId());
174
        $this->assertEquals('foo_secret', $request->getApplication()->getSecret());
175
        $this->assertEquals('foo_token', (string)$request->getAccessToken());
176
        $this->assertEquals('v1337', $request->getGraphVersion());
177
        $this->assertEquals(
178
            Client::BASE_GRAPH_URL_BETA,
179
            $fb->getClient()->getBaseGraphUrl()
180
        );
181
    }
182
183
    public function testCreatingANewBatchRequestWillDefaultToTheProperConfig()
184
    {
185
        $config = array_merge($this->config, [
186
            'default_access_token' => 'foo_token',
187
            'enable_beta_mode' => true,
188
            'default_graph_version' => 'v1337',
189
        ]);
190
        $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config);
191
192
        $batchRequest = $fb->newBatchRequest();
193
        $this->assertEquals('1337', $batchRequest->getApplication()->getId());
194
        $this->assertEquals('foo_secret', $batchRequest->getApplication()->getSecret());
195
        $this->assertEquals('foo_token', (string)$batchRequest->getAccessToken());
196
        $this->assertEquals('v1337', $batchRequest->getGraphVersion());
197
        $this->assertEquals(
198
            Client::BASE_GRAPH_URL_BETA,
199
            $fb->getClient()->getBaseGraphUrl()
200
        );
201
        $this->assertInstanceOf('Facebook\BatchRequest', $batchRequest);
202
        $this->assertCount(0, $batchRequest->getRequests());
203
    }
204
205
    public function testCanInjectCustomHandlers()
206
    {
207
        $config = array_merge($this->config, [
208
            'persistent_data_handler' => new FooPersistentDataInterface(),
209
            'url_detection_handler' => new FooUrlDetectionInterface(),
210
        ]);
211
        $fb = new Facebook(new FooHttpClientInterface(), new RequestFactory(), new StreamFactory(), $config);
212
213
        $this->assertInstanceOf(
214
            FooPersistentDataInterface::class,
215
            $fb->getRedirectLoginHelper()->getPersistentDataHandler()
216
        );
217
        $this->assertInstanceOf(
218
            FooUrlDetectionInterface::class,
219
            $fb->getRedirectLoginHelper()->getUrlDetectionHandler()
220
        );
221
    }
222
223
    public function testPaginationReturnsProperResponse()
224
    {
225
        $fb = new Facebook(new FooHttpClientInterface(), new RequestFactory(), new StreamFactory(), $this->config);
226
227
        $request = new Request($fb->getApplication(), 'foo_token', 'GET');
228
        $graphEdge = new GraphEdge(
229
            $request,
230
            [],
231
            [
232
                'paging' => [
233
                    'cursors' => [
234
                        'after' => 'bar_after_cursor',
235
                        'before' => 'bar_before_cursor',
236
                    ],
237
                    'previous' => 'previous_url',
238
                    'next' => 'next_url',
239
                ]
240
            ],
241
            '/1337/photos',
242
            GraphUser::class
243
        );
244
245
        $nextPage = $fb->next($graphEdge);
246
        $this->assertInstanceOf(GraphEdge::class, $nextPage);
247
        $this->assertInstanceOf(GraphUser::class, $nextPage[0]);
248
        $this->assertEquals('Foo', $nextPage[0]->getField('name'));
249
250
        $lastResponse = $fb->getLastResponse();
251
        $this->assertInstanceOf(Response::class, $lastResponse);
252
        $this->assertEquals(1337, $lastResponse->getHttpStatusCode());
253
    }
254
255
    public function testCanGetSuccessfulTransferWithMaxTries()
256
    {
257
        $fb = new Facebook(new FakeGraphApiForResumableUpload(), new RequestFactory(), new StreamFactory(), $this->config);
258
        $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

258
        $response = $fb->uploadVideo(/** @scrutinizer ignore-type */ 'me', __DIR__.'/foo.txt', [], 'foo-token', 3);
Loading history...
259
        $this->assertEquals([
260
          'video_id' => '1337',
261
          'success' => true,
262
        ], $response);
263
    }
264
265
    /**
266
     * @expectedException \Facebook\Exception\ResponseException
267
     */
268
    public function testMaxingOutRetriesWillThrow()
269
    {
270
        $client = new FakeGraphApiForResumableUpload();
271
        $client->failOnTransfer();
272
273
        $fb = new Facebook($client, new RequestFactory(), new StreamFactory(), $this->config);
274
        $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3);
275
    }
276
}
277