PortabilityTest.php 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use PDO;

require_once __DIR__ . '/../../TestInit.php';

/**
 * @group DBAL-56
 */
class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
    static private $hasTable = false;
18

19
    private $portableConnection;
20

21 22 23 24 25 26
    public function tearDown()
    {
        if ($this->portableConnection) {
            $this->portableConnection->close();
        }
    }
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER)
    {
        if (!$this->portableConnection) {
            $params = $this->_conn->getParams();
            $params['wrapperClass'] = 'Doctrine\DBAL\Portability\Connection';
            $params['portability'] = $portabilityMode;
            $params['fetch_case'] = $case;
            $this->portableConnection = DriverManager::getConnection($params, $this->_conn->getConfiguration(), $this->_conn->getEventManager());

            try {
                /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
                $table = new \Doctrine\DBAL\Schema\Table("portability_table");
                $table->addColumn('Test_Int', 'integer');
                $table->addColumn('Test_String', 'string', array('fixed' => true, 'length' => 32));
                $table->addColumn('Test_Null', 'string', array('notnull' => false));
43
                $table->setPrimaryKey(array('Test_Int'));
44 45 46 47 48

                $sm = $this->portableConnection->getSchemaManager();
                $sm->createTable($table);

                $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 56
        return $this->portableConnection;
    }
57

58 59 60 61
    public function testFullFetchMode()
    {
        $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
        $this->assertFetchResultRows($rows);
62

63
        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
64
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);
65 66 67 68 69
        foreach ($stmt as $row) {
            $this->assertFetchResultRow($row);
        }

        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
70 71 72
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $this->assertFetchResultRow($row);
        }
73

74 75
        $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
        $stmt->execute();
76

77 78 79 80
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $this->assertFetchResultRow($row);
        }
    }
81

82 83 84 85 86 87 88
    public function assertFetchResultRows($rows)
    {
        $this->assertEquals(2, count($rows));
        foreach ($rows AS $row) {
            $this->assertFetchResultRow($row);
        }
    }
89

90 91
    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 94 95 96
        $this->assertArrayHasKey('test_string', $row, "Case should be lowered.");
        $this->assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column.");
        $this->assertNull($row['test_null']);
    }
97
}