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