Total Complexity | 68 |
Total Lines | 874 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like DataAccessTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataAccessTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase |
||
12 | { |
||
13 | static private $generated = false; |
||
14 | |||
15 | protected function setUp() |
||
16 | { |
||
17 | parent::setUp(); |
||
18 | |||
19 | if (self::$generated === false) { |
||
20 | /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */ |
||
21 | $table = new \Doctrine\DBAL\Schema\Table("fetch_table"); |
||
22 | $table->addColumn('test_int', 'integer'); |
||
23 | $table->addColumn('test_string', 'string'); |
||
24 | $table->addColumn('test_datetime', 'datetime', array('notnull' => false)); |
||
25 | $table->setPrimaryKey(array('test_int')); |
||
26 | |||
27 | $sm = $this->_conn->getSchemaManager(); |
||
28 | $sm->createTable($table); |
||
29 | |||
30 | $this->_conn->insert('fetch_table', array('test_int' => 1, 'test_string' => 'foo', 'test_datetime' => '2010-01-01 10:10:10')); |
||
31 | self::$generated = true; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | public function testPrepareWithBindValue() |
||
36 | { |
||
37 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
38 | $stmt = $this->_conn->prepare($sql); |
||
39 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
40 | |||
41 | $stmt->bindValue(1, 1); |
||
42 | $stmt->bindValue(2, 'foo'); |
||
43 | $stmt->execute(); |
||
44 | |||
45 | $row = $stmt->fetch(ResultStatement::FETCH_ASSOC); |
||
46 | $row = array_change_key_case($row, \CASE_LOWER); |
||
47 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); |
||
48 | } |
||
49 | |||
50 | public function testPrepareWithBindParam() |
||
51 | { |
||
52 | $paramInt = 1; |
||
53 | $paramStr = 'foo'; |
||
54 | |||
55 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
56 | $stmt = $this->_conn->prepare($sql); |
||
57 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
58 | |||
59 | $stmt->bindParam(1, $paramInt); |
||
60 | $stmt->bindParam(2, $paramStr); |
||
61 | $stmt->execute(); |
||
62 | |||
63 | $row = $stmt->fetch(ResultStatement::FETCH_ASSOC); |
||
64 | $row = array_change_key_case($row, \CASE_LOWER); |
||
65 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); |
||
66 | } |
||
67 | |||
68 | public function testPrepareWithFetchAll() |
||
69 | { |
||
70 | $paramInt = 1; |
||
71 | $paramStr = 'foo'; |
||
72 | |||
73 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
74 | $stmt = $this->_conn->prepare($sql); |
||
75 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
76 | |||
77 | $stmt->bindParam(1, $paramInt); |
||
78 | $stmt->bindParam(2, $paramStr); |
||
79 | $stmt->execute(); |
||
80 | |||
81 | $rows = $stmt->fetchAll(ResultStatement::FETCH_ASSOC); |
||
82 | $rows[0] = array_change_key_case($rows[0], \CASE_LOWER); |
||
83 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @group DBAL-228 |
||
88 | */ |
||
89 | public function testPrepareWithFetchAllBoth() |
||
90 | { |
||
91 | $paramInt = 1; |
||
92 | $paramStr = 'foo'; |
||
93 | |||
94 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
95 | $stmt = $this->_conn->prepare($sql); |
||
96 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
97 | |||
98 | $stmt->bindParam(1, $paramInt); |
||
99 | $stmt->bindParam(2, $paramStr); |
||
100 | $stmt->execute(); |
||
101 | |||
102 | $rows = $stmt->fetchAll(ResultStatement::FETCH_BOTH); |
||
103 | $rows[0] = array_change_key_case($rows[0], \CASE_LOWER); |
||
104 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo', 0 => 1, 1 => 'foo'), $rows[0]); |
||
105 | } |
||
106 | |||
107 | public function testPrepareWithFetchColumn() |
||
108 | { |
||
109 | $paramInt = 1; |
||
110 | $paramStr = 'foo'; |
||
111 | |||
112 | $sql = "SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
113 | $stmt = $this->_conn->prepare($sql); |
||
114 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
115 | |||
116 | $stmt->bindParam(1, $paramInt); |
||
117 | $stmt->bindParam(2, $paramStr); |
||
118 | $stmt->execute(); |
||
119 | |||
120 | $column = $stmt->fetchColumn(); |
||
121 | self::assertEquals(1, $column); |
||
122 | } |
||
123 | |||
124 | public function testPrepareWithIterator() |
||
125 | { |
||
126 | $paramInt = 1; |
||
127 | $paramStr = 'foo'; |
||
128 | |||
129 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
130 | $stmt = $this->_conn->prepare($sql); |
||
131 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
132 | |||
133 | $stmt->bindParam(1, $paramInt); |
||
134 | $stmt->bindParam(2, $paramStr); |
||
135 | $stmt->execute(); |
||
136 | |||
137 | $rows = array(); |
||
138 | $stmt->setFetchMode(ResultStatement::FETCH_ASSOC); |
||
139 | foreach ($stmt as $row) { |
||
140 | $rows[] = array_change_key_case($row, \CASE_LOWER); |
||
141 | } |
||
142 | |||
143 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]); |
||
144 | } |
||
145 | |||
146 | public function testPrepareWithQuoted() |
||
147 | { |
||
148 | $table = 'fetch_table'; |
||
149 | $paramInt = 1; |
||
150 | $paramStr = 'foo'; |
||
151 | |||
152 | $sql = "SELECT test_int, test_string FROM " . $this->_conn->quoteIdentifier($table) . " ". |
||
153 | "WHERE test_int = " . $this->_conn->quote($paramInt) . " AND test_string = " . $this->_conn->quote($paramStr); |
||
154 | $stmt = $this->_conn->prepare($sql); |
||
155 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
156 | } |
||
157 | |||
158 | public function testPrepareWithExecuteParams() |
||
159 | { |
||
160 | $paramInt = 1; |
||
161 | $paramStr = 'foo'; |
||
162 | |||
163 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
164 | $stmt = $this->_conn->prepare($sql); |
||
165 | self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt); |
||
166 | $stmt->execute(array($paramInt, $paramStr)); |
||
167 | |||
168 | $row = $stmt->fetch(ResultStatement::FETCH_ASSOC); |
||
169 | self::assertNotFalse($row); |
||
170 | $row = array_change_key_case($row, \CASE_LOWER); |
||
171 | self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row); |
||
172 | } |
||
173 | |||
174 | public function testFetchAll() |
||
175 | { |
||
176 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
177 | $data = $this->_conn->fetchAll($sql, array(1, 'foo')); |
||
178 | |||
179 | self::assertCount(1, $data); |
||
180 | |||
181 | $row = $data[0]; |
||
182 | self::assertCount(2, $row); |
||
183 | |||
184 | $row = array_change_key_case($row, \CASE_LOWER); |
||
|
|||
185 | self::assertEquals(1, $row['test_int']); |
||
186 | self::assertEquals('foo', $row['test_string']); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @group DBAL-209 |
||
191 | */ |
||
192 | public function testFetchAllWithTypes() |
||
193 | { |
||
194 | $datetimeString = '2010-01-01 10:10:10'; |
||
195 | $datetime = new \DateTime($datetimeString); |
||
196 | |||
197 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
198 | $data = $this->_conn->fetchAll($sql, array(1, $datetime), array(Statement::PARAM_STR, Type::DATETIME)); |
||
199 | |||
200 | self::assertCount(1, $data); |
||
201 | |||
202 | $row = $data[0]; |
||
203 | self::assertCount(2, $row); |
||
204 | |||
205 | $row = array_change_key_case($row, \CASE_LOWER); |
||
206 | self::assertEquals(1, $row['test_int']); |
||
207 | self::assertStringStartsWith($datetimeString, $row['test_datetime']); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @group DBAL-209 |
||
212 | * @expectedException \Doctrine\DBAL\DBALException |
||
213 | */ |
||
214 | public function testFetchAllWithMissingTypes() |
||
215 | { |
||
216 | if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver || |
||
217 | $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) { |
||
218 | $this->markTestSkipped('mysqli and sqlsrv actually supports this'); |
||
219 | } |
||
220 | |||
221 | $datetimeString = '2010-01-01 10:10:10'; |
||
222 | $datetime = new \DateTime($datetimeString); |
||
223 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
224 | $data = $this->_conn->fetchAll($sql, array(1, $datetime)); |
||
225 | } |
||
226 | |||
227 | public function testFetchBoth() |
||
228 | { |
||
229 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
230 | $row = $this->_conn->executeQuery($sql, array(1, 'foo'))->fetch(ResultStatement::FETCH_BOTH); |
||
231 | |||
232 | self::assertNotFalse($row); |
||
233 | |||
234 | $row = array_change_key_case($row, \CASE_LOWER); |
||
235 | |||
236 | self::assertEquals(1, $row['test_int']); |
||
237 | self::assertEquals('foo', $row['test_string']); |
||
238 | self::assertEquals(1, $row[0]); |
||
239 | self::assertEquals('foo', $row[1]); |
||
240 | } |
||
241 | |||
242 | public function testFetchNoResult() |
||
243 | { |
||
244 | self::assertFalse( |
||
245 | $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])->fetch() |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | public function testFetchAssoc() |
||
250 | { |
||
251 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
252 | $row = $this->_conn->fetchAssoc($sql, array(1, 'foo')); |
||
253 | |||
254 | self::assertNotFalse($row); |
||
255 | |||
256 | $row = array_change_key_case($row, \CASE_LOWER); |
||
257 | |||
258 | self::assertEquals(1, $row['test_int']); |
||
259 | self::assertEquals('foo', $row['test_string']); |
||
260 | } |
||
261 | |||
262 | public function testFetchAssocWithTypes() |
||
263 | { |
||
264 | $datetimeString = '2010-01-01 10:10:10'; |
||
265 | $datetime = new \DateTime($datetimeString); |
||
266 | |||
267 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
268 | $row = $this->_conn->fetchAssoc($sql, array(1, $datetime), array(Statement::PARAM_STR, Type::DATETIME)); |
||
269 | |||
270 | self::assertNotFalse($row); |
||
271 | |||
272 | $row = array_change_key_case($row, \CASE_LOWER); |
||
273 | |||
274 | self::assertEquals(1, $row['test_int']); |
||
275 | self::assertStringStartsWith($datetimeString, $row['test_datetime']); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @expectedException \Doctrine\DBAL\DBALException |
||
280 | */ |
||
281 | public function testFetchAssocWithMissingTypes() |
||
282 | { |
||
283 | if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver || |
||
284 | $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) { |
||
285 | $this->markTestSkipped('mysqli and sqlsrv actually supports this'); |
||
286 | } |
||
287 | |||
288 | $datetimeString = '2010-01-01 10:10:10'; |
||
289 | $datetime = new \DateTime($datetimeString); |
||
290 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
291 | $row = $this->_conn->fetchAssoc($sql, array(1, $datetime)); |
||
292 | } |
||
293 | |||
294 | public function testFetchArray() |
||
295 | { |
||
296 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
297 | $row = $this->_conn->fetchArray($sql, array(1, 'foo')); |
||
298 | |||
299 | self::assertEquals(1, $row[0]); |
||
300 | self::assertEquals('foo', $row[1]); |
||
301 | } |
||
302 | |||
303 | public function testFetchArrayWithTypes() |
||
304 | { |
||
305 | $datetimeString = '2010-01-01 10:10:10'; |
||
306 | $datetime = new \DateTime($datetimeString); |
||
307 | |||
308 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
309 | $row = $this->_conn->fetchArray($sql, array(1, $datetime), array(Statement::PARAM_STR, Type::DATETIME)); |
||
310 | |||
311 | self::assertNotFalse($row); |
||
312 | |||
313 | $row = array_change_key_case($row, \CASE_LOWER); |
||
314 | |||
315 | self::assertEquals(1, $row[0]); |
||
316 | self::assertStringStartsWith($datetimeString, $row[1]); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @expectedException \Doctrine\DBAL\DBALException |
||
321 | */ |
||
322 | public function testFetchArrayWithMissingTypes() |
||
323 | { |
||
324 | if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver || |
||
325 | $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) { |
||
326 | $this->markTestSkipped('mysqli and sqlsrv actually supports this'); |
||
327 | } |
||
328 | |||
329 | $datetimeString = '2010-01-01 10:10:10'; |
||
330 | $datetime = new \DateTime($datetimeString); |
||
331 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
332 | $row = $this->_conn->fetchArray($sql, array(1, $datetime)); |
||
333 | } |
||
334 | |||
335 | public function testFetchColumn() |
||
336 | { |
||
337 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
338 | $testInt = $this->_conn->fetchColumn($sql, array(1, 'foo'), 0); |
||
339 | |||
340 | self::assertEquals(1, $testInt); |
||
341 | |||
342 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
343 | $testString = $this->_conn->fetchColumn($sql, array(1, 'foo'), 1); |
||
344 | |||
345 | self::assertEquals('foo', $testString); |
||
346 | } |
||
347 | |||
348 | public function testFetchColumnWithTypes() |
||
349 | { |
||
350 | $datetimeString = '2010-01-01 10:10:10'; |
||
351 | $datetime = new \DateTime($datetimeString); |
||
352 | |||
353 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
354 | $column = $this->_conn->fetchColumn( |
||
355 | $sql, |
||
356 | array(1, $datetime), |
||
357 | 1, |
||
358 | array(Statement::PARAM_STR, Type::DATETIME) |
||
359 | ); |
||
360 | |||
361 | self::assertNotFalse($column); |
||
362 | |||
363 | self::assertStringStartsWith($datetimeString, $column); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @expectedException \Doctrine\DBAL\DBALException |
||
368 | */ |
||
369 | public function testFetchColumnWithMissingTypes() |
||
370 | { |
||
371 | if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver || |
||
372 | $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) { |
||
373 | $this->markTestSkipped('mysqli and sqlsrv actually supports this'); |
||
374 | } |
||
375 | |||
376 | $datetimeString = '2010-01-01 10:10:10'; |
||
377 | $datetime = new \DateTime($datetimeString); |
||
378 | $sql = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"; |
||
379 | $column = $this->_conn->fetchColumn($sql, array(1, $datetime), 1); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @group DDC-697 |
||
384 | */ |
||
385 | public function testExecuteQueryBindDateTimeType() |
||
386 | { |
||
387 | $sql = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?'; |
||
388 | $stmt = $this->_conn->executeQuery($sql, |
||
389 | array(1 => new \DateTime('2010-01-01 10:10:10')), |
||
390 | array(1 => Type::DATETIME) |
||
391 | ); |
||
392 | |||
393 | self::assertEquals(1, $stmt->fetchColumn()); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @group DDC-697 |
||
398 | */ |
||
399 | public function testExecuteUpdateBindDateTimeType() |
||
400 | { |
||
401 | $datetime = new \DateTime('2010-02-02 20:20:20'); |
||
402 | |||
403 | $sql = 'INSERT INTO fetch_table (test_int, test_string, test_datetime) VALUES (?, ?, ?)'; |
||
404 | $affectedRows = $this->_conn->executeUpdate($sql, |
||
405 | array( |
||
406 | 1 => 50, |
||
407 | 2 => 'foo', |
||
408 | 3 => $datetime, |
||
409 | ), |
||
410 | array( |
||
411 | 1 => Statement::PARAM_INT, |
||
412 | 2 => Statement::PARAM_STR, |
||
413 | 3 => Type::DATETIME, |
||
414 | ) |
||
415 | ); |
||
416 | |||
417 | self::assertEquals(1, $affectedRows); |
||
418 | self::assertEquals(1, $this->_conn->executeQuery( |
||
419 | 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?', |
||
420 | array(1 => $datetime), |
||
421 | array(1 => Type::DATETIME) |
||
422 | )->fetchColumn()); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @group DDC-697 |
||
427 | */ |
||
428 | public function testPrepareQueryBindValueDateTimeType() |
||
429 | { |
||
430 | $sql = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?'; |
||
431 | $stmt = $this->_conn->prepare($sql); |
||
432 | $stmt->bindValue(1, new \DateTime('2010-01-01 10:10:10'), Type::DATETIME); |
||
433 | $stmt->execute(); |
||
434 | |||
435 | self::assertEquals(1, $stmt->fetchColumn()); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @group DBAL-78 |
||
440 | */ |
||
441 | public function testNativeArrayListSupport() |
||
442 | { |
||
443 | for ($i = 100; $i < 110; $i++) { |
||
444 | $this->_conn->insert('fetch_table', array('test_int' => $i, 'test_string' => 'foo' . $i, 'test_datetime' => '2010-01-01 10:10:10')); |
||
445 | } |
||
446 | |||
447 | $stmt = $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_int IN (?)', |
||
448 | array(array(100, 101, 102, 103, 104)), array(Connection::PARAM_INT_ARRAY)); |
||
449 | |||
450 | $data = $stmt->fetchAll(ResultStatement::FETCH_NUM); |
||
451 | self::assertCount(5, $data); |
||
452 | self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data); |
||
453 | |||
454 | $stmt = $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_string IN (?)', |
||
455 | array(array('foo100', 'foo101', 'foo102', 'foo103', 'foo104')), array(Connection::PARAM_STR_ARRAY)); |
||
456 | |||
457 | $data = $stmt->fetchAll(ResultStatement::FETCH_NUM); |
||
458 | self::assertCount(5, $data); |
||
459 | self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @dataProvider getTrimExpressionData |
||
464 | */ |
||
465 | public function testTrimExpression($value, $position, $char, $expectedResult) |
||
466 | { |
||
467 | $sql = 'SELECT ' . |
||
468 | $this->_conn->getDatabasePlatform()->getTrimExpression($value, $position, $char) . ' AS trimmed ' . |
||
469 | 'FROM fetch_table'; |
||
470 | |||
471 | $row = $this->_conn->fetchAssoc($sql); |
||
472 | $row = array_change_key_case($row, CASE_LOWER); |
||
473 | |||
474 | self::assertEquals($expectedResult, $row['trimmed']); |
||
475 | } |
||
476 | |||
477 | public function getTrimExpressionData() |
||
478 | { |
||
479 | return array( |
||
480 | array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'), |
||
481 | array('test_string', AbstractPlatform::TRIM_LEADING, false, 'foo'), |
||
482 | array('test_string', AbstractPlatform::TRIM_TRAILING, false, 'foo'), |
||
483 | array('test_string', AbstractPlatform::TRIM_BOTH, false, 'foo'), |
||
484 | array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'f'", 'oo'), |
||
485 | array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'o'", 'f'), |
||
486 | array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'.'", 'foo'), |
||
487 | array('test_string', AbstractPlatform::TRIM_LEADING, "'f'", 'oo'), |
||
488 | array('test_string', AbstractPlatform::TRIM_LEADING, "'o'", 'foo'), |
||
489 | array('test_string', AbstractPlatform::TRIM_LEADING, "'.'", 'foo'), |
||
490 | array('test_string', AbstractPlatform::TRIM_TRAILING, "'f'", 'foo'), |
||
491 | array('test_string', AbstractPlatform::TRIM_TRAILING, "'o'", 'f'), |
||
492 | array('test_string', AbstractPlatform::TRIM_TRAILING, "'.'", 'foo'), |
||
493 | array('test_string', AbstractPlatform::TRIM_BOTH, "'f'", 'oo'), |
||
494 | array('test_string', AbstractPlatform::TRIM_BOTH, "'o'", 'f'), |
||
495 | array('test_string', AbstractPlatform::TRIM_BOTH, "'.'", 'foo'), |
||
496 | array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'), |
||
497 | array("' foo '", AbstractPlatform::TRIM_LEADING, false, 'foo '), |
||
498 | array("' foo '", AbstractPlatform::TRIM_TRAILING, false, ' foo'), |
||
499 | array("' foo '", AbstractPlatform::TRIM_BOTH, false, 'foo'), |
||
500 | array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'f'", ' foo '), |
||
501 | array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'o'", ' foo '), |
||
502 | array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'.'", ' foo '), |
||
503 | array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "' '", 'foo'), |
||
504 | array("' foo '", AbstractPlatform::TRIM_LEADING, "'f'", ' foo '), |
||
505 | array("' foo '", AbstractPlatform::TRIM_LEADING, "'o'", ' foo '), |
||
506 | array("' foo '", AbstractPlatform::TRIM_LEADING, "'.'", ' foo '), |
||
507 | array("' foo '", AbstractPlatform::TRIM_LEADING, "' '", 'foo '), |
||
508 | array("' foo '", AbstractPlatform::TRIM_TRAILING, "'f'", ' foo '), |
||
509 | array("' foo '", AbstractPlatform::TRIM_TRAILING, "'o'", ' foo '), |
||
510 | array("' foo '", AbstractPlatform::TRIM_TRAILING, "'.'", ' foo '), |
||
511 | array("' foo '", AbstractPlatform::TRIM_TRAILING, "' '", ' foo'), |
||
512 | array("' foo '", AbstractPlatform::TRIM_BOTH, "'f'", ' foo '), |
||
513 | array("' foo '", AbstractPlatform::TRIM_BOTH, "'o'", ' foo '), |
||
514 | array("' foo '", AbstractPlatform::TRIM_BOTH, "'.'", ' foo '), |
||
515 | array("' foo '", AbstractPlatform::TRIM_BOTH, "' '", 'foo'), |
||
516 | ); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @group DDC-1014 |
||
521 | */ |
||
522 | public function testDateArithmetics() |
||
523 | { |
||
524 | $p = $this->_conn->getDatabasePlatform(); |
||
525 | $sql = 'SELECT '; |
||
526 | $sql .= $p->getDateDiffExpression('test_datetime', $p->getCurrentTimestampSQL()) .' AS diff, '; |
||
527 | $sql .= $p->getDateAddSecondsExpression('test_datetime', 1) .' AS add_seconds, '; |
||
528 | $sql .= $p->getDateSubSecondsExpression('test_datetime', 1) .' AS sub_seconds, '; |
||
529 | $sql .= $p->getDateAddMinutesExpression('test_datetime', 5) .' AS add_minutes, '; |
||
530 | $sql .= $p->getDateSubMinutesExpression('test_datetime', 5) .' AS sub_minutes, '; |
||
531 | $sql .= $p->getDateAddHourExpression('test_datetime', 3) .' AS add_hour, '; |
||
532 | $sql .= $p->getDateSubHourExpression('test_datetime', 3) .' AS sub_hour, '; |
||
533 | $sql .= $p->getDateAddDaysExpression('test_datetime', 10) .' AS add_days, '; |
||
534 | $sql .= $p->getDateSubDaysExpression('test_datetime', 10) .' AS sub_days, '; |
||
535 | $sql .= $p->getDateAddWeeksExpression('test_datetime', 1) .' AS add_weeks, '; |
||
536 | $sql .= $p->getDateSubWeeksExpression('test_datetime', 1) .' AS sub_weeks, '; |
||
537 | $sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, '; |
||
538 | $sql .= $p->getDateSubMonthExpression('test_datetime', 2) .' AS sub_month, '; |
||
539 | $sql .= $p->getDateAddQuartersExpression('test_datetime', 3) .' AS add_quarters, '; |
||
540 | $sql .= $p->getDateSubQuartersExpression('test_datetime', 3) .' AS sub_quarters, '; |
||
541 | $sql .= $p->getDateAddYearsExpression('test_datetime', 6) .' AS add_years, '; |
||
542 | $sql .= $p->getDateSubYearsExpression('test_datetime', 6) .' AS sub_years '; |
||
543 | $sql .= 'FROM fetch_table'; |
||
544 | |||
545 | $row = $this->_conn->fetchAssoc($sql); |
||
546 | $row = array_change_key_case($row, CASE_LOWER); |
||
547 | |||
548 | $diff = (strtotime('2010-01-01') - strtotime(date('Y-m-d'))) / 3600 / 24; |
||
549 | self::assertEquals($diff, $row['diff'], "Date difference should be approx. ".$diff." days.", 1); |
||
550 | self::assertEquals('2010-01-01 10:10:11', date('Y-m-d H:i:s', strtotime($row['add_seconds'])), "Adding second should end up on 2010-01-01 10:10:11"); |
||
551 | self::assertEquals('2010-01-01 10:10:09', date('Y-m-d H:i:s', strtotime($row['sub_seconds'])), "Subtracting second should end up on 2010-01-01 10:10:09"); |
||
552 | self::assertEquals('2010-01-01 10:15:10', date('Y-m-d H:i:s', strtotime($row['add_minutes'])), "Adding minutes should end up on 2010-01-01 10:15:10"); |
||
553 | self::assertEquals('2010-01-01 10:05:10', date('Y-m-d H:i:s', strtotime($row['sub_minutes'])), "Subtracting minutes should end up on 2010-01-01 10:05:10"); |
||
554 | self::assertEquals('2010-01-01 13:10', date('Y-m-d H:i', strtotime($row['add_hour'])), "Adding date should end up on 2010-01-01 13:10"); |
||
555 | self::assertEquals('2010-01-01 07:10', date('Y-m-d H:i', strtotime($row['sub_hour'])), "Subtracting date should end up on 2010-01-01 07:10"); |
||
556 | self::assertEquals('2010-01-11', date('Y-m-d', strtotime($row['add_days'])), "Adding date should end up on 2010-01-11"); |
||
557 | self::assertEquals('2009-12-22', date('Y-m-d', strtotime($row['sub_days'])), "Subtracting date should end up on 2009-12-22"); |
||
558 | self::assertEquals('2010-01-08', date('Y-m-d', strtotime($row['add_weeks'])), "Adding week should end up on 2010-01-08"); |
||
559 | self::assertEquals('2009-12-25', date('Y-m-d', strtotime($row['sub_weeks'])), "Subtracting week should end up on 2009-12-25"); |
||
560 | self::assertEquals('2010-03-01', date('Y-m-d', strtotime($row['add_month'])), "Adding month should end up on 2010-03-01"); |
||
561 | self::assertEquals('2009-11-01', date('Y-m-d', strtotime($row['sub_month'])), "Subtracting month should end up on 2009-11-01"); |
||
562 | self::assertEquals('2010-10-01', date('Y-m-d', strtotime($row['add_quarters'])), "Adding quarters should end up on 2010-04-01"); |
||
563 | self::assertEquals('2009-04-01', date('Y-m-d', strtotime($row['sub_quarters'])), "Subtracting quarters should end up on 2009-10-01"); |
||
564 | self::assertEquals('2016-01-01', date('Y-m-d', strtotime($row['add_years'])), "Adding years should end up on 2016-01-01"); |
||
565 | self::assertEquals('2004-01-01', date('Y-m-d', strtotime($row['sub_years'])), "Subtracting years should end up on 2004-01-01"); |
||
566 | } |
||
567 | |||
568 | public function testLocateExpression() |
||
569 | { |
||
570 | $platform = $this->_conn->getDatabasePlatform(); |
||
571 | |||
572 | $sql = 'SELECT '; |
||
573 | $sql .= $platform->getLocateExpression('test_string', "'oo'") .' AS locate1, '; |
||
574 | $sql .= $platform->getLocateExpression('test_string', "'foo'") .' AS locate2, '; |
||
575 | $sql .= $platform->getLocateExpression('test_string', "'bar'") .' AS locate3, '; |
||
576 | $sql .= $platform->getLocateExpression('test_string', 'test_string') .' AS locate4, '; |
||
577 | $sql .= $platform->getLocateExpression("'foo'", 'test_string') .' AS locate5, '; |
||
578 | $sql .= $platform->getLocateExpression("'barfoobaz'", 'test_string') .' AS locate6, '; |
||
579 | $sql .= $platform->getLocateExpression("'bar'", 'test_string') .' AS locate7, '; |
||
580 | $sql .= $platform->getLocateExpression('test_string', "'oo'", 2) .' AS locate8, '; |
||
581 | $sql .= $platform->getLocateExpression('test_string', "'oo'", 3) .' AS locate9 '; |
||
582 | $sql .= 'FROM fetch_table'; |
||
583 | |||
584 | $row = $this->_conn->fetchAssoc($sql); |
||
585 | $row = array_change_key_case($row, CASE_LOWER); |
||
586 | |||
587 | self::assertEquals(2, $row['locate1']); |
||
588 | self::assertEquals(1, $row['locate2']); |
||
589 | self::assertEquals(0, $row['locate3']); |
||
590 | self::assertEquals(1, $row['locate4']); |
||
591 | self::assertEquals(1, $row['locate5']); |
||
592 | self::assertEquals(4, $row['locate6']); |
||
593 | self::assertEquals(0, $row['locate7']); |
||
594 | self::assertEquals(2, $row['locate8']); |
||
595 | self::assertEquals(0, $row['locate9']); |
||
596 | } |
||
597 | |||
598 | public function testQuoteSQLInjection() |
||
599 | { |
||
600 | $sql = "SELECT * FROM fetch_table WHERE test_string = " . $this->_conn->quote("bar' OR '1'='1"); |
||
601 | $rows = $this->_conn->fetchAll($sql); |
||
602 | |||
603 | self::assertCount(0, $rows, "no result should be returned, otherwise SQL injection is possible"); |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * @group DDC-1213 |
||
608 | */ |
||
609 | public function testBitComparisonExpressionSupport() |
||
610 | { |
||
611 | $this->_conn->exec('DELETE FROM fetch_table'); |
||
612 | $platform = $this->_conn->getDatabasePlatform(); |
||
613 | $bitmap = array(); |
||
614 | |||
615 | for ($i = 2; $i < 9; $i = $i + 2) { |
||
616 | $bitmap[$i] = array( |
||
617 | 'bit_or' => ($i | 2), |
||
618 | 'bit_and' => ($i & 2) |
||
619 | ); |
||
620 | $this->_conn->insert('fetch_table', array( |
||
621 | 'test_int' => $i, |
||
622 | 'test_string' => json_encode($bitmap[$i]), |
||
623 | 'test_datetime' => '2010-01-01 10:10:10' |
||
624 | )); |
||
625 | } |
||
626 | |||
627 | $sql[] = 'SELECT '; |
||
628 | $sql[] = 'test_int, '; |
||
629 | $sql[] = 'test_string, '; |
||
630 | $sql[] = $platform->getBitOrComparisonExpression('test_int', 2) . ' AS bit_or, '; |
||
631 | $sql[] = $platform->getBitAndComparisonExpression('test_int', 2) . ' AS bit_and '; |
||
632 | $sql[] = 'FROM fetch_table'; |
||
633 | |||
634 | $stmt = $this->_conn->executeQuery(implode(PHP_EOL, $sql)); |
||
635 | $data = $stmt->fetchAll(ResultStatement::FETCH_ASSOC); |
||
636 | |||
637 | |||
638 | self::assertCount(4, $data); |
||
639 | self::assertEquals(count($bitmap), count($data)); |
||
640 | foreach ($data as $row) { |
||
641 | $row = array_change_key_case($row, CASE_LOWER); |
||
642 | |||
643 | self::assertArrayHasKey('test_int', $row); |
||
644 | |||
645 | $id = $row['test_int']; |
||
646 | |||
647 | self::assertArrayHasKey($id, $bitmap); |
||
648 | self::assertArrayHasKey($id, $bitmap); |
||
649 | |||
650 | self::assertArrayHasKey('bit_or', $row); |
||
651 | self::assertArrayHasKey('bit_and', $row); |
||
652 | |||
653 | self::assertEquals($row['bit_or'], $bitmap[$id]['bit_or']); |
||
654 | self::assertEquals($row['bit_and'], $bitmap[$id]['bit_and']); |
||
655 | } |
||
656 | } |
||
657 | |||
658 | public function testSetDefaultFetchMode() |
||
659 | { |
||
660 | $stmt = $this->_conn->query("SELECT * FROM fetch_table"); |
||
661 | $stmt->setFetchMode(ResultStatement::FETCH_NUM); |
||
662 | |||
663 | $row = array_keys($stmt->fetch()); |
||
664 | self::assertCount(0, array_filter($row, function($v) { return ! is_numeric($v); }), "should be no non-numerical elements in the result."); |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * @group DBAL-1091 |
||
669 | */ |
||
670 | public function testFetchAllStyleObject() |
||
671 | { |
||
672 | $this->setupFixture(); |
||
673 | |||
674 | $sql = 'SELECT test_int, test_string, test_datetime FROM fetch_table'; |
||
675 | $stmt = $this->_conn->prepare($sql); |
||
676 | |||
677 | $stmt->execute(); |
||
678 | |||
679 | $results = $stmt->fetchAll(ResultStatement::FETCH_OBJ); |
||
680 | |||
681 | self::assertCount(1, $results); |
||
682 | self::assertInstanceOf('stdClass', $results[0]); |
||
683 | |||
684 | self::assertEquals( |
||
685 | 1, |
||
686 | property_exists($results[0], 'test_int') ? $results[0]->test_int : $results[0]->TEST_INT |
||
687 | ); |
||
688 | self::assertEquals( |
||
689 | 'foo', |
||
690 | property_exists($results[0], 'test_string') ? $results[0]->test_string : $results[0]->TEST_STRING |
||
691 | ); |
||
692 | self::assertStringStartsWith( |
||
693 | '2010-01-01 10:10:10', |
||
694 | property_exists($results[0], 'test_datetime') ? $results[0]->test_datetime : $results[0]->TEST_DATETIME |
||
695 | ); |
||
696 | } |
||
697 | |||
698 | /** |
||
699 | * @group DBAL-196 |
||
700 | */ |
||
701 | public function testFetchAllSupportFetchClass() |
||
702 | { |
||
703 | $this->skipOci8AndMysqli(); |
||
704 | $this->setupFixture(); |
||
705 | |||
706 | $sql = "SELECT test_int, test_string, test_datetime FROM fetch_table"; |
||
707 | $stmt = $this->_conn->prepare($sql); |
||
708 | $stmt->execute(); |
||
709 | |||
710 | $results = $stmt->fetchAll( |
||
711 | ResultStatement::FETCH_CLASS, |
||
712 | __NAMESPACE__.'\\MyFetchClass' |
||
713 | ); |
||
714 | |||
715 | self::assertCount(1, $results); |
||
716 | self::assertInstanceOf(__NAMESPACE__.'\\MyFetchClass', $results[0]); |
||
717 | |||
718 | self::assertEquals(1, $results[0]->test_int); |
||
719 | self::assertEquals('foo', $results[0]->test_string); |
||
720 | self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime); |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * @group DBAL-241 |
||
725 | */ |
||
726 | public function testFetchAllStyleColumn() |
||
727 | { |
||
728 | $sql = "DELETE FROM fetch_table"; |
||
729 | $this->_conn->executeUpdate($sql); |
||
730 | |||
731 | $this->_conn->insert('fetch_table', array('test_int' => 1, 'test_string' => 'foo')); |
||
732 | $this->_conn->insert('fetch_table', array('test_int' => 10, 'test_string' => 'foo')); |
||
733 | |||
734 | $sql = "SELECT test_int FROM fetch_table"; |
||
735 | $rows = $this->_conn->query($sql)->fetchAll(ResultStatement::FETCH_COLUMN); |
||
736 | |||
737 | self::assertEquals(array(1, 10), $rows); |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * @group DBAL-214 |
||
742 | */ |
||
743 | public function testSetFetchModeClassFetchAll() |
||
744 | { |
||
745 | $this->skipOci8AndMysqli(); |
||
746 | $this->setupFixture(); |
||
747 | |||
748 | $sql = "SELECT * FROM fetch_table"; |
||
749 | $stmt = $this->_conn->query($sql); |
||
750 | $stmt->setFetchMode(ResultStatement::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass'); |
||
751 | |||
752 | $results = $stmt->fetchAll(); |
||
753 | |||
754 | self::assertCount(1, $results); |
||
755 | self::assertInstanceOf(__NAMESPACE__.'\\MyFetchClass', $results[0]); |
||
756 | |||
757 | self::assertEquals(1, $results[0]->test_int); |
||
758 | self::assertEquals('foo', $results[0]->test_string); |
||
759 | self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime); |
||
760 | } |
||
761 | |||
762 | /** |
||
763 | * @group DBAL-214 |
||
764 | */ |
||
765 | public function testSetFetchModeClassFetch() |
||
766 | { |
||
767 | $this->skipOci8AndMysqli(); |
||
768 | $this->setupFixture(); |
||
769 | |||
770 | $sql = "SELECT * FROM fetch_table"; |
||
771 | $stmt = $this->_conn->query($sql); |
||
772 | $stmt->setFetchMode(ResultStatement::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass'); |
||
773 | |||
774 | $results = array(); |
||
775 | while ($row = $stmt->fetch()) { |
||
776 | $results[] = $row; |
||
777 | } |
||
778 | |||
779 | self::assertCount(1, $results); |
||
780 | self::assertInstanceOf(__NAMESPACE__.'\\MyFetchClass', $results[0]); |
||
781 | |||
782 | self::assertEquals(1, $results[0]->test_int); |
||
783 | self::assertEquals('foo', $results[0]->test_string); |
||
784 | self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime); |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * @group DBAL-257 |
||
789 | */ |
||
790 | public function testEmptyFetchColumnReturnsFalse() |
||
791 | { |
||
792 | $this->_conn->beginTransaction(); |
||
793 | $this->_conn->exec('DELETE FROM fetch_table'); |
||
794 | self::assertFalse($this->_conn->fetchColumn('SELECT test_int FROM fetch_table')); |
||
795 | self::assertFalse($this->_conn->query('SELECT test_int FROM fetch_table')->fetchColumn()); |
||
796 | $this->_conn->rollBack(); |
||
797 | } |
||
798 | |||
799 | /** |
||
800 | * @group DBAL-339 |
||
801 | */ |
||
802 | public function testSetFetchModeOnDbalStatement() |
||
803 | { |
||
804 | $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"; |
||
805 | $stmt = $this->_conn->executeQuery($sql, array(1, "foo")); |
||
806 | $stmt->setFetchMode(ResultStatement::FETCH_NUM); |
||
807 | |||
808 | $row = $stmt->fetch(); |
||
809 | |||
810 | self::assertArrayHasKey(0, $row); |
||
811 | self::assertArrayHasKey(1, $row); |
||
812 | self::assertFalse($stmt->fetch()); |
||
813 | } |
||
814 | |||
815 | /** |
||
816 | * @group DBAL-435 |
||
817 | */ |
||
818 | public function testEmptyParameters() |
||
825 | } |
||
826 | |||
827 | /** |
||
828 | * @group DBAL-1028 |
||
829 | */ |
||
830 | public function testFetchColumnNullValue() |
||
831 | { |
||
832 | $this->_conn->executeUpdate( |
||
833 | 'INSERT INTO fetch_table (test_int, test_string) VALUES (?, ?)', |
||
834 | array(2, 'foo') |
||
835 | ); |
||
836 | |||
837 | self::assertNull( |
||
838 | $this->_conn->fetchColumn('SELECT test_datetime FROM fetch_table WHERE test_int = ?', array(2)) |
||
839 | ); |
||
840 | } |
||
841 | |||
842 | /** |
||
843 | * @group DBAL-1028 |
||
844 | */ |
||
845 | public function testFetchColumnNonExistingIndex() |
||
846 | { |
||
847 | if ($this->_conn->getDriver()->getName() === 'pdo_sqlsrv') { |
||
848 | $this->markTestSkipped( |
||
849 | 'Test does not work for pdo_sqlsrv driver as it throws a fatal error for a non-existing column index.' |
||
850 | ); |
||
851 | } |
||
852 | |||
853 | self::assertNull( |
||
854 | $this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(1), 1) |
||
855 | ); |
||
856 | } |
||
857 | |||
858 | /** |
||
859 | * @group DBAL-1028 |
||
860 | */ |
||
861 | public function testFetchColumnNoResult() |
||
862 | { |
||
863 | self::assertFalse( |
||
864 | $this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(-1)) |
||
865 | ); |
||
866 | } |
||
867 | |||
868 | private function setupFixture() |
||
869 | { |
||
870 | $this->_conn->exec('DELETE FROM fetch_table'); |
||
871 | $this->_conn->insert('fetch_table', array( |
||
872 | 'test_int' => 1, |
||
873 | 'test_string' => 'foo', |
||
874 | 'test_datetime' => '2010-01-01 10:10:10' |
||
875 | )); |
||
876 | } |
||
877 | |||
878 | private function skipOci8AndMysqli() |
||
885 | } |
||
886 | } |
||
887 | } |
||
888 | |||
889 | class MyFetchClass |
||
890 | { |
||
891 | public $test_int, $test_string, $test_datetime; |
||
892 | } |
||
893 |