-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathJoinSupportingQueryBuilderParserTest.php
More file actions
359 lines (278 loc) · 14.4 KB
/
JoinSupportingQueryBuilderParserTest.php
File metadata and controls
359 lines (278 loc) · 14.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace timgws\test;
use timgws\JoinSupportingQueryBuilderParser;
class JoinSupportingQueryBuilderParserTest extends CommonQueryBuilderTests
{
protected function getParserUnderTest($fields = null)
{
return new JoinSupportingQueryBuilderParser($fields, $this->getJoinFields());
}
private function getJoinFields()
{
return array(
'join1' => array(
'from_table' => 'master',
'from_col' => 'm_col',
'to_table' => 'subtable',
'to_col' => 's_col',
'to_value_column' => 's_value',
),
'join2' => array(
'from_table' => 'master2',
'from_col' => 'm2_col',
'to_table' => 'subtable2',
'to_col' => 's2_col',
'to_value_column' => 's2_value',
'not_exists' => true,
),
'joinwithclause' => array(
'from_table' => 'master',
'from_col' => 'm_col',
'to_table' => 'subtable',
'to_col' => 's_col',
'to_value_column' => 's_value',
'to_clause' => array('othercol' => 'value'),
),
'join2through' => array(
'from_table' => 'master2',
'from_col' => 'm2_col',
'to_table' => 'subtable2',
'to_col' => 's2_col',
'to_value_column' => 's2_value',
'not_exists' => true,
'through' => array(
'from_table' => 'subtable2',
'from_col' => 's2_col',
'to_table' => 'subtable3',
'to_col' => 's3_col',
'to_value_column' => 's3_value',
)
),
);
}
public function testJoinWhere()
{
$json = '{"condition":"AND","rules":[{"id":"join1","field":"join1","type":"double","input":"text","operator":"less","value":"10.25"}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$test = $parser->parse($json, $builder);
$this->assertEquals($test->toSql(), $builder->toSql());
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` < ?)',
$builder->toSql());
}
public function testJoinNotExistsWhere()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"double","input":"text","operator":"less","value":"10.25"}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` < ?)',
$builder->toSql());
}
public function testJoinIn()
{
$json = '{"condition":"AND","rules":[{"id":"join1","field":"join1","type":"text","input":"select","operator":"in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` in (?, ?))',
$builder->toSql());
}
public function testJoinNotExistsIn()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` in (?, ?))',
$builder->toSql());
}
public function testJoinNotIn()
{
$json = '{"condition":"AND","rules":[{"id":"join1","field":"join1","type":"text","input":"select","operator":"not_in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` not in (?, ?))',
$builder->toSql());
}
// Urgh, double negative
public function testJoinNotExistsNotIn()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"not_in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` not in (?, ?))',
$builder->toSql());
}
public function testJoinBetween()
{
$json = '{"condition":"AND","rules":[{"id":"join1","field":"join1","type":"text","input":"select","operator":"between","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$test = $parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` between ? and ?)',
$builder->toSql());
}
public function testJoinNotBetween()
{
$json = '{"condition":"AND","rules":[{"id":"join1","field":"join1","type":"text","input":"select","operator":"not_between","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$test = $parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` not between ? and ?)',
$builder->toSql());
}
public function testJoinNotExistsBetween()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"between","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` between ? and ?)',
$builder->toSql());
}
// Bugfix for #14
public function testJoinIsNull()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"is_null","value":""}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` is null)',
$builder->toSql());
}
// Bugfix for #14
public function testJoinIsNotNull()
{
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"is_not_null","value":""}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` is not null)',
$builder->toSql());
}
/**
* @expectedException timgws\QBParseException
* @expectedExceptionMessage s2_value should be an array with only two items.
*
* @throws \timgws\QBParseException
*/
public function testJoinNotExistsBetweenWithThreeItems()
{
$this->_testJoinNotExistsBetweenWithThreeItems(false);
}
/**
* @expectedException timgws\QBParseException
* @expectedExceptionMessage s2_value should be an array with only two items.
*
* @throws \timgws\QBParseException
*/
public function testJoinNotExistsNotBetweenWithThreeItems()
{
$this->_testJoinNotExistsBetweenWithThreeItems(true);
}
/**
* @see testJoinNotExistsBetweenWithThreeItems()
* @see testJoinNotExistsNotBetweenWithThreeItems()
*/
private function _testJoinNotExistsBetweenWithThreeItems($not_between = false)
{
$json_operator = ($not_between ? 'not_' : '') . 'between';
$sql_operator = ($not_between ? 'not ' : '') . 'between';
$json = '{"condition":"AND","rules":[{"id":"join2","field":"join2","type":"text","input":"select","operator":"'. $json_operator . '","value":["a","b","c"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` ' . $sql_operator . ' ? and ?)',
$builder->toSql());
}
/**
* @expectedException timgws\QBParseException
* @expectedExceptionMessage Field (join4) does not exist in fields list
*
* @throws \timgws\QBParseException
*/
public function testJoinNotExistsBetweenWithFieldThatDoesNotExist()
{
$json = '{"condition":"AND","rules":[{"id":"join4","field":"join4","type":"text","input":"select","operator":"between","value":["a","b","c"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest(array('this_field_is_allowed_but_is_not_present_in_the_json_string'));
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and `s2_value` between ? and ?)',
$builder->toSql());
}
public function testJoinWithClause()
{
$json = '{"condition":"AND","rules":[{"id":"joinwithclause","field":"joinwithclause","type":"text","input":"select","operator":"in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and (`othercol` = ?) and `s_value` in (?, ?))',
$builder->toSql());
}
/**
* JoinSupportingQueryBuilderParser always return AND even if OR is the condition
* (Fix for bug #13)
*/
public function testJoinWorksWithOrCondition()
{
$json = '{"condition":"OR","rules":[{"id":"joinwithclause","field":"joinwithclause","type":"double","input":"text","operator":"less","value":"10.25"},{"condition":"OR","rules":[{"id":"joinwithclause","field":"joinwithclause","type":"integer","input":"select","operator":"equal","value":"2"},{"id":"joinwithclause","field":"joinwithclause","type":"integer","input":"select","operator":"equal","value":"1"}]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and (`othercol` = ?) and `s_value` < ?) or (exists (select 1 from `subtable` where subtable.s_col = master.m_col and (`othercol` = ?) and `s_value` = ?) or exists (select 1 from `subtable` where subtable.s_col = master.m_col and (`othercol` = ?) and `s_value` = ?))',
$builder->toSql());
}
public function testCategoryIn()
{
$builder = $this->createQueryBuilder();
$qb = $this->getParserUnderTest();
$qb->parse($this->makeJSONForInNotInTest(), $builder);
$this->assertEquals('select * where `price` < ? and (`category` in (?, ?))', $builder->toSql());
}
/**
* Test for #21 (Cast datetimes and add 'not between' operator)
*/
public function testDateBetween()
{
$incoming = '{ "condition": "AND", "rules": [ { "id": "dollar_amount", "field": "dollar_amount", "type": "double", "input": "number", "operator": "less", "value": "546" }, { "id": "needed_by_date", "field": "needed_by_date", "type": "date", "input": "text", "operator": "between", "value": [ "10/22/2017", "10/28/2017" ] } ], "not": false, "valid": true }';
$builder = $this->createQueryBuilder();
$qb = $this->getParserUnderTest();
$qb->parse($incoming, $builder);
$this->assertEquals('select * where `dollar_amount` < ? and `needed_by_date` between ? and ?', $builder->toSql());
$bindings = $builder->getBindings();
$this->assertCount(3, $bindings);
$this->assertEquals('546', $bindings[0]);
$this->assertInstanceOf("Carbon\\Carbon", $bindings[1]);
$this->assertInstanceOf("Carbon\\Carbon", $bindings[2]);
}
/**
* Test for #21 (Cast datetimes and add 'not between' operator)
*/
public function testDateNotBetween()
{
$incoming = '{ "condition": "AND", "rules": [ { "id": "needed_by_date", "field": "needed_by_date", "type": "date", "input": "text", "operator": "not_between", "value": [ "10/22/2017", "10/28/2017" ] } ], "not": false, "valid": true }';
$builder = $this->createQueryBuilder();
$qb = $this->getParserUnderTest();
$qb->parse($incoming, $builder);
$this->assertEquals('select * where `needed_by_date` not between ? and ?', $builder->toSql());
$bindings = $builder->getBindings();
$this->assertCount(2, $bindings);
$this->assertInstanceOf("Carbon\\Carbon", $bindings[0]);
$this->assertInstanceOf("Carbon\\Carbon", $bindings[1]);
$this->assertEquals(2017, $bindings[0]->year);
$this->assertEquals(22, $bindings[0]->day);
$this->assertEquals(28, $bindings[1]->day);
}
public function testJoinNotExistsInThrough()
{
$json = '{"condition":"AND","rules":[{"id":"join2through","field":"join2through","type":"text","input":"select","operator":"in","value":["a","b"]}]}';
$builder = $this->createQueryBuilder();
$parser = $this->getParserUnderTest();
$parser->parse($json, $builder);
$this->assertEquals('select * where not exists (select 1 from `subtable2` where subtable2.s2_col = master2.m2_col and not exists (select 1 from `subtable3` where subtable3.s3_col = subtable2.s2_col and `s3_value` in (?, ?)))',
$builder->toSql());
}
}