forked from lox/pheasant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryTest.php
192 lines (165 loc) · 4.54 KB
/
QueryTest.php
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
<?php
namespace Pheasant\Tests;
use \Pheasant;
use \Pheasant\Query\Query;
class QueryTest extends \Pheasant\Tests\MysqlTestCase
{
public function testQuerying()
{
$query = new Query();
$query
->select('firstname')
->from('user')
->where('lastname=?','Castle')
;
$this->assertEquals(
"SELECT firstname FROM user WHERE (lastname='Castle')",
$query->toSql()
);
}
public function testAddingWhereClauses()
{
$query = new Query();
$query
->select('firstname')
->from('user')
->where('lastname=?','Castle')
->andWhere('firstname=?','Frank')
;
$this->assertEquals(
"SELECT firstname FROM user WHERE ((lastname='Castle') AND (firstname='Frank'))",
$query->toSql()
);
}
public function testJoiningMultipleQueryObjects()
{
// outer query
$query = new Query();
$query
->from('user')
->innerJoin('mytable', 'using(tableid)')
->where('userid=?',55)
;
$this->assertEquals(
"SELECT * FROM user INNER JOIN `mytable` using(tableid) WHERE (userid='55')",
$query->toSql()
);
}
public function testInnerJoinOnObjects()
{
// inner query
$innerQuery = new Query();
$innerQuery
->select('groupname', 'groupid')
->from('group')
;
// outer query
$query = new Query();
$query
->select('firstname')
->from('user')
->innerJoin($innerQuery, 'USING(groupid)')
->where('lastname=?','Castle')
;
$innerQuery
->where('derived.firstname = ?', 'frank');
$this->assertEquals('SELECT firstname FROM user '.
'INNER JOIN (SELECT groupname, groupid FROM group) derived USING(groupid) '.
'WHERE (lastname=\'Castle\')',
$query->toSql()
);
}
public function testAddingGroupBy()
{
$query = new Query();
$query
->select('userid')
->from('user')
->groupBy('userid')
;
$this->assertEquals(
'SELECT userid FROM user GROUP BY userid',
$query->toSql());
}
public function testAddingOrderBy()
{
$query = new Query();
$query
->select('userid')
->from('user')
->orderBy('userid')
;
$this->assertEquals(
'SELECT userid FROM user ORDER BY userid',
$query->toSql());
$query = new Query();
$query
->select('foo')
->from('bar')
->orderBy('baz')
->andOrderBy('moo')
;
$this->assertEquals(
'SELECT foo FROM bar ORDER BY baz, moo',
$query->toSql());
}
public function testChainedOrderByReplacesOrderBy()
{
$query = new Query();
$query
->select('first_name, last_name')
->from('users')
->orderBy('last_name ASC')
->orderBy('first_name')
;
$this->assertEquals(
'SELECT first_name, last_name FROM users ORDER BY first_name',
$query->toSql());
}
public function testAddingDefaultLock()
{
$query = new Query();
$query
->select('userid')
->from('user')
->lock()
;
$this->assertEquals(
'SELECT userid FROM user FOR UPDATE',
$query->toSql());
}
public function testAddingLockWithClause()
{
$query = new Query();
$query
->select('userid')
->from('user')
->lock('LOCK IN SHARE MODE')
;
$this->assertEquals(
'SELECT userid FROM user LOCK IN SHARE MODE',
$query->toSql());
}
public function testDistinctColumn()
{
$query = new Query();
$query
->distinct()
->select('userid')
->from('user')
;
$this->assertEquals(
'SELECT DISTINCT userid FROM user',
$query->toSql());
}
public function testCount()
{
$query = new Query();
$query
->select('firstname')
->from('user')
->where('lastname=?','Castle')
;
$this->assertSame(1, $query->count());
}
}