testAListFromGraphWillBeCastAsAGraphEdge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 37
rs 9.328
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\GraphNode;
24
25
use Facebook\Application;
26
use Facebook\Request;
27
use Facebook\Response;
28
use Facebook\GraphNode\GraphNodeFactory;
29
use Facebook\GraphNode\GraphNode;
30
use Facebook\GraphNode\GraphEdge;
31
use Facebook\Tests\Fixtures\MyFooGraphNode;
32
use Facebook\Tests\Fixtures\MyFooSubClassGraphNode;
33
use Facebook\GraphNode\GraphAlbum;
34
use PHPUnit\Framework\TestCase;
35
36
class GraphNodeFactoryTest extends TestCase
37
{
38
    /**
39
     * @var \Facebook\Request
40
     */
41
    protected $request;
42
43
    protected function setUp()
44
    {
45
        $app = new Application('123', 'foo_app_secret');
46
        $this->request = new Request(
47
            $app,
48
            'foo_token',
49
            'GET',
50
            '/me/photos?keep=me',
51
            ['foo' => 'bar'],
52
            'foo_eTag',
53
            'v1337'
54
        );
55
    }
56
57
    public function testAValidGraphNodeResponseWillNotThrow()
58
    {
59
        $data = '{"id":"123","name":"foo"}';
60
        $res = new Response($this->request, $data);
61
62
        $factory = new GraphNodeFactory($res);
63
        $factory->validateResponseCastableAsGraphNode();
64
65
        $this->assertTrue(true);
66
    }
67
68
    /**
69
     * @expectedException \Facebook\Exception\SDKException
70
     */
71
    public function testANonGraphNodeResponseWillThrow()
72
    {
73
        $data = '{"data":[{"id":"123","name":"foo"},{"id":"1337","name":"bar"}]}';
74
        $res = new Response($this->request, $data);
75
76
        $factory = new GraphNodeFactory($res);
77
        $factory->validateResponseCastableAsGraphNode();
78
    }
79
80
    public function testAValidGraphEdgeResponseWillNotThrow()
81
    {
82
        $data = '{"data":[{"id":"123","name":"foo"},{"id":"1337","name":"bar"}]}';
83
        $res = new Response($this->request, $data);
84
85
        $factory = new GraphNodeFactory($res);
86
        $factory->validateResponseCastableAsGraphEdge();
87
88
        $this->assertTrue(true);
89
    }
90
91
    /**
92
     * @expectedException \Facebook\Exception\SDKException
93
     */
94
    public function testANonGraphEdgeResponseWillThrow()
95
    {
96
        $data = '{"id":"123","name":"foo"}';
97
        $res = new Response($this->request, $data);
98
99
        $factory = new GraphNodeFactory($res);
100
        $factory->validateResponseCastableAsGraphEdge();
101
    }
102
103
    public function testOnlyNumericArraysAreCastableAsAGraphEdge()
104
    {
105
        $shouldPassOne = GraphNodeFactory::isCastableAsGraphEdge([]);
106
        $shouldPassTwo = GraphNodeFactory::isCastableAsGraphEdge(['foo', 'bar']);
107
        $shouldFail = GraphNodeFactory::isCastableAsGraphEdge(['faz' => 'baz']);
108
109
        $this->assertTrue($shouldPassOne, 'Expected the given array to be castable as a GraphEdge.');
110
        $this->assertTrue($shouldPassTwo, 'Expected the given array to be castable as a GraphEdge.');
111
        $this->assertFalse($shouldFail, 'Expected the given array to not be castable as a GraphEdge.');
112
    }
113
114
    /**
115
     * @expectedException \Facebook\Exception\SDKException
116
     */
117
    public function testInvalidSubClassesWillThrow()
118
    {
119
        GraphNodeFactory::validateSubclass('FooSubClass');
120
    }
121
122
    public function testValidSubClassesWillNotThrow()
123
    {
124
        GraphNodeFactory::validateSubclass(GraphNode::class);
125
        GraphNodeFactory::validateSubclass(GraphAlbum::class);
126
        GraphNodeFactory::validateSubclass(MyFooGraphNode::class);
127
128
        $this->assertTrue(true);
129
    }
130
131
    public function testCastingAsASubClassObjectWillInstantiateTheSubClass()
132
    {
133
        $data = '{"id":"123","name":"foo"}';
134
        $res = new Response($this->request, $data);
135
136
        $factory = new GraphNodeFactory($res);
137
        $mySubClassObject = $factory->makeGraphNode(MyFooGraphNode::class);
138
139
        $this->assertInstanceOf(MyFooGraphNode::class, $mySubClassObject);
140
    }
141
142
    public function testASubClassMappingWillAutomaticallyInstantiateSubClass()
143
    {
144
        $data = '{"id":"123","name":"Foo Name","foo_object":{"id":"1337","name":"Should be sub classed!"}}';
145
        $res = new Response($this->request, $data);
146
147
        $factory = new GraphNodeFactory($res);
148
        $mySubClassObject = $factory->makeGraphNode(MyFooGraphNode::class);
149
        $fooObject = $mySubClassObject->getField('foo_object');
150
151
        $this->assertInstanceOf(MyFooGraphNode::class, $mySubClassObject);
152
        $this->assertInstanceOf(MyFooSubClassGraphNode::class, $fooObject);
153
    }
154
155
    public function testAnUnknownGraphNodeWillBeCastAsAGenericGraphNode()
156
    {
157
        $data = json_encode([
158
            'id' => '123',
159
            'name' => 'Foo Name',
160
            'unknown_object' => [
161
                'id' => '1337',
162
                'name' => 'Should be generic!',
163
            ],
164
        ]);
165
        $res = new Response($this->request, $data);
166
167
        $factory = new GraphNodeFactory($res);
168
169
        $mySubClassObject = $factory->makeGraphNode(MyFooGraphNode::class);
170
        $unknownObject = $mySubClassObject->getField('unknown_object');
171
172
        $this->assertInstanceOf(MyFooGraphNode::class, $mySubClassObject);
173
        $this->assertInstanceOf(GraphNode::class, $unknownObject);
174
        $this->assertNotInstanceOf(MyFooGraphNode::class, $unknownObject);
175
    }
176
177
    public function testAListFromGraphWillBeCastAsAGraphEdge()
178
    {
179
        $data = json_encode([
180
            'data' => [
181
                [
182
                    'id' => '123',
183
                    'name' => 'Foo McBar',
184
                    'link' => 'http://facebook/foo',
185
                ],
186
                [
187
                    'id' => '1337',
188
                    'name' => 'Bar McBaz',
189
                    'link' => 'http://facebook/bar',
190
                ],
191
            ],
192
            'paging' => [
193
                'next' => 'http://facebook/next',
194
                'previous' => 'http://facebook/prev',
195
            ],
196
        ]);
197
        $res = new Response($this->request, $data);
198
199
        $factory = new GraphNodeFactory($res);
200
        $graphEdge = $factory->makeGraphEdge();
201
        $graphData = $graphEdge->asArray();
202
203
        $this->assertInstanceOf(GraphEdge::class, $graphEdge);
204
        $this->assertEquals([
205
            'id' => '123',
206
            'name' => 'Foo McBar',
207
            'link' => 'http://facebook/foo',
208
        ], $graphData[0]);
209
        $this->assertEquals([
210
            'id' => '1337',
211
            'name' => 'Bar McBaz',
212
            'link' => 'http://facebook/bar',
213
        ], $graphData[1]);
214
    }
215
216
    public function testAGraphNodeWillBeCastAsAGraphNode()
217
    {
218
        $data = json_encode([
219
            'id' => '123',
220
            'name' => 'Foo McBar',
221
            'link' => 'http://facebook/foo',
222
        ]);
223
        $res = new Response($this->request, $data);
224
225
        $factory = new GraphNodeFactory($res);
226
        $graphNode = $factory->makeGraphNode();
227
        $graphData = $graphNode->asArray();
228
229
        $this->assertInstanceOf(GraphNode::class, $graphNode);
230
        $this->assertEquals([
231
            'id' => '123',
232
            'name' => 'Foo McBar',
233
            'link' => 'http://facebook/foo',
234
        ], $graphData);
235
    }
236
237
    public function testAGraphNodeWithARootDataKeyWillBeCastAsAGraphNode()
238
    {
239
        $data = json_encode([
240
            'data' => [
241
                'id' => '123',
242
                'name' => 'Foo McBar',
243
                'link' => 'http://facebook/foo',
244
            ],
245
        ]);
246
247
        $res = new Response($this->request, $data);
248
249
        $factory = new GraphNodeFactory($res);
250
        $graphNode = $factory->makeGraphNode();
251
        $graphData = $graphNode->asArray();
252
253
        $this->assertInstanceOf(GraphNode::class, $graphNode);
254
        $this->assertEquals([
255
            'id' => '123',
256
            'name' => 'Foo McBar',
257
            'link' => 'http://facebook/foo',
258
        ], $graphData);
259
    }
260
261
    public function testAGraphEdgeWillBeCastRecursively()
262
    {
263
        $someUser = [
264
            'id' => '123',
265
            'name' => 'Foo McBar',
266
        ];
267
        $likesCollection = [
268
            'data' => [
269
                [
270
                    'id' => '1',
271
                    'name' => 'Sammy Kaye Powers',
272
                    'is_friendly' => true,
273
                ],
274
                [
275
                    'id' => '2',
276
                    'name' => 'Yassine Guedidi',
277
                    'is_friendly' => true,
278
                ],
279
                [
280
                    'id' => '3',
281
                    'name' => 'Fosco Marotto',
282
                    'is_friendly' => true,
283
                ],
284
                [
285
                    'id' => '4',
286
                    'name' => 'Foo McUnfriendly',
287
                    'is_friendly' => false,
288
                ],
289
            ],
290
            'paging' => [
291
                'next' => 'http://facebook/next_likes',
292
                'previous' => 'http://facebook/prev_likes',
293
            ],
294
        ];
295
        $commentsCollection = [
296
            'data' => [
297
                [
298
                    'id' => '42_1',
299
                    'from' => $someUser,
300
                    'message' => 'Foo comment.',
301
                    'created_time' => '2014-07-15T03:54:34+0000',
302
                    'likes' => $likesCollection,
303
                ],
304
                [
305
                    'id' => '42_2',
306
                    'from' => $someUser,
307
                    'message' => 'Bar comment.',
308
                    'created_time' => '2014-07-15T04:11:24+0000',
309
                    'likes' => $likesCollection,
310
                ],
311
            ],
312
            'paging' => [
313
                'next' => 'http://facebook/next_comments',
314
                'previous' => 'http://facebook/prev_comments',
315
            ],
316
        ];
317
        $dataFromGraph = [
318
            'data' => [
319
                [
320
                    'id' => '1337_1',
321
                    'from' => $someUser,
322
                    'story' => 'Some great foo story.',
323
                    'likes' => $likesCollection,
324
                    'comments' => $commentsCollection,
325
                ],
326
                [
327
                    'id' => '1337_2',
328
                    'from' => $someUser,
329
                    'to' => [
330
                        'data' => [$someUser],
331
                    ],
332
                    'message' => 'Some great bar message.',
333
                    'likes' => $likesCollection,
334
                    'comments' => $commentsCollection,
335
                ],
336
            ],
337
            'paging' => [
338
                'next' => 'http://facebook/next',
339
                'previous' => 'http://facebook/prev',
340
            ],
341
        ];
342
        $data = json_encode($dataFromGraph);
343
        $res = new Response($this->request, $data);
344
345
        $factory = new GraphNodeFactory($res);
346
        $graphNode = $factory->makeGraphEdge();
347
        $this->assertInstanceOf(GraphEdge::class, $graphNode);
348
349
        // Story
350
        $storyObject = $graphNode[0];
351
        $this->assertInstanceOf(GraphNode::class, $storyObject->getField('from'));
352
        $this->assertInstanceOf(GraphEdge::class, $storyObject->getField('likes'));
353
        $this->assertInstanceOf(GraphEdge::class, $storyObject->getField('comments'));
354
355
        // Story Comments
356
        $storyComments = $storyObject->getField('comments');
357
        $firstStoryComment = $storyComments[0];
358
        $this->assertInstanceOf(GraphNode::class, $firstStoryComment->getField('from'));
359
360
        // Message
361
        $messageObject = $graphNode[1];
362
        $this->assertInstanceOf(GraphEdge::class, $messageObject->getField('to'));
363
        $toUsers = $messageObject->getField('to');
364
        $this->assertInstanceOf(GraphNode::class, $toUsers[0]);
365
    }
366
367
    public function testAGraphEdgeWillGenerateTheProperParentGraphEdges()
368
    {
369
        $likesList = [
370
            'data' => [
371
                [
372
                    'id' => '1',
373
                    'name' => 'Sammy Kaye Powers',
374
                ],
375
            ],
376
            'paging' => [
377
                'cursors' => [
378
                    'after' => 'like_after_cursor',
379
                    'before' => 'like_before_cursor',
380
                ],
381
            ],
382
        ];
383
384
        $photosList = [
385
            'data' => [
386
                [
387
                    'id' => '777',
388
                    'name' => 'Foo Photo',
389
                    'likes' => $likesList,
390
                ],
391
            ],
392
            'paging' => [
393
                'cursors' => [
394
                    'after' => 'photo_after_cursor',
395
                    'before' => 'photo_before_cursor',
396
                ],
397
            ],
398
        ];
399
400
        $data = json_encode([
401
            'data' => [
402
                [
403
                    'id' => '111',
404
                    'name' => 'Foo McBar',
405
                    'likes' => $likesList,
406
                    'photos' => $photosList,
407
                ],
408
                [
409
                    'id' => '222',
410
                    'name' => 'Bar McBaz',
411
                    'likes' => $likesList,
412
                    'photos' => $photosList,
413
                ],
414
            ],
415
            'paging' => [
416
                'next' => 'http://facebook/next',
417
                'previous' => 'http://facebook/prev',
418
            ],
419
        ]);
420
        $res = new Response($this->request, $data);
421
422
        $factory = new GraphNodeFactory($res);
423
        $graphEdge = $factory->makeGraphEdge();
424
        $topGraphEdge = $graphEdge->getParentGraphEdge();
425
        $childGraphEdgeOne = $graphEdge[0]->getField('likes')->getParentGraphEdge();
426
        $childGraphEdgeTwo = $graphEdge[1]->getField('likes')->getParentGraphEdge();
427
        $childGraphEdgeThree = $graphEdge[1]->getField('photos')->getParentGraphEdge();
428
        $childGraphEdgeFour = $graphEdge[1]->getField('photos')[0]->getField('likes')->getParentGraphEdge();
429
430
        $this->assertNull($topGraphEdge);
431
        $this->assertEquals('/111/likes', $childGraphEdgeOne);
432
        $this->assertEquals('/222/likes', $childGraphEdgeTwo);
433
        $this->assertEquals('/222/photos', $childGraphEdgeThree);
434
        $this->assertEquals('/777/likes', $childGraphEdgeFour);
435
    }
436
}
437