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
Pull Request — master (#79)
by Jérémiah
04:33
created

testThrowsAnErrorIfLastLessThan0()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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($this->letters, ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']);
76
77
        $expected = $this->getExpectedConnection(['C', 'D'], false, true);
78
79
        $this->assertEquals($expected, $actual);
80
    }
81
82 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...
83
    {
84
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['first' => 10, 'after' => 'YXJyYXljb25uZWN0aW9uOjE=']);
85
86
        $expected = $this->getExpectedConnection(['C', 'D', 'E'], false, false);
87
88
        $this->assertEquals($expected, $actual);
89
    }
90
91
    public function testRespectsLastAndBefore()
92
    {
93
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 2, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']);
94
95
        $expected = $this->getExpectedConnection(['B', 'C'], true, false);
96
97
        $this->assertEquals($expected, $actual);
98
    }
99
100 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...
101
    {
102
        $actual = ConnectionBuilder::connectionFromArray($this->letters, ['last' => 10, 'before' => 'YXJyYXljb25uZWN0aW9uOjM=']);
103
104
        $expected = $this->getExpectedConnection(['A', 'B', 'C'], false, false);
105
106
        $this->assertEquals($expected, $actual);
107
    }
108
109 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...
110
    {
111
        $actual = ConnectionBuilder::connectionFromArray(
112
            $this->letters,
113
            ['first' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
114
        );
115
116
        $expected = $this->getExpectedConnection(['B', 'C'], false, true);
117
118
        $this->assertEquals($expected, $actual);
119
    }
120
121 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...
122
    {
123
        $actual = ConnectionBuilder::connectionFromArray(
124
            $this->letters,
125
            ['first' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
126
        );
127
128
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
129
130
        $this->assertEquals($expected, $actual);
131
    }
132
133 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...
134
    {
135
        $actual = ConnectionBuilder::connectionFromArray(
136
            $this->letters,
137
            ['first' => 3, '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 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...
146
    {
147
        $actual = ConnectionBuilder::connectionFromArray(
148
            $this->letters,
149
            ['last' => 2, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
150
        );
151
152
        $expected = $this->getExpectedConnection(['C', 'D'], true, false);
153
154
        $this->assertEquals($expected, $actual);
155
    }
156
157 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...
158
    {
159
        $actual = ConnectionBuilder::connectionFromArray(
160
            $this->letters,
161
            ['last' => 4, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
162
        );
163
164
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
165
166
        $this->assertEquals($expected, $actual);
167
    }
168
169 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...
170
    {
171
        $actual = ConnectionBuilder::connectionFromArray(
172
            $this->letters,
173
            ['last' => 3, 'after' => 'YXJyYXljb25uZWN0aW9uOjA=', 'before' => 'YXJyYXljb25uZWN0aW9uOjQ=']
174
        );
175
176
        $expected = $this->getExpectedConnection(['B', 'C', 'D'], false, false);
177
178
        $this->assertEquals($expected, $actual);
179
    }
180
181
    /**
182
     * @expectedException \InvalidArgumentException
183
     * @expectedExceptionMessage Argument "first" must be a non-negative integer
184
     */
185
    public function testThrowsAnErrorIfFirstLessThan0()
186
    {
187
        ConnectionBuilder::connectionFromArray(
188
            $this->letters,
189
            ['first' => -1]
190
        );
191
    }
192
193
    /**
194
     * @expectedException \InvalidArgumentException
195
     * @expectedExceptionMessage Argument "last" must be a non-negative integer
196
     */
197
    public function testThrowsAnErrorIfLastLessThan0()
198
    {
199
        ConnectionBuilder::connectionFromArray(
200
            $this->letters,
201
            ['last' => -1]
202
        );
203
    }
204
205
    public function testReturnsNoElementsIfFirstIs0()
206
    {
207
        $actual = ConnectionBuilder::connectionFromArray(
208
            $this->letters,
209
            ['first' => 0]
210
        );
211
212
        $expected = new Connection(
213
            [
214
            ],
215
            new PageInfo(null, null, false, true)
216
        );
217
218
        $this->assertEquals($expected, $actual);
219
    }
220
221
    public function testReturnsAllElementsIfCursorsAreInvalid()
222
    {
223
        $actual = ConnectionBuilder::connectionFromArray(
224
            $this->letters,
225
            ['before' => 'invalid', 'after' => 'invalid']
226
        );
227
228
        $expected = $this->getExpectedConnection($this->letters, false, false);
229
230
        $this->assertEquals($expected, $actual);
231
    }
232
233
    public function testReturnsAllElementsIfCursorsAreOnTheOutside()
234
    {
235
        $actual = ConnectionBuilder::connectionFromArray(
236
            $this->letters,
237
            ['before' => 'YXJyYXljb25uZWN0aW9uOjYK', 'after' => 'YXJyYXljb25uZWN0aW9uOi0xCg==']
238
        );
239
240
        $expected = $this->getExpectedConnection($this->letters, false, false);
241
242
        $this->assertEquals($expected, $actual);
243
    }
244
245
    public function testReturnsNoElementsIfCursorsCross()
246
    {
247
        $actual = ConnectionBuilder::connectionFromArray(
248
            $this->letters,
249
            ['before' => 'YXJyYXljb25uZWN0aW9uOjI=', 'after' => 'YXJyYXljb25uZWN0aW9uOjQ=']
250
        );
251
252
        $expected = $this->getExpectedConnection([], false, false);
253
254
        $this->assertEquals($expected, $actual);
255
    }
256
257
    public function testReturnsAnEdgesCursorGivenAnArrayAndAMemberObject()
258
    {
259
        $letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'B');
260
261
        $this->assertEquals('YXJyYXljb25uZWN0aW9uOjE=', $letterCursor);
262
    }
263
264
    public function testReturnsAnEdgesCursorGivenAnArrayAndANonMemberObject()
265
    {
266
        $letterCursor = ConnectionBuilder::cursorForObjectInConnection($this->letters, 'F');
267
268
        $this->assertNull($letterCursor);
269
    }
270
271
    private function getExpectedConnection(array $wantedEdges, $hasPreviousPage, $hasNextPage)
272
    {
273
        $edges = [
274
            'A' => new Edge('YXJyYXljb25uZWN0aW9uOjA=', 'A'),
275
            'B' => new Edge('YXJyYXljb25uZWN0aW9uOjE=', 'B'),
276
            'C' => new Edge('YXJyYXljb25uZWN0aW9uOjI=', 'C'),
277
            'D' => new Edge('YXJyYXljb25uZWN0aW9uOjM=', 'D'),
278
            'E' => new Edge('YXJyYXljb25uZWN0aW9uOjQ=', 'E'),
279
        ];
280
281
        $expectedEdges = array_values(array_intersect_key($edges, array_flip($wantedEdges)));
282
283
        return new Connection(
284
            $expectedEdges,
285
            new PageInfo(
286
                isset($expectedEdges[0]) ? $expectedEdges[0]->cursor : null,
287
                end($expectedEdges) ? end($expectedEdges)->cursor : null,
288
                $hasPreviousPage,
289
                $hasNextPage
290
            )
291
        );
292
    }
293
}
294