Completed
Pull Request — master (#3104)
by Joas
64:51 queued 03:40
created

testNamedParametersAfterEscape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\FetchMode;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
9
use Doctrine\DBAL\Platforms\SqlitePlatform;
10
use Doctrine\DBAL\Schema\Table;
11
use const CASE_LOWER;
12
use function array_change_key_case;
13
14
/**
15
 * @group DDC-1372
16
 */
17
class NamedParametersTest extends \Doctrine\Tests\DbalFunctionalTestCase
18
{
19
    public function ticketProvider()
20
    {
21
        return [
22
            [
23
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
24
                [
25
                    'foo' => 1,
26
                    'bar' => [1, 2, 3],
27
                ],
28
                [
29
                    'foo' => ParameterType::INTEGER,
30
                    'bar' => Connection::PARAM_INT_ARRAY,
31
                ],
32
                [
33
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
34
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
35
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
36
                ],
37
            ],
38
39
            [
40
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
41
                [
42
                    'foo' => 1,
43
                    'bar' => [1, 2, 3],
44
                ],
45
                [
46
                    'bar' => Connection::PARAM_INT_ARRAY,
47
                    'foo' => ParameterType::INTEGER,
48
                ],
49
                [
50
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
51
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
52
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
53
                ],
54
            ],
55
56
            [
57
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
58
                [
59
                    'foo' => 1,
60
                    'bar' => [1, 2, 3],
61
                ],
62
                [
63
                    'bar' => Connection::PARAM_INT_ARRAY,
64
                    'foo' => ParameterType::INTEGER,
65
                ],
66
                [
67
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
68
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
69
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
70
                ],
71
            ],
72
73
            [
74
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
75
                [
76
                    'foo' => 1,
77
                    'bar' => ['1', '2', '3'],
78
                ],
79
                [
80
                    'bar' => Connection::PARAM_STR_ARRAY,
81
                    'foo' => ParameterType::INTEGER,
82
                ],
83
                [
84
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
85
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
86
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
87
                ],
88
            ],
89
90
            [
91
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
92
                [
93
                    'foo' => ['1'],
94
                    'bar' => [1, 2, 3, 4],
95
                ],
96
                [
97
                    'bar' => Connection::PARAM_STR_ARRAY,
98
                    'foo' => Connection::PARAM_INT_ARRAY,
99
                ],
100
                [
101
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
102
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
103
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
104
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
105
                ],
106
            ],
107
108
            [
109
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
110
                [
111
                    'foo' => 1,
112
                    'bar' => 2,
113
                ],
114
                [
115
                    'bar' => ParameterType::INTEGER,
116
                    'foo' => ParameterType::INTEGER,
117
                ],
118
                [
119
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
120
                ],
121
            ],
122
123
            [
124
                'SELECT * FROM ddc1372_foobar f WHERE f.bar = :arg AND f.foo <> :arg',
125
                ['arg' => '1'],
126
                [
127
                    'arg' => ParameterType::STRING,
128
                ],
129
                [
130
                    ['id' => 5, 'foo' => 2, 'bar' => 1],
131
                ],
132
            ],
133
134
            [
135
                'SELECT * FROM ddc1372_foobar f WHERE f.bar NOT IN (:arg) AND f.foo IN (:arg)',
136
                [
137
                    'arg' => [1, 2],
138
                ],
139
                [
140
                    'arg' => Connection::PARAM_INT_ARRAY,
141
                ],
142
                [
143
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
144
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
145
                ],
146
            ],
147
        ];
148
    }
149
150
    protected function setUp()
151
    {
152
        parent::setUp();
153
154
        if (! $this->_conn->getSchemaManager()->tablesExist('ddc1372_foobar')) {
0 ignored issues
show
Bug introduced by
'ddc1372_foobar' of type string is incompatible with the type array expected by parameter $tableNames of Doctrine\DBAL\Schema\Abs...aManager::tablesExist(). ( Ignorable by Annotation )

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

154
        if (! $this->_conn->getSchemaManager()->tablesExist(/** @scrutinizer ignore-type */ 'ddc1372_foobar')) {
Loading history...
155
            try {
156
                $table = new Table('ddc1372_foobar');
157
                $table->addColumn('id', 'integer');
158
                $table->addColumn('foo', 'string');
159
                $table->addColumn('bar', 'string');
160
                $table->setPrimaryKey(['id']);
161
162
                $sm = $this->_conn->getSchemaManager();
163
                $sm->createTable($table);
164
165
                $this->_conn->insert('ddc1372_foobar', [
166
                    'id'  => 1,
167
                    'foo' => 1,
168
                    'bar' => 1,
169
                ]);
170
                $this->_conn->insert('ddc1372_foobar', [
171
                    'id'  => 2,
172
                    'foo' => 1,
173
                    'bar' => 2,
174
                ]);
175
                $this->_conn->insert('ddc1372_foobar', [
176
                    'id'  => 3,
177
                    'foo' => 1,
178
                    'bar' => 3,
179
                ]);
180
                $this->_conn->insert('ddc1372_foobar', [
181
                    'id'  => 4,
182
                    'foo' => 1,
183
                    'bar' => 4,
184
                ]);
185
                $this->_conn->insert('ddc1372_foobar', [
186
                    'id'  => 5,
187
                    'foo' => 2,
188
                    'bar' => 1,
189
                ]);
190
                $this->_conn->insert('ddc1372_foobar', [
191
                    'id'  => 6,
192
                    'foo' => 2,
193
                    'bar' => 2,
194
                ]);
195
            } catch(\Exception $e) {
196
                $this->fail($e->getMessage());
197
            }
198
        }
199
    }
200
201
    /**
202
     * @dataProvider ticketProvider
203
     * @param string $query
204
     * @param array  $params
205
     * @param array  $types
206
     * @param array  $expected
207
     */
208
    public function testTicket($query,$params,$types,$expected)
209
    {
210
        $stmt   = $this->_conn->executeQuery($query, $params, $types);
211
        $result = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
212
213
        foreach ($result as $k => $v) {
214
            $result[$k] = array_change_key_case($v, CASE_LOWER);
215
        }
216
217
        self::assertEquals($expected, $result);
218
    }
219
220
    public function namedParametersAfterEscapeProvider()
221
    {
222
        return [
223
            [['param_0' => 'bar', 'param_1' => 'foo', 'find' => '%a%']],
224
            [['param_0' => 'bar', 'param_1' => 'foo', 'find' => '%o%']],
225
        ];
226
    }
227
228
    /**
229
     * @dataProvider namedParametersAfterEscapeProvider
230
     * @param array $params
231
     */
232
    public function testNamedParametersAfterEscape(array $params)
233
    {
234
        $escapeChar = $this->_conn->getDatabasePlatform()->quoteStringLiteral('\\');
235
        $query = str_replace('{{escape}}', $escapeChar, "SELECT 1 FROM ddc1372_foobar WHERE (:param_0 LIKE :find ESCAPE {{escape}}) OR (:param_1 LIKE :find ESCAPE {{escape}}) LIMIT 1");
236
237
        $stmt = $this->_conn->executeQuery(
238
            $query,
239
            $params,
240
            ['param_0' => 2, 'param_1' => 2, 'find' => 2]
241
        );
242
        $result = $stmt->fetchAll(FetchMode::COLUMN);
243
244
        self::assertEquals([
245
            1,
246
        ], $result);
247
    }
248
}
249