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