DmsColumnTest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 149
c 1
b 0
f 0
dl 0
loc 328
rs 10
wmc 16

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetType() 0 12 1
A testGetDefault() 0 12 1
A testIsNullable() 0 12 1
A testGetExtra() 0 12 1
A getKeyData() 0 14 1
A typeData() 0 30 1
A getPhpNameData() 0 26 1
A testGetPhpName() 0 12 1
A testGetKey() 0 12 1
A isNullableData() 0 10 1
A testGetName() 0 12 1
A testGetPhpType() 0 13 2
A testGetPhpTypeInvalid() 0 14 1
A testGetPhpDefaultType() 0 12 1
A defaultData() 0 22 1
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Dms;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsColumn;
6
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsException;
7
use PHPUnit\Framework\TestCase;
8
9
class DmsColumnTest extends TestCase
10
{
11
    const COLUMN_NAME = 'name_snake_case';
12
    const COLUMN_NAME_PHP = 'NameSnakeCase';
13
    const COLUMN_TYPE = 'varchar';
14
    const COLUMN_TYPE_INVALID = 'invalid';
15
    const COLUMN_TYPE_PHP = 'string';
16
    const COLUMN_NULLABLE = true;
17
    const COLUMN_KEY = 'PRI';
18
    const COLUMN_DEFAULT = 'string';
19
    const COLUMN_DEFAULT_PHP = 'string';
20
    const COLUMN_EXTRA = 'auto_increment';
21
22
    public function testGetName()
23
    {
24
        $dmsColumn = new DmsColumn(
25
            static::COLUMN_NAME,
26
            static::COLUMN_TYPE,
27
            static::COLUMN_NULLABLE,
28
            static::COLUMN_KEY,
29
            static::COLUMN_DEFAULT,
30
            static::COLUMN_EXTRA
31
        );
32
33
        $this->assertEquals(static::COLUMN_NAME, $dmsColumn->getName());
34
    }
35
36
    public function testGetType()
37
    {
38
        $dmsColumn = new DmsColumn(
39
            static::COLUMN_NAME,
40
            static::COLUMN_TYPE,
41
            static::COLUMN_NULLABLE,
42
            static::COLUMN_KEY,
43
            static::COLUMN_DEFAULT,
44
            static::COLUMN_EXTRA
45
        );
46
47
        $this->assertEquals(static::COLUMN_TYPE, $dmsColumn->getType());
48
    }
49
50
    public function testGetExtra()
51
    {
52
        $dmsColumn = new DmsColumn(
53
            static::COLUMN_NAME,
54
            static::COLUMN_TYPE,
55
            static::COLUMN_NULLABLE,
56
            static::COLUMN_KEY,
57
            static::COLUMN_DEFAULT,
58
            static::COLUMN_EXTRA
59
        );
60
61
        $this->assertEquals(static::COLUMN_EXTRA, $dmsColumn->getExtra());
62
    }
63
64
    /**
65
     * @dataProvider getPhpNameData
66
     *
67
     * @param string $name
68
     * @param string $phpName
69
     */
70
    public function testGetPhpName(string $name, string $phpName)
71
    {
72
        $dmsColumn = new DmsColumn(
73
            $name,
74
            static::COLUMN_TYPE,
75
            static::COLUMN_NULLABLE,
76
            static::COLUMN_KEY,
77
            static::COLUMN_DEFAULT,
78
            static::COLUMN_EXTRA
79
        );
80
81
        $this->assertEquals($phpName, $dmsColumn->getPhpName());
82
    }
83
84
    /**
85
     *
86
     * @return array
87
     */
88
    public function getPhpNameData()
89
    {
90
        return [
91
            [
92
                'name_snake_case',
93
                'NameSnakeCase',
94
            ],
95
            [
96
                'name__snake__case',
97
                'NameSnakeCase',
98
            ],
99
            [
100
                'name-snake-case',
101
                'NameSnakeCase',
102
            ],
103
            [
104
                'name1-snake2-case3',
105
                'Name1Snake2Case3',
106
            ],
107
            [
108
                '1name-2snake-3case',
109
                '1name2snake3case',
110
            ],
111
            [
112
                'name',
113
                'Name',
114
            ],
115
        ];
116
    }
117
118
    public function testGetDefault()
119
    {
120
        $dmsColumn = new DmsColumn(
121
            static::COLUMN_NAME,
122
            static::COLUMN_TYPE,
123
            static::COLUMN_NULLABLE,
124
            static::COLUMN_KEY,
125
            static::COLUMN_DEFAULT,
126
            static::COLUMN_EXTRA
127
        );
128
129
        $this->assertEquals(static::COLUMN_DEFAULT, $dmsColumn->getDefault());
130
    }
131
132
    /**
133
     * @dataProvider defaultData
134
     *
135
     * @param string $default
136
     * @param string $dmsType
137
     * @param string $phpDefaultType
138
     */
139
    public function testGetPhpDefaultType(string $default, string $dmsType, string $phpDefaultType)
140
    {
141
        $dmsColumn = new DmsColumn(
142
            static::COLUMN_NAME,
143
            $dmsType,
144
            static::COLUMN_NULLABLE,
145
            static::COLUMN_KEY,
146
            $default,
147
            static::COLUMN_EXTRA
148
        );
149
150
        $this->assertEquals($phpDefaultType, $dmsColumn->getPhpDefaultType());
151
    }
152
153
    /**
154
     *
155
     * @return array
156
     */
157
    public function defaultData()
158
    {
159
        return [
160
            [
161
                '1',
162
                'int',
163
                'int',
164
            ],
165
            [
166
                '1.1',
167
                'float',
168
                'float',
169
            ],
170
            [
171
                '1',
172
                'varchar',
173
                'string',
174
            ],
175
            [
176
                '',
177
                'varchar',
178
                'null',
179
            ],
180
        ];
181
    }
182
183
    /**
184
     * @dataProvider isNullableData
185
     *
186
     * @param bool $isNullable
187
     * @param bool $expectedIsNullable
188
     */
189
    public function testIsNullable(bool $isNullable, bool $expectedIsNullable)
190
    {
191
        $dmsColumn = new DmsColumn(
192
            static::COLUMN_NAME,
193
            static::COLUMN_TYPE,
194
            $isNullable,
195
            static::COLUMN_KEY,
196
            static::COLUMN_DEFAULT,
197
            static::COLUMN_EXTRA
198
        );
199
200
        $this->assertEquals($expectedIsNullable, $dmsColumn->isNullable());
201
    }
202
203
    /**
204
     *
205
     * @return array
206
     */
207
    public function isNullableData()
208
    {
209
        return [
210
            [
211
                true,
212
                true,
213
            ],
214
            [
215
                false,
216
                false,
217
            ]
218
        ];
219
    }
220
221
    /**
222
     * @dataProvider getKeyData
223
     *
224
     * @param string $key
225
     * @param string $expectedKey
226
     */
227
    public function testGetKey(string $key, string $expectedKey)
228
    {
229
        $dmsColumn = new DmsColumn(
230
            static::COLUMN_NAME,
231
            static::COLUMN_TYPE,
232
            static::COLUMN_NULLABLE,
233
            $key,
234
            static::COLUMN_DEFAULT,
235
            static::COLUMN_EXTRA
236
        );
237
238
        $this->assertEquals($expectedKey, $dmsColumn->getKey());
239
    }
240
241
    /**
242
     *
243
     * @return array
244
     */
245
    public function getKeyData()
246
    {
247
        return [
248
            [
249
                'PRI',
250
                'PRI',
251
            ],
252
            [
253
                'CUSTOM',
254
                'CUSTOM',
255
            ],
256
            [
257
                '',
258
                '',
259
            ],
260
        ];
261
    }
262
263
    /**
264
     * @dataProvider typeData
265
     *
266
     * @param array $dmsTypes
267
     * @param $phpType
268
     */
269
    public function testGetPhpType(array $dmsTypes, string $phpType)
270
    {
271
        foreach ($dmsTypes as $dmsType) {
272
            $dmsColumn = new DmsColumn(
273
                static::COLUMN_NAME,
274
                $dmsType,
275
                static::COLUMN_NULLABLE,
276
                static::COLUMN_KEY,
277
                static::COLUMN_DEFAULT,
278
                static::COLUMN_EXTRA
279
            );
280
281
            $this->assertEquals($phpType, $dmsColumn->getPhpType());
282
        }
283
    }
284
285
    /**
286
     *
287
     * @return array
288
     */
289
    public function typeData()
290
    {
291
        return [
292
            [
293
                [
294
                    'bigint',
295
                    'int',
296
                    'timestamp',
297
                    'tinyint',
298
                ],
299
                'int'
300
            ],
301
            [
302
                [
303
                    'float',
304
                ],
305
                'float',
306
            ],
307
            [
308
                [
309
                    'char',
310
                    'date',
311
                    'datetime',
312
                    'json',
313
                    'longtext',
314
                    'mediumtext',
315
                    'text',
316
                    'varchar',
317
                ],
318
                'string',
319
            ],
320
        ];
321
    }
322
323
    public function testGetPhpTypeInvalid()
324
    {
325
        $this->expectException(DmsException::class);
326
        $this->expectExceptionMessage('Could not determine type for column "name_snake_case" with type "invalid"');
327
328
        (new DmsColumn(
329
            static::COLUMN_NAME,
330
            static::COLUMN_TYPE_INVALID,
331
            static::COLUMN_NULLABLE,
332
            static::COLUMN_KEY,
333
            static::COLUMN_DEFAULT,
334
            static::COLUMN_EXTRA
335
        ))
336
            ->getPhpType()
337
        ;
338
    }
339
}
340