3 namespace Doctrine\Tests\DBAL\Functional;
5 use Doctrine\DBAL\Types\Type;
6 use Doctrine\DBAL\Connection;
7 use Doctrine\DBAL\DriverManager;
10 require_once __DIR__ . '/../../TestInit.php';
15 class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
17 static private $hasTable = false;
19 private $portableConnection;
21 public function tearDown()
23 if ($this->portableConnection) {
24 $this->portableConnection->close();
28 private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER)
30 if (!$this->portableConnection) {
31 $params = $this->_conn->getParams();
32 $params['wrapperClass'] = 'Doctrine\DBAL\Portability\Connection';
33 $params['portability'] = $portabilityMode;
34 $params['fetch_case'] = $case;
35 $this->portableConnection = DriverManager::getConnection($params, $this->_conn->getConfiguration(), $this->_conn->getEventManager());
38 /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
39 $table = new \Doctrine\DBAL\Schema\Table("portability_table");
40 $table->addColumn('Test_Int', 'integer');
41 $table->addColumn('Test_String', 'string', array('fixed' => true, 'length' => 32));
42 $table->addColumn('Test_Null', 'string', array('notnull' => false));
43 $table->setPrimaryKey(array('Test_Int'));
45 $sm = $this->portableConnection->getSchemaManager();
46 $sm->createTable($table);
48 $this->portableConnection->insert('portability_table', array('Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => ''));
49 $this->portableConnection->insert('portability_table', array('Test_Int' => 2, 'Test_String' => 'foo ', 'Test_Null' => null));
50 } catch(\Exception $e) {
55 return $this->portableConnection;
58 public function testFullFetchMode()
60 $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
61 $this->assertFetchResultRows($rows);
63 $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
64 $stmt->setFetchMode(\PDO::FETCH_ASSOC);
65 foreach ($stmt as $row) {
66 $this->assertFetchResultRow($row);
69 $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
70 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
71 $this->assertFetchResultRow($row);
74 $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
77 while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
78 $this->assertFetchResultRow($row);
82 public function assertFetchResultRows($rows)
84 $this->assertEquals(2, count($rows));
85 foreach ($rows AS $row) {
86 $this->assertFetchResultRow($row);
90 public function assertFetchResultRow($row)
92 $this->assertTrue(in_array($row['test_int'], array(1, 2)), "Primary key test_int should either be 1 or 2.");
93 $this->assertArrayHasKey('test_string', $row, "Case should be lowered.");
94 $this->assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column.");
95 $this->assertNull($row['test_null']);