-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
QueryExpressionTest.php
244 lines (216 loc) · 6.49 KB
/
QueryExpressionTest.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
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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.6
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Database\Expression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\ValueBinder;
use Cake\TestSuite\TestCase;
/**
* Tests QueryExpression class
*/
class QueryExpressionTest extends TestCase
{
/**
* Test setConjunction()/getConjunction() works.
*
* @return
*/
public function testConjunction()
{
$expr = new QueryExpression(['1', '2']);
$binder = new ValueBinder();
$this->assertSame($expr, $expr->setConjunction('+'));
$this->assertSame('+', $expr->getConjunction());
$result = $expr->sql($binder);
$this->assertEquals('(1 + 2)', $result);
}
/**
* Test tieWith() works.
*
* @group deprecated
* @return
*/
public function testTieWith()
{
$this->deprecated(function () {
$expr = new QueryExpression(['1', '2']);
$binder = new ValueBinder();
$this->assertSame($expr, $expr->tieWith('+'));
$this->assertSame('+', $expr->tieWith());
$result = $expr->sql($binder);
$this->assertEquals('(1 + 2)', $result);
});
}
/**
* Test type() works.
*
* @group deprecated
* @return
*/
public function testType()
{
$this->deprecated(function () {
$expr = new QueryExpression(['1', '2']);
$binder = new ValueBinder();
$this->assertSame($expr, $expr->type('+'));
$this->assertSame('+', $expr->type());
$result = $expr->sql($binder);
$this->assertEquals('(1 + 2)', $result);
});
}
/**
* Test and() and or() calls work transparently
*
* @return void
*/
public function testAndOrCalls()
{
$expr = new QueryExpression();
$expected = '\Cake\Database\Expression\QueryExpression';
$this->assertInstanceOf($expected, $expr->and([]));
$this->assertInstanceOf($expected, $expr->or([]));
}
/**
* Test SQL generation with one element
*
* @return void
*/
public function testSqlGenerationOneClause()
{
$expr = new QueryExpression();
$binder = new ValueBinder();
$expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
$result = $expr->sql($binder);
$this->assertEquals('Users.username = :c0', $result);
}
/**
* Test SQL generation with many elements
*
* @return void
*/
public function testSqlGenerationMultipleClauses()
{
$expr = new QueryExpression();
$binder = new ValueBinder();
$expr->add(
[
'Users.username' => 'sally',
'Users.active' => 1,
],
[
'Users.username' => 'string',
'Users.active' => 'boolean'
]
);
$result = $expr->sql($binder);
$this->assertEquals('(Users.username = :c0 AND Users.active = :c1)', $result);
}
/**
* Test that empty expressions don't emit invalid SQL.
*
* @return void
*/
public function testSqlWhenEmpty()
{
$expr = new QueryExpression();
$binder = new ValueBinder();
$result = $expr->sql($binder);
$this->assertEquals('', $result);
}
/**
* Test deep cloning of expression trees.
*
* @return void
*/
public function testDeepCloning()
{
$expr = new QueryExpression();
$expr = $expr->add(new QueryExpression('1 + 1'))
->isNull('deleted')
->like('title', 'things%');
$dupe = clone $expr;
$this->assertEquals($dupe, $expr);
$this->assertNotSame($dupe, $expr);
$originalParts = [];
$expr->iterateParts(function ($part) use (&$originalParts) {
$originalParts[] = $part;
});
$dupe->iterateParts(function ($part, $i) use ($originalParts) {
$this->assertNotSame($originalParts[$i], $part);
});
}
/**
* Tests the hasNestedExpression() function
*
* @return void
*/
public function testHasNestedExpression()
{
$expr = new QueryExpression();
$this->assertFalse($expr->hasNestedExpression());
$expr->add(['a' => 'b']);
$this->assertTrue($expr->hasNestedExpression());
$expr = new QueryExpression();
$expr->add('a = b');
$this->assertFalse($expr->hasNestedExpression());
$expr->add(new QueryExpression('1 = 1'));
$this->assertTrue($expr->hasNestedExpression());
}
/**
* Returns the list of specific comparison methods
*
* @return void
*/
public function methodsProvider()
{
return [
['eq'], ['notEq'], ['gt'], ['lt'], ['gte'], ['lte'], ['like'],
['notLike'], ['in'], ['notIn']
];
}
/**
* Tests that the query expression uses the type map when the
* specific comparison functions are used.
*
* @dataProvider methodsProvider
* @return void
*/
public function testTypeMapUsage($method)
{
$expr = new QueryExpression([], ['created' => 'date']);
$expr->{$method}('created', 'foo');
$binder = new ValueBinder();
$expr->sql($binder);
$bindings = $binder->bindings();
$type = current($bindings)['type'];
$this->assertEquals('date', $type);
}
/**
* Tests that creating query expressions with either the
* array notation or using the combinators will produce a
* zero-count expression object.
*
* @see https://github.com/cakephp/cakephp/issues/12081
* @return void
*/
public function testEmptyOr()
{
$expr = new QueryExpression();
$expr = $expr->or_([]);
$expr = $expr->or_([]);
$this->assertCount(0, $expr);
$expr = new QueryExpression(['OR' => []]);
$this->assertCount(0, $expr);
}
}