1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\ColumnCase; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\DriverManager; |
10
|
|
|
use Doctrine\DBAL\FetchMode; |
11
|
|
|
use Doctrine\DBAL\Portability\Connection as ConnectionPortability; |
12
|
|
|
use Doctrine\DBAL\Schema\Table; |
13
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
14
|
|
|
use function strlen; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @group DBAL-56 |
18
|
|
|
*/ |
19
|
|
|
class PortabilityTest extends DbalFunctionalTestCase |
20
|
|
|
{ |
21
|
|
|
/** @var Connection */ |
22
|
|
|
private $portableConnection; |
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->portableConnection = $this->getPortableConnection(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function tearDown() : void |
32
|
|
|
{ |
33
|
|
|
$this->portableConnection->close(); |
34
|
|
|
|
35
|
|
|
parent::tearDown(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function getPortableConnection( |
39
|
|
|
int $portabilityMode = ConnectionPortability::PORTABILITY_ALL, |
40
|
|
|
int $case = ColumnCase::LOWER |
41
|
|
|
) : Connection { |
42
|
|
|
$params = $this->connection->getParams(); |
43
|
|
|
|
44
|
|
|
$params['wrapperClass'] = ConnectionPortability::class; |
45
|
|
|
$params['portability'] = $portabilityMode; |
46
|
|
|
$params['fetch_case'] = $case; |
47
|
|
|
|
48
|
|
|
$portableConnection = DriverManager::getConnection($params, $this->connection->getConfiguration(), $this->connection->getEventManager()); |
49
|
|
|
|
50
|
|
|
$table = new Table('portability_table'); |
51
|
|
|
$table->addColumn('Test_Int', 'integer'); |
52
|
|
|
$table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]); |
53
|
|
|
$table->addColumn('Test_Null', 'string', ['notnull' => false]); |
54
|
|
|
$table->setPrimaryKey(['Test_Int']); |
55
|
|
|
|
56
|
|
|
$sm = $portableConnection->getSchemaManager(); |
57
|
|
|
$sm->dropAndCreateTable($table); |
58
|
|
|
|
59
|
|
|
$portableConnection->insert('portability_table', ['Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => '']); |
60
|
|
|
$portableConnection->insert('portability_table', ['Test_Int' => 2, 'Test_String' => 'foo ', 'Test_Null' => null]); |
61
|
|
|
|
62
|
|
|
return $portableConnection; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFullFetchMode() : void |
66
|
|
|
{ |
67
|
|
|
$rows = $this->portableConnection->fetchAll('SELECT * FROM portability_table'); |
68
|
|
|
$this->assertFetchResultRows($rows); |
69
|
|
|
|
70
|
|
|
$stmt = $this->portableConnection->query('SELECT * FROM portability_table'); |
71
|
|
|
$stmt->setFetchMode(FetchMode::ASSOCIATIVE); |
72
|
|
|
|
73
|
|
|
foreach ($stmt as $row) { |
74
|
|
|
$this->assertFetchResultRow($row); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$stmt = $this->portableConnection->query('SELECT * FROM portability_table'); |
78
|
|
|
|
79
|
|
|
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) { |
80
|
|
|
$this->assertFetchResultRow($row); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$stmt = $this->portableConnection->prepare('SELECT * FROM portability_table'); |
84
|
|
|
$stmt->execute(); |
85
|
|
|
|
86
|
|
|
while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) { |
87
|
|
|
$this->assertFetchResultRow($row); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testConnFetchMode() : void |
92
|
|
|
{ |
93
|
|
|
$this->portableConnection->setFetchMode(FetchMode::ASSOCIATIVE); |
94
|
|
|
|
95
|
|
|
$rows = $this->portableConnection->fetchAll('SELECT * FROM portability_table'); |
96
|
|
|
$this->assertFetchResultRows($rows); |
97
|
|
|
|
98
|
|
|
$stmt = $this->portableConnection->query('SELECT * FROM portability_table'); |
99
|
|
|
foreach ($stmt as $row) { |
100
|
|
|
$this->assertFetchResultRow($row); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$stmt = $this->portableConnection->query('SELECT * FROM portability_table'); |
104
|
|
|
while (($row = $stmt->fetch())) { |
105
|
|
|
$this->assertFetchResultRow($row); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$stmt = $this->portableConnection->prepare('SELECT * FROM portability_table'); |
109
|
|
|
$stmt->execute(); |
110
|
|
|
while (($row = $stmt->fetch())) { |
111
|
|
|
$this->assertFetchResultRow($row); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array<int, array<string, mixed>> $rows |
117
|
|
|
*/ |
118
|
|
|
private function assertFetchResultRows(array $rows) : void |
119
|
|
|
{ |
120
|
|
|
self::assertCount(2, $rows); |
121
|
|
|
foreach ($rows as $row) { |
122
|
|
|
$this->assertFetchResultRow($row); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array<string, mixed> $row |
128
|
|
|
*/ |
129
|
|
|
public function assertFetchResultRow(array $row) : void |
130
|
|
|
{ |
131
|
|
|
self::assertContains($row['test_int'], [1, 2], 'Primary key test_int should either be 1 or 2.'); |
132
|
|
|
self::assertArrayHasKey('test_string', $row, 'Case should be lowered.'); |
133
|
|
|
self::assertEquals(3, strlen($row['test_string']), 'test_string should be rtrimed to length of three for CHAR(32) column.'); |
134
|
|
|
self::assertNull($row['test_null']); |
135
|
|
|
self::assertArrayNotHasKey(0, $row, 'The row should not contain numerical keys.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed[] $expected |
140
|
|
|
* |
141
|
|
|
* @dataProvider fetchAllColumnProvider |
142
|
|
|
*/ |
143
|
|
|
public function testFetchAllColumn(string $field, array $expected) : void |
144
|
|
|
{ |
145
|
|
|
$stmt = $this->portableConnection->query('SELECT ' . $field . ' FROM portability_table'); |
146
|
|
|
|
147
|
|
|
$column = $stmt->fetchAll(FetchMode::COLUMN); |
148
|
|
|
self::assertEquals($expected, $column); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return iterable<string, array<int, mixed>> |
153
|
|
|
*/ |
154
|
|
|
public static function fetchAllColumnProvider() : iterable |
155
|
|
|
{ |
156
|
|
|
return [ |
157
|
|
|
'int' => [ |
158
|
|
|
'Test_Int', |
159
|
|
|
[1, 2], |
160
|
|
|
], |
161
|
|
|
'string' => [ |
162
|
|
|
'Test_String', |
163
|
|
|
['foo', 'foo'], |
164
|
|
|
], |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testFetchAllNullColumn() : void |
169
|
|
|
{ |
170
|
|
|
$stmt = $this->portableConnection->query('SELECT Test_Null FROM portability_table'); |
171
|
|
|
|
172
|
|
|
$column = $stmt->fetchAll(FetchMode::COLUMN); |
173
|
|
|
self::assertSame([null, null], $column); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|