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