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