Failed Conditions
Pull Request — master (#2849)
by Luís
63:28
created

DataAccessTest::testSetFetchModeClassFetchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Types\Type;
8
use PDO;
9
10
class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
11
{
12
    private static $generated = false;
13
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
18
        if (self::$generated === false) {
19
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
20
            $table = new \Doctrine\DBAL\Schema\Table("fetch_table");
21
            $table->addColumn('test_int', 'integer');
22
            $table->addColumn('test_string', 'string');
23
            $table->addColumn('test_datetime', 'datetime', array('notnull' => false));
24
            $table->setPrimaryKey(array('test_int'));
25
26
            $sm = $this->_conn->getSchemaManager();
27
            $sm->createTable($table);
28
29
            $this->_conn->insert('fetch_table', array('test_int' => 1, 'test_string' => 'foo', 'test_datetime' => '2010-01-01 10:10:10'));
30
            self::$generated = true;
31
        }
32
    }
33
34
    public function testPrepareWithBindValue()
35
    {
36
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
37
        $stmt = $this->_conn->prepare($sql);
38
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
39
40
        $stmt->bindValue(1, 1);
41
        $stmt->bindValue(2, 'foo');
42
        $stmt->execute();
43
44
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
45
        $row = array_change_key_case($row, \CASE_LOWER);
46
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
47
    }
48
49
    public function testPrepareWithBindParam()
50
    {
51
        $paramInt = 1;
52
        $paramStr = 'foo';
53
54
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
55
        $stmt = $this->_conn->prepare($sql);
56
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
57
58
        $stmt->bindParam(1, $paramInt);
59
        $stmt->bindParam(2, $paramStr);
60
        $stmt->execute();
61
62
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
63
        $row = array_change_key_case($row, \CASE_LOWER);
64
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
65
    }
66
67
    public function testPrepareWithFetchAll()
68
    {
69
        $paramInt = 1;
70
        $paramStr = 'foo';
71
72
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
73
        $stmt = $this->_conn->prepare($sql);
74
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
75
76
        $stmt->bindParam(1, $paramInt);
77
        $stmt->bindParam(2, $paramStr);
78
        $stmt->execute();
79
80
        $rows    = $stmt->fetchAll(\PDO::FETCH_ASSOC);
81
        $rows[0] = array_change_key_case($rows[0], \CASE_LOWER);
82
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]);
83
    }
84
85
    /**
86
     * @group DBAL-228
87
     */
88
    public function testPrepareWithFetchAllBoth()
89
    {
90
        $paramInt = 1;
91
        $paramStr = 'foo';
92
93
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
94
        $stmt = $this->_conn->prepare($sql);
95
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
96
97
        $stmt->bindParam(1, $paramInt);
98
        $stmt->bindParam(2, $paramStr);
99
        $stmt->execute();
100
101
        $rows    = $stmt->fetchAll(\PDO::FETCH_BOTH);
102
        $rows[0] = array_change_key_case($rows[0], \CASE_LOWER);
103
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo', 0 => 1, 1 => 'foo'), $rows[0]);
104
    }
105
106
    public function testPrepareWithFetchColumn()
107
    {
108
        $paramInt = 1;
109
        $paramStr = 'foo';
110
111
        $sql  = "SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?";
112
        $stmt = $this->_conn->prepare($sql);
113
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
114
115
        $stmt->bindParam(1, $paramInt);
116
        $stmt->bindParam(2, $paramStr);
117
        $stmt->execute();
118
119
        $column = $stmt->fetchColumn();
120
        self::assertEquals(1, $column);
121
    }
122
123
    public function testPrepareWithIterator()
124
    {
125
        $paramInt = 1;
126
        $paramStr = 'foo';
127
128
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
129
        $stmt = $this->_conn->prepare($sql);
130
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
131
132
        $stmt->bindParam(1, $paramInt);
133
        $stmt->bindParam(2, $paramStr);
134
        $stmt->execute();
135
136
        $rows = array();
137
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);
138
        foreach ($stmt as $row) {
139
            $rows[] = array_change_key_case($row, \CASE_LOWER);
140
        }
141
142
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]);
143
    }
144
145
    public function testPrepareWithQuoted()
146
    {
147
        $table    = 'fetch_table';
148
        $paramInt = 1;
149
        $paramStr = 'foo';
150
151
        $sql  = "SELECT test_int, test_string FROM " . $this->_conn->quoteIdentifier($table) . " " .
152
               "WHERE test_int = " . $this->_conn->quote($paramInt) . " AND test_string = " . $this->_conn->quote($paramStr);
153
        $stmt = $this->_conn->prepare($sql);
154
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
155
    }
156
157
    public function testPrepareWithExecuteParams()
158
    {
159
        $paramInt = 1;
160
        $paramStr = 'foo';
161
162
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
163
        $stmt = $this->_conn->prepare($sql);
164
        self::assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
165
        $stmt->execute(array($paramInt, $paramStr));
166
167
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
168
        self::assertTrue($row !== false);
169
        $row = array_change_key_case($row, \CASE_LOWER);
170
        self::assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
171
    }
172
173
    public function testFetchAll()
174
    {
175
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
176
        $data = $this->_conn->fetchAll($sql, array(1, 'foo'));
177
178
        self::assertEquals(1, count($data));
179
180
        $row = $data[0];
181
        self::assertEquals(2, count($row));
182
183
        $row = array_change_key_case($row, \CASE_LOWER);
184
        self::assertEquals(1, $row['test_int']);
185
        self::assertEquals('foo', $row['test_string']);
186
    }
187
188
    /**
189
     * @group DBAL-209
190
     */
191
    public function testFetchAllWithTypes()
192
    {
193
        $datetimeString = '2010-01-01 10:10:10';
194
        $datetime       = new \DateTime($datetimeString);
195
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
196
        $data           = $this->_conn->fetchAll($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME));
197
198
        self::assertEquals(1, count($data));
199
200
        $row = $data[0];
201
        self::assertEquals(2, count($row));
202
203
        $row = array_change_key_case($row, \CASE_LOWER);
204
        self::assertEquals(1, $row['test_int']);
205
        self::assertStringStartsWith($datetimeString, $row['test_datetime']);
206
    }
207
208
    /**
209
     * @group DBAL-209
210
     * @expectedException \Doctrine\DBAL\DBALException
211
     */
212 View Code Duplication
    public function testFetchAllWithMissingTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver ||
215
            $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) {
216
            $this->markTestSkipped('mysqli and sqlsrv actually supports this');
217
        }
218
219
        $datetimeString = '2010-01-01 10:10:10';
220
        $datetime       = new \DateTime($datetimeString);
221
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
222
        $data           = $this->_conn->fetchAll($sql, array(1, $datetime));
0 ignored issues
show
Unused Code introduced by
$data 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...
223
    }
224
225
    public function testFetchBoth()
226
    {
227
        $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
228
        $row = $this->_conn->executeQuery($sql, array(1, 'foo'))->fetch(\PDO::FETCH_BOTH);
229
230
        self::assertTrue($row !== false);
231
232
        $row = array_change_key_case($row, \CASE_LOWER);
233
234
        self::assertEquals(1, $row['test_int']);
235
        self::assertEquals('foo', $row['test_string']);
236
        self::assertEquals(1, $row[0]);
237
        self::assertEquals('foo', $row[1]);
238
    }
239
240
    public function testFetchNoResult()
241
    {
242
        self::assertFalse(
243
            $this->_conn->executeQuery('SELECT test_int FROM fetch_table WHERE test_int = ?', [-1])->fetch()
244
        );
245
    }
246
247
    public function testFetchAssoc()
248
    {
249
        $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
250
        $row = $this->_conn->fetchAssoc($sql, array(1, 'foo'));
251
252
        self::assertTrue($row !== false);
253
254
        $row = array_change_key_case($row, \CASE_LOWER);
255
256
        self::assertEquals(1, $row['test_int']);
257
        self::assertEquals('foo', $row['test_string']);
258
    }
259
260 View Code Duplication
    public function testFetchAssocWithTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262
        $datetimeString = '2010-01-01 10:10:10';
263
        $datetime       = new \DateTime($datetimeString);
264
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
265
        $row            = $this->_conn->fetchAssoc($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME));
266
267
        self::assertTrue($row !== false);
268
269
        $row = array_change_key_case($row, \CASE_LOWER);
270
271
        self::assertEquals(1, $row['test_int']);
272
        self::assertStringStartsWith($datetimeString, $row['test_datetime']);
273
    }
274
275
    /**
276
     * @expectedException \Doctrine\DBAL\DBALException
277
     */
278 View Code Duplication
    public function testFetchAssocWithMissingTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
    {
280
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver ||
281
            $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) {
282
            $this->markTestSkipped('mysqli and sqlsrv actually supports this');
283
        }
284
285
        $datetimeString = '2010-01-01 10:10:10';
286
        $datetime       = new \DateTime($datetimeString);
287
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
288
        $row            = $this->_conn->fetchAssoc($sql, array(1, $datetime));
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...
289
    }
290
291
    public function testFetchArray()
292
    {
293
        $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
294
        $row = $this->_conn->fetchArray($sql, array(1, 'foo'));
295
296
        self::assertEquals(1, $row[0]);
297
        self::assertEquals('foo', $row[1]);
298
    }
299
300 View Code Duplication
    public function testFetchArrayWithTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
    {
302
        $datetimeString = '2010-01-01 10:10:10';
303
        $datetime       = new \DateTime($datetimeString);
304
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
305
        $row            = $this->_conn->fetchArray($sql, array(1, $datetime), array(PDO::PARAM_STR, Type::DATETIME));
306
307
        self::assertTrue($row !== false);
308
309
        $row = array_change_key_case($row, \CASE_LOWER);
310
311
        self::assertEquals(1, $row[0]);
312
        self::assertStringStartsWith($datetimeString, $row[1]);
313
    }
314
315
    /**
316
     * @expectedException \Doctrine\DBAL\DBALException
317
     */
318 View Code Duplication
    public function testFetchArrayWithMissingTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
    {
320
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver ||
321
            $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) {
322
            $this->markTestSkipped('mysqli and sqlsrv actually supports this');
323
        }
324
325
        $datetimeString = '2010-01-01 10:10:10';
326
        $datetime       = new \DateTime($datetimeString);
327
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
328
        $row            = $this->_conn->fetchArray($sql, array(1, $datetime));
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...
329
    }
330
331
    public function testFetchColumn()
332
    {
333
        $sql     = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
334
        $testInt = $this->_conn->fetchColumn($sql, array(1, 'foo'), 0);
335
336
        self::assertEquals(1, $testInt);
337
338
        $sql        = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
339
        $testString = $this->_conn->fetchColumn($sql, array(1, 'foo'), 1);
340
341
        self::assertEquals('foo', $testString);
342
    }
343
344
    public function testFetchColumnWithTypes()
345
    {
346
        $datetimeString = '2010-01-01 10:10:10';
347
        $datetime       = new \DateTime($datetimeString);
348
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
349
        $column         = $this->_conn->fetchColumn($sql, array(1, $datetime), 1, array(PDO::PARAM_STR, Type::DATETIME));
350
351
        self::assertTrue($column !== false);
352
353
        self::assertStringStartsWith($datetimeString, $column);
0 ignored issues
show
Bug introduced by
It seems like $column defined by $this->_conn->fetchColum...\Types\Type::DATETIME)) on line 349 can also be of type boolean; however, PHPUnit\Framework\Assert::assertStringStartsWith() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
354
    }
355
356
    /**
357
     * @expectedException \Doctrine\DBAL\DBALException
358
     */
359 View Code Duplication
    public function testFetchColumnWithMissingTypes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360
    {
361
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver ||
362
            $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\SQLSrv\Driver) {
363
            $this->markTestSkipped('mysqli and sqlsrv actually supports this');
364
        }
365
366
        $datetimeString = '2010-01-01 10:10:10';
367
        $datetime       = new \DateTime($datetimeString);
368
        $sql            = "SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?";
369
        $column         = $this->_conn->fetchColumn($sql, array(1, $datetime), 1);
0 ignored issues
show
Unused Code introduced by
$column 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...
370
    }
371
372
    /**
373
     * @group DDC-697
374
     */
375
    public function testExecuteQueryBindDateTimeType()
376
    {
377
        $sql  = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?';
378
        $stmt = $this->_conn->executeQuery(
379
            $sql,
380
            array(1 => new \DateTime('2010-01-01 10:10:10')),
381
            array(1 => Type::DATETIME)
382
        );
383
384
        self::assertEquals(1, $stmt->fetchColumn());
385
    }
386
387
    /**
388
     * @group DDC-697
389
     */
390
    public function testExecuteUpdateBindDateTimeType()
391
    {
392
        $datetime = new \DateTime('2010-02-02 20:20:20');
393
394
        $sql          = 'INSERT INTO fetch_table (test_int, test_string, test_datetime) VALUES (?, ?, ?)';
395
        $affectedRows = $this->_conn->executeUpdate(
396
            $sql,
397
            array(1 => 50,              2 => 'foo',             3 => $datetime),
398
            array(1 => PDO::PARAM_INT,  2 => PDO::PARAM_STR,    3 => Type::DATETIME)
399
        );
400
401
        self::assertEquals(1, $affectedRows);
402
        self::assertEquals(1, $this->_conn->executeQuery(
403
            'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?',
404
            array(1 => $datetime),
405
            array(1 => Type::DATETIME)
406
        )->fetchColumn());
407
    }
408
409
    /**
410
     * @group DDC-697
411
     */
412
    public function testPrepareQueryBindValueDateTimeType()
413
    {
414
        $sql  = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?';
415
        $stmt = $this->_conn->prepare($sql);
416
        $stmt->bindValue(1, new \DateTime('2010-01-01 10:10:10'), Type::DATETIME);
417
        $stmt->execute();
418
419
        self::assertEquals(1, $stmt->fetchColumn());
420
    }
421
422
    /**
423
     * @group DBAL-78
424
     */
425
    public function testNativeArrayListSupport()
426
    {
427
        for ($i = 100; $i < 110; $i++) {
428
            $this->_conn->insert('fetch_table', array('test_int' => $i, 'test_string' => 'foo' . $i, 'test_datetime' => '2010-01-01 10:10:10'));
429
        }
430
431
        $stmt = $this->_conn->executeQuery(
432
            'SELECT test_int FROM fetch_table WHERE test_int IN (?)',
433
            array(array(100, 101, 102, 103, 104)),
434
            array(Connection::PARAM_INT_ARRAY)
435
        );
436
437
        $data = $stmt->fetchAll(PDO::FETCH_NUM);
438
        self::assertEquals(5, count($data));
439
        self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data);
440
441
        $stmt = $this->_conn->executeQuery(
442
            'SELECT test_int FROM fetch_table WHERE test_string IN (?)',
443
            array(array('foo100', 'foo101', 'foo102', 'foo103', 'foo104')),
444
            array(Connection::PARAM_STR_ARRAY)
445
        );
446
447
        $data = $stmt->fetchAll(PDO::FETCH_NUM);
448
        self::assertEquals(5, count($data));
449
        self::assertEquals(array(array(100), array(101), array(102), array(103), array(104)), $data);
450
    }
451
452
    /**
453
     * @dataProvider getTrimExpressionData
454
     */
455
    public function testTrimExpression($value, $position, $char, $expectedResult)
456
    {
457
        $sql = 'SELECT ' .
458
            $this->_conn->getDatabasePlatform()->getTrimExpression($value, $position, $char) . ' AS trimmed ' .
459
            'FROM fetch_table';
460
461
        $row = $this->_conn->fetchAssoc($sql);
462
        $row = array_change_key_case($row, CASE_LOWER);
463
464
        self::assertEquals($expectedResult, $row['trimmed']);
465
    }
466
467
    public function getTrimExpressionData()
468
    {
469
        return array(
470
            array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'),
471
            array('test_string', AbstractPlatform::TRIM_LEADING, false, 'foo'),
472
            array('test_string', AbstractPlatform::TRIM_TRAILING, false, 'foo'),
473
            array('test_string', AbstractPlatform::TRIM_BOTH, false, 'foo'),
474
            array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'f'", 'oo'),
475
            array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'o'", 'f'),
476
            array('test_string', AbstractPlatform::TRIM_UNSPECIFIED, "'.'", 'foo'),
477
            array('test_string', AbstractPlatform::TRIM_LEADING, "'f'", 'oo'),
478
            array('test_string', AbstractPlatform::TRIM_LEADING, "'o'", 'foo'),
479
            array('test_string', AbstractPlatform::TRIM_LEADING, "'.'", 'foo'),
480
            array('test_string', AbstractPlatform::TRIM_TRAILING, "'f'", 'foo'),
481
            array('test_string', AbstractPlatform::TRIM_TRAILING, "'o'", 'f'),
482
            array('test_string', AbstractPlatform::TRIM_TRAILING, "'.'", 'foo'),
483
            array('test_string', AbstractPlatform::TRIM_BOTH, "'f'", 'oo'),
484
            array('test_string', AbstractPlatform::TRIM_BOTH, "'o'", 'f'),
485
            array('test_string', AbstractPlatform::TRIM_BOTH, "'.'", 'foo'),
486
            array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, false, 'foo'),
487
            array("' foo '", AbstractPlatform::TRIM_LEADING, false, 'foo '),
488
            array("' foo '", AbstractPlatform::TRIM_TRAILING, false, ' foo'),
489
            array("' foo '", AbstractPlatform::TRIM_BOTH, false, 'foo'),
490
            array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'f'", ' foo '),
491
            array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'o'", ' foo '),
492
            array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "'.'", ' foo '),
493
            array("' foo '", AbstractPlatform::TRIM_UNSPECIFIED, "' '", 'foo'),
494
            array("' foo '", AbstractPlatform::TRIM_LEADING, "'f'", ' foo '),
495
            array("' foo '", AbstractPlatform::TRIM_LEADING, "'o'", ' foo '),
496
            array("' foo '", AbstractPlatform::TRIM_LEADING, "'.'", ' foo '),
497
            array("' foo '", AbstractPlatform::TRIM_LEADING, "' '", 'foo '),
498
            array("' foo '", AbstractPlatform::TRIM_TRAILING, "'f'", ' foo '),
499
            array("' foo '", AbstractPlatform::TRIM_TRAILING, "'o'", ' foo '),
500
            array("' foo '", AbstractPlatform::TRIM_TRAILING, "'.'", ' foo '),
501
            array("' foo '", AbstractPlatform::TRIM_TRAILING, "' '", ' foo'),
502
            array("' foo '", AbstractPlatform::TRIM_BOTH, "'f'", ' foo '),
503
            array("' foo '", AbstractPlatform::TRIM_BOTH, "'o'", ' foo '),
504
            array("' foo '", AbstractPlatform::TRIM_BOTH, "'.'", ' foo '),
505
            array("' foo '", AbstractPlatform::TRIM_BOTH, "' '", 'foo'),
506
        );
507
    }
508
509
    /**
510
     * @group DDC-1014
511
     */
512
    public function testDateArithmetics()
513
    {
514
        $p    = $this->_conn->getDatabasePlatform();
515
        $sql  = 'SELECT ';
516
        $sql .= $p->getDateDiffExpression('test_datetime', $p->getCurrentTimestampSQL()) . ' AS diff, ';
517
        $sql .= $p->getDateAddSecondsExpression('test_datetime', 1) . ' AS add_seconds, ';
518
        $sql .= $p->getDateSubSecondsExpression('test_datetime', 1) . ' AS sub_seconds, ';
519
        $sql .= $p->getDateAddMinutesExpression('test_datetime', 5) . ' AS add_minutes, ';
520
        $sql .= $p->getDateSubMinutesExpression('test_datetime', 5) . ' AS sub_minutes, ';
521
        $sql .= $p->getDateAddHourExpression('test_datetime', 3) . ' AS add_hour, ';
522
        $sql .= $p->getDateSubHourExpression('test_datetime', 3) . ' AS sub_hour, ';
523
        $sql .= $p->getDateAddDaysExpression('test_datetime', 10) . ' AS add_days, ';
524
        $sql .= $p->getDateSubDaysExpression('test_datetime', 10) . ' AS sub_days, ';
525
        $sql .= $p->getDateAddWeeksExpression('test_datetime', 1) . ' AS add_weeks, ';
526
        $sql .= $p->getDateSubWeeksExpression('test_datetime', 1) . ' AS sub_weeks, ';
527
        $sql .= $p->getDateAddMonthExpression('test_datetime', 2) . ' AS add_month, ';
528
        $sql .= $p->getDateSubMonthExpression('test_datetime', 2) . ' AS sub_month, ';
529
        $sql .= $p->getDateAddQuartersExpression('test_datetime', 3) . ' AS add_quarters, ';
530
        $sql .= $p->getDateSubQuartersExpression('test_datetime', 3) . ' AS sub_quarters, ';
531
        $sql .= $p->getDateAddYearsExpression('test_datetime', 6) . ' AS add_years, ';
532
        $sql .= $p->getDateSubYearsExpression('test_datetime', 6) . ' AS sub_years ';
533
        $sql .= 'FROM fetch_table';
534
535
        $row = $this->_conn->fetchAssoc($sql);
536
        $row = array_change_key_case($row, CASE_LOWER);
537
538
        $diff = (strtotime('2010-01-01') - strtotime(date('Y-m-d'))) / 3600 / 24;
539
        self::assertEquals($diff, $row['diff'], "Date difference should be approx. " . $diff . " days.", 1);
540
        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");
541
        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");
542
        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");
543
        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");
544
        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");
545
        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");
546
        self::assertEquals('2010-01-11', date('Y-m-d', strtotime($row['add_days'])), "Adding date should end up on 2010-01-11");
547
        self::assertEquals('2009-12-22', date('Y-m-d', strtotime($row['sub_days'])), "Subtracting date should end up on 2009-12-22");
548
        self::assertEquals('2010-01-08', date('Y-m-d', strtotime($row['add_weeks'])), "Adding week should end up on 2010-01-08");
549
        self::assertEquals('2009-12-25', date('Y-m-d', strtotime($row['sub_weeks'])), "Subtracting week should end up on 2009-12-25");
550
        self::assertEquals('2010-03-01', date('Y-m-d', strtotime($row['add_month'])), "Adding month should end up on 2010-03-01");
551
        self::assertEquals('2009-11-01', date('Y-m-d', strtotime($row['sub_month'])), "Subtracting month should end up on 2009-11-01");
552
        self::assertEquals('2010-10-01', date('Y-m-d', strtotime($row['add_quarters'])), "Adding quarters should end up on 2010-04-01");
553
        self::assertEquals('2009-04-01', date('Y-m-d', strtotime($row['sub_quarters'])), "Subtracting quarters should end up on 2009-10-01");
554
        self::assertEquals('2016-01-01', date('Y-m-d', strtotime($row['add_years'])), "Adding years should end up on 2016-01-01");
555
        self::assertEquals('2004-01-01', date('Y-m-d', strtotime($row['sub_years'])), "Subtracting years should end up on 2004-01-01");
556
    }
557
558
    public function testLocateExpression()
559
    {
560
        $platform = $this->_conn->getDatabasePlatform();
561
562
        $sql  = 'SELECT ';
563
        $sql .= $platform->getLocateExpression('test_string', "'oo'") . ' AS locate1, ';
564
        $sql .= $platform->getLocateExpression('test_string', "'foo'") . ' AS locate2, ';
565
        $sql .= $platform->getLocateExpression('test_string', "'bar'") . ' AS locate3, ';
566
        $sql .= $platform->getLocateExpression('test_string', 'test_string') . ' AS locate4, ';
567
        $sql .= $platform->getLocateExpression("'foo'", 'test_string') . ' AS locate5, ';
568
        $sql .= $platform->getLocateExpression("'barfoobaz'", 'test_string') . ' AS locate6, ';
569
        $sql .= $platform->getLocateExpression("'bar'", 'test_string') . ' AS locate7, ';
570
        $sql .= $platform->getLocateExpression('test_string', "'oo'", 2) . ' AS locate8, ';
571
        $sql .= $platform->getLocateExpression('test_string', "'oo'", 3) . ' AS locate9 ';
572
        $sql .= 'FROM fetch_table';
573
574
        $row = $this->_conn->fetchAssoc($sql);
575
        $row = array_change_key_case($row, CASE_LOWER);
576
577
        self::assertEquals(2, $row['locate1']);
578
        self::assertEquals(1, $row['locate2']);
579
        self::assertEquals(0, $row['locate3']);
580
        self::assertEquals(1, $row['locate4']);
581
        self::assertEquals(1, $row['locate5']);
582
        self::assertEquals(4, $row['locate6']);
583
        self::assertEquals(0, $row['locate7']);
584
        self::assertEquals(2, $row['locate8']);
585
        self::assertEquals(0, $row['locate9']);
586
    }
587
588 View Code Duplication
    public function testQuoteSQLInjection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
589
    {
590
        $sql  = "SELECT * FROM fetch_table WHERE test_string = " . $this->_conn->quote("bar' OR '1'='1");
591
        $rows = $this->_conn->fetchAll($sql);
592
593
        self::assertEquals(0, count($rows), "no result should be returned, otherwise SQL injection is possible");
594
    }
595
596
    /**
597
     * @group DDC-1213
598
     */
599
    public function testBitComparisonExpressionSupport()
600
    {
601
        $this->_conn->exec('DELETE FROM fetch_table');
602
        $platform = $this->_conn->getDatabasePlatform();
603
        $bitmap   = array();
604
605
        for ($i = 2; $i < 9; $i = $i + 2) {
606
            $bitmap[$i] = array(
607
                'bit_or'    => ($i | 2),
608
                'bit_and'   => ($i & 2)
609
            );
610
            $this->_conn->insert('fetch_table', array(
611
                'test_int'      => $i,
612
                'test_string'   => json_encode($bitmap[$i]),
613
                'test_datetime' => '2010-01-01 10:10:10'
614
            ));
615
        }
616
617
        $sql[] = 'SELECT ';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sql was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sql = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
618
        $sql[] = 'test_int, ';
619
        $sql[] = 'test_string, ';
620
        $sql[] = $platform->getBitOrComparisonExpression('test_int', 2) . ' AS bit_or, ';
621
        $sql[] = $platform->getBitAndComparisonExpression('test_int', 2) . ' AS bit_and ';
622
        $sql[] = 'FROM fetch_table';
623
624
        $stmt = $this->_conn->executeQuery(implode(PHP_EOL, $sql));
625
        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
626
627
628
        self::assertEquals(4, count($data));
629
        self::assertEquals(count($bitmap), count($data));
630
        foreach ($data as $row) {
631
            $row = array_change_key_case($row, CASE_LOWER);
632
633
            self::assertArrayHasKey('test_int', $row);
634
635
            $id = $row['test_int'];
636
637
            self::assertArrayHasKey($id, $bitmap);
638
            self::assertArrayHasKey($id, $bitmap);
639
640
            self::assertArrayHasKey('bit_or', $row);
641
            self::assertArrayHasKey('bit_and', $row);
642
643
            self::assertEquals($row['bit_or'], $bitmap[$id]['bit_or']);
644
            self::assertEquals($row['bit_and'], $bitmap[$id]['bit_and']);
645
        }
646
    }
647
648
    public function testSetDefaultFetchMode()
649
    {
650
        $stmt = $this->_conn->query("SELECT * FROM fetch_table");
651
        $stmt->setFetchMode(\PDO::FETCH_NUM);
652
653
        $row = array_keys($stmt->fetch());
654
        self::assertEquals(0, count(array_filter($row, function ($v) {
655
            return ! is_numeric($v);
656
        })), "should be no non-numerical elements in the result.");
657
    }
658
659
    /**
660
     * @group DBAL-1091
661
     */
662
    public function testFetchAllStyleObject()
663
    {
664
        $this->setupFixture();
665
666
        $sql  = 'SELECT test_int, test_string, test_datetime FROM fetch_table';
667
        $stmt = $this->_conn->prepare($sql);
668
669
        $stmt->execute();
670
671
        $results = $stmt->fetchAll(\PDO::FETCH_OBJ);
672
673
        self::assertCount(1, $results);
674
        self::assertInstanceOf('stdClass', $results[0]);
675
676
        self::assertEquals(
677
            1,
678
            property_exists($results[0], 'test_int') ? $results[0]->test_int : $results[0]->TEST_INT
679
        );
680
        self::assertEquals(
681
            'foo',
682
            property_exists($results[0], 'test_string') ? $results[0]->test_string : $results[0]->TEST_STRING
683
        );
684
        self::assertStringStartsWith(
685
            '2010-01-01 10:10:10',
686
            property_exists($results[0], 'test_datetime') ? $results[0]->test_datetime : $results[0]->TEST_DATETIME
687
        );
688
    }
689
690
    /**
691
     * @group DBAL-196
692
     */
693 View Code Duplication
    public function testFetchAllSupportFetchClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
694
    {
695
        $this->skipOci8AndMysqli();
696
        $this->setupFixture();
697
698
        $sql  = "SELECT test_int, test_string, test_datetime FROM fetch_table";
699
        $stmt = $this->_conn->prepare($sql);
700
        $stmt->execute();
701
702
        $results = $stmt->fetchAll(
703
            \PDO::FETCH_CLASS,
704
            __NAMESPACE__ . '\\MyFetchClass'
705
        );
706
707
        self::assertEquals(1, count($results));
708
        self::assertInstanceOf(__NAMESPACE__ . '\\MyFetchClass', $results[0]);
709
710
        self::assertEquals(1, $results[0]->test_int);
711
        self::assertEquals('foo', $results[0]->test_string);
712
        self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime);
713
    }
714
715
    /**
716
     * @group DBAL-241
717
     */
718
    public function testFetchAllStyleColumn()
719
    {
720
        $sql = "DELETE FROM fetch_table";
721
        $this->_conn->executeUpdate($sql);
722
723
        $this->_conn->insert('fetch_table', array('test_int' => 1, 'test_string' => 'foo'));
724
        $this->_conn->insert('fetch_table', array('test_int' => 10, 'test_string' => 'foo'));
725
726
        $sql  = "SELECT test_int FROM fetch_table";
727
        $rows = $this->_conn->query($sql)->fetchAll(\PDO::FETCH_COLUMN);
728
729
        self::assertEquals(array(1, 10), $rows);
730
    }
731
732
    /**
733
     * @group DBAL-214
734
     */
735 View Code Duplication
    public function testSetFetchModeClassFetchAll()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
736
    {
737
        $this->skipOci8AndMysqli();
738
        $this->setupFixture();
739
740
        $sql  = "SELECT * FROM fetch_table";
741
        $stmt = $this->_conn->query($sql);
742
        $stmt->setFetchMode(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass');
743
744
        $results = $stmt->fetchAll();
745
746
        self::assertEquals(1, count($results));
747
        self::assertInstanceOf(__NAMESPACE__ . '\\MyFetchClass', $results[0]);
748
749
        self::assertEquals(1, $results[0]->test_int);
750
        self::assertEquals('foo', $results[0]->test_string);
751
        self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime);
752
    }
753
754
    /**
755
     * @group DBAL-214
756
     */
757
    public function testSetFetchModeClassFetch()
758
    {
759
        $this->skipOci8AndMysqli();
760
        $this->setupFixture();
761
762
        $sql  = "SELECT * FROM fetch_table";
763
        $stmt = $this->_conn->query($sql);
764
        $stmt->setFetchMode(\PDO::FETCH_CLASS, __NAMESPACE__ . '\\MyFetchClass');
765
766
        $results = array();
767
        while ($row = $stmt->fetch()) {
768
            $results[] = $row;
769
        }
770
771
        self::assertEquals(1, count($results));
772
        self::assertInstanceOf(__NAMESPACE__ . '\\MyFetchClass', $results[0]);
773
774
        self::assertEquals(1, $results[0]->test_int);
775
        self::assertEquals('foo', $results[0]->test_string);
776
        self::assertStringStartsWith('2010-01-01 10:10:10', $results[0]->test_datetime);
777
    }
778
779
    /**
780
     * @group DBAL-257
781
     */
782
    public function testEmptyFetchColumnReturnsFalse()
783
    {
784
        $this->_conn->beginTransaction();
785
        $this->_conn->exec('DELETE FROM fetch_table');
786
        self::assertFalse($this->_conn->fetchColumn('SELECT test_int FROM fetch_table'));
0 ignored issues
show
Bug introduced by
It seems like $this->_conn->fetchColum..._int FROM fetch_table') targeting Doctrine\DBAL\Connection::fetchColumn() can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
787
        self::assertFalse($this->_conn->query('SELECT test_int FROM fetch_table')->fetchColumn());
0 ignored issues
show
Bug introduced by
It seems like $this->_conn->query('SEL..._table')->fetchColumn() targeting Doctrine\DBAL\Driver\Res...tatement::fetchColumn() can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
788
        $this->_conn->rollBack();
789
    }
790
791
    /**
792
     * @group DBAL-339
793
     */
794
    public function testSetFetchModeOnDbalStatement()
795
    {
796
        $sql  = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
797
        $stmt = $this->_conn->executeQuery($sql, array(1, "foo"));
798
        $stmt->setFetchMode(\PDO::FETCH_NUM);
799
800
        $row = $stmt->fetch();
801
802
        self::assertArrayHasKey(0, $row);
803
        self::assertArrayHasKey(1, $row);
804
        self::assertFalse($stmt->fetch());
805
    }
806
807
    /**
808
     * @group DBAL-435
809
     */
810
    public function testEmptyParameters()
811
    {
812
        $sql  = "SELECT * FROM fetch_table WHERE test_int IN (?)";
813
        $stmt = $this->_conn->executeQuery($sql, array(array()), array(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY));
814
        $rows = $stmt->fetchAll();
815
816
        self::assertEquals(array(), $rows);
817
    }
818
819
    /**
820
     * @group DBAL-1028
821
     */
822
    public function testFetchColumnNullValue()
823
    {
824
        $this->_conn->executeUpdate(
825
            'INSERT INTO fetch_table (test_int, test_string) VALUES (?, ?)',
826
            array(2, 'foo')
827
        );
828
829
        self::assertNull(
830
            $this->_conn->fetchColumn('SELECT test_datetime FROM fetch_table WHERE test_int = ?', array(2))
831
        );
832
    }
833
834
    /**
835
     * @group DBAL-1028
836
     */
837
    public function testFetchColumnNonExistingIndex()
838
    {
839
        if ($this->_conn->getDriver()->getName() === 'pdo_sqlsrv') {
840
            $this->markTestSkipped(
841
                'Test does not work for pdo_sqlsrv driver as it throws a fatal error for a non-existing column index.'
842
            );
843
        }
844
845
        self::assertNull(
846
            $this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(1), 1)
847
        );
848
    }
849
850
    /**
851
     * @group DBAL-1028
852
     */
853
    public function testFetchColumnNoResult()
854
    {
855
        self::assertFalse(
856
            $this->_conn->fetchColumn('SELECT test_int FROM fetch_table WHERE test_int = ?', array(-1))
0 ignored issues
show
Bug introduced by
It seems like $this->_conn->fetchColum...st_int = ?', array(-1)) targeting Doctrine\DBAL\Connection::fetchColumn() can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
857
        );
858
    }
859
860
    private function setupFixture()
861
    {
862
        $this->_conn->exec('DELETE FROM fetch_table');
863
        $this->_conn->insert('fetch_table', array(
864
            'test_int'      => 1,
865
            'test_string'   => 'foo',
866
            'test_datetime' => '2010-01-01 10:10:10'
867
        ));
868
    }
869
870
    private function skipOci8AndMysqli()
0 ignored issues
show
Coding Style introduced by
skipOci8AndMysqli uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
871
    {
872
        if (isset($GLOBALS['db_type']) && $GLOBALS['db_type'] == "oci8") {
873
            $this->markTestSkipped("Not supported by OCI8");
874
        }
875
        if ('mysqli' == $this->_conn->getDriver()->getName()) {
876
            $this->markTestSkipped('Mysqli driver dont support this feature.');
877
        }
878
    }
879
}
880
881
class MyFetchClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
882
{
883
    public $test_int;
884
    public $test_string;
885
    public $test_datetime;
886
}
887