3
namespace Upside\Db\Test\SQL;
5
use Upside\Db\SQL\Expression;
7
class WhereTest extends BaseClass
9
public function testWhereIs()
11
$expected = 'SELECT * FROM "users" WHERE "age" = 21';
12
$actual = $this->db->from('users')->where('age')->is(21)->select();
13
$this->assertEquals($expected, $actual);
16
public function testWhereIsNot()
18
$expected = 'SELECT * FROM "users" WHERE "age" != 21';
19
$actual = $this->db->from('users')->where('age')->is_not(21)->select();
20
$this->assertEquals($expected, $actual);
23
public function testWhereLT()
25
$expected = 'SELECT * FROM "users" WHERE "age" < 21';
26
$actual = $this->db->from('users')->where('age')->less_than(21)->select();
27
$this->assertEquals($expected, $actual);
30
public function testWhereLTAlt()
32
$expected = 'SELECT * FROM "users" WHERE "age" < 21';
33
$actual = $this->db->from('users')->where('age')->lt(21)->select();
34
$this->assertEquals($expected, $actual);
37
public function testWhereGT()
39
$expected = 'SELECT * FROM "users" WHERE "age" > 21';
40
$actual = $this->db->from('users')->where('age')->greater_than(21)->select();
41
$this->assertEquals($expected, $actual);
44
public function testWhereGTAlt()
46
$expected = 'SELECT * FROM "users" WHERE "age" > 21';
47
$actual = $this->db->from('users')->where('age')->gt(21)->select();
48
$this->assertEquals($expected, $actual);
51
public function testWhereLTE()
53
$expected = 'SELECT * FROM "users" WHERE "age" <= 21';
54
$actual = $this->db->from('users')->where('age')->at_most(21)->select();
55
$this->assertEquals($expected, $actual);
58
public function testWhereLTEAlt()
60
$expected = 'SELECT * FROM "users" WHERE "age" <= 21';
61
$actual = $this->db->from('users')->where('age')->lte(21)->select();
62
$this->assertEquals($expected, $actual);
65
public function testWhereGTE()
67
$expected = 'SELECT * FROM "users" WHERE "age" >= 21';
68
$actual = $this->db->from('users')->where('age')->at_least(21)->select();
69
$this->assertEquals($expected, $actual);
72
public function testWhereGTEAlt()
74
$expected = 'SELECT * FROM "users" WHERE "age" >= 21';
75
$actual = $this->db->from('users')->where('age')->gte(21)->select();
76
$this->assertEquals($expected, $actual);
79
public function testBetween()
81
$expected = 'SELECT * FROM "users" WHERE "age" BETWEEN 18 AND 21';
82
$actual = $this->db->from('users')->where('age')->between(18, 21)->select();
83
$this->assertEquals($expected, $actual);
86
public function testNotBetween()
88
$expected = 'SELECT * FROM "users" WHERE "age" NOT BETWEEN 18 AND 21';
89
$actual = $this->db->from('users')->where('age')->not_between(18, 21)->select();
90
$this->assertEquals($expected, $actual);
93
public function testWhereInArray()
95
$expected = 'SELECT * FROM "users" WHERE "age" IN (18, 21, 31)';
96
$actual = $this->db->from('users')->where('age')->in([18, 21, 31])->select();
97
$this->assertEquals($expected, $actual);
100
public function testWhereNotInArray()
102
$expected = 'SELECT * FROM "users" WHERE "age" NOT IN (18, 21, 31)';
103
$actual = $this->db->from('users')->where('age')->not_in([18, 21, 31])->select();
104
$this->assertEquals($expected, $actual);
107
public function testWhereInQuery()
109
$expected = 'SELECT * FROM "users" WHERE "age" IN (SELECT "name" FROM "customers")';
110
$actual = $this->db->from('users')->where('age')->in(function ($query) {
111
$query->from('customers')->select('name');
113
$this->assertEquals($expected, $actual);
117
public function testWhereNotInQuery()
119
$expected = 'SELECT * FROM "users" WHERE "age" NOT IN (SELECT "name" FROM "customers")';
120
$actual = $this->db->from('users')->where('age')->not_in(function ($query) {
121
$query->from('customers')->select('name');
123
$this->assertEquals($expected, $actual);
126
public function testWhereLike()
128
$expected = 'SELECT * FROM "users" WHERE "name" LIKE \'%foo%\'';
129
$actual = $this->db->from('users')->where('name')->like('%foo%')->select();
130
$this->assertEquals($expected, $actual);
133
public function testWhereNotLike()
135
$expected = 'SELECT * FROM "users" WHERE "name" NOT LIKE \'%foo%\'';
136
$actual = $this->db->from('users')->where('name')->not_like('%foo%')->select();
137
$this->assertEquals($expected, $actual);
140
public function testWhereIsNull()
142
$expected = 'SELECT * FROM "users" WHERE "name" IS NULL';
143
$actual = $this->db->from('users')->where('name')->is_null()->select();
144
$this->assertEquals($expected, $actual);
147
public function testWhereIsNotNull()
149
$expected = 'SELECT * FROM "users" WHERE "name" IS NOT NULL';
150
$actual = $this->db->from('users')->where('name')->not_null()->select();
151
$this->assertEquals($expected, $actual);
154
public function testWhereAndCondition()
156
$expected = 'SELECT * FROM "users" WHERE "age" = 18 AND "city" = \'London\'';
157
$actual = $this->db->from('users')
158
->where('age')->is(18)
159
->and_where('city')->is('London')
161
$this->assertEquals($expected, $actual);
164
public function testWhereOrCondition()
166
$expected = 'SELECT * FROM "users" WHERE "age" = 18 OR "city" = \'London\'';
167
$actual = $this->db->from('users')
168
->where('age')->is(18)
169
->or_where('city')->is('London')
171
$this->assertEquals($expected, $actual);
174
public function testWhereGroupCondition()
176
$expected = 'SELECT * FROM "users" WHERE "age" = 18 AND ("city" = \'London\' OR "city" = \'Paris\')';
177
$actual = $this->db->from('users')
178
->where('age')->is(18)
179
->and_where(function ($group) {
180
$group->where('city')->is('London')
181
->or_where('city')->is('Paris');
184
$this->assertEquals($expected, $actual);
187
public function testWhereIsColumn()
189
$expected = 'SELECT * FROM "users" WHERE "age" = "foo"';
190
$actual = $this->db->from('users')->where('age')->is('foo', true)->select();
191
$this->assertEquals($expected, $actual);
194
public function testWhereIsNotColumn()
196
$expected = 'SELECT * FROM "users" WHERE "age" != "foo"';
197
$actual = $this->db->from('users')->where('age')->is_not('foo', true)->select();
198
$this->assertEquals($expected, $actual);
201
public function testWhereLTColumn()
203
$expected = 'SELECT * FROM "users" WHERE "age" < "foo"';
204
$actual = $this->db->from('users')->where('age')->less_than('foo', true)->select();
205
$this->assertEquals($expected, $actual);
208
public function testWhereLTAltColumn()
210
$expected = 'SELECT * FROM "users" WHERE "age" < "foo"';
211
$actual = $this->db->from('users')->where('age')->lt('foo', true)->select();
212
$this->assertEquals($expected, $actual);
215
public function testWhereGTColumn()
217
$expected = 'SELECT * FROM "users" WHERE "age" > "foo"';
218
$actual = $this->db->from('users')->where('age')->greater_than('foo', true)->select();
219
$this->assertEquals($expected, $actual);
222
public function testWhereGTAltColumn()
224
$expected = 'SELECT * FROM "users" WHERE "age" > "foo"';
225
$actual = $this->db->from('users')->where('age')->gt('foo', true)->select();
226
$this->assertEquals($expected, $actual);
229
public function testWhereLTEColumn()
231
$expected = 'SELECT * FROM "users" WHERE "age" <= "foo"';
232
$actual = $this->db->from('users')->where('age')->at_most('foo', true)->select();
233
$this->assertEquals($expected, $actual);
236
public function testWhereLTEAltColumn()
238
$expected = 'SELECT * FROM "users" WHERE "age" <= "foo"';
239
$actual = $this->db->from('users')->where('age')->lte('foo', true)->select();
240
$this->assertEquals($expected, $actual);
243
public function testWhereGTEColumn()
245
$expected = 'SELECT * FROM "users" WHERE "age" >= "foo"';
246
$actual = $this->db->from('users')->where('age')->at_least('foo', true)->select();
247
$this->assertEquals($expected, $actual);
250
public function testWhereGTEAltColumn()
252
$expected = 'SELECT * FROM "users" WHERE "age" >= "foo"';
253
$actual = $this->db->from('users')->where('age')->gte('foo', true)->select();
254
$this->assertEquals($expected, $actual);
257
public function testWhereExists()
259
$expected = 'SELECT * FROM "users" WHERE EXISTS (SELECT * FROM "orders" WHERE "orders"."name" = "users"."name")';
260
$actual = $this->db->from('users')
261
->where_exists(function ($query) {
262
$query->from('orders')
263
->where('orders.name')->eq('users.name', true)
267
$this->assertEquals($expected, $actual);
270
public function testWhereExpression1()
272
$expected = 'SELECT * FROM "numbers" WHERE "c" = "b" + 10';
273
$actual = $this->db->from('numbers')
274
->where('c')->eq(function ($expr) {
275
$expr->column('b')->op('+')->value(10);
278
$this->assertEquals($expected, $actual);
281
public function testWhereExpression2()
283
$expected = 'SELECT * FROM "numbers" WHERE "c" = "a" + "b"';
284
$actual = $this->db->from('numbers')
285
->where('c')->eq(function ($expr) {
286
$expr->column('a')->{'+'}->column('b');
289
$this->assertEquals($expected, $actual);
292
public function testWhereExpression3()
294
$expected = 'SELECT * FROM "names" WHERE LCASE("name") LIKE \'%test%\'';
295
$actual = $this->db->from('names')
296
->where(function (Expression $expr) {
297
$expr->lcase('name');
301
$this->assertEquals($expected, $actual);
304
public function testWhereNop() {
305
$expected = 'SELECT * FROM "users" WHERE match( "username" ) against( \'expression\' )';
306
$actual = $this->db->from('users')
307
->where(function (Expression $expr) {
308
$expr->op('match(')->column('username')->op(') against(')
309
->value('expression')->op(')');
312
$this->assertEquals($expected, $actual);