Passed
Pull Request — master (#7)
by Chito
01:28
created

PaginationResultTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 47
c 1
b 0
f 0
dl 0
loc 95
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testIterator() 0 38 1
A testJsonSerialize() 0 53 1
1
<?php
2
3
namespace Lampager\Cake\Test\TestCase;
4
5
use Cake\I18n\Time;
6
use Cake\ORM\Entity;
7
use Lampager\Cake\PaginationResult;
8
9
class PaginationResultTest extends TestCase
10
{
11
    public function testIterator()
12
    {
13
        $entities = [
14
            new Entity([
15
                'id' => 1,
16
                'modified' => new Time('2017-01-01 10:00:00'),
17
            ]),
18
            new Entity([
19
                'id' => 3,
20
                'modified' => new Time('2017-01-01 10:00:00'),
21
            ]),
22
            new Entity([
23
                'id' => 5,
24
                'modified' => new Time('2017-01-01 10:00:00'),
25
            ]),
26
        ];
27
28
        $actual = new PaginationResult($entities, []);
29
30
        $this->assertTrue($actual->valid());
31
        $this->assertSame(0, $actual->key());
32
        $this->assertSame($entities[0], $actual->current());
33
34
        $actual->next();
35
        $this->assertTrue($actual->valid());
36
        $this->assertSame(1, $actual->key());
37
        $this->assertSame($entities[1], $actual->current());
38
39
        $actual->next();
40
        $this->assertTrue($actual->valid());
41
        $this->assertSame(2, $actual->key());
42
        $this->assertSame($entities[2], $actual->current());
43
44
        $actual->next();
45
        $this->assertFalse($actual->valid());
46
47
        $actual->rewind();
48
        $this->assertTrue($actual->valid());
49
    }
50
51
    public function testJsonSerialize()
52
    {
53
        $entities = [
54
            new Entity([
55
                'id' => 1,
56
                'modified' => new Time('2017-01-01 10:00:00'),
57
            ]),
58
            new Entity([
59
                'id' => 3,
60
                'modified' => new Time('2017-01-01 10:00:00'),
61
            ]),
62
            new Entity([
63
                'id' => 5,
64
                'modified' => new Time('2017-01-01 10:00:00'),
65
            ]),
66
        ];
67
68
        $meta = [
69
            'hasPrevious' => null,
70
            'previousCursor' => null,
71
            'hasNext' => true,
72
            'nextCursor' => [
73
                'Posts.id' => 2,
74
                'Posts.modified' => new Time('2017-01-01 11:00:00'),
75
            ],
76
        ];
77
78
        $actual = json_encode(new PaginationResult($entities, $meta));
79
        $expected = '{
80
            "records": [
81
                {
82
                    "id": 1,
83
                    "modified": "2017-01-01T10:00:00+00:00"
84
                },
85
                {
86
                    "id": 3,
87
                    "modified": "2017-01-01T10:00:00+00:00"
88
                },
89
                {
90
                    "id": 5,
91
                    "modified": "2017-01-01T10:00:00+00:00"
92
                }
93
            ],
94
            "hasPrevious": null,
95
            "previousCursor": null,
96
            "hasNext": true,
97
            "nextCursor": {
98
                "Posts.id": 2,
99
                "Posts.modified": "2017-01-01T11:00:00+00:00"
100
            }
101
        }';
102
103
        $this->assertJsonStringEqualsJsonString($expected, $actual);
104
    }
105
}
106