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