Completed
Push — master ( 5dd66e...5b5c2c )
by Marco
114:19 queued 111:15
created

Tests/DBAL/Functional/Ticket/DBAL630Test.php (1 issue)

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