|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace QB\Generic\Expr; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use QB\Generic\Params\Params; |
|
11
|
|
|
use stdClass; |
|
12
|
|
|
|
|
13
|
|
|
class ExprTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
|
17
|
|
|
* |
|
18
|
|
|
* @return array[] |
|
19
|
|
|
*/ |
|
20
|
|
|
public function successProvider(): array |
|
21
|
|
|
{ |
|
22
|
|
|
return [ |
|
23
|
|
|
'param-unnamed-auto-null' => [ |
|
24
|
|
|
'foo = ?', |
|
25
|
|
|
[null], |
|
26
|
|
|
Params::ALL_AUTO, |
|
27
|
|
|
'foo = ?', |
|
28
|
|
|
[[null, PDO::PARAM_NULL]], |
|
29
|
|
|
], |
|
30
|
|
|
'param-unnamed-auto-int' => ['foo = ?', [2], Params::ALL_AUTO, 'foo = ?', [[2, PDO::PARAM_INT]]], |
|
31
|
|
|
'param-unnamed-auto-false' => [ |
|
32
|
|
|
'foo = ?', |
|
33
|
|
|
[false], |
|
34
|
|
|
Params::ALL_AUTO, |
|
35
|
|
|
'foo = ?', |
|
36
|
|
|
[[false, PDO::PARAM_BOOL]], |
|
37
|
|
|
], |
|
38
|
|
|
'param-unnamed-auto-true' => [ |
|
39
|
|
|
'foo = ?', |
|
40
|
|
|
[true], |
|
41
|
|
|
Params::ALL_AUTO, |
|
42
|
|
|
'foo = ?', |
|
43
|
|
|
[[true, PDO::PARAM_BOOL]], |
|
44
|
|
|
], |
|
45
|
|
|
'param-unnamed-auto-str' => [ |
|
46
|
|
|
'foo = ?', |
|
47
|
|
|
['bar'], |
|
48
|
|
|
Params::ALL_AUTO, |
|
49
|
|
|
'foo = ?', |
|
50
|
|
|
[['bar', PDO::PARAM_STR]], |
|
51
|
|
|
], |
|
52
|
|
|
'param-unnamed-str' => [ |
|
53
|
|
|
'foo = ?', |
|
54
|
|
|
[2], |
|
55
|
|
|
Params::ALL_STRING, |
|
56
|
|
|
'foo = ?', |
|
57
|
|
|
[[2, PDO::PARAM_STR]], |
|
58
|
|
|
], |
|
59
|
|
|
'param-unnamed-manual' => [ |
|
60
|
|
|
'foo = ?', |
|
61
|
|
|
[[2, PDO::PARAM_BOOL]], |
|
62
|
|
|
Params::ALL_MANUAL, |
|
63
|
|
|
'foo = ?', |
|
64
|
|
|
[[2, PDO::PARAM_BOOL]], |
|
65
|
|
|
], |
|
66
|
|
|
'param-named-auto' => [ |
|
67
|
|
|
'foo = :foo', |
|
68
|
|
|
['foo' => 2], |
|
69
|
|
|
Params::ALL_AUTO, |
|
70
|
|
|
'foo = :foo', |
|
71
|
|
|
['foo' => [2, PDO::PARAM_INT]], |
|
72
|
|
|
], |
|
73
|
|
|
'param-named-str' => [ |
|
74
|
|
|
'foo = :foo', |
|
75
|
|
|
['foo' => 2], |
|
76
|
|
|
Params::ALL_STRING, |
|
77
|
|
|
'foo = :foo', |
|
78
|
|
|
['foo' => [2, PDO::PARAM_STR]], |
|
79
|
|
|
], |
|
80
|
|
|
'param-named-manual' => [ |
|
81
|
|
|
'foo = :foo', |
|
82
|
|
|
['foo' => [2, PDO::PARAM_BOOL]], |
|
83
|
|
|
Params::ALL_MANUAL, |
|
84
|
|
|
'foo = :foo', |
|
85
|
|
|
['foo' => [2, PDO::PARAM_BOOL]], |
|
86
|
|
|
], |
|
87
|
|
|
'unnamed-extend-auto-simple' => [ |
|
88
|
|
|
'foo IN (?)', |
|
89
|
|
|
[['bar', 'baz']], |
|
90
|
|
|
Params::ALL_AUTO, |
|
91
|
|
|
'foo IN (?, ?)', |
|
92
|
|
|
[['bar', PDO::PARAM_STR], ['baz', PDO::PARAM_STR]], |
|
93
|
|
|
], |
|
94
|
|
|
'unnamed-extend-auto-end' => [ |
|
95
|
|
|
'foo IN (?', |
|
96
|
|
|
[['bar', 'baz']], |
|
97
|
|
|
Params::ALL_AUTO, |
|
98
|
|
|
'foo IN (?, ?', |
|
99
|
|
|
[['bar', PDO::PARAM_STR], ['baz', PDO::PARAM_STR]], |
|
100
|
|
|
], |
|
101
|
|
|
'unnamed-extend-auto-beginning' => [ |
|
102
|
|
|
'?)', |
|
103
|
|
|
[['bar', 'baz']], |
|
104
|
|
|
Params::ALL_AUTO, |
|
105
|
|
|
'?, ?)', |
|
106
|
|
|
[['bar', PDO::PARAM_STR], ['baz', PDO::PARAM_STR]], |
|
107
|
|
|
], |
|
108
|
|
|
'unnamed-extend-auto-complex' => [ |
|
109
|
|
|
'foo IN (?) AND bar = ? AND baz IN (?)', |
|
110
|
|
|
[['bar', 'baz'], 'quix', [17, 34]], |
|
111
|
|
|
Params::ALL_AUTO, |
|
112
|
|
|
'foo IN (?, ?) AND bar = ? AND baz IN (?, ?)', |
|
113
|
|
|
[ |
|
114
|
|
|
['bar', PDO::PARAM_STR], |
|
115
|
|
|
['baz', PDO::PARAM_STR], |
|
116
|
|
|
['quix', PDO::PARAM_STR], |
|
117
|
|
|
[17, PDO::PARAM_INT], |
|
118
|
|
|
[34, PDO::PARAM_INT], |
|
119
|
|
|
], |
|
120
|
|
|
], |
|
121
|
|
|
'unnamed-extend-manual-complex' => [ |
|
122
|
|
|
'foo IN (?) AND bar = ? AND baz IN (?)', |
|
123
|
|
|
[ |
|
124
|
|
|
[['bar', PDO::PARAM_STR], ['baz', PDO::PARAM_STR]], |
|
125
|
|
|
['quix', PDO::PARAM_STR], |
|
126
|
|
|
[[17, PDO::PARAM_INT], [34, PDO::PARAM_STR]], |
|
127
|
|
|
], |
|
128
|
|
|
Params::ALL_MANUAL, |
|
129
|
|
|
'foo IN (?, ?) AND bar = ? AND baz IN (?, ?)', |
|
130
|
|
|
[ |
|
131
|
|
|
['bar', PDO::PARAM_STR], |
|
132
|
|
|
['baz', PDO::PARAM_STR], |
|
133
|
|
|
['quix', PDO::PARAM_STR], |
|
134
|
|
|
[17, PDO::PARAM_INT], |
|
135
|
|
|
[34, PDO::PARAM_STR], |
|
136
|
|
|
], |
|
137
|
|
|
], |
|
138
|
|
|
'unnamed-extend-str-complex' => [ |
|
139
|
|
|
'foo IN (?) AND bar = ? AND baz IN (?)', |
|
140
|
|
|
[['bar', 'baz'], 'quix', [17, 34]], |
|
141
|
|
|
Params::ALL_STRING, |
|
142
|
|
|
'foo IN (?, ?) AND bar = ? AND baz IN (?, ?)', |
|
143
|
|
|
[ |
|
144
|
|
|
['bar', PDO::PARAM_STR], |
|
145
|
|
|
['baz', PDO::PARAM_STR], |
|
146
|
|
|
['quix', PDO::PARAM_STR], |
|
147
|
|
|
[17, PDO::PARAM_STR], |
|
148
|
|
|
[34, PDO::PARAM_STR], |
|
149
|
|
|
], |
|
150
|
|
|
], |
|
151
|
|
|
'named-extend-auto-simple' => [ |
|
152
|
|
|
'foo IN (:foo)', |
|
153
|
|
|
['foo' => ['bar', 'baz']], |
|
154
|
|
|
Params::ALL_AUTO, |
|
155
|
|
|
'foo IN (:foo_0, :foo_1)', |
|
156
|
|
|
['foo_0' => ['bar', PDO::PARAM_STR], 'foo_1' => ['baz', PDO::PARAM_STR]], |
|
157
|
|
|
], |
|
158
|
|
|
'named-extend-auto-end' => [ |
|
159
|
|
|
'foo IN (:foo', |
|
160
|
|
|
['foo' => ['bar', 'baz']], |
|
161
|
|
|
Params::ALL_AUTO, |
|
162
|
|
|
'foo IN (:foo_0, :foo_1', |
|
163
|
|
|
['foo_0' => ['bar', PDO::PARAM_STR], 'foo_1' => ['baz', PDO::PARAM_STR]], |
|
164
|
|
|
], |
|
165
|
|
|
'named-extend-auto-beginning' => [ |
|
166
|
|
|
':foo)', |
|
167
|
|
|
['foo' => ['bar', 'baz']], |
|
168
|
|
|
Params::ALL_AUTO, |
|
169
|
|
|
':foo_0, :foo_1)', |
|
170
|
|
|
['foo_0' => ['bar', PDO::PARAM_STR], 'foo_1' => ['baz', PDO::PARAM_STR]], |
|
171
|
|
|
], |
|
172
|
|
|
'named-extend-auto-complex' => [ |
|
173
|
|
|
'foo IN (:foo) AND bar = :bar AND baz IN (:baz)', |
|
174
|
|
|
['foo' => ['bar', 'baz'], 'bar' => 'quix', 'baz' => [17, 34]], |
|
175
|
|
|
Params::ALL_AUTO, |
|
176
|
|
|
'foo IN (:foo_0, :foo_1) AND bar = :bar AND baz IN (:baz_0, :baz_1)', |
|
177
|
|
|
[ |
|
178
|
|
|
'foo_0' => ['bar', PDO::PARAM_STR], |
|
179
|
|
|
'foo_1' => ['baz', PDO::PARAM_STR], |
|
180
|
|
|
'bar' => ['quix', PDO::PARAM_STR], |
|
181
|
|
|
'baz_0' => [17, PDO::PARAM_INT], |
|
182
|
|
|
'baz_1' => [34, PDO::PARAM_INT], |
|
183
|
|
|
], |
|
184
|
|
|
], |
|
185
|
|
|
'named-extend-manual-complex' => [ |
|
186
|
|
|
'foo IN (:foo) AND bar = :bar AND baz IN (:baz)', |
|
187
|
|
|
[ |
|
188
|
|
|
'foo' => [['bar', PDO::PARAM_STR], ['baz', PDO::PARAM_STR]], |
|
189
|
|
|
'bar' => ['quix', PDO::PARAM_STR], |
|
190
|
|
|
'baz' => [[17, PDO::PARAM_INT], [34, PDO::PARAM_STR]], |
|
191
|
|
|
], |
|
192
|
|
|
Params::ALL_MANUAL, |
|
193
|
|
|
'foo IN (:foo_0, :foo_1) AND bar = :bar AND baz IN (:baz_0, :baz_1)', |
|
194
|
|
|
[ |
|
195
|
|
|
'foo_0' => ['bar', PDO::PARAM_STR], |
|
196
|
|
|
'foo_1' => ['baz', PDO::PARAM_STR], |
|
197
|
|
|
'bar' => ['quix', PDO::PARAM_STR], |
|
198
|
|
|
'baz_0' => [17, PDO::PARAM_INT], |
|
199
|
|
|
'baz_1' => [34, PDO::PARAM_STR], |
|
200
|
|
|
], |
|
201
|
|
|
], |
|
202
|
|
|
'named-extend-str-complex' => [ |
|
203
|
|
|
'foo IN (:foo) AND bar = :bar AND baz IN (:baz)', |
|
204
|
|
|
['foo' => ['bar', 'baz'], 'bar' => 'quix', 'baz' => [17, 34]], |
|
205
|
|
|
Params::ALL_STRING, |
|
206
|
|
|
'foo IN (:foo_0, :foo_1) AND bar = :bar AND baz IN (:baz_0, :baz_1)', |
|
207
|
|
|
[ |
|
208
|
|
|
'foo_0' => ['bar', PDO::PARAM_STR], |
|
209
|
|
|
'foo_1' => ['baz', PDO::PARAM_STR], |
|
210
|
|
|
'bar' => ['quix', PDO::PARAM_STR], |
|
211
|
|
|
'baz_0' => [17, PDO::PARAM_STR], |
|
212
|
|
|
'baz_1' => [34, PDO::PARAM_STR], |
|
213
|
|
|
], |
|
214
|
|
|
], |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @dataProvider successProvider |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $sql |
|
222
|
|
|
* @param array $params |
|
223
|
|
|
* @param int $paramHandle |
|
224
|
|
|
* @param string $expectedSql |
|
225
|
|
|
* @param array $expectedParams |
|
226
|
|
|
*/ |
|
227
|
|
|
public function testSuccess( |
|
228
|
|
|
string $sql, |
|
229
|
|
|
array $params, |
|
230
|
|
|
int $paramHandle, |
|
231
|
|
|
string $expectedSql, |
|
232
|
|
|
array $expectedParams |
|
233
|
|
|
) { |
|
234
|
|
|
$sut = $this->createSut($sql, $params, $paramHandle); |
|
235
|
|
|
|
|
236
|
|
|
$this->assertSame($expectedSql, (string)$sut); |
|
237
|
|
|
$this->assertSame($expectedParams, $sut->getParams()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function constructExceptionsProvider(): array |
|
241
|
|
|
{ |
|
242
|
|
|
return [ |
|
243
|
|
|
'object parameter' => ['foo = ?', [new stdClass()], Params::ALL_AUTO], |
|
244
|
|
|
'invalid parameter handling' => ['foo', [0 => null], 123], |
|
245
|
|
|
'string parameter key not expected' => ['foo', [0 => null, 'foo' => null], 123], |
|
246
|
|
|
'int parameter key not expected' => ['foo', [1 => null], Params::ALL_AUTO], |
|
247
|
|
|
]; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @dataProvider constructExceptionsProvider |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $sql |
|
254
|
|
|
* @param array $params |
|
255
|
|
|
* @param int $paramHandle |
|
256
|
|
|
*/ |
|
257
|
|
|
public function testConstructExceptions(string $sql, array $params, int $paramHandle) |
|
258
|
|
|
{ |
|
259
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
260
|
|
|
|
|
261
|
|
|
$this->createSut($sql, $params, $paramHandle); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $sql |
|
266
|
|
|
* @param array $params |
|
267
|
|
|
* @param int $paramHandle |
|
268
|
|
|
* |
|
269
|
|
|
* @return Expr |
|
270
|
|
|
*/ |
|
271
|
|
|
protected function createSut(string $sql, array $params, int $paramHandle): Expr |
|
272
|
|
|
{ |
|
273
|
|
|
return new Expr($sql, $params, $paramHandle); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|