Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( cf18f9...42c7f7 )
by Jérémiah
41s
created

testWorksWithAJustRightArraySlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Tests\Relay\Connection\Output;
13
14
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
15
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
16
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
17
use Overblog\GraphQLBundle\Relay\Connection\Output\PageInfo;
18
19
/**
20
 * Class ConnectionBuilderTest.
21
 *
22
 * @see https://github.com/graphql/graphql-relay-js/blob/master/src/connection/__tests__/arrayconnection.js
23
 */
24
class ConnectionBuilderTest extends \PHPUnit_Framework_TestCase
25
{
26
    private $letters = ['A', 'B', 'C', 'D', 'E'];
27
28 View Code Duplication
    public function testBasicSlicing()
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...
29
    {
30
        $actual = ConnectionBuilder::connectionFromArray($this->letters);
31
32
        $expected = $this->getExpectedConnection($this->letters, false, false);
33
34
        $this->assertEquals($expected, $actual);
35
    }
36
37
    public function testRespectsASmallerFirst()
38
    {
39
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 2]);
40
41
        $expected = $this->getExpectedConnection(['A', 'B'], false, true);
42
43
        $this->assertEquals($expected, $actual);
44
    }
45
46 View Code Duplication
    public function testRespectsAnOverlyLargeFirst()
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...
47
    {
48
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 10]);
49
50
        $expected = $this->getExpectedConnection($this->letters, false, false);
51
52
        $this->assertEquals($expected, $actual);
53
    }
54
55
    public function testRespectsASmallerLast()
56
    {
57
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 2]);
58
59
        $expected = $this->getExpectedConnection(['D', 'E'], true, false);
60
61
        $this->assertEquals($expected, $actual);
62
    }
63
64 View Code Duplication
    public function testRespectsAnOverlyLargeLast()
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...
65
    {
66
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 10]);
67
68
        $expected = $this->getExpectedConnection($this->letters, false, false);
69
70
        $this->assertEquals($expected, $actual);
71
    }
72
73
    public function testRespectsFirstAndAfter()
74
    {
75
        $actual = ConnectionBuilder::connectionFromArray(
76
            $this->letters,
77
            ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']
78
        );
79
80
        $expected = $this->getExpectedConnection(['C', 'D'], false, true);
81
82
        $this->assertEquals($expected, $actual);
83
    }
84
85 View Code Duplication
    public function testRespectsFirstAndAfterWithLongFirst()
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...
86
    {
87
        $actual = ConnectionBuilder::connectionFromArray(
88
            $this->letters,
89
            ['first' => 10, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']
90
        );
91
92
        $expected = $this->getExpectedConnection(['C', 'D', 'E'], false, false);
93
94
        $this->assertEquals($expected, $actual);
95
    }
96
97
    public function testRespectsLastAndBefore()
98
    {
99
        $actual = ConnectionBuilder::connectionFromArray(
100
            $this->letters,
101
            ['last' => 2, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']
102
        );
103
104
        $expected = $this->getExpectedConnection(['B', 'C'], true, false);
105
106
        $this->assertEquals($expected, $actual);
107
    }
108
109 View Code Duplication
    public function testRespectsLastAndBeforeWithLongLast()
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...
110
    {
111
        $actual = ConnectionBuilder::connectionFromArray(
112
            $this->letters,
113
            ['last' => 10, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']
114
        );
115
116
        $expected = $this->getExpectedConnection(['A', 'B', 'C'], false, false);
117
118
        $this->assertEquals($expected, $actual);
119
    }
120
121 View Code Duplication
    public function testRespectsFirstAndAfterAndBeforeTooFew()
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...
122
    {
123
        $actual = ConnectionBuilder::connectionFromArray(
124
            $this->letters,
125
            ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
126
        );
127
128
        $expected = $this->getExpectedConnection(['B', 'C'], false, true);
129
130
        $this->assertEquals($expected, $actual);
131
    }
132
133 View Code Duplication
    public function testRespectsFirstAndAfterAndBeforeTooMany()
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...
134
    {
135
        $actual = ConnectionBuilder::connectionFromArray(
136
            $this->letters,
137
            ['first' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
138
        );
139
140
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
141
142
        $this->assertEquals($expected, $actual);
143
    }
144
145 View Code Duplication
    public function testRespectsFirstAndAfterAndBeforeExactlyRight()
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...
146
    {
147
        $actual = ConnectionBuilder::connectionFromArray(
148
            $this->letters,
149
            ['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
150
        );
151
152
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
153
154
        $this->assertEquals($expected, $actual);
155
    }
156
157 View Code Duplication
    public function testRespectsLastAndAfterAndBeforeTooFew()
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...
158
    {
159
        $actual = ConnectionBuilder::connectionFromArray(
160
            $this->letters,
161
            ['last' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
162
        );
163
164
        $expected = $this->getExpectedConnection(['C', 'D'], true, false);
165
166
        $this->assertEquals($expected, $actual);
167
    }
168
169 View Code Duplication
    public function testRespectsLastAndAfterAndBeforeTooMany()
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...
170
    {
171
        $actual = ConnectionBuilder::connectionFromArray(
172
            $this->letters,
173
            ['last' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
174
        );
175
176
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
177
178
        $this->assertEquals($expected, $actual);
179
    }
180
181 View Code Duplication
    public function testRespectsLastAndAfterAndBeforeExactlyRight()
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...
182
    {
183
        $actual = ConnectionBuilder::connectionFromArray(
184
            $this->letters,
185
            ['last' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
186
        );
187
188
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
189
190
        $this->assertEquals($expected, $actual);
191
    }
192
193
    /**
194
     * @expectedException \InvalidArgumentException
195
     * @expectedExceptionMessage Argument "first" must be a non-negative integer
196
     */
197
    public function testThrowsAnErrorIfFirstLessThan0()
198
    {
199
        ConnectionBuilder::connectionFromArray(
200
            $this->letters,
201
            ['first' => -1]
202
        );
203
    }
204
205
    /**
206
     * @expectedException \InvalidArgumentException
207
     * @expectedExceptionMessage Argument "last" must be a non-negative integer
208
     */
209
    public function testThrowsAnErrorIfLastLessThan0()
210
    {
211
        ConnectionBuilder::connectionFromArray(
212
            $this->letters,
213
            ['last' => -1]
214
        );
215
    }
216
217
    public function testReturnsNoElementsIfFirstIs0()
218
    {
219
        $actual = ConnectionBuilder::connectionFromArray(
220
            $this->letters,
221
            ['first' => 0]
222
        );
223
224
        $expected = new Connection(
225
            [
226
            ],
227
            new PageInfo(null, null, false, true)
228
        );
229
230
        $this->assertEquals($expected, $actual);
231
    }
232
233
    public function testReturnsAllElementsIfCursorsAreInvalid()
234
    {
235
        $actual = ConnectionBuilder::connectionFromArray(
236
            $this->letters,
237
            ['before' => 'invalid', 'after' => 'invalid']
238
        );
239
240
        $expected = $this->getExpectedConnection($this->letters, false, false);
241
242
        $this->assertEquals($expected, $actual);
243
    }
244
245
    public function testReturnsAllElementsIfCursorsAreOnTheOutside()
246
    {
247
        $actual = ConnectionBuilder::connectionFromArray(
248
            $this->letters,
249
            ['before' => 'YXJyYXljb25uZWN0aW9uOjYK', 'after' => 'YXJyYXljb25uZWN0aW9uOi0xCg==']
250
        );
251
252
        $expected = $this->getExpectedConnection($this->letters, false, false);
253
254
        $this->assertEquals($expected, $actual);
255
    }
256
257
    public function testReturnsNoElementsIfCursorsCross()
258
    {
259
        $actual = ConnectionBuilder::connectionFromArray(
260
            $this->letters,
261
            ['before' => 'YXJyYXljb25uZWN0aW9uOjI=', 'after' => 'YXJyYXljb25uZWN0aW9uOjQ=']
262
        );
263
264
        $expected = $this->getExpectedConnection([], false, false);
265
266
        $this->assertEquals($expected, $actual);
267
    }
268
269
    /**
270
     * transcript of JS implementation test : works with a just-right array slice.
271
     */
272 View Code Duplication
    public function testWorksWithAJustRightArraySlice()
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...
273
    {
274
        $actual = ConnectionBuilder::connectionFromArraySlice(
275
            array_slice($this->letters, 1, 2), // equals to letters.slice(1,3) in JS
276
            ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA='],
277
            ['sliceStart' => 1, 'arrayLength' => 5]
278
        );
279
280
        $expected = $this->getExpectedConnection(['B', 'C'], false, true);
281
282
        $this->assertEquals($expected, $actual);
283
    }
284
285
    /**
286
     * transcript of JS implementation test : works with an oversized array slice ("left" side).
287
     */
288 View Code Duplication
    public function testWorksWithAnOversizedArraySliceLeftSide()
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...
289
    {
290
        $actual = ConnectionBuilder::connectionFromArraySlice(
291
            array_slice($this->letters, 0, 3), // equals to letters.slice(0,3) in JS
292
            ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA='],
293
            ['sliceStart' => 0, 'arrayLength' => 5]
294
        );
295
296
        $expected = $this->getExpectedConnection(['B', 'C'], false, true);
297
298
        $this->assertEquals($expected, $actual);
299
    }
300
301
    /**
302
     * transcript of JS implementation test : works with an oversized array slice ("right" side).
303
     */
304 View Code Duplication
    public function testWorksWithAnOversizedArraySliceRightSide()
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...
305
    {
306
        $actual = ConnectionBuilder::connectionFromArraySlice(
307
            array_slice($this->letters, 2, 2), // equals to letters.slice(2,4) in JS
308
            ['first' => 1, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='],
309
            ['sliceStart' => 2, 'arrayLength' => 5]
310
        );
311
312
        $expected = $this->getExpectedConnection(['C'], false, true);
313
314
        $this->assertEquals($expected, $actual);
315
    }
316
317
    /**
318
     * transcript of JS implementation test : works with an oversized array slice (both sides).
319
     */
320 View Code Duplication
    public function testWorksWithAnOversizedArraySliceBothSides()
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...
321
    {
322
        $actual = ConnectionBuilder::connectionFromArraySlice(
323
            array_slice($this->letters, 1, 3), // equals to letters.slice(1,4) in JS
324
            ['first' => 1, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='],
325
            ['sliceStart' => 1, 'arrayLength' => 5]
326
        );
327
328
        $expected = $this->getExpectedConnection(['C'], false, true);
329
330
        $this->assertEquals($expected, $actual);
331
    }
332
333
    /**
334
     * transcript of JS implementation test : works with an undersized array slice ("left" side).
335
     */
336 View Code Duplication
    public function testWorksWithAnUndersizedArraySliceLeftSide()
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...
337
    {
338
        $actual = ConnectionBuilder::connectionFromArraySlice(
339
            array_slice($this->letters, 3, 2), // equals to letters.slice(3,5) in JS
340
            ['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='],
341
            ['sliceStart' => 3, 'arrayLength' => 5]
342
        );
343
344
        $expected = $this->getExpectedConnection(['D', 'E'], false, false);
345
346
        $this->assertEquals($expected, $actual);
347
    }
348
349
    /**
350
     * transcript of JS implementation test : works with an undersized array slice ("right" side).
351
     */
352 View Code Duplication
    public function testWorksWithAnUndersizedArraySliceRightSide()
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...
353
    {
354
        $actual = ConnectionBuilder::connectionFromArraySlice(
355
            array_slice($this->letters, 2, 2), // equals to letters.slice(2,4) in JS
356
            ['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='],
357
            ['sliceStart' => 2, 'arrayLength' => 5]
358
        );
359
360
        $expected = $this->getExpectedConnection(['C', 'D'], false, true);
361
362
        $this->assertEquals($expected, $actual);
363
    }
364
365
    /**
366
     * transcript of JS implementation test : works with an undersized array slice (both sides).
367
     */
368 View Code Duplication
    public function worksWithAnUndersizedArraySliceBothSides()
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...
369
    {
370
        $actual = ConnectionBuilder::connectionFromArraySlice(
371
            array_slice($this->letters, 3, 1), // equals to letters.slice(3,4) in JS
372
            ['first' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjE='],
373
            ['sliceStart' => 3, 'arrayLength' => 5]
374
        );
375
376
        $expected = $this->getExpectedConnection(['D'], false, true);
377
378
        $this->assertEquals($expected, $actual);
379
    }
380
381
    public function testReturnsAnEdgesCursorGivenAnArrayAndAMemberObject()
382
    {
383
        $letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'B');
384
385
        $this->assertEquals('YXJyYXljb25uZWN0aW9uOjE=', $letterCursor);
386
    }
387
388
    public function testReturnsAnEdgesCursorGivenAnArrayAndANonMemberObject()
389
    {
390
        $letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'F');
391
392
        $this->assertNull($letterCursor);
393
    }
394
395
    private function getExpectedConnection(array $wantedEdges, $hasPreviousPage, $hasNextPage)
396
    {
397
        $edges = [
398
            'A' => new Edge('YXJyYXljb25uZWN0aW9uOjA=', 'A'),
399
            'B' => new Edge('YXJyYXljb25uZWN0aW9uOjE=', 'B'),
400
            'C' => new Edge('YXJyYXljb25uZWN0aW9uOjI=', 'C'),
401
            'D' => new Edge('YXJyYXljb25uZWN0aW9uOjM=', 'D'),
402
            'E' => new Edge('YXJyYXljb25uZWN0aW9uOjQ=', 'E'),
403
        ];
404
405
        $expectedEdges = array_values(array_intersect_key($edges, array_flip($wantedEdges)));
406
407
        return new Connection(
408
            $expectedEdges,
409
            new PageInfo(
410
                isset($expectedEdges[0]) ? $expectedEdges[0]->cursor : null,
411
                end($expectedEdges) ? end($expectedEdges)->cursor : null,
412
                $hasPreviousPage,
413
                $hasNextPage
414
            )
415
        );
416
    }
417
}
418