1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver; |
7
|
|
|
use Doctrine\DBAL\Schema\Table; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
10
|
|
|
use stdClass; |
11
|
|
|
use function str_repeat; |
12
|
|
|
|
13
|
|
|
class TypeConversionTest extends DbalFunctionalTestCase |
14
|
|
|
{ |
15
|
|
|
/** @var int */ |
16
|
|
|
static private $typeCounter = 0; |
17
|
|
|
|
18
|
|
|
protected function setUp() : void |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$table = new Table('type_conversion'); |
23
|
|
|
$table->addColumn('id', 'integer', ['notnull' => false]); |
24
|
|
|
$table->addColumn('test_string', 'string', ['notnull' => false]); |
25
|
|
|
$table->addColumn('test_boolean', 'boolean', ['notnull' => false]); |
26
|
|
|
$table->addColumn('test_bigint', 'bigint', ['notnull' => false]); |
27
|
|
|
$table->addColumn('test_smallint', 'bigint', ['notnull' => false]); |
28
|
|
|
$table->addColumn('test_datetime', 'datetime', ['notnull' => false]); |
29
|
|
|
$table->addColumn('test_datetimetz', 'datetimetz', ['notnull' => false]); |
30
|
|
|
$table->addColumn('test_date', 'date', ['notnull' => false]); |
31
|
|
|
$table->addColumn('test_time', 'time', ['notnull' => false]); |
32
|
|
|
$table->addColumn('test_text', 'text', ['notnull' => false]); |
33
|
|
|
$table->addColumn('test_array', 'array', ['notnull' => false]); |
34
|
|
|
$table->addColumn('test_json_array', 'json_array', ['notnull' => false]); |
35
|
|
|
$table->addColumn('test_object', 'object', ['notnull' => false]); |
36
|
|
|
$table->addColumn('test_float', 'float', ['notnull' => false]); |
37
|
|
|
$table->addColumn('test_decimal', 'decimal', ['notnull' => false, 'scale' => 2, 'precision' => 10]); |
38
|
|
|
$table->setPrimaryKey(['id']); |
39
|
|
|
|
40
|
|
|
$this->connection |
41
|
|
|
->getSchemaManager() |
42
|
|
|
->dropAndCreateTable($table); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider booleanProvider |
47
|
|
|
*/ |
48
|
|
|
public function testIdempotentConversionToBoolean(string $type, $originalValue) : void |
49
|
|
|
{ |
50
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
51
|
|
|
|
52
|
|
|
self::assertIsBool($dbValue); |
53
|
|
|
self::assertEquals($originalValue, $dbValue); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return mixed[][] |
58
|
|
|
*/ |
59
|
|
|
public static function booleanProvider() : iterable |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'true' => ['boolean', true], |
63
|
|
|
'false' => ['boolean', false], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @dataProvider integerProvider |
69
|
|
|
*/ |
70
|
|
|
public function testIdempotentConversionToInteger(string $type, $originalValue) : void |
71
|
|
|
{ |
72
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
73
|
|
|
|
74
|
|
|
self::assertIsInt($dbValue); |
75
|
|
|
self::assertEquals($originalValue, $dbValue); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed[][] |
80
|
|
|
*/ |
81
|
|
|
public static function integerProvider() : iterable |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'smallint' => ['smallint', 123], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider floatProvider |
90
|
|
|
*/ |
91
|
|
|
public function testIdempotentConversionToFloat(string $type, $originalValue) : void |
92
|
|
|
{ |
93
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
94
|
|
|
|
95
|
|
|
self::assertIsFloat($dbValue); |
96
|
|
|
self::assertEquals($originalValue, $dbValue); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed[][] |
101
|
|
|
*/ |
102
|
|
|
public static function floatProvider() : iterable |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'float' => ['float', 1.5], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider toStringProvider |
111
|
|
|
*/ |
112
|
|
|
public function testIdempotentConversionToString(string $type, $originalValue) : void |
113
|
|
|
{ |
114
|
|
|
if ($type === 'text' && $this->connection->getDriver() instanceof PDOOracleDriver) { |
115
|
|
|
// inserting BLOBs as streams on Oracle requires Oracle-specific SQL syntax which is currently not supported |
116
|
|
|
// see http://php.net/manual/en/pdo.lobs.php#example-1035 |
117
|
|
|
$this->markTestSkipped('DBAL doesn\'t support storing LOBs represented as streams using PDO_OCI'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
121
|
|
|
|
122
|
|
|
self::assertIsString($dbValue); |
123
|
|
|
self::assertEquals($originalValue, $dbValue); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return mixed[][] |
128
|
|
|
*/ |
129
|
|
|
public static function toStringProvider() : iterable |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
'string' => ['string', 'ABCDEFGabcdefg'], |
133
|
|
|
'bigint' => ['bigint', 12345678], |
134
|
|
|
'text' => ['text', str_repeat('foo ', 1000)], |
135
|
|
|
'decimal' => ['decimal', 1.55], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @dataProvider toArrayProvider |
141
|
|
|
*/ |
142
|
|
|
public function testIdempotentConversionToArray(string $type, $originalValue) : void |
143
|
|
|
{ |
144
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
145
|
|
|
|
146
|
|
|
self::assertIsArray($dbValue); |
147
|
|
|
self::assertEquals($originalValue, $dbValue); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return mixed[][] |
152
|
|
|
*/ |
153
|
|
|
public static function toArrayProvider() : iterable |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
'array' => ['array', ['foo' => 'bar']], |
157
|
|
|
'json_array' => ['json_array', ['foo' => 'bar']], |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @dataProvider toObjectProvider |
163
|
|
|
*/ |
164
|
|
|
public function testIdempotentConversionToObject(string $type, $originalValue) : void |
165
|
|
|
{ |
166
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
167
|
|
|
|
168
|
|
|
self::assertIsObject($dbValue); |
169
|
|
|
self::assertEquals($originalValue, $dbValue); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return mixed[][] |
174
|
|
|
*/ |
175
|
|
|
public static function toObjectProvider() : iterable |
176
|
|
|
{ |
177
|
|
|
$obj = new stdClass(); |
178
|
|
|
$obj->foo = 'bar'; |
179
|
|
|
$obj->bar = 'baz'; |
180
|
|
|
|
181
|
|
|
return [ |
182
|
|
|
'object' => ['object', $obj], |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @dataProvider toDateTimeProvider |
188
|
|
|
*/ |
189
|
|
|
public function testIdempotentConversionToDateTime(string $type, DateTime $originalValue) : void |
190
|
|
|
{ |
191
|
|
|
$dbValue = $this->processValue($type, $originalValue); |
192
|
|
|
|
193
|
|
|
self::assertInstanceOf(DateTime::class, $dbValue); |
194
|
|
|
|
195
|
|
|
if ($type === 'datetimetz') { |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
self::assertEquals($originalValue, $dbValue); |
200
|
|
|
self::assertEquals( |
201
|
|
|
$originalValue->getTimezone(), |
202
|
|
|
$dbValue->getTimezone() |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return mixed[][] |
208
|
|
|
*/ |
209
|
|
|
public static function toDateTimeProvider() : iterable |
210
|
|
|
{ |
211
|
|
|
return [ |
212
|
|
|
'datetime' => ['datetime', new DateTime('2010-04-05 10:10:10')], |
213
|
|
|
'datetimetz' => ['datetimetz', new DateTime('2010-04-05 10:10:10')], |
214
|
|
|
'date' => ['date', new DateTime('2010-04-05')], |
215
|
|
|
'time' => ['time', new DateTime('1970-01-01 10:10:10')], |
216
|
|
|
]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param mixed $originalValue |
221
|
|
|
*/ |
222
|
|
|
private function processValue(string $type, $originalValue) |
223
|
|
|
{ |
224
|
|
|
$columnName = 'test_' . $type; |
225
|
|
|
$typeInstance = Type::getType($type); |
226
|
|
|
$insertionValue = $typeInstance->convertToDatabaseValue($originalValue, $this->connection->getDatabasePlatform()); |
227
|
|
|
|
228
|
|
|
$this->connection->insert('type_conversion', ['id' => ++self::$typeCounter, $columnName => $insertionValue]); |
229
|
|
|
|
230
|
|
|
$sql = 'SELECT ' . $columnName . ' FROM type_conversion WHERE id = ' . self::$typeCounter; |
231
|
|
|
|
232
|
|
|
return $typeInstance->convertToPHPValue( |
233
|
|
|
$this->connection->fetchColumn($sql), |
234
|
|
|
$this->connection->getDatabasePlatform() |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|