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

PaginationResultTest::testJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 53
rs 9.6
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function testSerializeAndUnserialize()
107
    {
108
        $entities = [
109
            new Entity([
110
                'id' => 1,
111
                'modified' => new Time('2017-01-01 10:00:00'),
112
            ]),
113
            new Entity([
114
                'id' => 3,
115
                'modified' => new Time('2017-01-01 10:00:00'),
116
            ]),
117
            new Entity([
118
                'id' => 5,
119
                'modified' => new Time('2017-01-01 10:00:00'),
120
            ]),
121
        ];
122
123
        $meta = [
124
            'hasPrevious' => null,
125
            'previousCursor' => null,
126
            'hasNext' => true,
127
            'nextCursor' => [
128
                'Posts.id' => 2,
129
                'Posts.modified' => new Time('2017-01-01 11:00:00'),
130
            ],
131
        ];
132
133
        $actual = unserialize(serialize(new PaginationResult($entities, $meta)));
134
        $expected = new PaginationResult($entities, $meta);
135
        $this->assertJsonEquals($expected, $actual);
136
    }
137
}
138