-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSelectQueryTest.php
More file actions
266 lines (206 loc) · 9.1 KB
/
Copy pathSelectQueryTest.php
File metadata and controls
266 lines (206 loc) · 9.1 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
require_once 'PHPUnit/Framework.php';
require_once "..".DIRECTORY_SEPARATOR."autoload.php";
class SelectQueryTest extends PHPUnit_Framework_TestCase
{
public function testSelectAllFromOneTable()
{
$q = new SelectQuery(array('test'));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0`', $q->sql());
$this->assertEquals(0, count($q->parameters()));
$q = new SelectQuery(array('test'));
$q->setSelect(array(new AllFields()), true);
$this->assertEquals('SELECT DISTINCT `t0`.* FROM `test` AS `t0`', $q->sql());
$this->assertEquals(0, count($q->parameters()));
$q = new SelectQuery(array(new QBTable('test')));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0`', $q->sql());
$this->assertEquals(0, count($q->parameters()));
}
public function testSelectSomeFromOneTable()
{
$q = new SelectQuery(array('test'));
$q->setWhere(new Condition('=', new Field('somefield'), 35));
$q->setLimit(10, 2);
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE `t0`.`somefield` = :p1 LIMIT 10 OFFSET 2', $q->sql());
$params = $q->parameters();
$this->assertEquals(35, $params[':p1']);
}
public function testNestedConditions()
{
$q = new SelectQuery(array('test'));
$q->setWhere(new AndOp(array(
new Condition('>', new Field('id'), 12),
new OrOp(array(
new Condition('=', new Field('status'), 'demolished'),
new NotOp(
new Condition('<', new Field('age'), 5)
)
))
)));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE (`t0`.`id` > :p1 AND (`t0`.`status` = :p2 OR NOT (`t0`.`age` < :p3)))', $q->sql());
}
public function testNotOp()
{
try {
new NotOp(array(
new Condition('=', new Field('test'), 1),
new Condition('=', new Field('test'), 2),
));
fail(); // exception should happen
} catch (InvalidArgumentException $e) {
}
}
public function testInCondition()
{
$q = new SelectQuery(array('test'));
$q->setWhere(new Condition('in', new Field('id'), array(1, 3, 5)));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE `t0`.`id` IN (1, 3, 5)', $q->sql());
}
public function testSelectSpecificFields()
{
$q = new SelectQuery(array('test', 'test2'));
$q->setSelect(array(new AllFields(), new Field('id', 1)));
$this->assertEquals('SELECT `t0`.*, `t1`.`id` FROM `test` AS `t0`, `test2` AS `t1`', $q->sql());
}
public function testAlias()
{
$field1 = new Field('id', 0, 'test');
$q = new SelectQuery(array('test', 'test2'));
$q->setSelect(array($field1, new AllFields(1)));
$q->setWhere(new Condition('=', $field1, '2'));
$this->assertEquals('SELECT `t0`.`id` AS `test`, `t1`.* FROM `test` AS `t0`, `test2` AS `t1` WHERE `test` = :p1', $q->sql());
}
public function testWhereNull()
{
$q = new SelectQuery(array('test'));
$q->setWhere(new AndOp(array(
new Condition('=', new Field('a'), null),
new Condition('<>', new Field('b'), null),
)));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE (`t0`.`a` IS NULL AND `t0`.`b` IS NOT NULL)', $q->sql());
$q->setWhere();
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0`', $q->sql());
}
public function testSelectWrongs()
{
try {
$q = new SelectQuery('test');
$q->setSelect(array());
$this->fail("noone is allowed to select nothing!");
} catch (InvalidArgumentException $e) {
}
try {
$q = new SelectQuery('test');
$q->setSelect(array('field1'), 'test');
$this->fail("second params should be boolean!");
} catch (InvalidArgumentException $e) {
}
try {
$q = new SelectQuery(array(123));
$this->fail("tables param should be either string or QBTable!");
} catch (LogicException $e) {
}
}
public function testAggregate()
{
$q = new SelectQuery('test');
$q->setSelect(new Aggregate('count'));
$this->assertEquals('SELECT COUNT(*) FROM `test` AS `t0`', $q->sql());
// This should throw exception, as count accept only '*' not 't0.*'
try {
new Aggregate('count', new AllFields());
fail();
} catch (InvalidArgumentException $e) {
}
}
public function testGroupBy()
{
$group_by = array(new Field('year'));
$q = new SelectQuery(array('test'));
$q->setGroupby($group_by);
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` GROUP BY `t0`.`year`', $q->sql());
$q->setHaving(new Condition('>', new Aggregate('count', new Field('commit')), 20));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` GROUP BY `t0`.`year` HAVING COUNT(`t0`.`commit`) > :p1', $q->sql());
$q->setHaving();
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` GROUP BY `t0`.`year`', $q->sql());
$this->assertEquals(var_export($group_by, true), var_export($q->showGroupBy(), true)); // check required by BebopCMS(tm)
}
public function testGroupByAggregate()
{
$group_by = new Aggregate('count', new Field('user'), true, 'c');
$field = new Field('very_long_identifier', 0, 'url');
$q = new SelectQuery('test');
$q->setSelect(array($group_by, $field));
$q->setGroupby(array($group_by));
$q->setOrderBy(array($field));
$this->assertEquals('SELECT COUNT(DISTINCT `t0`.`user`) AS `c`, `t0`.`very_long_identifier` AS `url` FROM `test` AS `t0` GROUP BY `c` ORDER BY `url` ASC', $q->sql());
}
public function testOrderByFunction()
{
$f = new SqlFunction('year', new Field('stamp'), 'year');
$q = new SelectQuery('test');
$q->setSelect(array($f, new Field('profit')));
$q->setOrderBy(array($f));
$this->assertEquals('SELECT YEAR(`t0`.`stamp`) AS `year`, `t0`.`profit` FROM `test` AS `t0` ORDER BY `year` ASC', $q->sql());
}
public function testOrderBy()
{
$q = new SelectQuery('test');
$q->setOrderBy(array(new Field('id')), array(true));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` ORDER BY `t0`.`id` DESC', $q->sql());
}
public function testInfo()
{
$tbls = array('test1', 'test2', 'test3');
$q = new SelectQuery($tbls);
$this->assertEquals(var_export($tbls, true), var_export($q->showTables(), true));
$condition = new Condition('=', new Field('id'), 1);
$q->setWhere($condition);
$this->assertEquals(var_export($condition, true), var_export($q->showConditions(), true));
}
public function testMultiSchema()
{
$q = new SelectQuery(new QBTable('test', 'db2'));
$this->assertEquals('SELECT `t0`.* FROM `db2`.`test` AS `t0`', $q->sql());
}
public function testUseIndex()
{
$q = new SelectQuery('test');
$q->setIndices(array('abc', 'def'));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` USE INDEX (`abc`, `def`)', $q->sql());
}
public function testSelectWithSubquery()
{
$field1 = new Field('id', 0);
$subq = new SelectQuery(array('sub_test'));
$subq->setSelect(array($field1));
$q = new SelectQuery(array('test'));
$q->setWhere(new Condition('in', new Field('id'), $subq));
$this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE `t0`.`id` IN (SELECT `t0`.`id` FROM `sub_test` AS `t0`)', $q->sql());
}
/**
* testing nested subquery (query, which uses subquery, which uses subquery) and checking, that conditions are correct (for use in prepared queries)
*/
public function testSubqueryWithConditions()
{
$field1 = new Field('id', 0);
$subq2 = new SelectQuery(array('bans'));
$subq2->setSelect(array($field1));
$subq2->setWhere(new Condition('=', new Field('abc'), 10));
$subq = new SelectQuery(array('users'));
$subq->setSelect(array($field1));
$subq->setWhere(new AndOp(
new Condition('>', new Field('age'), 14),
new Condition('in', new Field('id'), $subq2),
new Condition('=', new Field('regdt'), '2009-01-01')
));
$q = new SelectQuery(array('posts'));
$q->setWhere(new AndOp(
new Condition('>', new Field('comment'), 1),
new Condition('in', new Field('user_id'), $subq),
new Condition('=', new Field('funny'), 1)
));
$this->assertEquals('SELECT `t0`.* FROM `posts` AS `t0` WHERE (`t0`.`comment` > :p1 AND `t0`.`user_id` IN (SELECT `t0`.`id` FROM `users` AS `t0` WHERE (`t0`.`age` > :p2 AND `t0`.`id` IN (SELECT `t0`.`id` FROM `bans` AS `t0` WHERE `t0`.`abc` = :p3) AND `t0`.`regdt` = :p4)) AND `t0`.`funny` = :p5)', $q->sql());
}
}