1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Porpaginas\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Zenstruck\Porpaginas\Result; |
7
|
|
|
|
8
|
|
|
abstract class ResultTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @test |
12
|
|
|
*/ |
13
|
|
|
public function it_counts_total_items(): void |
14
|
|
|
{ |
15
|
|
|
$result = $this->createResultWithItems(2); |
16
|
|
|
|
17
|
|
|
$this->assertCount(2, $result); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
*/ |
23
|
|
|
public function it_iterates_over_all_items(): void |
24
|
|
|
{ |
25
|
|
|
$result = $this->createResultWithItems(11); |
26
|
|
|
|
27
|
|
|
$this->assertCount(11, \iterator_to_array($result)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function it_takes_slice_as_page(): void |
34
|
|
|
{ |
35
|
|
|
$result = $this->createResultWithItems(11); |
36
|
|
|
|
37
|
|
|
$page = $result->take(0, 10); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(1, $page->currentPage()); |
40
|
|
|
$this->assertEquals(0, $page->currentOffset()); |
41
|
|
|
$this->assertEquals(10, $page->currentLimit()); |
42
|
|
|
$this->assertCount(10, $page); |
43
|
|
|
$this->assertEquals(11, $page->totalCount()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function it_counts_last_page_of_slice_correctly(): void |
50
|
|
|
{ |
51
|
|
|
$result = $this->createResultWithItems(11); |
52
|
|
|
|
53
|
|
|
$page = $result->take(10, 10); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(2, $page->currentPage()); |
56
|
|
|
$this->assertEquals(10, $page->currentOffset()); |
57
|
|
|
$this->assertEquals(10, $page->currentLimit()); |
58
|
|
|
$this->assertCount(1, $page); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
public function it_counts_page_first_then_iterates(): void |
65
|
|
|
{ |
66
|
|
|
$result = $this->createResultWithItems(16); |
67
|
|
|
|
68
|
|
|
$page = $result->take(10, 5); |
69
|
|
|
|
70
|
|
|
$this->assertCount(5, $page); |
71
|
|
|
$this->assertCount(5, \iterator_to_array($page)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function it_itereates_first_then_counts_page(): void |
78
|
|
|
{ |
79
|
|
|
$result = $this->createResultWithItems(16); |
80
|
|
|
|
81
|
|
|
$page = $result->take(10, 5); |
82
|
|
|
|
83
|
|
|
$this->assertCount(5, \iterator_to_array($page)); |
84
|
|
|
$this->assertCount(5, $page); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function results_match_the_expected_value(): void |
91
|
|
|
{ |
92
|
|
|
$result = $this->createResultWithItems(11); |
93
|
|
|
|
94
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(1), \iterator_to_array($result)[0]); |
95
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(5), \iterator_to_array($result)[4]); |
96
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(11), \iterator_to_array($result)[10]); |
97
|
|
|
|
98
|
|
|
$page = $result->take(0, 10); |
99
|
|
|
|
100
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(1), \iterator_to_array($page)[0]); |
101
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(10), \iterator_to_array($page)[9]); |
102
|
|
|
|
103
|
|
|
$page = $result->take(10, 10); |
104
|
|
|
|
105
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(11), \iterator_to_array($page)[0]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @test |
110
|
|
|
*/ |
111
|
|
|
public function it_can_have_empty_results(): void |
112
|
|
|
{ |
113
|
|
|
$result = $this->createResultWithItems(0); |
114
|
|
|
|
115
|
|
|
$this->assertCount(0, $result); |
116
|
|
|
$this->assertSame([], \iterator_to_array($result)); |
117
|
|
|
$this->assertSame([], \iterator_to_array($result->take(0, 10))); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @test |
122
|
|
|
*/ |
123
|
|
|
public function it_can_paginate(): void |
124
|
|
|
{ |
125
|
|
|
$result = $this->createResultWithItems(11); |
126
|
|
|
|
127
|
|
|
$pager = $result->paginate(); |
128
|
|
|
|
129
|
|
|
$this->assertCount(11, $pager); |
130
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(1), \iterator_to_array($pager)[0]); |
131
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(5), \iterator_to_array($pager)[4]); |
132
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(11), \iterator_to_array($pager)[10]); |
133
|
|
|
|
134
|
|
|
$pager = $result->paginate(2, 10); |
135
|
|
|
|
136
|
|
|
$this->assertCount(1, $pager); |
137
|
|
|
$this->assertEquals($this->getExpectedValueAtPosition(11), \iterator_to_array($pager)[0]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
abstract protected function createResultWithItems(int $count): Result; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
abstract protected function getExpectedValueAtPosition(int $position); |
146
|
|
|
} |
147
|
|
|
|