Completed
Push — atv/fetch-object ( c62dd2 )
by
unknown
03:53
created

PDOStatementTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 27
lcom 1
cbo 2
dl 0
loc 314
rs 10
c 8
b 0
f 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testFetchColumn() 0 14 2
B testFetchBound() 0 30 3
A testFetchAllWithNumStyle() 0 17 2
A testFetchAllWithAssocStyle() 0 17 2
A testFetchAllWithObjectStyle() 0 17 2
A testFetchSameColumnTwiceWithAssocStyle() 0 14 1
A testFetchAllWithBothStyle() 0 18 2
B testFetchAllWithFuncStyle() 0 29 3
A testBindParam() 0 22 2
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
A testNullParamBinding() 0 11 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 testFetchAllWithObjectStyle()
119
    {
120
        $expected = [
121
            (object)['id' => 1, 'name' => 'first'],
122
            (object)['id' => 2, 'name' => 'second'],
123
            (object)['id' => 3, '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
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_OBJ));
134
    }
135
136
    public function testFetchSameColumnTwiceWithAssocStyle()
137
    {
138
        $this->insertRows(3);
139
        $expected = [
140
            ['id' => 1, 'id' => 1],
141
            ['id' => 2, 'id' => 2],
142
            ['id' => 3, 'id' => 3],
143
        ];
144
145
        $statement = $this->pdo->prepare('SELECT id, id FROM test_table');
146
        $statement->execute();
147
148
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_ASSOC));
149
    }
150
151
    public function testFetchAllWithBothStyle()
152
    {
153
        $expected = [
154
            [0 => 1, 'id' => 1, 1 => 'first', 'name' => 'first'],
155
            [0 => 2, 'id' => 2, 1 => 'second', 'name' => 'second'],
156
            [0 => 3, 'id' => 3, 1 => 'third', 'name' => 'third'],
157
        ];
158
159
        foreach ($expected as $row) {
160
            $this->insertRow($row['id'], $row['name']);
161
        }
162
163
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
164
        $statement->execute();
165
166
        // In theory this should be assertSame, but implementing that would be incredibly slow
167
        $this->assertEquals($expected, $statement->fetchAll(PDO::FETCH_BOTH));
168
    }
169
170
    public function testFetchAllWithFuncStyle()
171
    {
172
        $expected = [
173
            ['id' => 1, 'name' => 'first'],
174
            ['id' => 2, 'name' => 'second'],
175
            ['id' => 3, 'name' => 'third'],
176
        ];
177
178
        foreach ($expected as $row) {
179
            $this->insertRow($row['id'], $row['name']);
180
        }
181
182
        $statement = $this->pdo->prepare('SELECT id, name FROM test_table');
183
        $statement->execute();
184
185
        $index    = 0;
186
        $callback = function ($id, $name) {
187
            return sprintf('%d:%s', $id, $name);
188
        };
189
190
        $resultSet = $statement->fetchAll(PDO::FETCH_FUNC, $callback);
191
192
        foreach ($resultSet as $result) {
193
            $this->assertEquals(sprintf('%d:%s', $expected[$index]['id'], $expected[$index]['name']), $result);
194
            $index++;
195
        }
196
197
        $this->assertEquals(count($expected), $index);
198
    }
199
200
    public function testBindParam()
201
    {
202
        $expected = [
203
            ['id' => 1, 'name' => 'first'],
204
            ['id' => 2, 'name' => 'second'],
205
            ['id' => 3, 'name' => 'third'],
206
        ];
207
208
        foreach ($expected as $row) {
209
            $this->insertRow($row['id'], $row['name']);
210
        }
211
212
        $name = 'second';
213
        $statement = $this->pdo->prepare('SELECT * FROM test_table where name = ?');
214
        $statement->bindParam(1, $name);
215
        $statement->execute();
216
        $this->assertEquals(1, $statement->rowCount());
217
218
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
219
        $this->assertEquals(2, $resultSet[0]['id']);
220
        $this->assertEquals($name, $resultSet[0]['name']);
221
    }
222
223
    public function testBindNamedParam()
224
    {
225
        $expected = [
226
            ['id' => 1, 'name' => 'first'],
227
            ['id' => 2, 'name' => 'second'],
228
            ['id' => 3, 'name' => 'third'],
229
        ];
230
231
        foreach ($expected as $row) {
232
            $this->insertRow($row['id'], $row['name']);
233
        }
234
235
        $name = 'second';
236
        $id = 2;
237
        $sql = 'SELECT * FROM test_table where name = :name and id = :id';
238
239
        $statement = $this->pdo->prepare($sql);
240
        $statement->bindParam('name', $name);
241
        $statement->bindParam('id', $id);
242
        $statement->execute();
243
        $this->assertEquals(1, $statement->rowCount());
244
245
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
246
        $this->assertEquals(2, $resultSet[0]['id']);
247
        $this->assertEquals($name, $resultSet[0]['name']);
248
249
        $statement = $this->pdo->prepare($sql);
250
        $statement->bindParam(':name', $name);
251
        $statement->bindParam(':id', $id);
252
        $statement->execute();
253
        $this->assertEquals(1, $statement->rowCount());
254
255
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
256
        $this->assertEquals(2, $resultSet[0]['id']);
257
        $this->assertEquals($name, $resultSet[0]['name']);
258
    }
259
260
    public function testBindNamedParamUnordered()
261
    {
262
        $this->insertRows(2);
263
264
        $statement = $this->pdo->prepare('UPDATE test_table SET name = concat(name, :name) where id = :id');
265
        $statement->bindValue(':id', 1);
266
        $statement->bindValue(':name', '_abc');
267
        $statement->execute();
268
269
        $this->pdo->exec('REFRESH TABLE test_table');
270
271
        $statement = $this->pdo->prepare('SELECT name FROM test_table WHERE ID=1');
272
        $resultSet = $statement->fetch();
273
        $this->assertEquals('hello world_abc', $resultSet[0]);
274
    }
275
276
    public function testBindNamedParamMultiple()
277
    {
278
        $this->pdo->exec("INSERT INTO test_table (id, name, int_type) VALUES (1, 'hello', 1), (2, 'world', 1), (3, 'hello', 2), (4, 'world', 3)");
279
        $this->pdo->exec("REFRESH TABLE test_table");
280
281
        $statement = $this->pdo->prepare('update test_table set name = concat(name, :name) where int_type = :int_type and name != :name');
282
        $statement->bindValue(':int_type', 1, PDO::PARAM_INT);
283
        $statement->bindValue(':name', 'world', PDO::PARAM_STR);
284
        $statement->execute();
285
286
        $this->pdo->exec("REFRESH TABLE test_table");
287
288
        $statement = $this->pdo->prepare("SELECT id, name, int_type FROM test_table WHERE id=1");
289
        $resultSet = $statement->fetch();
290
        $this->assertEquals(1, $resultSet[0]);
291
        $this->assertEquals('helloworld', $resultSet[1]);
292
        $this->assertEquals(1, $resultSet[2]);
293
    }
294
295
    public function testBindValue()
296
    {
297
        $expected = [
298
            ['id' => 1, 'name' => 'first'],
299
            ['id' => 2, 'name' => 'second'],
300
            ['id' => 3, 'name' => 'third'],
301
        ];
302
303
        foreach ($expected as $row) {
304
            $this->insertRow($row['id'], $row['name']);
305
        }
306
307
        $statement = $this->pdo->prepare('SELECT * FROM test_table where name = ?');
308
        $statement->bindValue(1, 'second');
309
        $statement->execute();
310
        $this->assertEquals(1, $statement->rowCount());
311
312
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
313
        $this->assertEquals(2, $resultSet[0]['id']);
314
        $this->assertEquals('second', $resultSet[0]['name']);
315
    }
316
317
    public function testArrayValue()
318
    {
319
        $statement = $this->pdo->prepare('INSERT INTO test_table (id, array_type, object_type) VALUES(?, ?, ?)');
320
        $statement->bindValue(1, 1, PDO::PARAM_INT);
321
        $statement->bindValue(2, [1, 2], PDO::PARAM_ARRAY);
322
        $statement->bindValue(3, ["foo" => "bar"], PDO::PARAM_OBJECT);
323
        $statement->execute();
324
        $this->assertEquals(1, $statement->rowCount());
325
326
        $this->pdo->exec('REFRESH TABLE test_table');
327
328
        $statement = $this->pdo->prepare('SELECT id, array_type, object_type FROM test_table');
329
        $resultSet = $statement->fetchAll(PDO::FETCH_ASSOC);
330
        $this->assertEquals(1, $resultSet[0]['id']);
331
        $this->assertEquals([1, 2], $resultSet[0]['array_type']);
332
        $this->assertEquals(["foo" => "bar"], $resultSet[0]['object_type']);
333
    }
334
335
    public function testNullParamBinding()
336
    {
337
        $name = NULL;
338
        $statement = $this->pdo->prepare('INSERT INTO test_table (name) VALUES (?)');
339
        $statement->bindParam(1, $name);
340
        $statement->execute();
341
342
        $resultSet = $statement->fetchAll(PDO::FETCH_NAMED);
343
        $this->assertEquals($name, $resultSet[0]['name']);
344
345
    }
346
347
}
348