1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Notice this should only be tests to ensure that the extend from Ilib_DBQuery works |
4
|
|
|
* The actual tests of DBQuery should be in Intraface_3Party |
5
|
|
|
*/ |
6
|
|
|
class DBQueryTest extends PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
private $db; |
9
|
|
|
private $table = 'dbquery_test'; |
10
|
|
|
|
11
|
|
|
function setUp() |
12
|
|
|
{ |
13
|
|
|
$this->db = MDB2::singleton(DB_DSN); |
14
|
|
|
if (PEAR::isError($this->db)) { |
15
|
|
|
die($this->db->getUserInfo()); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$result = $this->db->exec('TRUNCATE TABLE dbquery_result'); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$result = $this->db->exec('DROP TABLE ' . $this->table); |
|
|
|
|
21
|
|
|
/* |
|
|
|
|
22
|
|
|
TODO: DROP THE TABLE IF IT EXISTS |
23
|
|
|
|
24
|
|
|
$result = $this->db->exec('DROP TABLE ' . $this->table); |
25
|
|
|
|
26
|
|
|
if (PEAR::isError($result)) { |
27
|
|
|
die($result->getUserInfo()); |
28
|
|
|
} |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
$result = $this->db->exec('CREATE TABLE ' . $this->table . '( |
32
|
|
|
id int(11) NOT NULL auto_increment, name varchar(255) NOT NULL, PRIMARY KEY (id))'); |
33
|
|
|
|
34
|
|
|
if (PEAR::isError($result)) { |
35
|
|
|
die($result->getUserInfo()); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->insertPosts(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function createDBQuery($session_id = '') |
42
|
|
|
{ |
43
|
|
|
$kernel = new Stub_Kernel($session_id); |
44
|
|
|
return new Intraface_DBQuery($kernel, $this->table); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function insertPosts() |
48
|
|
|
{ |
49
|
|
|
$data = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone'); |
50
|
|
|
foreach ($data as $d) { |
51
|
|
|
$this->createPost($d); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function createPost($post) |
56
|
|
|
{ |
57
|
|
|
$result = $this->db->exec('INSERT INTO ' . $this->table . ' (name) VALUES ('.$this->db->quote($post, 'text').')'); |
58
|
|
|
if (PEAR::isError($result)) { |
59
|
|
|
die($result->getUserInfo()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function tearDown() |
64
|
|
|
{ |
65
|
|
|
$result = $this->db->exec('DROP TABLE ' . $this->table); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/////////////////////////////////////////////////////////////////////////// |
69
|
|
|
|
70
|
|
|
function testConstructor() |
71
|
|
|
{ |
72
|
|
|
$dbquery = $this->createDBQuery(); |
73
|
|
|
$this->assertTrue(is_object($dbquery)); |
74
|
|
|
$this->assertEquals($this->table, $dbquery->getTableName()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
function testRequiredConditions() |
78
|
|
|
{ |
79
|
|
|
$condition = 'name = 1'; |
80
|
|
|
$kernel = new Stub_Kernel; |
81
|
|
|
$dbquery = new Intraface_DBQuery($kernel, $this->table, $condition); |
82
|
|
|
$this->assertEquals($condition, $dbquery->required_conditions); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
function testGetCharacters() |
86
|
|
|
{ |
87
|
|
|
$dbquery = $this->createDBQuery(); |
88
|
|
|
$db = $dbquery->getRecordset('*', '', false); |
89
|
|
|
$this->assertEquals(21, $db->numRows()); |
90
|
|
|
$dbquery->useCharacter(); |
91
|
|
|
$dbquery->defineCharacter('t', 'name'); |
92
|
|
|
$this->assertTrue($dbquery->getUseCharacter()); |
93
|
|
|
$characters = $dbquery->getCharacters(); |
94
|
|
|
$this->assertEquals(6, count($characters)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function testPaging() |
98
|
|
|
{ |
99
|
|
|
$dbquery = $this->createDBQuery(); |
100
|
|
|
$paging_name = 'paging'; |
101
|
|
|
$rows_pr_page = 2; |
102
|
|
|
$dbquery->usePaging($paging_name, $rows_pr_page); |
103
|
|
|
$db = $dbquery->getRecordset('*', '', false); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->assertEquals($paging_name, $dbquery->getPagingVarName()); |
106
|
|
|
|
107
|
|
|
$paging = $dbquery->getPaging(); |
108
|
|
|
$expected_offset = array(1=>0, 2=>2, 3=>4, 4=>6, 5=>8, 6=>10, 7=>12, 8=>14, 9=>16,10=>18,11=>20); |
109
|
|
|
$this->assertEquals($expected_offset, $paging['offset']); |
110
|
|
|
$this->assertEquals(0, $paging['previous']); |
111
|
|
|
$this->assertEquals(2, $paging['next']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
function testGetRecordset() |
115
|
|
|
{ |
116
|
|
|
$dbquery = $this->createDBQuery(); |
117
|
|
|
|
118
|
|
|
$dbquery->setCondition('id > 2'); |
119
|
|
|
|
120
|
|
|
$db = $dbquery->getRecordset('id, name'); |
121
|
|
|
$i = 0; |
122
|
|
|
while($db->nextRecord()) { |
123
|
|
|
$result[$i]['id'] = $db->f('id'); |
|
|
|
|
124
|
|
|
$result[$i]['name'] = $db->f('name'); |
|
|
|
|
125
|
|
|
$i++; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->assertEquals(19, count($result)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
function testUseStoreOnTopLevel() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$dbquery = $this->createDBQuery(); |
134
|
|
|
$dbquery->setCondition('id > 10'); |
135
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
136
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
$dbquery = $this->createDBQuery(); |
140
|
|
|
$_GET['use_stored'] = 'true'; |
141
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
142
|
|
|
$db = $dbquery->getRecordset('id, name'); |
143
|
|
|
$i = 0; |
144
|
|
|
while($db->nextRecord()) { |
145
|
|
|
$result[$i]['id'] = $db->f('id'); |
|
|
|
|
146
|
|
|
$result[$i]['name'] = $db->f('name'); |
|
|
|
|
147
|
|
|
$i++; |
148
|
|
|
} |
149
|
|
|
$this->assertEquals(11, count($result)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
function testUseStoreOnTopLevelWithAnotherOneInBetween() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
// the first page |
155
|
|
|
$dbquery = $this->createDBQuery(); |
156
|
|
|
$dbquery->setCondition('id > 10'); |
157
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
158
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
159
|
|
|
|
160
|
|
|
// another page also with toplevel - overrides the first one saved |
161
|
|
|
$dbquery = $this->createDBQuery(); |
162
|
|
|
$dbquery->storeResult("use_stored", 'unittest-on-another-page', "toplevel"); |
163
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
164
|
|
|
|
165
|
|
|
// then back to the first page again - the result should not be saved |
166
|
|
|
$dbquery = $this->createDBQuery(); |
167
|
|
|
$_GET['use_stored'] = 'true'; |
168
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
169
|
|
|
$db = $dbquery->getRecordset('id, name'); |
170
|
|
|
$i = 0; |
171
|
|
|
while($db->nextRecord()) { |
172
|
|
|
$result[$i]['id'] = $db->f('id'); |
|
|
|
|
173
|
|
|
$result[$i]['name'] = $db->f('name'); |
|
|
|
|
174
|
|
|
$i++; |
175
|
|
|
} |
176
|
|
|
$this->assertEquals(21, count($result)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
function testUseStoreOnSublevelNotChangingToplevel() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
// the first page |
182
|
|
|
$dbquery = $this->createDBQuery(); |
183
|
|
|
$dbquery->setCondition('id > 10'); |
184
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
185
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
186
|
|
|
|
187
|
|
|
// another page with sublevel - does not override the first one saved |
188
|
|
|
$dbquery = $this->createDBQuery(); |
189
|
|
|
$dbquery->storeResult("use_stored", 'unittest-on-another-page', "sublevel"); |
190
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
191
|
|
|
|
192
|
|
|
// then back to the first page again - the result should be saved |
193
|
|
|
$dbquery = $this->createDBQuery(); |
194
|
|
|
$_GET['use_stored'] = 'true'; |
195
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
196
|
|
|
$db = $dbquery->getRecordset('id, name'); |
197
|
|
|
$i = 0; |
198
|
|
|
while($db->nextRecord()) { |
199
|
|
|
$result[$i]['id'] = $db->f('id'); |
|
|
|
|
200
|
|
|
$result[$i]['name'] = $db->f('name'); |
|
|
|
|
201
|
|
|
$i++; |
202
|
|
|
} |
203
|
|
|
$this->assertEquals(11, count($result)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
function testUseStoreWithTwoDifferentUsers() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
// the first page |
209
|
|
|
$dbquery = $this->createDBQuery(); |
210
|
|
|
$dbquery->setCondition('id > 10'); |
211
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
212
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
213
|
|
|
|
214
|
|
|
// another user on the same page |
215
|
|
|
$dbquery = $this->createDBQuery('another-session-id-passed-to-kernel-and-then-to-dbquery'); |
216
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
217
|
|
|
$db = $dbquery->getRecordset('id, name'); |
|
|
|
|
218
|
|
|
|
219
|
|
|
// then back to the first page again - the result should be saved |
220
|
|
|
$dbquery = $this->createDBQuery(); |
221
|
|
|
$_GET['use_stored'] = 'true'; |
222
|
|
|
$dbquery->storeResult("use_stored", 'unittest', "toplevel"); |
223
|
|
|
$db = $dbquery->getRecordset('id, name'); |
224
|
|
|
$i = 0; |
225
|
|
|
while($db->nextRecord()) { |
226
|
|
|
$result[$i]['id'] = $db->f('id'); |
|
|
|
|
227
|
|
|
$result[$i]['name'] = $db->f('name'); |
|
|
|
|
228
|
|
|
$i++; |
229
|
|
|
} |
230
|
|
|
$this->assertEquals(11, count($result)); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.