Failed Conditions
Pull Request — master (#1145)
by
unknown
01:50
created

testTheHttpClientWillFallbackToDefault()   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\Exception\SDKException;
26
use Facebook\Application;
27
use Facebook\Request;
28
use Facebook\BatchRequest;
29
use Facebook\Client;
30
use Facebook\FileUpload\File;
31
use Facebook\FileUpload\Video;
32
// These are needed when you uncomment the HTTP clients below.
33
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload;
34
use Facebook\Tests\Fixtures\FooHttpClientInterface;
35
use Facebook\Tests\Fixtures\MyFooBatchHttpClient;
36
use Facebook\Tests\Fixtures\MyFooHttpClient;
37
use Facebook\Response;
38
use Facebook\BatchResponse;
39
use Facebook\GraphNode\GraphNode;
40
41
use Http\Factory\Guzzle\RequestFactory;
42
use Http\Factory\Guzzle\StreamFactory;
43
use PHPUnit\Framework\TestCase;
44
45
class ClientTest extends TestCase
46
{
47
    /**
48
     * @var Application
49
     */
50
    public $fbApp;
51
52
    /**
53
     * @var Client
54
     */
55
    public $fbClient;
56
57
    /**
58
     * @var Application
59
     */
60
    public static $testApp;
61
62
    /**
63
     * @var Client
64
     */
65
    public static $testClient;
66
67
    protected function setUp()
68
    {
69
        $this->fbApp = new Application('id', 'shhhh!');
70
        $this->fbClient = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory());
71
    }
72
73
    public function testACustomHttpClientCanBeInjected()
74
    {
75
        $handler = new MyFooHttpClient();
76
        $client = new Client($handler, new RequestFactory(), new StreamFactory());
77
        $httpClient = $client->getHttpClient();
78
79
        $this->assertInstanceOf(MyFooHttpClient::class, $httpClient);
80
    }
81
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
    public function testTheHttpClientWillFallbackToDefault()
83
    {
84
        $client = new Client();
85
        $httpClient = $client->getHttpClient();
86
87
        $this->assertInstanceOf(HttpClient::class, $httpClient);
88
    }
89
*/
90
    public function testBetaModeCanBeDisabledOrEnabledViaConstructor()
91
    {
92
        $client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), false);
93
        $url = $client->getBaseGraphUrl();
94
        $this->assertEquals(Client::BASE_GRAPH_URL, $url);
95
96
        $client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), true);
97
        $url = $client->getBaseGraphUrl();
98
        $this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url);
99
    }
100
101
    public function testBetaModeCanBeDisabledOrEnabledViaMethod()
102
    {
103
        $client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory());
104
        $client->enableBetaMode(false);
105
        $url = $client->getBaseGraphUrl();
106
        $this->assertEquals(Client::BASE_GRAPH_URL, $url);
107
108
        $client->enableBetaMode(true);
109
        $url = $client->getBaseGraphUrl();
110
        $this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url);
111
    }
112
113
    public function testGraphVideoUrlCanBeSet()
114
    {
115
        $client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory());
116
        $client->enableBetaMode(false);
117
        $url = $client->getBaseGraphUrl($postToVideoUrl = true);
118
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url);
119
120
        $client->enableBetaMode(true);
121
        $url = $client->getBaseGraphUrl($postToVideoUrl = true);
122
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL_BETA, $url);
123
    }
124
125
    public function testARequestEntityCanBeUsedToSendARequestToGraph()
126
    {
127
        $fbRequest = new Request($this->fbApp, 'token', 'GET', '/foo');
128
        $response = $this->fbClient->sendRequest($fbRequest);
129
130
        $this->assertInstanceOf(Response::class, $response);
131
        $this->assertEquals(200, $response->getHttpStatusCode());
132
        $this->assertEquals('{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}', $response->getBody());
133
    }
134
135
    public function testABatchRequestEntityCanBeUsedToSendABatchRequestToGraph()
136
    {
137
        $fbRequests = [
138
            new Request($this->fbApp, 'token', 'GET', '/foo'),
139
            new Request($this->fbApp, 'token', 'POST', '/bar'),
140
        ];
141
        $fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests);
142
143
        $fbBatchClient = new Client(new MyFooBatchHttpClient(), new RequestFactory(), new StreamFactory());
144
        $response = $fbBatchClient->sendBatchRequest($fbBatchRequest);
145
146
        $this->assertInstanceOf(BatchResponse::class, $response);
147
        $this->assertEquals('GET', $response[0]->getRequest()->getMethod());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on null. ( Ignorable by Annotation )

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

147
        $this->assertEquals('GET', $response[0]->/** @scrutinizer ignore-call */ getRequest()->getMethod());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
        $this->assertEquals('POST', $response[1]->getRequest()->getMethod());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on null. ( Ignorable by Annotation )

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

148
        $this->assertEquals('POST', $response[1]->/** @scrutinizer ignore-call */ getRequest()->getMethod());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
    }
150
151
    public function testABatchRequestWillProperlyBatchFiles()
152
    {
153
        $fbRequests = [
154
            new Request($this->fbApp, 'token', 'POST', '/photo', [
155
                'message' => 'foobar',
156
                'source' => new File(__DIR__ . '/foo.txt'),
157
            ]),
158
            new Request($this->fbApp, 'token', 'POST', '/video', [
159
                'message' => 'foobar',
160
                'source' => new Video(__DIR__ . '/foo.txt'),
161
            ]),
162
        ];
163
        $fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests);
164
        $fbBatchRequest->prepareRequestsForBatch();
165
166
        list($url, $method, $headers, $body) = $this->fbClient->prepareRequestMessage($fbBatchRequest);
0 ignored issues
show
Deprecated Code introduced by
The function Facebook\Client::prepareRequestMessage() has been deprecated. ( Ignorable by Annotation )

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

166
        list($url, $method, $headers, $body) = /** @scrutinizer ignore-deprecated */ $this->fbClient->prepareRequestMessage($fbBatchRequest);
Loading history...
167
168
        $this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url);
169
        $this->assertEquals('POST', $method);
170
        $this->assertContains('multipart/form-data; boundary=', $headers['Content-Type']);
171
        $this->assertContains('Content-Disposition: form-data; name="batch"', $body);
172
        $this->assertContains('Content-Disposition: form-data; name="include_headers"', $body);
173
        $this->assertContains('"name":0,"attached_files":', $body);
174
        $this->assertContains('"name":1,"attached_files":', $body);
175
        $this->assertContains('"; filename="foo.txt"', $body);
176
    }
177
178
    public function testARequestOfParamsWillBeUrlEncoded()
179
    {
180
        $fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['foo' => 'bar']);
181
        $response = $this->fbClient->sendRequest($fbRequest);
182
183
        $headersSent = $response->getRequest()->getHeaders();
184
185
        $this->assertEquals('application/x-www-form-urlencoded', $headersSent['Content-Type']);
186
    }
187
188
    public function testARequestWithFilesWillBeMultipart()
189
    {
190
        $myFile = new File(__DIR__ . '/foo.txt');
191
        $fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['file' => $myFile]);
192
        $response = $this->fbClient->sendRequest($fbRequest);
193
194
        $headersSent = $response->getRequest()->getHeaders();
195
196
        $this->assertContains('multipart/form-data; boundary=', $headersSent['Content-Type']);
197
    }
198
199
    /**
200
     * @expectedException \Facebook\Exception\SDKException
201
     */
202
    public function testARequestValidatesTheAccessTokenWhenOneIsNotProvided()
203
    {
204
        $fbRequest = new Request($this->fbApp, null, 'GET', '/foo');
205
        $this->fbClient->sendRequest($fbRequest);
206
    }
207
208
    /**
209
     * @group integration
210
     */
211
    public function testCanCreateATestUserAndGetTheProfileAndThenDeleteTheTestUser()
212
    {
213
        $this->initializeTestApp();
214
215
        // Create a test user
216
        $testUserPath = '/' . TestCredentials::$appId . '/accounts/test-users';
0 ignored issues
show
Bug introduced by
The type Facebook\Tests\TestCredentials 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...
217
        $params = [
218
            'installed' => true,
219
            'name' => 'Foo Phpunit User',
220
            'locale' => 'en_US',
221
            'permissions' => implode(',', ['read_stream', 'user_photos']),
222
        ];
223
224
        $request = new Request(
225
            static::$testApp,
226
            static::$testApp->getAccessToken(),
227
            'POST',
228
            $testUserPath,
229
            $params
230
        );
231
        $response = static::$testClient->sendRequest($request)->getGraphNode();
232
233
        $testUserId = $response->getField('id');
234
        $testUserAccessToken = $response->getField('access_token');
235
236
        // Get the test user's profile
237
        $request = new Request(
238
            static::$testApp,
239
            $testUserAccessToken,
240
            'GET',
241
            '/me'
242
        );
243
        $graphNode = static::$testClient->sendRequest($request)->getGraphNode();
244
245
        $this->assertInstanceOf(GraphNode::class, $graphNode);
246
        $this->assertNotNull($graphNode->getField('id'));
247
        $this->assertEquals('Foo Phpunit User', $graphNode->getField('name'));
248
249
        // Delete test user
250
        $request = new Request(
251
            static::$testApp,
252
            static::$testApp->getAccessToken(),
253
            'DELETE',
254
            '/' . $testUserId
255
        );
256
        $graphNode = static::$testClient->sendRequest($request)->getGraphNode();
257
258
        $this->assertTrue($graphNode->getField('success'));
259
    }
260
261
    public function initializeTestApp()
262
    {
263
        if (!file_exists(__DIR__ . '/TestCredentials.php')) {
264
            throw new SDKException(
265
                'You must create a TestCredentials.php file from TestCredentials.php.dist'
266
            );
267
        }
268
269
        if (!strlen(TestCredentials::$appId) ||
270
            !strlen(TestCredentials::$appSecret)
271
        ) {
272
            throw new SDKException(
273
                'You must fill out TestCredentials.php'
274
            );
275
        }
276
        static::$testApp = new Application(
277
            TestCredentials::$appId,
278
            TestCredentials::$appSecret
279
        );
280
281
        static::$testClient = new Client(new \RicardoFiorani\GuzzlePsr18Adapter\Client(), new RequestFactory(), new StreamFactory());
282
    }
283
}
284