1 | <?php |
||
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([ |
||
70 |