PortabilityTest.php 5.09 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
7
use Doctrine\DBAL\Portability\Connection as ConnectionPortability;
jeroendedauw's avatar
jeroendedauw committed
8
use PDO;
9 10 11 12 13 14 15 16 17

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

/**
 * @group DBAL-56
 */
class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
    private $portableConnection;
18

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

        parent::tearDown();
26
    }
27

28 29 30 31 32
    /**
     * @param   integer     $portabilityMode
     * @param   integer     $case
     * @return  Connection
     */
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    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));
48
                $table->setPrimaryKey(array('Test_Int'));
49 50 51 52 53

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

                $this->portableConnection->insert('portability_table', array('Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => ''));
54
                $this->portableConnection->insert('portability_table', array('Test_Int' => 2, 'Test_String' => 'foo  ', 'Test_Null' => null));
55
            } catch(\Exception $e) {
56

57 58
            }
        }
59

60 61
        return $this->portableConnection;
    }
62

63 64 65 66
    public function testFullFetchMode()
    {
        $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
        $this->assertFetchResultRows($rows);
67

68
        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
69
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);
70 71 72 73 74
        foreach ($stmt as $row) {
            $this->assertFetchResultRow($row);
        }

        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
75
        while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
76 77
            $this->assertFetchResultRow($row);
        }
78

79 80
        $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
        $stmt->execute();
81
        while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
82 83 84
            $this->assertFetchResultRow($row);
        }
    }
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    public function testConnFetchMode()
    {
        $conn = $this->getPortableConnection();
        $conn->setFetchMode(\PDO::FETCH_ASSOC);

        $rows = $conn->fetchAll('SELECT * FROM portability_table');
        $this->assertFetchResultRows($rows);

        $stmt = $conn->query('SELECT * FROM portability_table');
        foreach ($stmt as $row) {
          $this->assertFetchResultRow($row);
        }

        $stmt = $conn->query('SELECT * FROM portability_table');
        while (($row = $stmt->fetch())) {
          $this->assertFetchResultRow($row);
        }

        $stmt = $conn->prepare('SELECT * FROM portability_table');
        $stmt->execute();
        while (($row = $stmt->fetch())) {
          $this->assertFetchResultRow($row);
        }
    }

111 112 113
    public function assertFetchResultRows($rows)
    {
        $this->assertEquals(2, count($rows));
jeroendedauw's avatar
jeroendedauw committed
114
        foreach ($rows as $row) {
115 116 117
            $this->assertFetchResultRow($row);
        }
    }
118

119 120
    public function assertFetchResultRow($row)
    {
121
        $this->assertTrue(in_array($row['test_int'], array(1, 2)), "Primary key test_int should either be 1 or 2.");
122 123 124
        $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']);
125
        $this->assertArrayNotHasKey(0, $row, "PDO::FETCH_ASSOC should not return numerical keys.");
126
    }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    public function testPortabilitySqlServer()
    {
        $portability = ConnectionPortability::PORTABILITY_SQLSRV;
        $params = array(
            'portability' => $portability
        );

        $driverMock = $this->getMock('Doctrine\\DBAL\\Driver\\PDOSqlsrv\\Driver', array('connect'));

        $driverMock->expects($this->once())
                   ->method('connect')
                   ->will($this->returnValue(null));

        $connection = new ConnectionPortability($params, $driverMock);

        $connection->connect($params);

        $this->assertEquals($portability, $connection->getPortability());
    }
147
}