summaryrefslogtreecommitdiff
path: root/tests/unit/Collections/TPagedListTest.php
blob: c20c031920380817944e33e5dd3f015d1ec4e972 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

use Prado\Collections\TPagedList;
use Prado\Collections\TPagedListFetchDataEventParameter;
use Prado\Collections\TPagedListPageChangedEventParameter;
use Prado\Exceptions\TInvalidDataValueException;


class MyPagedList extends TPagedList {

	private $_isPageIndexChanged = false;
	private $_hasFetchedData = false;

	public function pageIndexChanged($sender, $param) {
		$this->_isPageIndexChanged = true;
	}

	public function fetchData($sender, $param) {
		$this->_hasFetchedData = true;
	}

	public function isPageIndexChanged() {
		return $this->_isPageIndexChanged;
	}

	public function hasFetchedData() {
		return $this->_hasFetchedData;
	}

}

/**
 * @package System.Collections
 */
class TPagedListTest extends PHPUnit_Framework_TestCase {

	public function setUp() {
	}

	public function tearDown() {
	}

	public function testConstruct() {
		$list = new TPagedList();
		self::assertEquals(true, $list->ReadOnly);
		self::assertEquals(-1, $list->VirtualCount);
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 3;
		self::assertEquals(3, $list->Count);
	}

	public function testCustomPaging() {
		$list = new TPagedList();
		$list->CustomPaging = true;
		self::assertEquals(true, $list->CustomPaging);
		$list->CustomPaging = false;
		self::assertEquals(false, $list->CustomPaging);
	}

	public function testPageSize() {
		$list = new TPagedList();
		$list->PageSize = 5;
		self::assertEquals(5, $list->PageSize);
	}

	public function testCanNotSetInvalidPageSize() {
		$list = new TPagedList();
		try {
			$list->PageSize = 0;
		} catch(TInvalidDataValueException $e) {
			return;
		}
		self::fail('An expected TInvalidDataValueException was not raised');
	}

	public function testCurrentPageIndex() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		$list->CurrentPageIndex = 2;
		self::assertEquals(2, $list->CurrentPageIndex);
	}

	public function testOnPageIndexChanged() {
		$list = new TPagedList(array(1, 2, 3, 4, 5));
		$list->PageSize = 1;
		$list->CurrentPageIndex = 1;
		$oldPage = $list->CurrentPageIndex;
		$myList = new MyPagedList();
		$list->attachEventHandler('OnPageIndexChanged', array($myList, 'pageIndexChanged'));
		self::assertEquals(false, $myList->isPageIndexChanged());
		$list->onPageIndexChanged(new TPagedListPageChangedEventParameter($oldPage));
		self::assertEquals(true, $myList->isPageIndexChanged());
	}

	public function testOnFetchData() {
		$list = new TPagedList(array(1, 2, 3, 4));
		$list->CustomPaging = true;
		$list->PageSize = 2;
		$list->gotoPage(0);
		$myList = new MyPagedList();
		$list->attachEventHandler('OnFetchData', array($myList, 'fetchData'));
		self::assertEquals(false, $myList->hasFetchedData());
		$list->onFetchData(new TPagedListFetchDataEventParameter($list->CurrentPageIndex, $list->PageSize*$list->CurrentPageIndex, $list->PageSize));
		self::assertEquals(true, $myList->hasFetchedData());
	}

	public function testGotoPage() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		self::assertEquals(2, $list->gotoPage(2));
		self::assertEquals(false, $list->gotoPage(4));
	}

	public function testNextPage() {
		$list = new TPagedList(array(1, 2));
		$list->PageSize = 1;
		$list->gotoPage(0);
		self::assertEquals(1, $list->nextPage());
		self::assertEquals(false, $list->nextPage());
	}

	public function testPreviousPage() {
		$list = new TPagedList(array(1, 2));
		$list->PageSize = 1;
		$list->gotoPage(1);
		self::assertEquals(0, $list->previousPage());
		self::assertEquals(false, $list->previousPage());
	}

	public function testVirtualCount() {
		$list = new TPagedList(array(1, 2));
		$list->VirtualCount = -10;
		self::assertEquals(-1, $list->VirtualCount);
		$list->VirtualCount = 5;
		self::assertEquals(5, $list->VirtualCount);
	}

	public function testPageCount() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		$list->CustomPaging = true;
		self::assertEquals(-1, $list->PageCount);
		$list->VirtualCount = 3;
		self::assertEquals(3, $list->PageCount);
		$list->CustomPaging = false;
		self::assertEquals(3, $list->PageCount);
	}

	public function testIsFirstPage() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		$list->gotoPage(0);
		self::assertEquals(true, $list->IsFirstPage);
		$list->gotoPage(1);
		self::assertEquals(false, $list->IsFirstPage);
	}

	public function testIsLastPage() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		$list->gotoPage(0);
		self::assertEquals(false, $list->IsLastPage);
		$list->gotoPage(2);
		self::assertEquals(true, $list->IsLastPage);
	}

	public function testGetCount() {
		$list = new TPagedList(array(1, 2, 3));
		$list->PageSize = 1;
		self::assertEquals(1, $list->Count);
		$list->CustomPaging = true;
		self::assertEquals(3, $list->Count);
	}

	public function testGetIterator() {
		$list = new TPagedList(array(1, 2));
		$list->CustomPaging = true;
		self::assertInstanceOf('ArrayIterator', $list->getIterator());
		$n = 0;
		$found = 0;
		foreach($list as $index => $item) {
			foreach($list as $a => $b); // test of iterator
			$n++;
			if($index === 0 && $item === 1) {
				$found++;
			}
			if($index === 1 && $item === 2) {
				$found++;
			}
		}
		self::assertTrue($n == 2 && $found == 2);
	}

	public function testItemAt() {
		$list = new TPagedList(array(1, 2, 3));
		$list->CustomPaging = true;
		self::assertEquals(1, $list[0]);
		$list->CustomPaging = false;
		$list->PageSize = 1;
		$list->CurrentPageIndex = 0;
		self::assertEquals(1, $list[0]);
	}

	public function testIndexOf() {
		$list = new TPagedList(array(1, 2, 3));
		$list->CustomPaging = true;
		self::assertEquals(0, $list->indexOf(1));
		self::assertEquals(-1, $list->indexOf(0));
	}

	public function testOffsetExists() {
		$list = new TPagedList(array(1, 2, 3));
		$list->CustomPaging = true;
		self::assertEquals(true, isset($list[0]));
		self::assertEquals(false, isset($list[4]));
	}

	public function testOffsetGet() {
		$list = new TPagedList(array(1, 2, 3));
		$list->CustomPaging = true;
		self::assertEquals(2, $list[1]);
	}

	public function testToArray() {
		$list = new TPagedList(array(1, 2, 3));
		$list->CustomPaging = true;
		self::assertEquals(array(1, 2, 3), $list->toArray());
	}

}