Completed
Push — nomi/archive-docs ( 959cb6...8e5004 )
by
unknown
15:52 queued 05:59
created

testFetchSameColumnTwiceWithAssocStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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