1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Crate\Test\DBAL\Functional; |
24
|
|
|
|
25
|
|
|
use Crate\DBAL\Platforms\CratePlatform; |
26
|
|
|
use Crate\DBAL\Types\TimestampType; |
27
|
|
|
use Crate\Test\DBAL\DBALFunctionalTestCase; |
28
|
|
|
use Crate\DBAL\Types\ArrayType; |
29
|
|
|
use Crate\DBAL\Types\MapType; |
30
|
|
|
use Doctrine\DBAL\Types\Type; |
31
|
|
|
use PHPUnit\Framework\TestCase; |
32
|
|
|
|
33
|
|
|
class TypeConversionTestCase extends TestCase { |
34
|
|
|
|
35
|
|
|
private $platform; |
36
|
|
|
|
37
|
|
|
public function setUp() : void |
38
|
|
|
{ |
39
|
|
|
$this->platform = new CratePlatform(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testTimestampType() |
43
|
|
|
{ |
44
|
|
|
$input = new \DateTime("2014-10-21 15:23:38"); |
45
|
|
|
|
46
|
|
|
// datetimetz |
47
|
|
|
$type = Type::getType(Type::DATETIMETZ); |
48
|
|
|
$expected = $input->format('Y-m-d\TH:i:sO'); |
49
|
|
|
$output = $type->convertToDatabaseValue($input, $this->platform); |
50
|
|
|
$this->assertEquals($output, $expected); |
51
|
|
|
$inputRestored = $type->convertToPHPValue($output, $this->platform); |
52
|
|
|
$this->assertEquals($inputRestored, $input); |
53
|
|
|
$inputRestored = $type->convertToPHPValue($input, $this->platform); |
54
|
|
|
$this->assertEquals($inputRestored, $input); |
55
|
|
|
|
56
|
|
|
// datetime |
57
|
|
|
$type = Type::getType(Type::DATETIME); |
58
|
|
|
$expected = $input->format('Y-m-d\TH:i:s'); |
59
|
|
|
$output = $type->convertToDatabaseValue($input, $this->platform); |
60
|
|
|
$this->assertEquals($output, $expected); |
61
|
|
|
$inputRestored = $type->convertToPHPValue($output, $this->platform); |
62
|
|
|
$this->assertEquals($inputRestored, $input); |
63
|
|
|
$inputRestored = $type->convertToPHPValue($input, $this->platform); |
64
|
|
|
$this->assertEquals($inputRestored, $input); |
65
|
|
|
|
66
|
|
|
// date |
67
|
|
|
$type = Type::getType(Type::DATE); |
68
|
|
|
$expected = $input->format('Y-m-d\TH:i:s'); |
69
|
|
|
$output = $type->convertToDatabaseValue($input, $this->platform); |
70
|
|
|
$this->assertEquals($output, $expected); |
71
|
|
|
$inputRestored = $type->convertToPHPValue($output, $this->platform); |
72
|
|
|
$this->assertEquals($inputRestored, $input); |
73
|
|
|
$inputRestored = $type->convertToPHPValue($input, $this->platform); |
74
|
|
|
$this->assertEquals($inputRestored, $input); |
75
|
|
|
|
76
|
|
|
// time |
77
|
|
|
$type = Type::getType(Type::TIME); |
78
|
|
|
$expected = $input->format('Y-m-d\TH:i:s'); |
79
|
|
|
$output = $type->convertToDatabaseValue($input, $this->platform); |
80
|
|
|
$this->assertEquals($output, $expected); |
81
|
|
|
$inputRestored = $type->convertToPHPValue($output, $this->platform); |
82
|
|
|
$this->assertEquals($inputRestored, $input); |
83
|
|
|
$inputRestored = $type->convertToPHPValue($input, $this->platform); |
84
|
|
|
$this->assertEquals($inputRestored, $input); |
85
|
|
|
|
86
|
|
|
// timestamp |
87
|
|
|
$type = Type::getType(TimestampType::NAME); |
88
|
|
|
$expected = $input->format('U')*TimestampType::S_TO_MS; |
89
|
|
|
$output = $type->convertToDatabaseValue($input, $this->platform); |
90
|
|
|
$this->assertEquals($output, $expected); |
91
|
|
|
$inputRestored = $type->convertToPHPValue($output, $this->platform); |
92
|
|
|
$this->assertEquals($inputRestored, $input); |
93
|
|
|
$inputRestored = $type->convertToPHPValue($input, $this->platform); |
94
|
|
|
$this->assertEquals($inputRestored, $input); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testTimestampTypeNull() |
98
|
|
|
{ |
99
|
|
|
$types = array(Type::getType(Type::DATETIMETZ), |
100
|
|
|
Type::getType(Type::DATETIME), |
101
|
|
|
Type::getType(Type::DATE), |
102
|
|
|
Type::getType(Type::TIME), |
103
|
|
|
Type::getType(TimestampType::NAME) |
104
|
|
|
); |
105
|
|
|
foreach ($types as $type) { |
106
|
|
|
// to DB value |
107
|
|
|
$value = $type->convertToDatabaseValue(null, $this->platform); |
108
|
|
|
$this->assertEquals($value, null); |
109
|
|
|
|
110
|
|
|
// to PHP value |
111
|
|
|
$value = $type->convertToPHPValue(null, $this->platform); |
112
|
|
|
$this->assertEquals($value, null); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testMapType() |
117
|
|
|
{ |
118
|
|
|
$type = Type::getType(MapType::NAME); |
119
|
|
|
|
120
|
|
|
// to DB value |
121
|
|
|
$output = $type->convertToDatabaseValue(array('foo'=>'bar'), $this->platform); |
122
|
|
|
$this->assertEquals($output, array('foo'=>'bar')); |
123
|
|
|
|
124
|
|
|
$output = $type->convertToDatabaseValue(array(), $this->platform); |
125
|
|
|
$this->assertEquals($output, array()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testMapTypeNullValue() |
129
|
|
|
{ |
130
|
|
|
$type = Type::getType(MapType::NAME); |
131
|
|
|
|
132
|
|
|
// to DB value |
133
|
|
|
$output = $type->convertToDatabaseValue(null, $this->platform); |
134
|
|
|
$this->assertEquals($output, null); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testMapTypeInvalid() |
138
|
|
|
{ |
139
|
|
|
$type = Type::getType(MapType::NAME); |
140
|
|
|
|
141
|
|
|
// to DB value |
142
|
|
|
$notAMap = array('foo', 'bar'); |
143
|
|
|
$output = $type->convertToDatabaseValue($notAMap, $this->platform); |
144
|
|
|
$this->assertEquals($output, null); |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testArrayType() |
149
|
|
|
{ |
150
|
|
|
$type = Type::getType(ArrayType::NAME); |
151
|
|
|
|
152
|
|
|
// to DB value |
153
|
|
|
$output = $type->convertToDatabaseValue(array('foo', 'bar'), $this->platform); |
154
|
|
|
$this->assertEquals($output, array('foo', 'bar')); |
155
|
|
|
|
156
|
|
|
$output = $type->convertToDatabaseValue(array(), $this->platform); |
157
|
|
|
$this->assertEquals($output, array()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testArrayTypeNullValue() |
161
|
|
|
{ |
162
|
|
|
$type = Type::getType(ArrayType::NAME); |
163
|
|
|
|
164
|
|
|
// to DB value |
165
|
|
|
$output = $type->convertToDatabaseValue(null, $this->platform); |
166
|
|
|
$this->assertEquals($output, null); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testArrayTypeInvalid() |
170
|
|
|
{ |
171
|
|
|
$type = Type::getType(ArrayType::NAME); |
172
|
|
|
|
173
|
|
|
// to DB value |
174
|
|
|
$notAnArray = array('foo'=>'bar'); |
175
|
|
|
$output = $type->convertToDatabaseValue($notAnArray, $this->platform); |
176
|
|
|
$this->assertEquals($output, null); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|