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