-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResultSetTest.php
More file actions
164 lines (136 loc) · 4.17 KB
/
ResultSetTest.php
File metadata and controls
164 lines (136 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace ipl\Tests\Orm;
use ArrayIterator;
use BadMethodCallException;
use ipl\Orm\ResultSet;
use PHPUnit\Framework\TestCase;
class ResultSetTest extends TestCase
{
public function testResultIsProperlyAdvancedOnPhp56()
{
$set = new ResultSet(new ArrayIterator(['a', 'b', 'c']));
$items = [];
foreach ($set as $item) {
$items[] = $item;
}
$this->assertEquals(
['a', 'b', 'c'],
$items,
'ArrayIterator::offsetSet() (used in ResultSet::advance()) does not seek automatically on PHP 5.6'
);
}
public function testResultWithCacheDisabled()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c'])))->disableCache();
$items = [];
foreach ($set as $item) {
$items[] = $item;
}
// When cache disabled, $set can be iterated only once, so this loop will be skipped
foreach ($set as $item) {
$items[] = $item;
}
$this->assertEquals(
$items,
['a', 'b', 'c']
);
}
public function testResultWithCacheEnabled()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c'])));
$items = [];
foreach ($set as $item) {
$items[] = $item;
}
foreach ($set as $item) {
$items[] = $item;
}
$this->assertEquals(
$items,
['a', 'b', 'c', 'a', 'b', 'c']
);
}
public function testResultWithCacheEnabledWithLimit()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c']), 2));
$items = [];
foreach ($set as $item) {
$items[] = $item;
}
foreach ($set as $item) {
$items[] = $item;
}
$this->assertEquals(
$items,
['a', 'b', 'a', 'b']
);
}
public function testResultPaging()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g'])))
->setPageSize(2);
$count = 0;
foreach ($set as $item) {
++$count;
if ($count > 2) {
if ($count % 2 === 0) {
// a multiple of two, page should equal to count / 2
$this->assertEquals(
$set->getCurrentPage(),
$count / 2
);
} elseif ($count % 2 === 1) {
$this->assertEquals(
$set->getCurrentPage(),
intval(ceil($count / 2))
);
}
} else {
$this->assertEquals(
$set->getCurrentPage(),
1
);
}
}
}
public function testResultPagingWithoutPageSize()
{
$this->expectException(BadMethodCallException::class);
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g'])));
foreach ($set as $_) {
// this raises an exception as no page size has been set
$set->getCurrentPage();
}
}
public function testResultPagingWithOffset()
{
$set = (new ResultSet(new ArrayIterator(['d', 'e', 'f', 'g', 'h', 'i', 'j']), null, 3))
->setPageSize(2);
$count = 0;
foreach ($set as $_) {
++$count;
$offsetCount = $count + 3;
if ($offsetCount % 2 === 0) {
// a multiple of two, page should equal to offsetCount / 2
$this->assertEquals(
$set->getCurrentPage(),
$offsetCount / 2
);
} elseif ($offsetCount % 2 === 1) {
$this->assertEquals(
$set->getCurrentPage(),
intval(ceil($offsetCount / 2))
);
}
}
}
public function testResultPagingBeforeIteration()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g'])))
->setPageSize(2);
$this->assertEquals(
$set->getCurrentPage(),
1
);
}
}