Completed
Push — master ( 62fe3f...89a52c )
by Marco
18s queued 13s
created

NamedParametersTest::setUp()   A

Complexity

Conditions 3
Paths 15

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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

152
        if (! $this->_conn->getSchemaManager()->tablesExist(/** @scrutinizer ignore-type */ 'ddc1372_foobar')) {
Loading history...
153
            try {
154
                $table = new Table('ddc1372_foobar');
155
                $table->addColumn('id', 'integer');
156
                $table->addColumn('foo', 'string');
157
                $table->addColumn('bar', 'string');
158
                $table->setPrimaryKey(['id']);
159
160
                $sm = $this->_conn->getSchemaManager();
161
                $sm->createTable($table);
162
163
                $this->_conn->insert('ddc1372_foobar', [
164
                    'id'  => 1,
165
                    'foo' => 1,
166
                    'bar' => 1,
167
                ]);
168
                $this->_conn->insert('ddc1372_foobar', [
169
                    'id'  => 2,
170
                    'foo' => 1,
171
                    'bar' => 2,
172
                ]);
173
                $this->_conn->insert('ddc1372_foobar', [
174
                    'id'  => 3,
175
                    'foo' => 1,
176
                    'bar' => 3,
177
                ]);
178
                $this->_conn->insert('ddc1372_foobar', [
179
                    'id'  => 4,
180
                    'foo' => 1,
181
                    'bar' => 4,
182
                ]);
183
                $this->_conn->insert('ddc1372_foobar', [
184
                    'id'  => 5,
185
                    'foo' => 2,
186
                    'bar' => 1,
187
                ]);
188
                $this->_conn->insert('ddc1372_foobar', [
189
                    'id'  => 6,
190
                    'foo' => 2,
191
                    'bar' => 2,
192
                ]);
193
            } catch(\Exception $e) {
194
                $this->fail($e->getMessage());
195
            }
196
        }
197
    }
198
199
    /**
200
     * @dataProvider ticketProvider
201
     * @param string $query
202
     * @param array  $params
203
     * @param array  $types
204
     * @param array  $expected
205
     */
206
    public function testTicket($query,$params,$types,$expected)
207
    {
208
        $stmt   = $this->_conn->executeQuery($query, $params, $types);
209
        $result = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
210
211
        foreach ($result as $k => $v) {
212
            $result[$k] = array_change_key_case($v, CASE_LOWER);
213
        }
214
215
        self::assertEquals($result, $expected);
216
    }
217
}
218