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 testBetaModeCanBeDisabledOrEnabledViaConstructor() |
74
|
|
|
{ |
75
|
|
|
$client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), false); |
76
|
|
|
$url = $client->getBaseGraphUrl(); |
77
|
|
|
$this->assertEquals(Client::BASE_GRAPH_URL, $url); |
78
|
|
|
|
79
|
|
|
$client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), true); |
80
|
|
|
$url = $client->getBaseGraphUrl(); |
81
|
|
|
$this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testBetaModeCanBeDisabledOrEnabledViaMethod() |
85
|
|
|
{ |
86
|
|
|
$client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory()); |
87
|
|
|
$client->enableBetaMode(false); |
88
|
|
|
$url = $client->getBaseGraphUrl(); |
89
|
|
|
$this->assertEquals(Client::BASE_GRAPH_URL, $url); |
90
|
|
|
|
91
|
|
|
$client->enableBetaMode(true); |
92
|
|
|
$url = $client->getBaseGraphUrl(); |
93
|
|
|
$this->assertEquals(Client::BASE_GRAPH_URL_BETA, $url); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGraphVideoUrlCanBeSet() |
97
|
|
|
{ |
98
|
|
|
$client = new Client(new MyFooHttpClient(), new RequestFactory(), new StreamFactory()); |
99
|
|
|
$client->enableBetaMode(false); |
100
|
|
|
$url = $client->getBaseGraphUrl($postToVideoUrl = true); |
101
|
|
|
$this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url); |
102
|
|
|
|
103
|
|
|
$client->enableBetaMode(true); |
104
|
|
|
$url = $client->getBaseGraphUrl($postToVideoUrl = true); |
105
|
|
|
$this->assertEquals(Client::BASE_GRAPH_VIDEO_URL_BETA, $url); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testARequestEntityCanBeUsedToSendARequestToGraph() |
109
|
|
|
{ |
110
|
|
|
$fbRequest = new Request($this->fbApp, 'token', 'GET', '/foo'); |
111
|
|
|
$response = $this->fbClient->sendRequest($fbRequest); |
112
|
|
|
|
113
|
|
|
$this->assertInstanceOf(Response::class, $response); |
114
|
|
|
$this->assertEquals(200, $response->getHttpStatusCode()); |
115
|
|
|
$this->assertEquals('{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}', $response->getBody()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testABatchRequestEntityCanBeUsedToSendABatchRequestToGraph() |
119
|
|
|
{ |
120
|
|
|
$fbRequests = [ |
121
|
|
|
new Request($this->fbApp, 'token', 'GET', '/foo'), |
122
|
|
|
new Request($this->fbApp, 'token', 'POST', '/bar'), |
123
|
|
|
]; |
124
|
|
|
$fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests); |
125
|
|
|
|
126
|
|
|
$fbBatchClient = new Client(new MyFooBatchHttpClient(), new RequestFactory(), new StreamFactory()); |
127
|
|
|
$response = $fbBatchClient->sendBatchRequest($fbBatchRequest); |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf(BatchResponse::class, $response); |
130
|
|
|
$this->assertEquals('GET', $response[0]->getRequest()->getMethod()); |
|
|
|
|
131
|
|
|
$this->assertEquals('POST', $response[1]->getRequest()->getMethod()); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testABatchRequestWillProperlyBatchFiles() |
135
|
|
|
{ |
136
|
|
|
$fbRequests = [ |
137
|
|
|
new Request($this->fbApp, 'token', 'POST', '/photo', [ |
138
|
|
|
'message' => 'foobar', |
139
|
|
|
'source' => new File(__DIR__ . '/foo.txt'), |
140
|
|
|
]), |
141
|
|
|
new Request($this->fbApp, 'token', 'POST', '/video', [ |
142
|
|
|
'message' => 'foobar', |
143
|
|
|
'source' => new Video(__DIR__ . '/foo.txt'), |
144
|
|
|
]), |
145
|
|
|
]; |
146
|
|
|
$fbBatchRequest = new BatchRequest($this->fbApp, $fbRequests); |
147
|
|
|
$fbBatchRequest->prepareRequestsForBatch(); |
148
|
|
|
|
149
|
|
|
list($url, $method, $headers, $body) = $this->fbClient->prepareRequestMessage($fbBatchRequest); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$this->assertEquals(Client::BASE_GRAPH_VIDEO_URL, $url); |
152
|
|
|
$this->assertEquals('POST', $method); |
153
|
|
|
$this->assertContains('multipart/form-data; boundary=', $headers['Content-Type']); |
154
|
|
|
$this->assertContains('Content-Disposition: form-data; name="batch"', $body); |
155
|
|
|
$this->assertContains('Content-Disposition: form-data; name="include_headers"', $body); |
156
|
|
|
$this->assertContains('"name":0,"attached_files":', $body); |
157
|
|
|
$this->assertContains('"name":1,"attached_files":', $body); |
158
|
|
|
$this->assertContains('"; filename="foo.txt"', $body); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testARequestOfParamsWillBeUrlEncoded() |
162
|
|
|
{ |
163
|
|
|
$fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['foo' => 'bar']); |
164
|
|
|
$response = $this->fbClient->sendRequest($fbRequest); |
165
|
|
|
|
166
|
|
|
$headersSent = $response->getRequest()->getHeaders(); |
167
|
|
|
|
168
|
|
|
$this->assertEquals('application/x-www-form-urlencoded', $headersSent['Content-Type']); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testARequestWithFilesWillBeMultipart() |
172
|
|
|
{ |
173
|
|
|
$myFile = new File(__DIR__ . '/foo.txt'); |
174
|
|
|
$fbRequest = new Request($this->fbApp, 'token', 'POST', '/foo', ['file' => $myFile]); |
175
|
|
|
$response = $this->fbClient->sendRequest($fbRequest); |
176
|
|
|
|
177
|
|
|
$headersSent = $response->getRequest()->getHeaders(); |
178
|
|
|
|
179
|
|
|
$this->assertContains('multipart/form-data; boundary=', $headersSent['Content-Type']); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @expectedException \Facebook\Exception\SDKException |
184
|
|
|
*/ |
185
|
|
|
public function testARequestValidatesTheAccessTokenWhenOneIsNotProvided() |
186
|
|
|
{ |
187
|
|
|
$fbRequest = new Request($this->fbApp, null, 'GET', '/foo'); |
188
|
|
|
$this->fbClient->sendRequest($fbRequest); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @group integration |
193
|
|
|
*/ |
194
|
|
|
public function testCanCreateATestUserAndGetTheProfileAndThenDeleteTheTestUser() |
195
|
|
|
{ |
196
|
|
|
$this->initializeTestApp(); |
197
|
|
|
|
198
|
|
|
// Create a test user |
199
|
|
|
$testUserPath = '/' . TestCredentials::$appId . '/accounts/test-users'; |
|
|
|
|
200
|
|
|
$params = [ |
201
|
|
|
'installed' => true, |
202
|
|
|
'name' => 'Foo Phpunit User', |
203
|
|
|
'locale' => 'en_US', |
204
|
|
|
'permissions' => implode(',', ['read_stream', 'user_photos']), |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
$request = new Request( |
208
|
|
|
static::$testApp, |
209
|
|
|
static::$testApp->getAccessToken(), |
210
|
|
|
'POST', |
211
|
|
|
$testUserPath, |
212
|
|
|
$params |
213
|
|
|
); |
214
|
|
|
$response = static::$testClient->sendRequest($request)->getGraphNode(); |
215
|
|
|
|
216
|
|
|
$testUserId = $response->getField('id'); |
217
|
|
|
$testUserAccessToken = $response->getField('access_token'); |
218
|
|
|
|
219
|
|
|
// Get the test user's profile |
220
|
|
|
$request = new Request( |
221
|
|
|
static::$testApp, |
222
|
|
|
$testUserAccessToken, |
223
|
|
|
'GET', |
224
|
|
|
'/me' |
225
|
|
|
); |
226
|
|
|
$graphNode = static::$testClient->sendRequest($request)->getGraphNode(); |
227
|
|
|
|
228
|
|
|
$this->assertInstanceOf(GraphNode::class, $graphNode); |
229
|
|
|
$this->assertNotNull($graphNode->getField('id')); |
230
|
|
|
$this->assertEquals('Foo Phpunit User', $graphNode->getField('name')); |
231
|
|
|
|
232
|
|
|
// Delete test user |
233
|
|
|
$request = new Request( |
234
|
|
|
static::$testApp, |
235
|
|
|
static::$testApp->getAccessToken(), |
236
|
|
|
'DELETE', |
237
|
|
|
'/' . $testUserId |
238
|
|
|
); |
239
|
|
|
$graphNode = static::$testClient->sendRequest($request)->getGraphNode(); |
240
|
|
|
|
241
|
|
|
$this->assertTrue($graphNode->getField('success')); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function initializeTestApp() |
245
|
|
|
{ |
246
|
|
|
if (!file_exists(__DIR__ . '/TestCredentials.php')) { |
247
|
|
|
throw new SDKException( |
248
|
|
|
'You must create a TestCredentials.php file from TestCredentials.php.dist' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if (!strlen(TestCredentials::$appId) || |
253
|
|
|
!strlen(TestCredentials::$appSecret) |
254
|
|
|
) { |
255
|
|
|
throw new SDKException( |
256
|
|
|
'You must fill out TestCredentials.php' |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
static::$testApp = new Application( |
260
|
|
|
TestCredentials::$appId, |
261
|
|
|
TestCredentials::$appSecret |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
static::$testClient = new Client(new \RicardoFiorani\GuzzlePsr18Adapter\Client(), new RequestFactory(), new StreamFactory()); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
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.