Passed
Pull Request — master (#427)
by Wilmer
03:10 queued 10s
created

testDefaultExpressionWithEmptyString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
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 testComment(): void
53
    {
54
        $column = new ColumnSchemaBuilder('string');
55
56
        $this->assertSame('string', (string) $column);
57
        $this->assertSame('string', (string) $column->comment('comment'));
58
    }
59
60
    public function testCommentWithEmptyString(): void
61
    {
62
        $column = new ColumnSchemaBuilder('string');
63
64
        $this->assertSame('string', (string) $column);
65
        $this->assertSame('string', (string) $column->comment(''));
66
    }
67
68
    public function testDefaultExpression(): void
69
    {
70
        $column = new ColumnSchemaBuilder('string');
71
72
        $this->assertSame('string', (string) $column);
73
        $this->assertSame("string DEFAULT 'expression'", (string) $column->defaultExpression("'expression'"));
74
    }
75
76
    public function testDefaultExpressionWithEmptyString(): void
77
    {
78
        $column = new ColumnSchemaBuilder('string');
79
80
        $this->assertSame('string', (string) $column);
81
        $this->assertSame("string DEFAULT ''", (string) $column->defaultExpression(''));
82
    }
83
84
    public function testDefaultValue(): void
85
    {
86
        $column = new ColumnSchemaBuilder('string');
87
88
        $this->assertSame('string', (string) $column);
89
        $this->assertSame("string DEFAULT ''value''", (string) $column->defaultValue("'value'"));
90
    }
91
92
    public function testDefaultValueWithEmptyString(): void
93
    {
94
        $column = new ColumnSchemaBuilder('string');
95
96
        $this->assertSame('string', (string) $column);
97
        $this->assertSame("string DEFAULT ''", (string) $column->defaultValue(''));
98
    }
99
100
    public function testDefaultValueWithNull(): void
101
    {
102
        $column = new ColumnSchemaBuilder('string');
103
104
        $this->assertSame('string', (string) $column);
105
        $this->assertSame('string NULL DEFAULT NULL', (string) $column->defaultValue(null));
106
    }
107
108
    public function testGetAppend(): void
109
    {
110
        $column = new ColumnSchemaBuilder('string');
111
112
        $this->assertSame('', $column->getAppend());
113
        $this->assertSame('bar', $column->append('bar')->getAppend());
114
        $this->assertSame('bar', $column->getAppend());
115
    }
116
117
    public function testGetCategoryMap(): void
118
    {
119
        $column = new ColumnSchemaBuilder('string');
120
121
        $this->assertSame(
122
            [
123
                'pk' => 'pk',
124
                'upk' => 'pk',
125
                'bigpk' => 'pk',
126
                'ubigpk' => 'pk',
127
                'char' => 'string',
128
                'string' => 'string',
129
                'text' => 'string',
130
                'tinyint' => 'numeric',
131
                'smallint' => 'numeric',
132
                'integer' => 'numeric',
133
                'bigint' => 'numeric',
134
                'float' => 'numeric',
135
                'double' => 'numeric',
136
                'decimal' => 'numeric',
137
                'datetime' => 'time',
138
                'timestamp' => 'time',
139
                'time' => 'time',
140
                'date' => 'time',
141
                'binary' => 'other',
142
                'boolean' => 'numeric',
143
                'money' => 'numeric',
144
            ],
145
            $column->getCategoryMap(),
146
        );
147
    }
148
149
    public function testGetCheck(): void
150
    {
151
        $column = new ColumnSchemaBuilder('string');
152
153
        $this->assertSame('', $column->getCheck());
154
        $this->assertSame('value > 5', $column->check('value > 5')->getCheck());
155
        $this->assertSame('value > 5', $column->getCheck());
156
    }
157
158
    public function testGetComment(): void
159
    {
160
        $column = new ColumnSchemaBuilder('string');
161
162
        $this->assertSame('', $column->getComment());
163
        $this->assertSame('comment', $column->comment('comment')->getComment());
164
        $this->assertSame('comment', $column->getComment());
165
    }
166
167
    public function testGetDefault(): void
168
    {
169
        $column = new ColumnSchemaBuilder('string');
170
171
        $this->assertSame(null, $column->getDefault());
172
        $this->assertSame("'value'", $column->defaultValue("'value'")->getDefault());
173
        $this->assertSame("'value'", $column->getDefault());
174
    }
175
176
    public function testGetDefaultExpression(): void
177
    {
178
        $column = new ColumnSchemaBuilder('string');
179
180
        $this->assertSame(null, $column->getDefault());
181
        $this->assertInstanceOf(Expression::class, $column->defaultExpression("'expression'")->getDefault());
182
        $this->assertInstanceOf(Expression::class, $column->getDefault());
183
    }
184
185
    public function testGetLength(): void
186
    {
187
        $column = new ColumnSchemaBuilder('string', 10);
188
189
        $this->assertSame(10, $column->getLength());
190
    }
191
192
    public function testIsNotNull(): void
193
    {
194
        $column = new ColumnSchemaBuilder('string');
195
196
        $this->assertNull($column->isNotNull());
197
        $this->assertTrue($column->notNull()->isNotNull());
198
    }
199
200
    public function testIsUnique(): void
201
    {
202
        $column = new ColumnSchemaBuilder('string');
203
204
        $this->assertFalse($column->isUnique());
205
        $this->assertTrue($column->unique()->isUnique());
206
    }
207
208
    public function testIsUnsigned(): void
209
    {
210
        $column = new ColumnSchemaBuilder('pk');
211
212
        $this->assertFalse($column->isUnsigned());
213
        $this->assertTrue($column->unsigned()->isUnsigned());
214
    }
215
216
    public function testLengthWithArray(): void
217
    {
218
        $column = new ColumnSchemaBuilder('integer', [10, 2]);
219
220
        $this->assertSame('integer(10,2)', (string) $column);
221
    }
222
223
    public function testNotnull(): void
224
    {
225
        $column = new ColumnSchemaBuilder('string');
226
227
        $this->assertSame('string', (string) $column);
228
        $this->assertSame('string NOT NULL', (string) $column->notNull());
229
    }
230
231
    public function testNull(): void
232
    {
233
        $column = new ColumnSchemaBuilder('string');
234
235
        $this->assertSame('string', (string) $column);
236
        $this->assertSame('string NULL DEFAULT NULL', (string) $column->null());
237
    }
238
239
    public function testUnique(): void
240
    {
241
        $column = new ColumnSchemaBuilder('string');
242
243
        $this->assertSame('string', (string) $column);
244
        $this->assertSame('string UNIQUE', (string) $column->unique());
245
    }
246
247
    public function testUnsignedTypePk(): void
248
    {
249
        $column = new ColumnSchemaBuilder(Schema::TYPE_PK);
250
251
        $this->assertSame('pk', (string) $column);
252
        $this->assertSame('upk', (string) $column->unsigned());
253
    }
254
255
    public function testUnsignedTypeUbigPk(): void
256
    {
257
        $column = new ColumnSchemaBuilder(Schema::TYPE_BIGPK);
258
259
        $this->assertSame('bigpk', (string) $column);
260
        $this->assertSame('ubigpk', (string) $column->unsigned());
261
    }
262
}
263