DBAL630Test.php 5.07 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Functional\Ticket;

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\ParameterType;
7 8 9 10 11 12 13
use PDO;

/**
 * @group DBAL-630
 */
class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
{
Gabriel Caruso's avatar
Gabriel Caruso committed
14 15 16
    /**
     * @var bool
     */
17 18 19 20 21 22 23 24 25 26 27 28
    private $running = false;

    protected function setUp()
    {
        parent::setUp();

        $platform = $this->_conn->getDatabasePlatform()->getName();

        if (!in_array($platform, array('postgresql'))) {
            $this->markTestSkipped('Currently restricted to PostgreSQL');
        }

29 30
        try {
            $this->_conn->exec('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);');
31
            $this->_conn->exec('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
32 33
        } catch (DBALException $e) {
        }
34 35 36 37 38 39 40 41
        $this->running = true;
    }

    protected function tearDown()
    {
        if ($this->running) {
            $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        }
42 43

        parent::tearDown();
44 45 46 47 48 49
    }

    public function testBooleanConversionSqlLiteral()
    {
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(false)');
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
50
        self::assertNotEmpty($id);
51 52 53

        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));

54
        self::assertFalse($row['bool_col']);
55 56 57 58
    }

    public function testBooleanConversionBoolParamRealPrepares()
    {
59 60
        $this->_conn->executeUpdate(
            'INSERT INTO dbal630 (bool_col) VALUES(?)',
Sergei Morozov's avatar
Sergei Morozov committed
61 62
            ['false'],
            [ParameterType::BOOLEAN]
63
        );
64
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
65
        self::assertNotEmpty($id);
66 67 68

        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));

69
        self::assertFalse($row['bool_col']);
70 71 72 73 74
    }

    public function testBooleanConversionBoolParamEmulatedPrepares()
    {
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
75

76
        $platform = $this->_conn->getDatabasePlatform();
77 78

        $stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)');
79
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), ParameterType::BOOLEAN);
80 81 82 83
        $stmt->execute();

        $id = $this->_conn->lastInsertId('dbal630_id_seq');

84
        self::assertNotEmpty($id);
85 86 87

        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));

88
        self::assertFalse($row['bool_col']);
89
    }
90

91 92 93 94 95 96 97
    /**
     * @dataProvider booleanTypeConversionWithoutPdoTypeProvider
     */
    public function testBooleanConversionNullParamEmulatedPrepares(
        $statementValue,
        $databaseConvertedValue
    ) {
98 99 100 101 102
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

        $platform = $this->_conn->getDatabasePlatform();

        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
103
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
104 105
        $stmt->execute();

106 107
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');

108
        self::assertNotEmpty($id);
109 110 111

        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));

112
        self::assertSame($databaseConvertedValue, $row['bool_col']);
113 114 115 116 117 118 119 120 121 122 123 124 125 126
    }

    /**
     * @dataProvider booleanTypeConversionUsingBooleanTypeProvider
     */
    public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInBindValue(
        $statementValue,
        $databaseConvertedValue
    ) {
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

        $platform = $this->_conn->getDatabasePlatform();

        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
127 128 129 130 131
        $stmt->bindValue(
            1,
            $platform->convertBooleansToDatabaseValue($statementValue),
            ParameterType::BOOLEAN
        );
132 133 134
        $stmt->execute();

        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
135

136
        self::assertNotEmpty($id);
137 138 139

        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));

140
        self::assertSame($databaseConvertedValue, $row['bool_col']);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    }

    /**
     * Boolean conversion mapping provider
     * @return array
     */
    public function booleanTypeConversionUsingBooleanTypeProvider()
    {
        return array(
            // statement value, database converted value result
            array(true, true),
            array(false, false),
            array(null, false)
        );
    }

    /**
     * Boolean conversion mapping provider
     * @return array
     */
    public function booleanTypeConversionWithoutPdoTypeProvider()
    {
        return array(
            // statement value, database converted value result
            array(true, true),
            array(false, false),
            array(null, null)
        );
169
    }
170
}