ArrayConnectionTest::testCursors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * This file is a part of GraphQL project.
4
 *
5
 * @author Alexandr Viniychuk <[email protected]>
6
 * created: 4:33 PM 5/18/16
7
 */
8
9
namespace Youshido\Tests\Library\Relay;
10
11
use Youshido\GraphQL\Relay\Connection\ArrayConnection;
12
13
class ArrayConnectionTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function testCursors()
16
    {
17
        $offset = 3;
18
        $data   = ['a', 'b', 'c', 'd', 'e'];
19
        $cursor = ArrayConnection::keyToCursor($offset);
20
21
        $this->assertEquals($offset, ArrayConnection::cursorToKey($cursor));
22
        $this->assertEquals($cursor, ArrayConnection::cursorForObjectInConnection($data, 'd'));
23
        $this->assertNull(null, ArrayConnection::cursorToKey(null));
24
25
        $this->assertEquals($offset, ArrayConnection::cursorToOffsetWithDefault($cursor, 2));
26
        $this->assertEquals(2, ArrayConnection::cursorToOffsetWithDefault(null, 2));
27
    }
28
29
    public function testConnectionDefinition()
30
    {
31
        $data  = ['a', 'b', 'c', 'd', 'e'];
32
        $edges = [];
33
34
        foreach ($data as $key => $item) {
35
            $edges[] = ArrayConnection::edgeForObjectWithIndex($item, $key);
36
        }
37
38
        $this->assertEquals([
39
            'totalCount' => count($data),
40
            'edges'      => $edges,
41
            'pageInfo'   => [
42
                'startCursor'     => $edges[0]['cursor'],
43
                'endCursor'       => $edges[count($edges) - 1]['cursor'],
44
                'hasPreviousPage' => false,
45
                'hasNextPage'     => false,
46
            ],
47
        ], ArrayConnection::connectionFromArray($data));
48
49
        $this->assertEquals([
50
            'totalCount' => count($data),
51
            'edges'      => array_slice($edges, 0, 2),
52
            'pageInfo'   => [
53
                'startCursor'     => $edges[0]['cursor'],
54
                'endCursor'       => $edges[1]['cursor'],
55
                'hasPreviousPage' => false,
56
                'hasNextPage'     => true,
57
            ],
58
        ], ArrayConnection::connectionFromArray($data, ['first' => 2, 'last' => 4]));
59
    }
60
}
61