Completed
Push — master ( fd3679...f7d236 )
by
unknown
171:43 queued 171:39
created

GraphEdgeTest::testCanMapOverNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 19
rs 9.4285
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\GraphNode\GraphEdge;
28
use Facebook\GraphNode\GraphNode;
29
use PHPUnit\Framework\TestCase;
30
31
class GraphEdgeTest extends TestCase
32
{
33
34
    /**
35
     * @var \Facebook\Request
36
     */
37
    protected $request;
38
39
    protected $pagination = [
40
        'next' => 'https://graph.facebook.com/v7.12/998899/photos?pretty=0&limit=25&after=foo_after_cursor',
41
        'previous' => 'https://graph.facebook.com/v7.12/998899/photos?pretty=0&limit=25&before=foo_before_cursor',
42
    ];
43
44 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $app = new Application('123', 'foo_app_secret');
47
        $this->request = new Request(
48
            $app,
49
            'foo_token',
50
            'GET',
51
            '/me/photos?keep=me',
52
            ['foo' => 'bar'],
53
            'foo_eTag',
54
            'v1337'
55
        );
56
    }
57
58
    /**
59
     * @expectedException \Facebook\Exception\SDKException
60
     */
61
    public function testNonGetRequestsWillThrow()
62
    {
63
        $this->request->setMethod('POST');
64
        $graphEdge = new GraphEdge($this->request);
65
        $graphEdge->validateForPagination();
66
    }
67
68
    public function testCanReturnGraphGeneratedPaginationEndpoints()
69
    {
70
        $graphEdge = new GraphEdge(
71
            $this->request,
72
            [],
73
            ['paging' => $this->pagination]
74
        );
75
        $nextPage = $graphEdge->getPaginationUrl('next');
76
        $prevPage = $graphEdge->getPaginationUrl('previous');
77
78
        $this->assertEquals('/998899/photos?pretty=0&limit=25&after=foo_after_cursor', $nextPage);
79
        $this->assertEquals('/998899/photos?pretty=0&limit=25&before=foo_before_cursor', $prevPage);
80
    }
81
82
    public function testCanInstantiateNewPaginationRequest()
83
    {
84
        $graphEdge = new GraphEdge(
85
            $this->request,
86
            [],
87
            ['paging' => $this->pagination],
88
            '/1234567890/likes'
89
        );
90
        $nextPage = $graphEdge->getNextPageRequest();
91
        $prevPage = $graphEdge->getPreviousPageRequest();
92
93
        $this->assertInstanceOf(Request::class, $nextPage);
94
        $this->assertInstanceOf(Request::class, $prevPage);
95
        $this->assertNotSame($this->request, $nextPage);
96
        $this->assertNotSame($this->request, $prevPage);
97
        $this->assertEquals('/v1337/998899/photos?access_token=foo_token&after=foo_after_cursor&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&foo=bar&limit=25&pretty=0', $nextPage->getUrl());
98
        $this->assertEquals('/v1337/998899/photos?access_token=foo_token&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&before=foo_before_cursor&foo=bar&limit=25&pretty=0', $prevPage->getUrl());
99
    }
100
101
    public function testCanMapOverNodes()
102
    {
103
        $graphEdge = new GraphEdge(
104
            $this->request,
105
            [
106
                new GraphNode(['name' => 'dummy1']),
107
                new GraphNode(['name' => 'dummy2']),
108
            ],
109
            ['paging' => $this->pagination],
110
            '/1234567890/likes'
111
        );
112
113
        $output = '';
114
115
        $graphEdge->map(function (GraphNode $node) use (&$output) {
116
            $output .= $node->getField('name');
117
        });
118
119
        $this->assertEquals('dummy1dummy2', $output);
120
    }
121
122 View Code Duplication
    public function testAnExistingPropertyCanBeAccessed()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
125
126
        $field = $graphEdge->getField('foo');
127
        $this->assertEquals('bar', $field);
128
    }
129
130 View Code Duplication
    public function testAMissingPropertyWillReturnNull()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
133
        $field = $graphEdge->getField('baz');
134
135
        $this->assertNull($field, 'Expected the property to return null.');
136
    }
137
138 View Code Duplication
    public function testAMissingPropertyWillReturnTheDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
141
142
        $field = $graphEdge->getField('baz', 'faz');
143
        $this->assertEquals('faz', $field);
144
    }
145
146 View Code Duplication
    public function testFalseDefaultsWillReturnSameType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
149
150
        $field = $graphEdge->getField('baz', '');
151
        $this->assertSame('', $field);
152
153
        $field = $graphEdge->getField('baz', 0);
154
        $this->assertSame(0, $field);
155
156
        $field = $graphEdge->getField('baz', false);
157
        $this->assertFalse($field);
158
    }
159
160
    public function testTheKeysFromTheCollectionCanBeReturned()
161
    {
162
        $graphEdge = new GraphEdge(
163
            $this->request, [
164
                'key1' => 'foo',
165
                'key2' => 'bar',
166
                'key3' => 'baz',
167
            ]
168
        );
169
170
        $fieldNames = $graphEdge->getFieldNames();
171
        $this->assertEquals(['key1', 'key2', 'key3'], $fieldNames);
172
    }
173
174
    public function testAnArrayCanBeInjectedViaTheConstructor()
175
    {
176
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar']);
177
        $this->assertEquals(['foo', 'bar'], $graphEdge->asArray());
178
    }
179
180 View Code Duplication
    public function testACollectionCanBeConvertedToProperJson()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar', 123]);
183
184
        $graphEdgeAsString = $graphEdge->asJson();
185
186
        $this->assertEquals('["foo","bar",123]', $graphEdgeAsString);
187
    }
188
189 View Code Duplication
    public function testACollectionCanBeCounted()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar', 'baz']);
192
193
        $graphEdgeCount = count($graphEdge);
194
195
        $this->assertEquals(3, $graphEdgeCount);
196
    }
197
198
    public function testACollectionCanBeAccessedAsAnArray()
199
    {
200
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar', 'faz' => 'baz']);
201
202
        $this->assertEquals('bar', $graphEdge['foo']);
203
        $this->assertEquals('baz', $graphEdge['faz']);
204
    }
205
206
    public function testACollectionCanBeIteratedOver()
207
    {
208
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar', 'faz' => 'baz']);
209
210
        $this->assertInstanceOf(\IteratorAggregate::class, $graphEdge);
211
212
        $newArray = [];
213
214
        foreach ($graphEdge as $k => $v) {
215
            $newArray[$k] = $v;
216
        }
217
218
        $this->assertEquals(['foo' => 'bar', 'faz' => 'baz'], $newArray);
219
    }
220
}
221