Completed
Push — h/allow-multiple-named-params ( 2eb7f0 )
by Christian
05:00
created

PDOStatementTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 11
Bugs 2 Features 5
Metric Value
wmc 23
c 11
b 2
f 5
lcom 1
cbo 2
dl 0
loc 269
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testBindParam() 0 22 2
A testFetchColumn() 0 14 2
B testFetchBound() 0 30 3
A testFetchAllWithNumStyle() 0 17 2
A testFetchAllWithAssocStyle() 0 17 2
A testFetchAllWithBothStyle() 0 18 2
B testFetchAllWithFuncStyle() 0 29 3
B testBindNamedParam() 0 36 2
A testBindNamedParamUnordered() 0 15 1
A testBindNamedParamMultiple() 0 18 1
A testBindValue() 0 21 2
A testArrayValue() 0 17 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace CrateIntegrationTest\PDO;
24
25
use Crate\PDO\PDO;
26
27
/**
28
 * Class PDOStatementTest
29
 *
30
 * @coversNothing
31
 *
32
 * @group integration
33
 */
34
class PDOStatementTest extends AbstractIntegrationTest
35
{
36
    public function testFetchColumn()
37
    {
38
        $this->insertRows(5);
39
40
        $statement = $this->pdo->prepare('SELECT id FROM test_table');
41
42
        $result = [];
43
44
        while ($columnValue = $statement->fetchColumn()) {
45
            $result[] = $columnValue;
46
        }
47
48
        $this->assertEquals([1, 2, 3, 4, 5], $result);
49
    }
50
51
    public function testFetchBound()
52
    {
53
        $expected = [
54
            ['id' => 1, 'name' => 'first'],
55
            ['id' => 2, 'name' => 'second'],
56
            ['id' => 3, 'name' => 'third'],
57
        ];
58
59
        foreach ($expected as $row) {
60
            $this->insertRow($row['id'], $row['name']);
61
        }
62
63
        $id    = null;
64
        $name  = null;
65
        $index = 0;
66
67
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
68
        $statement->bindColumn('id', $id);
69
        $statement->bindColumn('name', $name);
70
71
        while ($row = $statement->fetch(PDO::FETCH_BOUND)) {
0 ignored issues
show
Unused Code introduced by
$row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
73
            $this->assertEquals($expected[$index]['id'], $id);
74
            $this->assertEquals($expected[$index]['name'], $name);
75
76
            $index++;
77
        }
78
79
        $this->assertEquals(3, $index);
80
    }
81
82
    public function testFetchAllWithNumStyle()
83
    {
84
        $expected = [
85
            [1, 'first'],
86
            [2, 'second'],
87
            [3, 'third'],
88
        ];
89
90
        foreach ($expected as $row) {
91
            $this->insertRow($row[0], $row[1]);
92
        }
93
94
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
95
        $statement->execute();
96
97
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_NUM));
98
    }
99
100
    public function testFetchAllWithAssocStyle()
101
    {
102
        $expected = [
103
            ['id' => 1, 'name' => 'first'],
104
            ['id' => 2, 'name' => 'second'],
105
            ['id' => 3, 'name' => 'third'],
106
        ];
107
108
        foreach ($expected as $row) {
109
            $this->insertRow($row['id'], $row['name']);
110
        }
111
112
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
113
        $statement->execute();
114
115
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_ASSOC));
116
    }
117
118
    public function testFetchAllWithBothStyle()
119
    {
120
        $expected = [
121
            [0 => 1, 'id' => 1, 1 => 'first', 'name' => 'first'],
122
            [0 => 2, 'id' => 2, 1 => 'second', 'name' => 'second'],
123
            [0 => 3, 'id' => 3, 1 => 'third', 'name' => 'third'],
124
        ];
125
126
        foreach ($expected as $row) {
127
            $this->insertRow($row['id'], $row['name']);
128
        }
129
130
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
131
        $statement->execute();
132
133
        // In theory this should be assertSame, but implementing that would be incredibly slow
134
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_BOTH));
135
    }
136
137
    public function testFetchAllWithFuncStyle()
138
    {
139
        $expected = [
140
            ['id' => 1, 'name' => 'first'],
141
            ['id' => 2, 'name' => 'second'],
142
            ['id' => 3, 'name' => 'third'],
143
        ];
144
145
        foreach ($expected as $row) {
146
            $this->insertRow($row['id'], $row['name']);
147
        }
148
149
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
150
        $statement->execute();
151
152
        $index    = 0;
153
        $callback = function ($id, $name) {
154
            return sprintf('%d:%s', $id, $name);
155
        };
156
157
        $resultSet = $statement->fetchAll(PDO::FETCH_FUNC, $callback);
158
159
        foreach ($resultSet as $result) {
160
            $this->assertEquals(sprintf('%d:%s', $expected[$index]['id'], $expected[$index]['name']), $result);
161
            $index++;
162
        }
163
164
        $this->assertEquals(count($expected), $index);
165
    }
166
167
    public function testBindParam()
168
    {
169
        $expected = [
170
            ['id' => 1, 'name' => 'first'],
171
            ['id' => 2, 'name' => 'second'],
172
            ['id' => 3, 'name' => 'third'],
173
        ];
174
175
        foreach ($expected as $row) {
176
            $this->insertRow($row['id'], $row['name']);
177
        }
178
179
        $name = 'second';
180
        $statement = $this->pdo->prepare('SELECT * FROM test_table where name = ?');
181
        $statement->bindParam(1, $name);
182
        $statement->execute();
183
        $this->assertEquals(1, $statement->rowCount());
184
185
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
186
        $this->assertEquals(2, $resultSet[0]['id']);
187
        $this->assertEquals($name, $resultSet[0]['name']);
188
    }
189
190
    public function testBindNamedParam()
191
    {
192
        $expected = [
193
            ['id' => 1, 'name' => 'first'],
194
            ['id' => 2, 'name' => 'second'],
195
            ['id' => 3, 'name' => 'third'],
196
        ];
197
198
        foreach ($expected as $row) {
199
            $this->insertRow($row['id'], $row['name']);
200
        }
201
202
        $name = 'second';
203
        $id = 2;
204
        $sql = 'SELECT * FROM test_table where name = :name and id = :id';
205
206
        $statement = $this->pdo->prepare($sql);
207
        $statement->bindParam('name', $name);
208
        $statement->bindParam('id', $id);
209
        $statement->execute();
210
        $this->assertEquals(1, $statement->rowCount());
211
212
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
213
        $this->assertEquals(2, $resultSet[0]['id']);
214
        $this->assertEquals($name, $resultSet[0]['name']);
215
216
        $statement = $this->pdo->prepare($sql);
217
        $statement->bindParam(':name', $name);
218
        $statement->bindParam(':id', $id);
219
        $statement->execute();
220
        $this->assertEquals(1, $statement->rowCount());
221
222
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
223
        $this->assertEquals(2, $resultSet[0]['id']);
224
        $this->assertEquals($name, $resultSet[0]['name']);
225
    }
226
227
    public function testBindNamedParamUnordered()
228
    {
229
        $this->insertRows(2);
230
231
        $statement = $this->pdo->prepare('UPDATE test_table SET name = concat(name, :name) where id = :id');
232
        $statement->bindValue(':id', 1);
233
        $statement->bindValue(':name', '_abc');
234
        $statement->execute();
235
236
        $this->pdo->exec('REFRESH TABLE test_table');
237
238
        $statement = $this->pdo->prepare('SELECT name FROM test_table WHERE ID=1');
239
        $resultSet = $statement->fetch();
240
        $this->assertEquals('hello world_abc', $resultSet[0]);
241
    }
242
243
    public function testBindNamedParamMultiple()
244
    {
245
        $this->pdo->exec("INSERT INTO test_table (id, name, int_type) VALUES (1, 'hello', 1), (2, 'world', 1), (3, 'hello', 2), (4, 'world', 3)");
246
        $this->pdo->exec("REFRESH TABLE test_table");
247
248
        $statement = $this->pdo->prepare('update test_table set name = concat(name, :name) where int_type = :int_type and name != :name');
249
        $statement->bindValue(':int_type', 1, PDO::PARAM_INT);
250
        $statement->bindValue(':name', 'world', PDO::PARAM_STR);
251
        $statement->execute();
252
253
        $this->pdo->exec("REFRESH TABLE test_table");
254
255
        $statement = $this->pdo->prepare("SELECT id, name, int_type FROM test_table WHERE id=1");
256
        $resultSet = $statement->fetch();
257
        $this->assertEquals(1, $resultSet[0]);
258
        $this->assertEquals('helloworld', $resultSet[1]);
259
        $this->assertEquals(1, $resultSet[2]);
260
    }
261
    
262
    public function testBindValue()
263
    {
264
        $expected = [
265
            ['id' => 1, 'name' => 'first'],
266
            ['id' => 2, 'name' => 'second'],
267
            ['id' => 3, 'name' => 'third'],
268
        ];
269
270
        foreach ($expected as $row) {
271
            $this->insertRow($row['id'], $row['name']);
272
        }
273
274
        $statement = $this->pdo->prepare('SELECT * FROM test_table where name = ?');
275
        $statement->bindValue(1, 'second');
276
        $statement->execute();
277
        $this->assertEquals(1, $statement->rowCount());
278
279
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
280
        $this->assertEquals(2, $resultSet[0]['id']);
281
        $this->assertEquals('second', $resultSet[0]['name']);
282
    }
283
284
    public function testArrayValue()
285
    {
286
        $statement = $this->pdo->prepare('INSERT INTO test_table (id, array_type, object_type) VALUES(?, ?, ?)');
287
        $statement->bindValue(1, 1, PDO::PARAM_INT);
288
        $statement->bindValue(2, [1, 2], PDO::PARAM_ARRAY);
289
        $statement->bindValue(3, ["foo" => "bar"], PDO::PARAM_OBJECT);
290
        $statement->execute();
291
        $this->assertEquals(1, $statement->rowCount());
292
293
        $this->pdo->exec('REFRESH TABLE test_table');
294
295
        $statement = $this->pdo->prepare('SELECT id, array_type, object_type FROM test_table');
296
        $resultSet = $statement->fetchAll(PDO::FETCH_ASSOC);
297
        $this->assertEquals(1, $resultSet[0]['id']);
298
        $this->assertEquals([1, 2], $resultSet[0]['array_type']);
299
        $this->assertEquals(["foo" => "bar"], $resultSet[0]['object_type']);
300
    }
301
302
}
303