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\Response; |
||||
28 | use Facebook\BatchRequest; |
||||
29 | use Facebook\BatchResponse; |
||||
30 | use Facebook\GraphNode\GraphNode; |
||||
31 | use PHPUnit\Framework\TestCase; |
||||
32 | |||||
33 | class BatchResponseTest extends TestCase |
||||
34 | { |
||||
35 | /** |
||||
36 | * @var \Facebook\Application |
||||
37 | */ |
||||
38 | protected $app; |
||||
39 | |||||
40 | /** |
||||
41 | * @var \Facebook\Request |
||||
42 | */ |
||||
43 | protected $request; |
||||
44 | |||||
45 | protected function setUp() |
||||
46 | { |
||||
47 | $this->app = new Application('123', 'foo_secret'); |
||||
48 | $this->request = new Request( |
||||
49 | $this->app, |
||||
50 | 'foo_token', |
||||
51 | 'POST', |
||||
52 | '/', |
||||
53 | ['batch' => 'foo'], |
||||
54 | 'foo_eTag', |
||||
55 | 'v1337' |
||||
56 | ); |
||||
57 | } |
||||
58 | |||||
59 | public function testASuccessfulJsonBatchResponseWillBeDecoded() |
||||
60 | { |
||||
61 | $graphResponseJson = '['; |
||||
62 | // Single Graph object. |
||||
63 | $graphResponseJson .= '{"code":200,"headers":[{"name":"Connection","value":"close"},{"name":"Last-Modified","value":"2013-12-24T00:34:20+0000"},{"name":"Facebook-API-Version","value":"v2.0"},{"name":"ETag","value":"\"fooTag\""},{"name":"Content-Type","value":"text\/javascript; charset=UTF-8"},{"name":"Pragma","value":"no-cache"},{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Cache-Control","value":"private, no-cache, no-store, must-revalidate"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"}],"body":"{\"id\":\"123\",\"name\":\"Foo McBar\",\"updated_time\":\"2013-12-24T00:34:20+0000\",\"verified\":true}"}'; |
||||
64 | // Paginated list of Graph objects. |
||||
65 | $graphResponseJson .= ',{"code":200,"headers":[{"name":"Connection","value":"close"},{"name":"Facebook-API-Version","value":"v1.0"},{"name":"ETag","value":"\"barTag\""},{"name":"Content-Type","value":"text\/javascript; charset=UTF-8"},{"name":"Pragma","value":"no-cache"},{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Cache-Control","value":"private, no-cache, no-store, must-revalidate"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"}],"body":"{\"data\":[{\"id\":\"1337\",\"story\":\"Foo story.\"},{\"id\":\"1338\",\"story\":\"Bar story.\"}],\"paging\":{\"previous\":\"previous_url\",\"next\":\"next_url\"}}"}'; |
||||
66 | // After POST operation. |
||||
67 | $graphResponseJson .= ',{"code":200,"headers":[{"name":"Connection","value":"close"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"},{"name":"Cache-Control","value":"private, no-cache, no-store, must-revalidate"},{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Pragma","value":"no-cache"},{"name":"Content-Type","value":"text\/javascript; charset=UTF-8"},{"name":"Facebook-API-Version","value":"v2.0"}],"body":"{\"id\":\"123_1337\"}"}'; |
||||
68 | // After DELETE operation. |
||||
69 | $graphResponseJson .= ',{"code":200,"headers":[{"name":"Connection","value":"close"},{"name":"Expires","value":"Sat, 01 Jan 2000 00:00:00 GMT"},{"name":"Cache-Control","value":"private, no-cache, no-store, must-revalidate"},{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Pragma","value":"no-cache"},{"name":"Content-Type","value":"text\/javascript; charset=UTF-8"},{"name":"Facebook-API-Version","value":"v2.0"}],"body":"true"}'; |
||||
70 | $graphResponseJson .= ']'; |
||||
71 | $response = new Response($this->request, $graphResponseJson, 200); |
||||
72 | $batchRequest = new BatchRequest($this->app, [ |
||||
73 | new Request($this->app, 'token'), |
||||
74 | new Request($this->app, 'token'), |
||||
75 | new Request($this->app, 'token'), |
||||
76 | new Request($this->app, 'token'), |
||||
77 | ]); |
||||
78 | $batchResponse = new BatchResponse($batchRequest, $response); |
||||
79 | |||||
80 | $decodedResponses = $batchResponse->getResponses(); |
||||
81 | |||||
82 | // Single Graph object. |
||||
83 | $this->assertFalse($decodedResponses[0]->isError(), 'Did not expect Response to return an error for single Graph object.'); |
||||
84 | $this->assertInstanceOf(GraphNode::class, $decodedResponses[0]->getGraphNode()); |
||||
85 | // Paginated list of Graph objects. |
||||
86 | $this->assertFalse($decodedResponses[1]->isError(), 'Did not expect Response to return an error for paginated list of Graph objects.'); |
||||
87 | $graphEdge = $decodedResponses[1]->getGraphEdge(); |
||||
88 | $this->assertInstanceOf(GraphNode::class, $graphEdge[0]); |
||||
89 | $this->assertInstanceOf(GraphNode::class, $graphEdge[1]); |
||||
90 | } |
||||
91 | |||||
92 | public function testABatchResponseCanBeIteratedOver() |
||||
93 | { |
||||
94 | $graphResponseJson = '['; |
||||
95 | $graphResponseJson .= '{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
96 | $graphResponseJson .= ',{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
97 | $graphResponseJson .= ',{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
98 | $graphResponseJson .= ']'; |
||||
99 | $response = new Response($this->request, $graphResponseJson, 200); |
||||
100 | $batchRequest = new BatchRequest($this->app, [ |
||||
101 | 'req_one' => new Request($this->app, 'token'), |
||||
102 | 'req_two' => new Request($this->app, 'token'), |
||||
103 | 'req_three' => new Request($this->app, 'token'), |
||||
104 | ]); |
||||
105 | $batchResponse = new BatchResponse($batchRequest, $response); |
||||
106 | |||||
107 | $this->assertInstanceOf(\IteratorAggregate::class, $batchResponse); |
||||
108 | |||||
109 | foreach ($batchResponse as $key => $responseEntity) { |
||||
110 | $this->assertContains($key, ['req_one', 'req_two', 'req_three']); |
||||
111 | $this->assertInstanceOf(Response::class, $responseEntity); |
||||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | public function testTheOriginalRequestCanBeObtainedForEachRequest() |
||||
116 | { |
||||
117 | $graphResponseJson = '['; |
||||
118 | $graphResponseJson .= '{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
119 | $graphResponseJson .= ',{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
120 | $graphResponseJson .= ',{"code":200,"headers":[],"body":"{\"foo\":\"bar\"}"}'; |
||||
121 | $graphResponseJson .= ']'; |
||||
122 | $response = new Response($this->request, $graphResponseJson, 200); |
||||
123 | |||||
124 | $requests = [ |
||||
125 | new Request($this->app, 'foo_token_one', 'GET', '/me'), |
||||
126 | new Request($this->app, 'foo_token_two', 'POST', '/you'), |
||||
127 | new Request($this->app, 'foo_token_three', 'DELETE', '/123456'), |
||||
128 | ]; |
||||
129 | |||||
130 | $batchRequest = new BatchRequest($this->app, $requests); |
||||
131 | $batchResponse = new BatchResponse($batchRequest, $response); |
||||
132 | |||||
133 | $this->assertInstanceOf(Response::class, $batchResponse[0]); |
||||
134 | $this->assertInstanceOf(Request::class, $batchResponse[0]->getRequest()); |
||||
135 | $this->assertEquals('foo_token_one', $batchResponse[0]->getAccessToken()); |
||||
136 | $this->assertEquals('foo_token_two', $batchResponse[1]->getAccessToken()); |
||||
0 ignored issues
–
show
|
|||||
137 | $this->assertEquals('foo_token_three', $batchResponse[2]->getAccessToken()); |
||||
0 ignored issues
–
show
The method
getAccessToken() 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
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. ![]() |
|||||
138 | } |
||||
139 | |||||
140 | public function testHeadersFromBatchRequestCanBeAccessed() |
||||
141 | { |
||||
142 | $graphResponseJson = '['; |
||||
143 | $graphResponseJson .= '{"code":200,"headers":[{"name":"Facebook-API-Version","value":"v2.0"},{"name":"ETag","value":"\"fooTag\""}],"body":"{\"foo\":\"bar\"}"}'; |
||||
144 | $graphResponseJson .= ',{"code":200,"headers":[{"name":"Facebook-API-Version","value":"v2.5"},{"name":"ETag","value":"\"barTag\""}],"body":"{\"foo\":\"bar\"}"}'; |
||||
145 | $graphResponseJson .= ']'; |
||||
146 | $response = new Response($this->request, $graphResponseJson, 200); |
||||
147 | |||||
148 | $requests = [ |
||||
149 | new Request($this->app, 'foo_token_one', 'GET', '/me'), |
||||
150 | new Request($this->app, 'foo_token_two', 'GET', '/you'), |
||||
151 | ]; |
||||
152 | |||||
153 | $batchRequest = new BatchRequest($this->app, $requests); |
||||
154 | $batchResponse = new BatchResponse($batchRequest, $response); |
||||
155 | |||||
156 | $this->assertEquals('v2.0', $batchResponse[0]->getGraphVersion()); |
||||
157 | $this->assertEquals('"fooTag"', $batchResponse[0]->getETag()); |
||||
158 | $this->assertEquals('v2.5', $batchResponse[1]->getGraphVersion()); |
||||
159 | $this->assertEquals('"barTag"', $batchResponse[1]->getETag()); |
||||
160 | $this->assertEquals([ |
||||
161 | 'Facebook-API-Version' => 'v2.5', |
||||
162 | 'ETag' => '"barTag"', |
||||
163 | ], $batchResponse[1]->getHeaders()); |
||||
164 | } |
||||
165 | } |
||||
166 |
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.