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\Application; |
27
|
|
|
use Facebook\Request; |
28
|
|
|
use Facebook\BatchRequest; |
29
|
|
|
use Facebook\FileUpload\File; |
30
|
|
|
use PHPUnit\Framework\TestCase; |
31
|
|
|
|
32
|
|
|
class BatchRequestTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Application |
36
|
|
|
*/ |
37
|
|
|
private $app; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->app = new Application('123', 'foo_secret'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testABatchRequestWillInstantiateWithTheProperProperties() |
45
|
|
|
{ |
46
|
|
|
$batchRequest = new BatchRequest($this->app, [], 'foo_token', 'v0.1337'); |
47
|
|
|
|
48
|
|
|
$this->assertSame($this->app, $batchRequest->getApp()); |
49
|
|
|
$this->assertEquals('foo_token', $batchRequest->getAccessToken()); |
50
|
|
|
$this->assertEquals('POST', $batchRequest->getMethod()); |
51
|
|
|
$this->assertEquals('', $batchRequest->getEndpoint()); |
52
|
|
|
$this->assertEquals('v0.1337', $batchRequest->getGraphVersion()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testEmptyRequestWillFallbackToBatchDefaults() |
56
|
|
|
{ |
57
|
|
|
$request = new Request(); |
58
|
|
|
|
59
|
|
|
$this->createBatchRequest()->addFallbackDefaults($request); |
60
|
|
|
|
61
|
|
|
$this->assertRequestContainsAppAndToken($request, $this->app, 'foo_token'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testRequestWithTokenOnlyWillFallbackToBatchDefaults() |
65
|
|
|
{ |
66
|
|
|
$request = new Request(null, 'bar_token'); |
67
|
|
|
|
68
|
|
|
$this->createBatchRequest()->addFallbackDefaults($request); |
69
|
|
|
|
70
|
|
|
$this->assertRequestContainsAppAndToken($request, $this->app, 'bar_token'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testRequestWithAppOnlyWillFallbackToBatchDefaults() |
74
|
|
|
{ |
75
|
|
|
$customApp = new Application('1337', 'bar_secret'); |
76
|
|
|
$request = new Request($customApp); |
77
|
|
|
|
78
|
|
|
$this->createBatchRequest()->addFallbackDefaults($request); |
79
|
|
|
|
80
|
|
|
$this->assertRequestContainsAppAndToken($request, $customApp, 'foo_token'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @expectedException \Facebook\Exception\SDKException |
85
|
|
|
*/ |
86
|
|
|
public function testWillThrowWhenNoThereIsNoAppFallback() |
87
|
|
|
{ |
88
|
|
|
$batchRequest = new BatchRequest(); |
89
|
|
|
|
90
|
|
|
$batchRequest->addFallbackDefaults(new Request(null, 'foo_token')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @expectedException \Facebook\Exception\SDKException |
95
|
|
|
*/ |
96
|
|
|
public function testWillThrowWhenNoThereIsNoAccessTokenFallback() |
97
|
|
|
{ |
98
|
|
|
$request = new BatchRequest(); |
99
|
|
|
|
100
|
|
|
$request->addFallbackDefaults(new Request($this->app)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @expectedException \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function testAnInvalidTypeGivenToAddWillThrow() |
107
|
|
|
{ |
108
|
|
|
$request = new BatchRequest(); |
109
|
|
|
|
110
|
|
|
$request->add('foo'); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testAddingRequestsWillBeFormattedInAnArrayProperly() |
114
|
|
|
{ |
115
|
|
|
$requests = [ |
116
|
|
|
null => new Request(null, null, 'GET', '/foo'), |
117
|
|
|
'my-second-request' => new Request(null, null, 'POST', '/bar', ['foo' => 'bar']), |
118
|
|
|
'my-third-request' => new Request(null, null, 'DELETE', '/baz') |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$batchRequest = $this->createBatchRequest(); |
122
|
|
|
$batchRequest->add($requests[null]); |
123
|
|
|
$batchRequest->add($requests['my-second-request'], 'my-second-request'); |
124
|
|
|
$batchRequest->add($requests['my-third-request'], 'my-third-request'); |
125
|
|
|
|
126
|
|
|
$formattedRequests = $batchRequest->getRequests(); |
127
|
|
|
|
128
|
|
|
$this->assertRequestsMatch($requests, $formattedRequests); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
public function testANumericArrayOfRequestsCanBeAdded() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$requests = [ |
134
|
|
|
new Request(null, null, 'GET', '/foo'), |
135
|
|
|
new Request(null, null, 'POST', '/bar', ['foo' => 'bar']), |
136
|
|
|
new Request(null, null, 'DELETE', '/baz'), |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$formattedRequests = $this->createBatchRequestWithRequests($requests)->getRequests(); |
140
|
|
|
|
141
|
|
|
$this->assertRequestsMatch($requests, $formattedRequests); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function testAnAssociativeArrayOfRequestsCanBeAdded() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$requests = [ |
147
|
|
|
'req-one' => new Request(null, null, 'GET', '/foo'), |
148
|
|
|
'req-two' => new Request(null, null, 'POST', '/bar', ['foo' => 'bar']), |
149
|
|
|
'req-three' => new Request(null, null, 'DELETE', '/baz'), |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$formattedRequests = $this->createBatchRequestWithRequests($requests)->getRequests(); |
153
|
|
|
|
154
|
|
|
$this->assertRequestsMatch($requests, $formattedRequests); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testRequestsCanBeInjectedIntoConstructor() |
158
|
|
|
{ |
159
|
|
|
$requests = [ |
160
|
|
|
new Request(null, null, 'GET', '/foo'), |
161
|
|
|
new Request(null, null, 'POST', '/bar', ['foo' => 'bar']), |
162
|
|
|
new Request(null, null, 'DELETE', '/baz'), |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$batchRequest = new BatchRequest($this->app, $requests, 'foo_token'); |
166
|
|
|
$formattedRequests = $batchRequest->getRequests(); |
167
|
|
|
|
168
|
|
|
$this->assertRequestsMatch($requests, $formattedRequests); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @expectedException \Facebook\Exception\SDKException |
173
|
|
|
*/ |
174
|
|
|
public function testAZeroRequestCountWithThrow() |
175
|
|
|
{ |
176
|
|
|
$batchRequest = new BatchRequest($this->app, [], 'foo_token'); |
177
|
|
|
|
178
|
|
|
$batchRequest->validateBatchRequestCount(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @expectedException \Facebook\Exception\SDKException |
183
|
|
|
*/ |
184
|
|
|
public function testMoreThanFiftyRequestsWillThrow() |
185
|
|
|
{ |
186
|
|
|
$batchRequest = $this->createBatchRequest(); |
187
|
|
|
|
188
|
|
|
$this->createAndAppendRequestsTo($batchRequest, 51); |
189
|
|
|
|
190
|
|
|
$batchRequest->validateBatchRequestCount(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testLessOrEqualThanFiftyRequestsWillNotThrow() |
194
|
|
|
{ |
195
|
|
|
$batchRequest = $this->createBatchRequest(); |
196
|
|
|
|
197
|
|
|
$this->createAndAppendRequestsTo($batchRequest, 50); |
198
|
|
|
|
199
|
|
|
$batchRequest->validateBatchRequestCount(); |
200
|
|
|
|
201
|
|
|
$this->assertTrue(true); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @dataProvider requestsAndExpectedResponsesProvider |
206
|
|
|
*/ |
207
|
|
|
public function testBatchRequestEntitiesProperlyGetConvertedToAnArray($request, $expectedArray) |
208
|
|
|
{ |
209
|
|
|
$batchRequest = $this->createBatchRequest(); |
210
|
|
|
$batchRequest->add($request, 'foo_name'); |
211
|
|
|
|
212
|
|
|
$requests = $batchRequest->getRequests(); |
213
|
|
|
$batchRequestArray = $batchRequest->requestEntityToBatchArray($requests[0]['request'], $requests[0]['name']); |
214
|
|
|
|
215
|
|
|
$this->assertEquals($expectedArray, $batchRequestArray); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function requestsAndExpectedResponsesProvider() |
219
|
|
|
{ |
220
|
|
|
$headers = $this->defaultHeaders(); |
221
|
|
|
|
222
|
|
|
return [ |
223
|
|
|
[ |
224
|
|
|
new Request(null, null, 'GET', '/foo', ['foo' => 'bar']), |
225
|
|
|
[ |
226
|
|
|
'headers' => $headers, |
227
|
|
|
'method' => 'GET', |
228
|
|
|
'relative_url' => '/foo?foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
229
|
|
|
'name' => 'foo_name', |
230
|
|
|
], |
231
|
|
|
], |
232
|
|
|
[ |
233
|
|
|
new Request(null, null, 'POST', '/bar', ['bar' => 'baz']), |
234
|
|
|
[ |
235
|
|
|
'headers' => $headers, |
236
|
|
|
'method' => 'POST', |
237
|
|
|
'relative_url' => '/bar', |
238
|
|
|
'body' => 'bar=baz&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
239
|
|
|
'name' => 'foo_name', |
240
|
|
|
], |
241
|
|
|
], |
242
|
|
|
[ |
243
|
|
|
new Request(null, null, 'DELETE', '/bar'), |
244
|
|
|
[ |
245
|
|
|
'headers' => $headers, |
246
|
|
|
'method' => 'DELETE', |
247
|
|
|
'relative_url' => '/bar?access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
248
|
|
|
'name' => 'foo_name', |
249
|
|
|
], |
250
|
|
|
], |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testBatchRequestsWithFilesGetConvertedToAnArray() |
255
|
|
|
{ |
256
|
|
|
$request = new Request(null, null, 'POST', '/bar', [ |
257
|
|
|
'message' => 'foobar', |
258
|
|
|
'source' => new File(__DIR__ . '/foo.txt'), |
259
|
|
|
]); |
260
|
|
|
|
261
|
|
|
$batchRequest = $this->createBatchRequest(); |
262
|
|
|
$batchRequest->add($request, 'foo_name'); |
263
|
|
|
|
264
|
|
|
$requests = $batchRequest->getRequests(); |
265
|
|
|
|
266
|
|
|
$attachedFiles = $requests[0]['attached_files']; |
267
|
|
|
|
268
|
|
|
$batchRequestArray = $batchRequest->requestEntityToBatchArray( |
269
|
|
|
$requests[0]['request'], |
270
|
|
|
$requests[0]['name'], |
271
|
|
|
$attachedFiles |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$this->assertEquals([ |
275
|
|
|
'headers' => $this->defaultHeaders(), |
276
|
|
|
'method' => 'POST', |
277
|
|
|
'relative_url' => '/bar', |
278
|
|
|
'body' => 'message=foobar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
279
|
|
|
'name' => 'foo_name', |
280
|
|
|
'attached_files' => $attachedFiles, |
281
|
|
|
], $batchRequestArray); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function testPreppingABatchRequestProperlySetsThePostParams() |
285
|
|
|
{ |
286
|
|
|
$batchRequest = $this->createBatchRequest(); |
287
|
|
|
$batchRequest->add(new Request(null, 'bar_token', 'GET', '/foo'), 'foo_name'); |
288
|
|
|
$batchRequest->add(new Request(null, null, 'POST', '/bar', ['foo' => 'bar'])); |
289
|
|
|
$batchRequest->prepareRequestsForBatch(); |
290
|
|
|
|
291
|
|
|
$params = $batchRequest->getParams(); |
292
|
|
|
|
293
|
|
|
$expectedHeaders = json_encode($this->defaultHeaders()); |
294
|
|
|
$expectedBatchParams = [ |
295
|
|
|
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},' |
296
|
|
|
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/bar","body":"foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9"}]', |
297
|
|
|
'include_headers' => true, |
298
|
|
|
'access_token' => 'foo_token', |
299
|
|
|
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
300
|
|
|
]; |
301
|
|
|
$this->assertEquals($expectedBatchParams, $params); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function testPreppingABatchRequestProperlyMovesTheFiles() |
305
|
|
|
{ |
306
|
|
|
$batchRequest = $this->createBatchRequest(); |
307
|
|
|
$batchRequest->add(new Request(null, 'bar_token', 'GET', '/foo'), 'foo_name'); |
308
|
|
|
$batchRequest->add(new Request(null, null, 'POST', '/me/photos', [ |
309
|
|
|
'message' => 'foobar', |
310
|
|
|
'source' => new File(__DIR__ . '/foo.txt'), |
311
|
|
|
])); |
312
|
|
|
$batchRequest->prepareRequestsForBatch(); |
313
|
|
|
|
314
|
|
|
$params = $batchRequest->getParams(); |
315
|
|
|
$files = $batchRequest->getFiles(); |
316
|
|
|
|
317
|
|
|
$attachedFiles = implode(',', array_keys($files)); |
318
|
|
|
|
319
|
|
|
$expectedHeaders = json_encode($this->defaultHeaders()); |
320
|
|
|
$expectedBatchParams = [ |
321
|
|
|
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},' |
322
|
|
|
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/me\\/photos","body":"message=foobar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9","attached_files":"' . $attachedFiles . '"}]', |
323
|
|
|
'include_headers' => true, |
324
|
|
|
'access_token' => 'foo_token', |
325
|
|
|
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
326
|
|
|
]; |
327
|
|
|
$this->assertEquals($expectedBatchParams, $params); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
private function assertRequestContainsAppAndToken(Request $request, Application $expectedApp, $expectedToken) |
331
|
|
|
{ |
332
|
|
|
$app = $request->getApp(); |
333
|
|
|
$token = $request->getAccessToken(); |
334
|
|
|
|
335
|
|
|
$this->assertSame($expectedApp, $app); |
336
|
|
|
$this->assertEquals($expectedToken, $token); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
private function defaultHeaders() |
340
|
|
|
{ |
341
|
|
|
$headers = []; |
342
|
|
|
foreach (Request::getDefaultHeaders() as $name => $value) { |
343
|
|
|
$headers[] = $name . ': ' . $value; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $headers; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
private function createAndAppendRequestsTo(BatchRequest $batchRequest, $number) |
350
|
|
|
{ |
351
|
|
|
for ($i = 0; $i < $number; $i++) { |
352
|
|
|
$batchRequest->add(new Request()); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
private function createBatchRequest() |
357
|
|
|
{ |
358
|
|
|
return new BatchRequest($this->app, [], 'foo_token'); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
private function createBatchRequestWithRequests(array $requests) |
362
|
|
|
{ |
363
|
|
|
$batchRequest = $this->createBatchRequest(); |
364
|
|
|
$batchRequest->add($requests); |
365
|
|
|
|
366
|
|
|
return $batchRequest; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
private function assertRequestsMatch($requests, $formattedRequests) |
370
|
|
|
{ |
371
|
|
|
$expectedRequests = []; |
372
|
|
|
foreach ($requests as $name => $request) { |
373
|
|
|
$expectedRequests[] = [ |
374
|
|
|
'name' => $name, |
375
|
|
|
'request' => $request |
376
|
|
|
]; |
377
|
|
|
} |
378
|
|
|
$this->assertEquals($expectedRequests, $formattedRequests); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|