Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / PortabilityTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional;
4
5 use Doctrine\DBAL\Types\Type;
6 use Doctrine\DBAL\Connection;
7 use Doctrine\DBAL\DriverManager;
8 use PDO;
9
10 require_once __DIR__ . '/../../TestInit.php';
11
12 /**
13  * @group DBAL-56
14  */
15 class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
16 {
17     static private $hasTable = false;
18
19     private $portableConnection;
20
21     public function tearDown()
22     {
23         if ($this->portableConnection) {
24             $this->portableConnection->close();
25         }
26     }
27
28     private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER)
29     {
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());
36
37             try {
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'));
44
45                 $sm = $this->portableConnection->getSchemaManager();
46                 $sm->createTable($table);
47
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) {
51
52             }
53         }
54
55         return $this->portableConnection;
56     }
57
58     public function testFullFetchMode()
59     {
60         $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
61         $this->assertFetchResultRows($rows);
62
63         $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
64         $stmt->setFetchMode(\PDO::FETCH_ASSOC);
65         foreach ($stmt as $row) {
66             $this->assertFetchResultRow($row);
67         }
68
69         $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
70         while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
71             $this->assertFetchResultRow($row);
72         }
73
74         $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
75         $stmt->execute();
76
77         while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
78             $this->assertFetchResultRow($row);
79         }
80     }
81
82     public function assertFetchResultRows($rows)
83     {
84         $this->assertEquals(2, count($rows));
85         foreach ($rows AS $row) {
86             $this->assertFetchResultRow($row);
87         }
88     }
89
90     public function assertFetchResultRow($row)
91     {
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']);
96     }
97 }