1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Schema; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Db\Expression\Expression; |
9
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
10
|
|
|
use Yiisoft\Db\Schema\Schema; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @group db |
14
|
|
|
* |
15
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
16
|
|
|
*/ |
17
|
|
|
final class ColumnSchemaBuilderTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testAppend(): void |
20
|
|
|
{ |
21
|
|
|
$column = new ColumnSchemaBuilder('string'); |
22
|
|
|
|
23
|
|
|
$this->assertSame('string', (string) $column); |
24
|
|
|
$this->assertSame('string bar', (string) $column->append('bar')); |
25
|
|
|
$this->assertSame('string foo', (string) $column->append('foo')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAppendWithEmptyString(): void |
29
|
|
|
{ |
30
|
|
|
$column = new ColumnSchemaBuilder('string'); |
31
|
|
|
|
32
|
|
|
$this->assertSame('string', (string) $column); |
33
|
|
|
$this->assertSame('string', (string) $column->append('')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testCheck(): void |
37
|
|
|
{ |
38
|
|
|
$column = new ColumnSchemaBuilder('string'); |
39
|
|
|
|
40
|
|
|
$this->assertSame('string', (string) $column); |
41
|
|
|
$this->assertSame('string CHECK (value > 5)', (string) $column->check('value > 5')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCheckWithEmptyString(): void |
45
|
|
|
{ |
46
|
|
|
$column = new ColumnSchemaBuilder('string'); |
47
|
|
|
|
48
|
|
|
$this->assertSame('string', (string) $column); |
49
|
|
|
$this->assertSame('string', (string) $column->check('')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testCheckWithNull(): void |
53
|
|
|
{ |
54
|
|
|
$column = new ColumnSchemaBuilder('string'); |
55
|
|
|
|
56
|
|
|
$this->assertSame('string', (string) $column); |
57
|
|
|
$this->assertSame('string', (string) $column->check(null)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testComment(): void |
61
|
|
|
{ |
62
|
|
|
$column = new ColumnSchemaBuilder('string'); |
63
|
|
|
|
64
|
|
|
$this->assertSame('string', (string) $column); |
65
|
|
|
$this->assertSame('string', (string) $column->comment('comment')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testCommentWithEmptyString(): void |
69
|
|
|
{ |
70
|
|
|
$column = new ColumnSchemaBuilder('string'); |
71
|
|
|
|
72
|
|
|
$this->assertSame('string', (string) $column); |
73
|
|
|
$this->assertSame('string', (string) $column->comment('')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testCommentWithNull(): void |
77
|
|
|
{ |
78
|
|
|
$column = new ColumnSchemaBuilder('string'); |
79
|
|
|
|
80
|
|
|
$this->assertSame('string', (string) $column); |
81
|
|
|
$this->assertSame('string', (string) $column->comment(null)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testDefaultExpression(): void |
85
|
|
|
{ |
86
|
|
|
$column = new ColumnSchemaBuilder('string'); |
87
|
|
|
|
88
|
|
|
$this->assertSame('string', (string) $column); |
89
|
|
|
$this->assertSame("string DEFAULT 'expression'", (string) $column->defaultExpression("'expression'")); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testDefaultExpressionWithEmptyString(): void |
93
|
|
|
{ |
94
|
|
|
$column = new ColumnSchemaBuilder('string'); |
95
|
|
|
|
96
|
|
|
$this->assertSame('string', (string) $column); |
97
|
|
|
$this->assertSame('string DEFAULT ', (string) $column->defaultExpression('')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testDefaultValue(): void |
101
|
|
|
{ |
102
|
|
|
$column = new ColumnSchemaBuilder('string'); |
103
|
|
|
|
104
|
|
|
$this->assertSame('string', (string) $column); |
105
|
|
|
$this->assertSame("string DEFAULT ''value''", (string) $column->defaultValue("'value'")); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testDefaultValueWithEmptyString(): void |
109
|
|
|
{ |
110
|
|
|
$column = new ColumnSchemaBuilder('string'); |
111
|
|
|
|
112
|
|
|
$this->assertSame('string', (string) $column); |
113
|
|
|
$this->assertSame("string DEFAULT ''", (string) $column->defaultValue('')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testDefaultValueWithNull(): void |
117
|
|
|
{ |
118
|
|
|
$column = new ColumnSchemaBuilder('string'); |
119
|
|
|
|
120
|
|
|
$this->assertSame('string', (string) $column); |
121
|
|
|
$this->assertSame('string NULL DEFAULT NULL', (string) $column->defaultValue(null)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testGetAppend(): void |
125
|
|
|
{ |
126
|
|
|
$column = new ColumnSchemaBuilder('string'); |
127
|
|
|
|
128
|
|
|
$this->assertNull($column->getAppend()); |
129
|
|
|
$this->assertSame('bar', $column->append('bar')->getAppend()); |
130
|
|
|
$this->assertSame('bar', $column->getAppend()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetCategoryMap(): void |
134
|
|
|
{ |
135
|
|
|
$column = new ColumnSchemaBuilder('string'); |
136
|
|
|
|
137
|
|
|
$this->assertSame( |
138
|
|
|
[ |
139
|
|
|
'pk' => 'pk', |
140
|
|
|
'upk' => 'pk', |
141
|
|
|
'bigpk' => 'pk', |
142
|
|
|
'ubigpk' => 'pk', |
143
|
|
|
'char' => 'string', |
144
|
|
|
'string' => 'string', |
145
|
|
|
'text' => 'string', |
146
|
|
|
'tinyint' => 'numeric', |
147
|
|
|
'smallint' => 'numeric', |
148
|
|
|
'integer' => 'numeric', |
149
|
|
|
'bigint' => 'numeric', |
150
|
|
|
'float' => 'numeric', |
151
|
|
|
'double' => 'numeric', |
152
|
|
|
'decimal' => 'numeric', |
153
|
|
|
'datetime' => 'time', |
154
|
|
|
'timestamp' => 'time', |
155
|
|
|
'time' => 'time', |
156
|
|
|
'date' => 'time', |
157
|
|
|
'binary' => 'other', |
158
|
|
|
'boolean' => 'numeric', |
159
|
|
|
'money' => 'numeric', |
160
|
|
|
], |
161
|
|
|
$column->getCategoryMap(), |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testGetCheck(): void |
166
|
|
|
{ |
167
|
|
|
$column = new ColumnSchemaBuilder('string'); |
168
|
|
|
|
169
|
|
|
$this->assertNull($column->getCheck()); |
170
|
|
|
$this->assertSame('value > 5', $column->check('value > 5')->getCheck()); |
171
|
|
|
$this->assertSame('value > 5', $column->getCheck()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testGetComment(): void |
175
|
|
|
{ |
176
|
|
|
$column = new ColumnSchemaBuilder('string'); |
177
|
|
|
|
178
|
|
|
$this->assertNull($column->getComment()); |
179
|
|
|
$this->assertSame('comment', $column->comment('comment')->getComment()); |
180
|
|
|
$this->assertSame('comment', $column->getComment()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetDefault(): void |
184
|
|
|
{ |
185
|
|
|
$column = new ColumnSchemaBuilder('string'); |
186
|
|
|
|
187
|
|
|
$this->assertNull($column->getDefault()); |
188
|
|
|
$this->assertSame("'value'", $column->defaultValue("'value'")->getDefault()); |
189
|
|
|
$this->assertSame("'value'", $column->getDefault()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testGetDefaultExpression(): void |
193
|
|
|
{ |
194
|
|
|
$column = new ColumnSchemaBuilder('string'); |
195
|
|
|
|
196
|
|
|
$this->assertNull($column->getDefault()); |
197
|
|
|
$this->assertInstanceOf(Expression::class, $column->defaultExpression("'expression'")->getDefault()); |
198
|
|
|
$this->assertInstanceOf(Expression::class, $column->getDefault()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testGetLength(): void |
202
|
|
|
{ |
203
|
|
|
$column = new ColumnSchemaBuilder('string', 10); |
204
|
|
|
|
205
|
|
|
$this->assertSame(10, $column->getLength()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testIsNotNull(): void |
209
|
|
|
{ |
210
|
|
|
$column = new ColumnSchemaBuilder('string'); |
211
|
|
|
|
212
|
|
|
$this->assertNull($column->isNotNull()); |
213
|
|
|
$this->assertTrue($column->notNull()->isNotNull()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testIsUnique(): void |
217
|
|
|
{ |
218
|
|
|
$column = new ColumnSchemaBuilder('string'); |
219
|
|
|
|
220
|
|
|
$this->assertFalse($column->isUnique()); |
221
|
|
|
$this->assertTrue($column->unique()->isUnique()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testIsUnsigned(): void |
225
|
|
|
{ |
226
|
|
|
$column = new ColumnSchemaBuilder('pk'); |
227
|
|
|
|
228
|
|
|
$this->assertFalse($column->isUnsigned()); |
229
|
|
|
$this->assertTrue($column->unsigned()->isUnsigned()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testLengthWithArray(): void |
233
|
|
|
{ |
234
|
|
|
$column = new ColumnSchemaBuilder('integer', [10, 2]); |
235
|
|
|
|
236
|
|
|
$this->assertSame('integer(10,2)', (string) $column); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testNotnull(): void |
240
|
|
|
{ |
241
|
|
|
$column = new ColumnSchemaBuilder('string'); |
242
|
|
|
|
243
|
|
|
$this->assertSame('string', (string) $column); |
244
|
|
|
$this->assertSame('string NOT NULL', (string) $column->notNull()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testNull(): void |
248
|
|
|
{ |
249
|
|
|
$column = new ColumnSchemaBuilder('string'); |
250
|
|
|
|
251
|
|
|
$this->assertSame('string', (string) $column); |
252
|
|
|
$this->assertSame('string NULL DEFAULT NULL', (string) $column->null()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testUnique(): void |
256
|
|
|
{ |
257
|
|
|
$column = new ColumnSchemaBuilder('string'); |
258
|
|
|
|
259
|
|
|
$this->assertSame('string', (string) $column); |
260
|
|
|
$this->assertSame('string UNIQUE', (string) $column->unique()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testUnsignedTypePk(): void |
264
|
|
|
{ |
265
|
|
|
$column = new ColumnSchemaBuilder(Schema::TYPE_PK); |
266
|
|
|
|
267
|
|
|
$this->assertSame('pk', (string) $column); |
268
|
|
|
$this->assertSame('upk', (string) $column->unsigned()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function testUnsignedTypeUbigPk(): void |
272
|
|
|
{ |
273
|
|
|
$column = new ColumnSchemaBuilder(Schema::TYPE_BIGPK); |
274
|
|
|
|
275
|
|
|
$this->assertSame('bigpk', (string) $column); |
276
|
|
|
$this->assertSame('ubigpk', (string) $column->unsigned()); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|