1 | <?php |
||
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)) { |
||
|
|||
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() |
||
189 | |||
190 | public function testBindNamedParam() |
||
191 | { |
||
192 | $expected = [ |
||
193 | ['id' => 1, 'name' => 'first'], |
||
194 | ['id' => 2, 'name' => 'second'], |
||
195 | ['id' => 3, 'name' => 'third'], |
||
226 | |||
227 | public function testBindNamedParamUnordered() |
||
242 | |||
243 | public function testBindNamedParamMultiple() |
||
261 | |||
262 | public function testBindValue() |
||
283 | |||
284 | public function testArrayValue() |
||
301 | |||
302 | } |
||
303 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.