GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 4f73cb...936318 )
by Burhan
02:12
created

ColumnDefinitionTest::parseInvalidDefaultValuesProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Graze\Morphism\Parse;
4
5
class ColumnDefinitionTest extends \Graze\Morphism\Test\Parse\TestCase
6
{
7
    /**
8
     * @dataProvider parseDatatypesProvider
9
     * @param string $text
10
     * @param string $expected
11
     */
12 View Code Duplication
    public function testParseDatatypes($text, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
    {
14
        $stream = $this->makeStream($text);
15
        $collation = new CollationInfo();
16
17
        $column = new ColumnDefinition();
18
        $column->parse($stream);
19
20
        $this->assertSame($expected, $column->toString($collation));
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    public function parseDatatypesProvider()
27
    {
28
        return [
29
            ["x int",                       "`x` int(11) DEFAULT NULL"],
30
            ["x int signed",                "`x` int(11) DEFAULT NULL"],
31
            ["x int unsigned",              "`x` int(10) unsigned DEFAULT NULL"],
32
            ["x int(5)",                    "`x` int(5) DEFAULT NULL"],
33
            ["x int(5) default null",       "`x` int(5) DEFAULT NULL"],
34
            ["x int not null default 1",    "`x` int(11) NOT NULL DEFAULT '1'"],
35
            ["x int zerofill",              "`x` int(10) unsigned zerofill DEFAULT NULL"],
36
            ["x int zerofill unsigned",     "`x` int(10) unsigned zerofill DEFAULT NULL"],
37
            ["x int unsigned zerofill",     "`x` int(10) unsigned zerofill DEFAULT NULL"],
38
            ["x int default 1",             "`x` int(11) DEFAULT '1'"],
39
            ["x int(4) zerofill default 1", "`x` int(4) unsigned zerofill DEFAULT '0001'"],
40
            ["x int auto_increment",        "`x` int(11) NOT NULL AUTO_INCREMENT"],
41
            ["x int comment 'blah'",        "`x` int(11) DEFAULT NULL COMMENT 'blah'"],
42
            ["x int serial default value",  "`x` int(11) NOT NULL AUTO_INCREMENT"],
43
44
            ["x int primary key",           "`x` int(11) NOT NULL"],
45
            ["x int key",                   "`x` int(11) NOT NULL"],
46
            ["x int unique",                "`x` int(11) DEFAULT NULL"],
47
            ["x int unique key",            "`x` int(11) DEFAULT NULL"],
48
49
            ["x bit",                       "`x` bit(1) DEFAULT NULL"],
50
            ["x bit(4)",                    "`x` bit(4) DEFAULT NULL"],
51
            ["x bit(4) default 4",          "`x` bit(4) DEFAULT b'100'"],
52
            ["x bit(4) default b'0101'",    "`x` bit(4) DEFAULT b'101'"],
53
            ["x bit(8) default x'e4'",      "`x` bit(8) DEFAULT b'11100100'"],
54
55
            ["x tinyint",                   "`x` tinyint(4) DEFAULT NULL"],
56
            ["x tinyint unsigned",          "`x` tinyint(3) unsigned DEFAULT NULL"],
57
            ["x smallint",                  "`x` smallint(6) DEFAULT NULL"],
58
            ["x smallint unsigned",         "`x` smallint(5) unsigned DEFAULT NULL"],
59
            ["x mediumint",                 "`x` mediumint(9) DEFAULT NULL"],
60
            ["x mediumint unsigned",        "`x` mediumint(8) unsigned DEFAULT NULL"],
61
            ["x bigint",                    "`x` bigint(20) DEFAULT NULL"],
62
            ["x bigint unsigned",           "`x` bigint(20) unsigned DEFAULT NULL"],
63
64
            ["x double",                    "`x` double DEFAULT NULL"],
65
            ["x double unsigned",           "`x` double unsigned DEFAULT NULL"],
66
            ["x double(12,4)",              "`x` double(12,4) DEFAULT NULL"],
67
68
            ["x float",                     "`x` float DEFAULT NULL"],
69
            ["x float unsigned",            "`x` float unsigned DEFAULT NULL"],
70
            ["x float(8,3)",                "`x` float(8,3) DEFAULT NULL"],
71
72
            ["x decimal",                   "`x` decimal(10,0) DEFAULT NULL"],
73
            ["x decimal unsigned",          "`x` decimal(10,0) unsigned DEFAULT NULL"],
74
            ["x decimal(8)",                "`x` decimal(8,0) DEFAULT NULL"],
75
            ["x decimal(8,3)",              "`x` decimal(8,3) DEFAULT NULL"],
76
            ["x decimal default 1",         "`x` decimal(10,0) DEFAULT '1'"],
77
            ["x decimal(5,3) default 1",    "`x` decimal(5,3) DEFAULT '1.000'"],
78
            ["x decimal(7,3) zerofill default 1",   "`x` decimal(7,3) unsigned zerofill DEFAULT '001.000'"],
79
80
            ["x date",                      "`x` date DEFAULT NULL"],
81
            ["x date default 0",            "`x` date DEFAULT '0000-00-00'"],
82
            ["x date default '1970-08-12'", "`x` date DEFAULT '1970-08-12'"],
83
84
            ["x time",                      "`x` time DEFAULT NULL"],
85
            ["x time default 0",            "`x` time DEFAULT '00:00:00'"],
86
            ["x time default '23:59:59'",   "`x` time DEFAULT '23:59:59'"],
87
88
            ["x datetime",                               "`x` datetime DEFAULT NULL"],
89
            ["x datetime default 0",                     "`x` datetime DEFAULT '0000-00-00 00:00:00'"],
90
            ["x datetime default '1970-08-12 23:58:57'", "`x` datetime DEFAULT '1970-08-12 23:58:57'"],
91
            ["x datetime default current_timestamp",     "`x` datetime DEFAULT CURRENT_TIMESTAMP"],
92
            ["x datetime on update current_timestamp",   "`x` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP"],
93
            ["x datetime default current_timestamp on update current_timestamp",
94
                "`x` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"],
95
96
            ["x timestamp",                               "`x` timestamp NOT NULL"],
97
            ["x timestamp default 0",                     "`x` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'"],
98
            ["x timestamp default '1970-08-12 19:18:17'", "`x` timestamp NOT NULL DEFAULT '1970-08-12 19:18:17'"],
99
            ["x timestamp default current_timestamp",     "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"],
100
            ["x timestamp default localtime",             "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"],
101
            ["x timestamp default localtimestamp",        "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"],
102
            ["x timestamp default now()",                 "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"],
103
            ["x timestamp on update current_timestamp",   "`x` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP"],
104
            ["x timestamp on update localtime",           "`x` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP"],
105
            ["x timestamp on update localtimestamp",      "`x` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP"],
106
            ["x timestamp on update now()",               "`x` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP"],
107
            ["x timestamp default current_timestamp on update current_timestamp",
108
                "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"],
109
            ["x timestamp on update current_timestamp default current_timestamp",
110
                "`x` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"],
111
112
            ["x year",             "`x` year(4) DEFAULT NULL"],
113
            ["x year(4)",          "`x` year(4) DEFAULT NULL"],
114
            ["x year default 0",   "`x` year(4) DEFAULT '0000'"],
115
            ["x year default '0'", "`x` year(4) DEFAULT '2000'"],
116
            ["x year default 69",  "`x` year(4) DEFAULT '2069'"],
117
            ["x year default 70",  "`x` year(4) DEFAULT '1970'"],
118
            ["x year default 99",  "`x` year(4) DEFAULT '1999'"],
119
            ["x year default 1901","`x` year(4) DEFAULT '1901'"],
120
            ["x year default 2155","`x` year(4) DEFAULT '2155'"],
121
122
            ["x char",           "`x` char(1) DEFAULT NULL"],
123
            ["x char(4)",        "`x` char(4) DEFAULT NULL"],
124
125
            ["x varchar(255)",   "`x` varchar(255) DEFAULT NULL"],
126
127
            ["x binary",                "`x` binary(1) DEFAULT NULL"],
128
            ["x binary(255)",           "`x` binary(255) DEFAULT NULL"],
129
            ["x binary default 'a'",    "`x` binary(1) DEFAULT 'a'"],
130
131
            ["x varbinary(255)",        "`x` varbinary(255) DEFAULT NULL"],
132
133
            ["x tinyblob",            "`x` tinyblob"],
134
            ["x tinyblob NULL",       "`x` tinyblob"],
135
            ["x tinyblob NOT NULL",   "`x` tinyblob NOT NULL"],
136
137
            ["x blob",                "`x` blob"],
138
            ["x blob NULL",           "`x` blob"],
139
            ["x blob NOT NULL",       "`x` blob NOT NULL"],
140
141
            ["x mediumblob",          "`x` mediumblob"],
142
            ["x mediumblob NULL",     "`x` mediumblob"],
143
            ["x mediumblob NOT NULL", "`x` mediumblob NOT NULL"],
144
145
            ["x longblob",            "`x` longblob"],
146
            ["x longblob NULL",       "`x` longblob"],
147
            ["x longblob NOT NULL",   "`x` longblob NOT NULL"],
148
149
            ["x tinytext",            "`x` tinytext"],
150
            ["x tinytext NULL",       "`x` tinytext"],
151
            ["x tinytext NOT NULL",   "`x` tinytext NOT NULL"],
152
153
            ["x text",                "`x` text"],
154
            ["x text NULL",           "`x` text"],
155
            ["x text NOT NULL",       "`x` text NOT NULL"],
156
            ["x text binary",         "`x` text"],
157
            ["x text charset utf8",   "`x` text CHARACTER SET utf8"],
158
            ["x text collate utf8_unicode_ci", "`x` text CHARACTER SET utf8 COLLATE utf8_unicode_ci"],
159
160
            ["x mediumtext",          "`x` mediumtext"],
161
            ["x mediumtext NULL",     "`x` mediumtext"],
162
            ["x mediumtext NOT NULL", "`x` mediumtext NOT NULL"],
163
164
            ["x longtext",            "`x` longtext"],
165
            ["x longtext NULL",       "`x` longtext"],
166
            ["x longtext NOT NULL",   "`x` longtext NOT NULL"],
167
168
            ["x varchar(3) default 'abc'",  "`x` varchar(3) DEFAULT 'abc'"],
169
170
            ["x enum('a', 'b', 'c')",             "`x` enum('a','b','c') DEFAULT NULL"],
171
            ["x enum('a', 'b', 'c') DEFAULT 'b'", "`x` enum('a','b','c') DEFAULT 'b'"],
172
            ["x enum('a', 'b', 'c') NOT NULL",    "`x` enum('a','b','c') NOT NULL"],
173
174
            ["x set('a', 'b', 'c')",               "`x` set('a','b','c') DEFAULT NULL"],
175
            ["x set('a', 'b', 'c') DEFAULT 'a,c'", "`x` set('a','b','c') DEFAULT 'a,c'"],
176
            ["x set('a', 'b', 'c') NOT NULL",      "`x` set('a','b','c') NOT NULL"],
177
            ["x set('a', 'b', 'c') default ''",      "`x` set('a','b','c') DEFAULT ''"],
178
179
            ["x serial",                 "`x` bigint(20) unsigned NOT NULL AUTO_INCREMENT"],
180
181
            ["x character",              "`x` char(1) DEFAULT NULL"],
182
            ["x character(10)",          "`x` char(10) DEFAULT NULL"],
183
            ["x character varying(100)", "`x` varchar(100) DEFAULT NULL"],
184
185
            ["x double precision",          "`x` double DEFAULT NULL"],
186
            ["x double precision unsigned", "`x` double unsigned DEFAULT NULL"],
187
            ["x double precision(12,4)",    "`x` double(12,4) DEFAULT NULL"],
188
189
            ["x long varbinary",      "`x` mediumblob"],
190
            ["x long varchar",        "`x` mediumtext"],
191
            ["x long",                "`x` mediumtext"],
192
193
            ["x bool",                "`x` tinyint(1) DEFAULT NULL"],
194
            ["x boolean",             "`x` tinyint(1) DEFAULT NULL"],
195
            ["x bool NOT NULL",       "`x` tinyint(1) NOT NULL"],
196
            ["x boolean NOT NULL",    "`x` tinyint(1) NOT NULL"],
197
198
            ["x int1",                "`x` tinyint(4) DEFAULT NULL"],
199
            ["x int1(3)",             "`x` tinyint(3) DEFAULT NULL"],
200
            ["x int1 NOT NULL",       "`x` tinyint(4) NOT NULL"],
201
202
            ["x int2",                "`x` smallint(6) DEFAULT NULL"],
203
            ["x int2(3)",             "`x` smallint(3) DEFAULT NULL"],
204
            ["x int2 NOT NULL",       "`x` smallint(6) NOT NULL"],
205
206
            ["x int3",                "`x` mediumint(9) DEFAULT NULL"],
207
            ["x int3(3)",             "`x` mediumint(3) DEFAULT NULL"],
208
            ["x int3 NOT NULL",       "`x` mediumint(9) NOT NULL"],
209
210
            ["x middleint",           "`x` mediumint(9) DEFAULT NULL"],
211
            ["x middleint(3)",        "`x` mediumint(3) DEFAULT NULL"],
212
            ["x middleint NOT NULL",  "`x` mediumint(9) NOT NULL"],
213
214
            ["x int4",                "`x` int(11) DEFAULT NULL"],
215
            ["x int4(3)",             "`x` int(3) DEFAULT NULL"],
216
            ["x int4 NOT NULL",       "`x` int(11) NOT NULL"],
217
218
            ["x integer",             "`x` int(11) DEFAULT NULL"],
219
            ["x integer(3)",          "`x` int(3) DEFAULT NULL"],
220
            ["x integer NOT NULL",    "`x` int(11) NOT NULL"],
221
222
            ["x int8",                "`x` bigint(20) DEFAULT NULL"],
223
            ["x int8(3)",             "`x` bigint(3) DEFAULT NULL"],
224
            ["x int8 NOT NULL",       "`x` bigint(20) NOT NULL"],
225
226
            ["x dec",                 "`x` decimal(10,0) DEFAULT NULL"],
227
            ["x dec(8)",              "`x` decimal(8,0) DEFAULT NULL"],
228
            ["x dec(8,3)",            "`x` decimal(8,3) DEFAULT NULL"],
229
            ["x dec NOT NULL",        "`x` decimal(10,0) NOT NULL"],
230
231
            ["x numeric",             "`x` decimal(10,0) DEFAULT NULL"],
232
            ["x numeric(8)",          "`x` decimal(8,0) DEFAULT NULL"],
233
            ["x numeric(8,3)",        "`x` decimal(8,3) DEFAULT NULL"],
234
            ["x numeric NOT NULL",    "`x` decimal(10,0) NOT NULL"],
235
236
            ["x fixed",               "`x` decimal(10,0) DEFAULT NULL"],
237
            ["x fixed(8)",            "`x` decimal(8,0) DEFAULT NULL"],
238
            ["x fixed(8,3)",          "`x` decimal(8,3) DEFAULT NULL"],
239
            ["x fixed NOT NULL",      "`x` decimal(10,0) NOT NULL"],
240
241
            ["x real",                "`x` double DEFAULT NULL"],
242
            ["x real(8,3)",           "`x` double(8,3) DEFAULT NULL"],
243
244
            // Ignore unrecognised column options
245
            ["x int foo",             "`x` int(11) DEFAULT NULL"],
246
        ];
247
    }
248
249
    /**
250
     * @param string $text
251
     * @dataProvider parseInvalidDatatypesProvider
252
     * @expectedException \RuntimeException
253
     */
254
    public function testParseInvalidDatatypes($text)
255
    {
256
        $stream = $this->makeStream($text);
257
        $column = new ColumnDefinition();
258
        $column->parse($stream);
259
    }
260
261
    /**
262
     * @return array
263
     */
264
    public function parseInvalidDatatypesProvider()
265
    {
266
        return [
267
            // Invalid type
268
            ["x foo"],
269
            // Another invalid type
270
            ["x null"],
271
            // No identifier
272
            ["int"],
273
            // No comma separator
274
            ["x set('a'|'b')"],
275
            // Unexpected parentheses
276
            ["x text ()"],
277
            // Unexpected comma
278
            ["x year (1,)"],
279
            // No comma separator
280
            ["x double(1 2)"],
281
            // No parentheses
282
            ["x varchar"],
283
            // Unexpected ZEROFILL keyword
284
            ["x date zerofill"],
285
            // Unexpected UNSIGNED keyword
286
            ["x date unsigned"],
287
            // Unexpected SIGNED keyword
288
            ["x date signed"],
289
            // Unexpected BINARY keyword
290
            ["x date binary"],
291
            // Unexpected CHARSET keyword
292
            ["x date charset"],
293
            // Unexpected COLLATE keyword
294
            ["x date collate"],
295
            // Unexpected AUTO_INCREMENT keyword
296
            ["x date auto_increment"],
297
            // Unexpected SERIAL DEFAULT VALUE keywords
298
            ["x date serial default value"],
299
            // Invalid year length
300
            ["x year(1)"],
301
            ["x year(5)"],
302
            // Missing parentheses after keyword NOW
303
            ["x timestamp default now"],
304
            ["x timestamp on update now"],
305
            // "on update" with the current timestamp only available for datetime and timestamp
306
            ["x date on update current_timestamp"],
307
        ];
308
    }
309
310
    /**
311
     * @param string $text
312
     * @dataProvider parseInvalidDefaultValuesProvider
313
     * @expectedException \Exception
314
     */
315
    public function testParseInvalidDefaultValues($text)
316
    {
317
        $stream = $this->makeStream($text);
318
        $column = new ColumnDefinition();
319
        $column->parse($stream);
320
    }
321
322
    /**
323
     * @return array
324
     */
325
    public function parseInvalidDefaultValuesProvider()
326
    {
327
        return [
328
            ["x timestamp default null"],
329
            ["x date default current_timestamp"],
330
            ["x int default "],
331
            ["x year default 1900"],
332
            ["x year default 2156"],
333
            ["x enum('a', 'b', 'c') default 1"],
334
            ["x enum('a', 'b', 'c') default 'd'"],
335
            ["x set('a', 'b', 'c') default 1"],
336
            ["x set('a', 'b', 'c') default 'd'"],
337
            ["x text default 'abc'"],
338
        ];
339
    }
340
341
    /**
342
     * @dataProvider parseIndexesProvider
343
     * @param string $in
344
     * @param array $expectCount
345
     */
346
    public function testParseIndexes($in, array $expectCount)
347
    {
348
        $stream = $this->makeStream($in);
349
350
        $column = new ColumnDefinition();
351
        $column->parse($stream);
352
353
        $count = array_count_values(array_map(function ($e) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
354
            return $e->type;
355
        }, $column->indexes));
356
        ksort($count);
357
        ksort($expectCount);
358
359
        $this->assertSame($expectCount, $count);
360
    }
361
362
    /**
363
     * @return array
364
     */
365
    public function parseIndexesProvider()
366
    {
367
        return [
368
            ["x int",                       []],
369
            ["x serial",                    ['UNIQUE KEY' => 1]],
370
            ["x int serial default value",  ['UNIQUE KEY' => 1]],
371
            ["x int unique",                ['UNIQUE KEY'  => 1]],
372
            ["x int unique key",            ['UNIQUE KEY'  => 1]],
373
            ["x int primary key",           ['PRIMARY KEY' => 1]],
374
            ["x int key",                   ['PRIMARY KEY' => 1]],
375
        ];
376
    }
377
378
    /**
379
     * @param string $text
380
     * @param string|null $expected
381
     * @dataProvider getUninitialisedValuesProvider
382
     */
383
    public function testGetUninitialisedValues($text, $expected)
384
    {
385
        $stream = $this->makeStream($text);
386
        $column = new ColumnDefinition();
387
        $column->parse($stream);
388
389
        $this->assertSame($expected, $column->getUninitialisedValue());
390
    }
391
392
    /**
393
     * @return array
394
     */
395
    public function getUninitialisedValuesProvider()
396
    {
397
        return [
398
            // [ Column definition, uninitialised value ]
399
            ["x enum('a', 'b', 'c')",   "a"],
400
            ["x int",                   "0"],
401
            ["x int(5) zerofill",       "00000"],
402
            ["x decimal",               "0"],
403
            ["x decimal(5,3)",          "0.000"],
404
            ["x decimal(5,1) zerofill", "000.0"],
405
            ["x char",                  ""],
406
            ["x blob",                  null],
407
        ];
408
    }
409
410
    public function testApplyTableCollationSetsCollation()
411
    {
412
        // Make sure the given collation is applied if none exists
413
        $column = new ColumnDefinition();
414
        $utf8Collation = new CollationInfo("utf8");
415
        $column->applyTableCollation($utf8Collation);
416
417
        $this->assertEquals($utf8Collation, $column->collation);
418
419
        // Make sure the given collation is NOT applied in one exists
420
        $latin1Collation = new CollationInfo("latin1");
421
        $column->applyTableCollation($latin1Collation);
422
        $this->assertNotEquals($latin1Collation, $column->collation);
423
    }
424
425
    /**
426
     * @param string $text
427
     * @param string $expected
428
     * @dataProvider applyTableCollationEffectOnColumnTypeProvider
429
     */
430 View Code Duplication
    public function testApplyTableCollationEffectOnColumnType($text, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
    {
432
        $stream = $this->makeStream($text);
433
        $column = new ColumnDefinition();
434
        $column->parse($stream);
435
436
        $collation = new CollationInfo("binary");
437
        $column->applyTableCollation($collation);
438
439
        $this->assertSame($expected, $column->type);
440
    }
441
442
    /**
443
     * @return array
444
     */
445
    public function applyTableCollationEffectOnColumnTypeProvider()
446
    {
447
        return [
448
            // [ Column definition, expected column type ]
449
            // These are all types that will change
450
            ["x char",          "binary"],
451
            ["x varchar(1)",    "varbinary"],
452
            ["x tinytext",      "tinyblob"],
453
            ["x text",          "blob"],
454
            ["x mediumtext",    "mediumblob"],
455
            ["x longtext",      "longblob"],
456
            // An example of one that doesn't change
457
            ["x int",           "int"],
458
        ];
459
    }
460
}
461