Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php (1 issue)

1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\SQLParserUtils;
7
8
/**
9
 * @group DBAL-78
10
 * @group DDC-1372
11
 */
12
class SQLParserUtilsTest extends \Doctrine\Tests\DbalTestCase
13
{
14
    public function dataGetPlaceholderPositions()
15
    {
16
        return array(
17
            // none
18
            array('SELECT * FROM Foo', true, array()),
19
            array('SELECT * FROM Foo', false, array()),
20
21
            // Positionals
22
            array('SELECT ?', true, array(7)),
23
            array('SELECT * FROM Foo WHERE bar IN (?, ?, ?)', true, array(32, 35, 38)),
24
            array('SELECT ? FROM ?', true, array(7, 14)),
25
            array('SELECT "?" FROM foo', true, array()),
26
            array("SELECT '?' FROM foo", true, array()),
27
            array("SELECT `?` FROM foo", true, array()), // Ticket DBAL-552
28
            array("SELECT [?] FROM foo", true, array()),
29
            array("SELECT 'Doctrine\DBAL?' FROM foo", true, array()), // Ticket DBAL-558
30
            array('SELECT "Doctrine\DBAL?" FROM foo', true, array()), // Ticket DBAL-558
31
            array('SELECT `Doctrine\DBAL?` FROM foo', true, array()), // Ticket DBAL-558
32
            array('SELECT [Doctrine\DBAL?] FROM foo', true, array()), // Ticket DBAL-558
33
            array('SELECT "?" FROM foo WHERE bar = ?', true, array(32)),
34
            array("SELECT '?' FROM foo WHERE bar = ?", true, array(32)),
35
            array("SELECT `?` FROM foo WHERE bar = ?", true, array(32)), // Ticket DBAL-552
36
            array("SELECT [?] FROM foo WHERE bar = ?", true, array(32)),
37
            array('SELECT * FROM foo WHERE jsonb_exists_any(foo.bar, ARRAY[?])', true, array(56)), // Ticket GH-2295
38
            array("SELECT 'Doctrine\DBAL?' FROM foo WHERE bar = ?", true, array(45)), // Ticket DBAL-558
39
            array('SELECT "Doctrine\DBAL?" FROM foo WHERE bar = ?', true, array(45)), // Ticket DBAL-558
40
            array('SELECT `Doctrine\DBAL?` FROM foo WHERE bar = ?', true, array(45)), // Ticket DBAL-558
41
            array('SELECT [Doctrine\DBAL?] FROM foo WHERE bar = ?', true, array(45)), // Ticket DBAL-558
42
            array("SELECT * FROM FOO WHERE bar = 'it\\'s a trap? \\\\' OR bar = ?\nAND baz = \"\\\"quote\\\" me on it? \\\\\" OR baz = ?", true, array(58, 104)),
43
            array('SELECT * FROM foo WHERE foo = ? AND bar = ?', true, array(1 => 42, 0 => 30)), // explicit keys
44
45
            // named
46
            array('SELECT :foo FROM :bar', false, array(7 => 'foo', 17 => 'bar')),
47
            array('SELECT * FROM Foo WHERE bar IN (:name1, :name2)', false, array(32 => 'name1', 40 => 'name2')),
48
            array('SELECT ":foo" FROM Foo WHERE bar IN (:name1, :name2)', false, array(37 => 'name1', 45 => 'name2')),
49
            array("SELECT ':foo' FROM Foo WHERE bar IN (:name1, :name2)", false, array(37 => 'name1', 45 => 'name2')),
50
            array('SELECT :foo_id', false, array(7 => 'foo_id')), // Ticket DBAL-231
51
            array('SELECT @rank := 1', false, array()), // Ticket DBAL-398
52
            array('SELECT @rank := 1 AS rank, :foo AS foo FROM :bar', false, array(27 => 'foo', 44 => 'bar')), // Ticket DBAL-398
53
            array('SELECT * FROM Foo WHERE bar > :start_date AND baz > :start_date', false, array(30 => 'start_date', 52 =>  'start_date')), // Ticket GH-113
54
            array('SELECT foo::date as date FROM Foo WHERE bar > :start_date AND baz > :start_date', false, array(46 => 'start_date', 68 =>  'start_date')), // Ticket GH-259
55
            array('SELECT `d.ns:col_name` FROM my_table d WHERE `d.date` >= :param1', false, array(57 => 'param1')), // Ticket DBAL-552
56
            array('SELECT [d.ns:col_name] FROM my_table d WHERE [d.date] >= :param1', false, array(57 => 'param1')), // Ticket DBAL-552
57
            array('SELECT * FROM foo WHERE jsonb_exists_any(foo.bar, ARRAY[:foo])', false, array(56 => 'foo')), // Ticket GH-2295
58
            array(
59
<<<'SQLDATA'
60
SELECT * FROM foo WHERE 
61
bar = ':not_a_param1 ''":not_a_param2"'''
62
OR bar=:a_param1
63
OR bar=:a_param2||':not_a_param3'
64
OR bar=':not_a_param4 '':not_a_param5'' :not_a_param6'
65
OR bar=''
66
OR bar=:a_param3
67
SQLDATA
68
                , false, array(74 => 'a_param1', 91 => 'a_param2', 190 => 'a_param3')
69
            ),
70
            array("SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE '\\\\') AND (data.description LIKE :condition_1 ESCAPE '\\\\') ORDER BY id ASC", false, array(121 => 'condition_0', 174 => 'condition_1')),
71
            array('SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE "\\\\") AND (data.description LIKE :condition_1 ESCAPE "\\\\") ORDER BY id ASC', false, array(121 => 'condition_0', 174 => 'condition_1')),
72
            array('SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE "\\\\") AND (data.description LIKE :condition_1 ESCAPE \'\\\\\') ORDER BY id ASC', false, array(121 => 'condition_0', 174 => 'condition_1')),
73
            array('SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE `\\\\`) AND (data.description LIKE :condition_1 ESCAPE `\\\\`) ORDER BY id ASC', false, array(121 => 'condition_0', 174 => 'condition_1')),
74
            array('SELECT data.age AS age, data.id AS id, data.name AS name, data.id AS id FROM test_data data WHERE (data.description LIKE :condition_0 ESCAPE \'\\\\\') AND (data.description LIKE :condition_1 ESCAPE `\\\\`) ORDER BY id ASC', false, array(121 => 'condition_0', 174 => 'condition_1')),
75
76
        );
77
    }
78
79
    /**
80
     * @dataProvider dataGetPlaceholderPositions
81
     */
82
    public function testGetPlaceholderPositions($query, $isPositional, $expectedParamPos)
83
    {
84
        $actualParamPos = SQLParserUtils::getPlaceholderPositions($query, $isPositional);
85
        self::assertEquals($expectedParamPos, $actualParamPos);
86
    }
87
88
    public function dataExpandListParameters()
89
    {
90
        return array(
91
            // Positional: Very simple with one needle
92
            array(
93
                "SELECT * FROM Foo WHERE foo IN (?)",
94
                array(array(1, 2, 3)),
95
                array(Connection::PARAM_INT_ARRAY),
96
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
97
                array(1, 2, 3),
98
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
99
            ),
100
            // Positional: One non-list before d one after list-needle
101
            array(
102
                "SELECT * FROM Foo WHERE foo = ? AND bar IN (?)",
103
                array("string", array(1, 2, 3)),
104
                array(\PDO::PARAM_STR, Connection::PARAM_INT_ARRAY),
105
                'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
106
                array("string", 1, 2, 3),
107
                array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
108
            ),
109
            // Positional: One non-list after list-needle
110
            array(
111
                "SELECT * FROM Foo WHERE bar IN (?) AND baz = ?",
112
                array(array(1, 2, 3), "foo"),
113
                array(Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR),
114
                'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
115
                array(1, 2, 3, "foo"),
116
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
117
            ),
118
            // Positional: One non-list before and one after list-needle
119
            array(
120
                "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ?",
121
                array(1, array(1, 2, 3), 4),
122
                array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY, \PDO::PARAM_INT),
123
                'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
124
                array(1, 1, 2, 3, 4),
125
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
126
            ),
127
            // Positional: Two lists
128
            array(
129
                "SELECT * FROM Foo WHERE foo IN (?, ?)",
130
                array(array(1, 2, 3), array(4, 5)),
131
                array(Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY),
132
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
133
                array(1, 2, 3, 4, 5),
134
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
135
            ),
136
            // Positional: Empty "integer" array DDC-1978
137
            array(
138
                "SELECT * FROM Foo WHERE foo IN (?)",
139
                array(array()),
140
                array(Connection::PARAM_INT_ARRAY),
141
                'SELECT * FROM Foo WHERE foo IN (NULL)',
142
                array(),
143
                array()
144
            ),
145
            // Positional: Empty "str" array DDC-1978
146
            array(
147
                "SELECT * FROM Foo WHERE foo IN (?)",
148
                array(array()),
149
                array(Connection::PARAM_STR_ARRAY),
150
                'SELECT * FROM Foo WHERE foo IN (NULL)',
151
                array(),
152
                array()
153
            ),
154
            // Positional: explicit keys for params and types
155
            array(
156
                "SELECT * FROM Foo WHERE foo = ? AND bar = ? AND baz = ?",
157
                array(1 => 'bar', 2 => 'baz', 0 => 1),
158
                array(2 => \PDO::PARAM_STR, 1 => \PDO::PARAM_STR),
159
                'SELECT * FROM Foo WHERE foo = ? AND bar = ? AND baz = ?',
160
                array(1 => 'bar', 0 => 1, 2 => 'baz'),
161
                array(1 => \PDO::PARAM_STR, 2 => \PDO::PARAM_STR)
162
            ),
163
            // Positional: explicit keys for array params and array types
164
            array(
165
                "SELECT * FROM Foo WHERE foo IN (?) AND bar IN (?) AND baz = ?",
166
                array(1 => array('bar1', 'bar2'), 2 => true, 0 => array(1, 2, 3)),
167
                array(2 => \PDO::PARAM_BOOL, 1 => Connection::PARAM_STR_ARRAY, 0 => Connection::PARAM_INT_ARRAY),
168
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND bar IN (?, ?) AND baz = ?',
169
                array(1, 2, 3, 'bar1', 'bar2', true),
170
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_BOOL)
171
            ),
172
            // Positional starts from 1: One non-list before and one after list-needle
173
            array(
174
                "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ? AND foo IN (?)",
175
                array(1 => 1, 2 => array(1, 2, 3), 3 => 4, 4 => array(5, 6)),
176
                array(1 => \PDO::PARAM_INT, 2 => Connection::PARAM_INT_ARRAY, 3 => \PDO::PARAM_INT, 4 => Connection::PARAM_INT_ARRAY),
177
                'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ? AND foo IN (?, ?)',
178
                array(1, 1, 2, 3, 4, 5, 6),
179
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
180
            ),
181
            //  Named parameters : Very simple with param int
182
            array(
183
                "SELECT * FROM Foo WHERE foo = :foo",
184
                array('foo'=>1),
185
                array('foo'=>\PDO::PARAM_INT),
186
                'SELECT * FROM Foo WHERE foo = ?',
187
                array(1),
188
                array(\PDO::PARAM_INT)
189
            ),
190
191
             //  Named parameters : Very simple with param int and string
192
            array(
193
                "SELECT * FROM Foo WHERE foo = :foo AND bar = :bar",
194
                array('bar'=>'Some String','foo'=>1),
195
                array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_STR),
196
                'SELECT * FROM Foo WHERE foo = ? AND bar = ?',
197
                array(1,'Some String'),
198
                array(\PDO::PARAM_INT, \PDO::PARAM_STR)
199
            ),
200
            //  Named parameters : Very simple with one needle
201
            array(
202
                "SELECT * FROM Foo WHERE foo IN (:foo)",
203
                array('foo'=>array(1, 2, 3)),
204
                array('foo'=>Connection::PARAM_INT_ARRAY),
205
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
206
                array(1, 2, 3),
207
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
208
            ),
209
            // Named parameters: One non-list before d one after list-needle
210
            array(
211
                "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar)",
212
                array('foo'=>"string", 'bar'=>array(1, 2, 3)),
213
                array('foo'=>\PDO::PARAM_STR, 'bar'=>Connection::PARAM_INT_ARRAY),
214
                'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
215
                array("string", 1, 2, 3),
216
                array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
217
            ),
218
            // Named parameters: One non-list after list-needle
219
            array(
220
                "SELECT * FROM Foo WHERE bar IN (:bar) AND baz = :baz",
221
                array('bar'=>array(1, 2, 3), 'baz'=>"foo"),
222
                array('bar'=>Connection::PARAM_INT_ARRAY, 'baz'=>\PDO::PARAM_STR),
223
                'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
224
                array(1, 2, 3, "foo"),
225
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
226
            ),
227
            // Named parameters: One non-list before and one after list-needle
228
            array(
229
                "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar) AND baz = :baz",
230
                array('bar'=>array(1, 2, 3),'foo'=>1, 'baz'=>4),
231
                array('bar'=>Connection::PARAM_INT_ARRAY, 'foo'=>\PDO::PARAM_INT, 'baz'=>\PDO::PARAM_INT),
232
                'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
233
                array(1, 1, 2, 3, 4),
234
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
235
            ),
236
            // Named parameters: Two lists
237
            array(
238
                "SELECT * FROM Foo WHERE foo IN (:a, :b)",
239
                array('b'=>array(4, 5),'a'=>array(1, 2, 3)),
240
                array('a'=>Connection::PARAM_INT_ARRAY, 'b'=>Connection::PARAM_INT_ARRAY),
241
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
242
                array(1, 2, 3, 4, 5),
243
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
244
            ),
245
            //  Named parameters : With the same name arg type string
246
            array(
247
                "SELECT * FROM Foo WHERE foo <> :arg AND bar = :arg",
248
                array('arg'=>"Some String"),
249
                array('arg'=>\PDO::PARAM_STR),
250
                'SELECT * FROM Foo WHERE foo <> ? AND bar = ?',
251
                array("Some String","Some String"),
252
                array(\PDO::PARAM_STR,\PDO::PARAM_STR,)
253
            ),
254
             //  Named parameters : With the same name arg
255
            array(
256
                "SELECT * FROM Foo WHERE foo IN (:arg) AND NOT bar IN (:arg)",
257
                array('arg'=>array(1, 2, 3)),
258
                array('arg'=>Connection::PARAM_INT_ARRAY),
259
                'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND NOT bar IN (?, ?, ?)',
260
                array(1, 2, 3, 1, 2, 3),
261
                array(\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT,\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT)
262
            ),
263
264
             //  Named parameters : Same name, other name in between DBAL-299
265
            array(
266
                "SELECT * FROM Foo WHERE (:foo = 2) AND (:bar = 3) AND (:foo = 2)",
267
                array('foo'=>2,'bar'=>3),
268
                array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_INT),
269
                'SELECT * FROM Foo WHERE (? = 2) AND (? = 3) AND (? = 2)',
270
                array(2, 3, 2),
271
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
272
            ),
273
             //  Named parameters : Empty "integer" array DDC-1978
274
            array(
275
                "SELECT * FROM Foo WHERE foo IN (:foo)",
276
                array('foo'=>array()),
277
                array('foo'=>Connection::PARAM_INT_ARRAY),
278
                'SELECT * FROM Foo WHERE foo IN (NULL)',
279
                array(),
280
                array()
281
            ),
282
             //  Named parameters : Two empty "str" array DDC-1978
283
            array(
284
                "SELECT * FROM Foo WHERE foo IN (:foo) OR bar IN (:bar)",
285
                array('foo'=>array(), 'bar'=>array()),
286
                array('foo'=>Connection::PARAM_STR_ARRAY, 'bar'=>Connection::PARAM_STR_ARRAY),
287
                'SELECT * FROM Foo WHERE foo IN (NULL) OR bar IN (NULL)',
288
                array(),
289
                array()
290
            ),
291
            array(
292
                "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar OR baz = :baz",
293
                array('foo' => array(1, 2), 'bar' => 'bar', 'baz' => 'baz'),
294
                array('foo' => Connection::PARAM_INT_ARRAY, 'baz' => 'string'),
295
                'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ? OR baz = ?',
296
                array(1, 2, 'bar', 'baz'),
297
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR, 'string')
298
            ),
299
            array(
300
                "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
301
                array('foo' => array(1, 2), 'bar' => 'bar'),
302
                array('foo' => Connection::PARAM_INT_ARRAY),
303
                'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
304
                array(1, 2, 'bar'),
305
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
306
            ),
307
            // Params/types with colons
308
            array(
309
                "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar",
310
                array(':foo' => 'foo', ':bar' => 'bar'),
311
                array(':foo' => \PDO::PARAM_INT),
312
                'SELECT * FROM Foo WHERE foo = ? OR bar = ?',
313
                array('foo', 'bar'),
314
                array(\PDO::PARAM_INT, \PDO::PARAM_STR)
315
            ),
316
            array(
317
                "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar",
318
                array(':foo' => 'foo', ':bar' => 'bar'),
319
                array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT),
320
                'SELECT * FROM Foo WHERE foo = ? OR bar = ?',
321
                array('foo', 'bar'),
322
                array(\PDO::PARAM_INT, \PDO::PARAM_INT)
323
            ),
324
            array(
325
                "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
326
                array(':foo' => array(1, 2), ':bar' => 'bar'),
327
                array('foo' => Connection::PARAM_INT_ARRAY),
328
                'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
329
                array(1, 2, 'bar'),
330
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
331
            ),
332
            array(
333
                "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
334
                array('foo' => array(1, 2), 'bar' => 'bar'),
335
                array(':foo' => Connection::PARAM_INT_ARRAY),
336
                'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
337
                array(1, 2, 'bar'),
338
                array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
339
            ),
340
            // DBAL-522 - null valued parameters are not considered
341
            array(
342
                'INSERT INTO Foo (foo, bar) values (:foo, :bar)',
343
                array('foo' => 1, 'bar' => null),
344
                array(':foo' => \PDO::PARAM_INT, ':bar' => \PDO::PARAM_NULL),
345
                'INSERT INTO Foo (foo, bar) values (?, ?)',
346
                array(1, null),
347
                array(\PDO::PARAM_INT, \PDO::PARAM_NULL)
348
            ),
349
            array(
350
                'INSERT INTO Foo (foo, bar) values (?, ?)',
351
                array(1, null),
352
                array(\PDO::PARAM_INT, \PDO::PARAM_NULL),
353
                'INSERT INTO Foo (foo, bar) values (?, ?)',
354
                array(1, null),
355
                array(\PDO::PARAM_INT, \PDO::PARAM_NULL)
356
            ),
357
            // DBAL-1205 - Escaped single quotes SQL- and C-Style
358
            array(
359
                "SELECT * FROM Foo WHERE foo = :foo||''':not_a_param''\\'' OR bar = ''':not_a_param''\\'':bar",
360
                array(':foo' => 1, ':bar' => 2),
361
                array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT),
362
                'SELECT * FROM Foo WHERE foo = ?||\'\'\':not_a_param\'\'\\\'\' OR bar = \'\'\':not_a_param\'\'\\\'\'?',
363
                array(1, 2),
364
                array(\PDO::PARAM_INT, \PDO::PARAM_INT)
365
            ),
366
        );
367
    }
368
369
    /**
370
     * @dataProvider dataExpandListParameters
371
     */
372
    public function testExpandListParameters($q, $p, $t, $expectedQuery, $expectedParams, $expectedTypes)
373
    {
374
        list($query, $params, $types) = SQLParserUtils::expandListParameters($q, $p, $t);
375
376
        self::assertEquals($expectedQuery, $query, "Query was not rewritten correctly.");
377
        self::assertEquals($expectedParams, $params, "Params dont match");
378
        self::assertEquals($expectedTypes, $types, "Types dont match");
379
    }
380
381
    public function dataQueryWithMissingParameters()
382
    {
383
        return array(
384
            array(
385
                "SELECT * FROM foo WHERE bar = :param",
386
                array('other' => 'val'),
387
                array(),
388
            ),
389
            array(
390
                "SELECT * FROM foo WHERE bar = :param",
391
                array(),
392
                array(),
393
            ),
394
            array(
395
                "SELECT * FROM foo WHERE bar = :param",
396
                array(),
397
                array('param' => Connection::PARAM_INT_ARRAY),
398
            ),
399
            array(
400
                "SELECT * FROM foo WHERE bar = :param",
401
                array(),
402
                array(':param' => Connection::PARAM_INT_ARRAY),
403
            ),
404
            array(
405
                "SELECT * FROM foo WHERE bar = :param",
406
                array(),
407
                array('bar' => Connection::PARAM_INT_ARRAY),
408
            ),
409
             array(
410
                "SELECT * FROM foo WHERE bar = :param",
411
                array('bar' => 'value'),
412
                array('bar' => Connection::PARAM_INT_ARRAY),
413
            ),
414
        );
415
    }
416
417
    /**
418
     * @dataProvider dataQueryWithMissingParameters
419
     */
420
    public function testExceptionIsThrownForMissingParam($query, $params, $types = array())
421
    {
422
        $this->expectException(
423
            'Doctrine\DBAL\SQLParserUtilsException',
424
            'Value for :param not found in params array. Params array key should be "param"'
0 ignored issues
show
The call to PHPUnit\Framework\TestCase::expectException() has too many arguments starting with 'Value for :param not fo... key should be "param"'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

424
        $this->/** @scrutinizer ignore-call */ 
425
               expectException(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
425
        );
426
427
        SQLParserUtils::expandListParameters($query, $params, $types);
428
    }
429
}
430