Completed
Pull Request — master (#204)
by Ryan
11:34
created

ArrayConnectionTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 4:33 PM 5/18/16
18
 */
19
20
namespace Youshido\Tests\Library\Relay;
21
22
use Youshido\GraphQL\Relay\Connection\ArrayConnection;
23
24
class ArrayConnectionTest extends \PHPUnit_Framework_TestCase
25
{
26
    public function testCursors(): void
27
    {
28
        $offset = 3;
29
        $data   = ['a', 'b', 'c', 'd', 'e'];
30
        $cursor = ArrayConnection::keyToCursor($offset);
31
32
        $this->assertEquals($offset, ArrayConnection::cursorToKey($cursor));
33
        $this->assertEquals($cursor, ArrayConnection::cursorForObjectInConnection($data, 'd'));
34
        $this->assertNull(null, ArrayConnection::cursorToKey(null));
35
36
        $this->assertEquals($offset, ArrayConnection::cursorToOffsetWithDefault($cursor, 2));
37
        $this->assertEquals(2, ArrayConnection::cursorToOffsetWithDefault(null, 2));
38
    }
39
40
    public function testConnectionDefinition(): void
41
    {
42
        $data  = ['a', 'b', 'c', 'd', 'e'];
43
        $edges = [];
44
45
        foreach ($data as $key => $item) {
46
            $edges[] = ArrayConnection::edgeForObjectWithIndex($item, $key);
47
        }
48
49
        $this->assertEquals([
50
            'edges'    => $edges,
51
            'pageInfo' => [
52
                'startCursor'     => $edges[0]['cursor'],
53
                'endCursor'       => $edges[\count($edges) - 1]['cursor'],
54
                'hasPreviousPage' => false,
55
                'hasNextPage'     => false,
56
            ],
57
        ], ArrayConnection::connectionFromArray($data));
58
59
        $this->assertEquals([
60
            'edges'    => \array_slice($edges, 0, 2),
61
            'pageInfo' => [
62
                'startCursor'     => $edges[0]['cursor'],
63
                'endCursor'       => $edges[1]['cursor'],
64
                'hasPreviousPage' => false,
65
                'hasNextPage'     => true,
66
            ],
67
        ], ArrayConnection::connectionFromArray($data, ['first' => 2, 'last' => 4]));
68
    }
69
}
70