Failed Conditions
Pull Request — master (#3063)
by Gabriel
20:36
created

NamedParametersTest::testTicket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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