Passed
Pull Request — master (#428)
by Wilmer
02:55
created

testPhpTypecastWithStringIntegerValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Schema;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Db\Command\ParamInterface;
10
use Yiisoft\Db\Schema\ColumnSchema;
11
use Yiisoft\Db\Schema\Schema;
12
13
/**
14
 * @group db
15
 *
16
 * @psalm-suppress PropertyNotSetInConstructor
17
 */
18
final class ColumnSchemaTest extends TestCase
19
{
20
    public function testAllowNull(): void
21
    {
22
        $column = new ColumnSchema();
23
24
        $this->assertFalse($column->isAllowNull());
25
26
        $column->allowNull(true);
27
28
        $this->assertTrue($column->isAllowNull());
29
30
        $column->allowNull(false);
31
32
        $this->assertFalse($column->isAllowNull());
33
    }
34
35
    public function testAutoIncrement(): void
36
    {
37
        $column = new ColumnSchema();
38
39
        $this->assertFalse($column->isAutoIncrement());
40
41
        $column->autoIncrement(true);
42
43
        $this->assertTrue($column->isAutoIncrement());
44
45
        $column->autoIncrement(false);
46
47
        $this->assertFalse($column->isAutoIncrement());
48
    }
49
50
    public function testComment(): void
51
    {
52
        $column = new ColumnSchema();
53
54
        $this->assertNull($column->getComment());
55
56
        $column->comment('test');
57
58
        $this->assertSame('test', $column->getComment());
59
60
        $column->comment(null);
61
62
        $this->assertNull($column->getComment());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $column->getComment() targeting Yiisoft\Db\Schema\ColumnSchema::getComment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
    }
64
65
    public function testComputed(): void
66
    {
67
        $column = new ColumnSchema();
68
69
        $this->assertFalse($column->isComputed());
70
71
        $column->computed(true);
72
73
        $this->assertTrue($column->isComputed());
74
75
        $column->computed(false);
76
77
        $this->assertFalse($column->isComputed());
78
    }
79
80
    public function testDbType(): void
81
    {
82
        $column = new ColumnSchema();
83
84
        $this->assertSame('', $column->getDbType());
85
86
        $column->dbType('test');
87
88
        $this->assertSame('test', $column->getDbType());
89
90
        $column->dbType('');
91
92
        $this->assertSame('', $column->getDbType());
93
    }
94
95
    public function testDbTypecast(): void
96
    {
97
        $column = new ColumnSchema();
98
99
        $this->assertNull($column->dbTypecast(''));
100
    }
101
102
    public function testDefaultValue(): void
103
    {
104
        $column = new ColumnSchema();
105
106
        $this->assertNull($column->getDefaultValue());
107
108
        $column->defaultValue('test');
109
110
        $this->assertSame('test', $column->getDefaultValue());
111
112
        $column->defaultValue(null);
113
114
        $this->assertNull($column->getDefaultValue());
115
    }
116
117
    public function testEnumValues(): void
118
    {
119
        $column = new ColumnSchema();
120
121
        $this->assertNull($column->getEnumValues());
122
123
        $column->enumValues(['positive', 'negative']);
124
125
        $this->assertSame(['positive', 'negative'], $column->getEnumValues());
126
127
        $column->enumValues([]);
128
129
        $this->assertSame([], $column->getEnumValues());
130
    }
131
132
    public function testExtra(): void
133
    {
134
        $column = new ColumnSchema();
135
136
        $this->assertNull($column->getExtra());
137
138
        $column->extra('test');
139
140
        $this->assertSame('test', $column->getExtra());
141
142
        $column->extra('');
143
144
        $this->assertSame('', $column->getExtra());
145
    }
146
147
    public function testName(): void
148
    {
149
        $column = new ColumnSchema();
150
151
        $this->assertSame('', $column->getName());
152
153
        $column->name('test');
154
155
        $this->assertSame('test', $column->getName());
156
157
        $column->name('');
158
159
        $this->assertSame('', $column->getName());
160
    }
161
162
    public function testPhpType(): void
163
    {
164
        $column = new ColumnSchema();
165
166
        $this->assertNull($column->getPhpType());
167
168
        $column->phpType(Schema::PHP_TYPE_STRING);
169
170
        $this->assertSame(Schema::PHP_TYPE_STRING, $column->getPhpType());
171
172
        $column->phpType(null);
173
174
        $this->assertNull($column->getPhpType());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $column->getPhpType() targeting Yiisoft\Db\Schema\ColumnSchema::getPhpType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175
    }
176
177
    public function testPhpTypecast(): void
178
    {
179
        $column = new ColumnSchema();
180
181
        $column->phpType(Schema::PHP_TYPE_STRING);
182
183
        $this->assertSame('test', $column->phpTypecast('test'));
184
    }
185
186
    public function testPhpTypecastWithBoolean(): void
187
    {
188
        $column = new ColumnSchema();
189
190
        $column->phpType(Schema::PHP_TYPE_BOOLEAN);
191
192
        $this->assertTrue($column->phpTypecast(1));
193
    }
194
195
    public function testPhpTypecastWithDouble(): void
196
    {
197
        $column = new ColumnSchema();
198
199
        $column->phpType(Schema::PHP_TYPE_DOUBLE);
200
201
        $this->assertSame(1.2, $column->phpTypecast('1.2'));
202
    }
203
204
    public function testPhpTypecastWithInteger(): void
205
    {
206
        $column = new ColumnSchema();
207
208
        $column->phpType(Schema::PHP_TYPE_INTEGER);
209
210
        $this->assertSame(1, $column->phpTypecast('1'));
211
    }
212
213
    public function testPhpTypecastWithStringBooleanValue(): void
214
    {
215
        $column = new ColumnSchema();
216
217
        $column->phpType(Schema::PHP_TYPE_STRING);
218
219
        $this->assertSame('1', $column->phpTypecast(true));
220
    }
221
222
    public function testPhpTypecastWithStringFloatValue(): void
223
    {
224
        $column = new ColumnSchema();
225
226
        $column->phpType(Schema::PHP_TYPE_STRING);
227
228
        $this->assertSame('1.1', $column->phpTypecast(1.1));
229
    }
230
231
    public function testPhpTypecastWithStringIntegerValue(): void
232
    {
233
        $column = new ColumnSchema();
234
235
        $column->phpType(Schema::PHP_TYPE_STRING);
236
237
        $this->assertSame('1', $column->phpTypecast(1));
238
    }
239
240
    public function testPhpTypecastWithStringNullValue(): void
241
    {
242
        $column = new ColumnSchema();
243
244
        $column->phpType(Schema::PHP_TYPE_STRING);
245
246
        $this->assertNull($column->phpTypecast(null));
247
    }
248
249
    public function testPhpTypecastWithStringParamValue(): void
250
    {
251
        $column = new ColumnSchema();
252
253
        $column->phpType(Schema::PHP_TYPE_STRING);
254
255
        $this->assertInstanceOf(ParamInterface::class, $column->phpTypecast(['test', PDO::PARAM_STR]));
256
        $this->assertSame('test', $column->phpTypecast(['test', PDO::PARAM_STR])->getValue());
257
        $this->assertSame(PDO::PARAM_STR, $column->phpTypecast(['test', PDO::PARAM_STR])->getType());
258
    }
259
260
    public function testPhpTypecastWithStringResourceValue(): void
261
    {
262
        $column = new ColumnSchema();
263
264
        $column->phpType(Schema::PHP_TYPE_STRING);
265
266
        $this->assertIsResource($column->phpTypecast(fopen('php://memory', 'rb')));
267
    }
268
269
    public function testPresicion(): void
270
    {
271
        $column = new ColumnSchema();
272
273
        $this->assertNull($column->getPrecision());
274
275
        $column->precision(10);
276
277
        $this->assertSame(10, $column->getPrecision());
278
279
        $column->precision(0);
280
281
        $this->assertSame(0, $column->getPrecision());
282
    }
283
284
    public function testPrimaryKey(): void
285
    {
286
        $column = new ColumnSchema();
287
288
        $this->assertFalse($column->isPrimaryKey());
289
290
        $column->primaryKey(true);
291
292
        $this->assertTrue($column->isPrimaryKey());
293
294
        $column->primaryKey(false);
295
296
        $this->assertFalse($column->isPrimaryKey());
297
    }
298
299
    public function testScale(): void
300
    {
301
        $column = new ColumnSchema();
302
303
        $this->assertNull($column->getScale());
304
305
        $column->scale(10);
306
307
        $this->assertSame(10, $column->getScale());
308
309
        $column->scale(0);
310
311
        $this->assertSame(0, $column->getScale());
312
    }
313
314
    public function testSize(): void
315
    {
316
        $column = new ColumnSchema();
317
318
        $this->assertNull($column->getSize());
319
320
        $column->size(10);
321
322
        $this->assertSame(10, $column->getSize());
323
324
        $column->size(0);
325
326
        $this->assertSame(0, $column->getSize());
327
    }
328
329
    public function testType(): void
330
    {
331
        $column = new ColumnSchema();
332
333
        $this->assertSame('', $column->getType());
334
335
        $column->type('test');
336
337
        $this->assertSame('test', $column->getType());
338
339
        $column->type('');
340
341
        $this->assertSame('', $column->getType());
342
    }
343
344
    public function testUnsigned(): void
345
    {
346
        $column = new ColumnSchema();
347
348
        $this->assertFalse($column->isUnsigned());
349
350
        $column->unsigned(true);
351
352
        $this->assertTrue($column->isUnsigned());
353
354
        $column->unsigned(false);
355
356
        $this->assertFalse($column->isUnsigned());
357
    }
358
}
359