Passed
Pull Request — master (#899)
by Yassine
33:24
created

GraphEdgeTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 197
Duplicated Lines 38.58 %

Importance

Changes 0
Metric Value
dl 76
loc 197
rs 10
c 0
b 0
f 0
wmc 16

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanInstantiateNewPaginationRequest() 0 17 1
B testCanMapOverNodes() 0 28 1
A setUp() 11 11 1
A testNonGetRequestsWillThrow() 0 5 1
A testCanReturnGraphGeneratedPaginationEndpoints() 0 12 1
A testTheKeysFromTheCollectionCanBeReturned() 0 12 1
A testAMissingPropertyWillReturnTheDefault() 6 6 1
A testFalseDefaultsWillReturnSameType() 12 12 1
A testACollectionCanBeCounted() 7 7 1
A testACollectionCanBeConvertedToProperJson() 7 7 1
A testAnArrayCanBeInjectedViaTheConstructor() 0 4 1
A testACollectionCanBeAccessedAsAnArray() 0 6 1
A testACollectionCanBeIteratedOver() 13 13 2
A testAMissingPropertyWillReturnNull() 6 6 1
A testAnExistingPropertyCanBeAccessed() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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' => 'dummy']),
107
                new GraphNode(['name' => 'dummy']),
108
            ],
109
            ['paging' => $this->pagination],
110
            '/1234567890/likes'
111
        );
112
113
        $graphEdge = $graphEdge->map(function (GraphNode $node) {
114
            $node['name'] = str_replace('dummy', 'foo', $node['name']);
115
            return $node;
116
        });
117
118
        $graphEdgeToCompare = new GraphEdge(
119
            $this->request,
120
            [
121
                new GraphNode(['name' => 'foo']),
122
                new GraphNode(['name' => 'foo'])
123
            ],
124
            ['paging' => $this->pagination],
125
            '/1234567890/likes'
126
        );
127
128
        $this->assertEquals($graphEdgeToCompare, $graphEdge);
129
    }
130
131 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...
132
    {
133
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
134
135
        $field = $graphEdge->getField('foo');
136
        $this->assertEquals('bar', $field);
137
    }
138
139 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...
140
    {
141
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
142
        $field = $graphEdge->getField('baz');
143
144
        $this->assertNull($field, 'Expected the property to return null.');
145
    }
146
147 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...
148
    {
149
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
150
151
        $field = $graphEdge->getField('baz', 'faz');
152
        $this->assertEquals('faz', $field);
153
    }
154
155 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...
156
    {
157
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar']);
158
159
        $field = $graphEdge->getField('baz', '');
160
        $this->assertSame('', $field);
161
162
        $field = $graphEdge->getField('baz', 0);
163
        $this->assertSame(0, $field);
164
165
        $field = $graphEdge->getField('baz', false);
166
        $this->assertFalse($field);
167
    }
168
169
    public function testTheKeysFromTheCollectionCanBeReturned()
170
    {
171
        $graphEdge = new GraphEdge(
172
            $this->request, [
173
                'key1' => 'foo',
174
                'key2' => 'bar',
175
                'key3' => 'baz',
176
            ]
177
        );
178
179
        $fieldNames = $graphEdge->getFieldNames();
180
        $this->assertEquals(['key1', 'key2', 'key3'], $fieldNames);
181
    }
182
183
    public function testAnArrayCanBeInjectedViaTheConstructor()
184
    {
185
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar']);
186
        $this->assertEquals(['foo', 'bar'], $graphEdge->asArray());
187
    }
188
189 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...
190
    {
191
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar', 123]);
192
193
        $graphEdgeAsString = $graphEdge->asJson();
194
195
        $this->assertEquals('["foo","bar",123]', $graphEdgeAsString);
196
    }
197
198 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...
199
    {
200
        $graphEdge = new GraphEdge($this->request, ['foo', 'bar', 'baz']);
201
202
        $graphEdgeCount = count($graphEdge);
203
204
        $this->assertEquals(3, $graphEdgeCount);
205
    }
206
207
    public function testACollectionCanBeAccessedAsAnArray()
208
    {
209
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar', 'faz' => 'baz']);
210
211
        $this->assertEquals('bar', $graphEdge['foo']);
212
        $this->assertEquals('baz', $graphEdge['faz']);
213
    }
214
215 View Code Duplication
    public function testACollectionCanBeIteratedOver()
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...
216
    {
217
        $graphEdge = new GraphEdge($this->request, ['foo' => 'bar', 'faz' => 'baz']);
218
219
        $this->assertInstanceOf(\IteratorAggregate::class, $graphEdge);
220
221
        $newArray = [];
222
223
        foreach ($graphEdge as $k => $v) {
224
            $newArray[$k] = $v;
225
        }
226
227
        $this->assertEquals(['foo' => 'bar', 'faz' => 'baz'], $newArray);
228
    }
229
}
230