Completed
Pull Request — master (#3260)
by Michael
147:14 queued 82:09
created

DBAL630Test::configureEmulatedPrepares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\PDOConnection;
7
use Doctrine\DBAL\ParameterType;
8
use PDO;
9
use function assert;
10
use function in_array;
11
12
/**
13
 * @group DBAL-630
14
 */
15
class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
16
{
17
    /**
18
     * @var bool
19
     */
20
    private $running = false;
21
22
    protected function setUp()
23
    {
24
        parent::setUp();
25
26
        $platform = $this->_conn->getDatabasePlatform()->getName();
27
28
        if (!in_array($platform, array('postgresql'))) {
29
            $this->markTestSkipped('Currently restricted to PostgreSQL');
30
        }
31
32
        try {
33
            $this->_conn->exec('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);');
34
            $this->_conn->exec('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
35
        } catch (DBALException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
36
        }
37
        $this->running = true;
38
    }
39
40
    protected function tearDown()
41
    {
42
        if ($this->running) {
43
            $this->configureEmulatedPrepares(false);
44
        }
45
46
        parent::tearDown();
47
    }
48
49
    public function testBooleanConversionSqlLiteral()
50
    {
51
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(false)');
52
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
53
        self::assertNotEmpty($id);
54
55
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
56
57
        self::assertFalse($row['bool_col']);
58
    }
59
60
    public function testBooleanConversionBoolParamRealPrepares()
61
    {
62
        $this->_conn->executeUpdate(
63
            'INSERT INTO dbal630 (bool_col) VALUES(?)',
64
            ['false'],
65
            [ParameterType::BOOLEAN]
66
        );
67
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
68
        self::assertNotEmpty($id);
69
70
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
71
72
        self::assertFalse($row['bool_col']);
73
    }
74
75
    public function testBooleanConversionBoolParamEmulatedPrepares()
76
    {
77
        $this->configureEmulatedPrepares(true);
78
79
        $platform = $this->_conn->getDatabasePlatform();
80
81
        $stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)');
82
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), ParameterType::BOOLEAN);
83
        $stmt->execute();
84
85
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
86
87
        self::assertNotEmpty($id);
88
89
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
90
91
        self::assertFalse($row['bool_col']);
92
    }
93
94
    /**
95
     * @dataProvider booleanTypeConversionWithoutPdoTypeProvider
96
     */
97
    public function testBooleanConversionNullParamEmulatedPrepares(
98
        $statementValue,
99
        $databaseConvertedValue
100
    ) {
101
        $this->configureEmulatedPrepares(true);
102
103
        $platform = $this->_conn->getDatabasePlatform();
104
105
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
106
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
107
        $stmt->execute();
108
109
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
110
111
        self::assertNotEmpty($id);
112
113
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
114
115
        self::assertSame($databaseConvertedValue, $row['bool_col']);
116
    }
117
118
    /**
119
     * @dataProvider booleanTypeConversionUsingBooleanTypeProvider
120
     */
121
    public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInBindValue(
122
        $statementValue,
123
        $databaseConvertedValue
124
    ) {
125
        $this->configureEmulatedPrepares(true);
126
127
        $platform = $this->_conn->getDatabasePlatform();
128
129
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
130
        $stmt->bindValue(
131
            1,
132
            $platform->convertBooleansToDatabaseValue($statementValue),
133
            ParameterType::BOOLEAN
134
        );
135
        $stmt->execute();
136
137
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
138
139
        self::assertNotEmpty($id);
140
141
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
142
143
        self::assertSame($databaseConvertedValue, $row['bool_col']);
144
    }
145
146
    /**
147
     * Boolean conversion mapping provider
148
     * @return array
149
     */
150
    public function booleanTypeConversionUsingBooleanTypeProvider()
151
    {
152
        return array(
153
            // statement value, database converted value result
154
            array(true, true),
155
            array(false, false),
156
            array(null, false)
157
        );
158
    }
159
160
    /**
161
     * Boolean conversion mapping provider
162
     * @return array
163
     */
164
    public function booleanTypeConversionWithoutPdoTypeProvider()
165
    {
166
        return array(
167
            // statement value, database converted value result
168
            array(true, true),
169
            array(false, false),
170
            array(null, null)
171
        );
172
    }
173
174
    private function configureEmulatedPrepares(bool $enabled) : void
175
    {
176
        $wrappedConnection = $this->_conn->getWrappedConnection();
177
        assert($wrappedConnection instanceof PDOConnection);
178
179
        $wrappedConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $enabled);
180
    }
181
}
182