Skip to content

Commit 8929288

Browse files
committed
[ISSUE-17793] Add more test for coverage
Signed-off-by: Mo Sureerat <sureemo@gmail.com>
1 parent 250be6a commit 8929288

2 files changed

Lines changed: 160 additions & 35 deletions

File tree

libraries/classes/Table/ColumnsDefinition.php

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -284,41 +284,8 @@ public static function displayForm(
284284
$columnMeta['Expression'] = is_array($expressions) ? $expressions[$columnMeta['Field']] : null;
285285
}
286286

287-
$columnMeta['DefaultType'] = 'USER_DEFINED';
288-
$columnMeta['DefaultValue'] = '';
289-
290-
switch ($columnMeta['Default']) {
291-
case null:
292-
if ($columnMeta['Default'] === null) {
293-
$columnMeta['DefaultType'] = $columnMeta['Null'] === 'YES'
294-
? 'NULL'
295-
: 'NONE';
296-
} else { // empty
297-
$columnMeta['DefaultType'] = 'USER_DEFINED';
298-
$columnMeta['DefaultValue'] = $columnMeta['Default'];
299-
}
300-
301-
break;
302-
case 'CURRENT_TIMESTAMP':
303-
case 'current_timestamp()':
304-
$columnMeta['DefaultType'] = 'CURRENT_TIMESTAMP';
305-
306-
break;
307-
case 'UUID':
308-
case 'uuid()':
309-
$columnMeta['DefaultType'] = 'UUID';
310-
311-
break;
312-
default:
313-
$columnMeta['DefaultValue'] = $columnMeta['Default'];
314-
315-
if (substr($columnMeta['Type'], -4) === 'text') {
316-
$textDefault = substr($columnMeta['Default'], 1, -1);
317-
$columnMeta['Default'] = stripcslashes($textDefault);
318-
}
319-
320-
break;
321-
}
287+
$columnMetaDefault = self::decorateColumnMetaDefault($columnMeta);
288+
$columnMeta = array_merge($columnMeta, $columnMetaDefault);
322289
}
323290

324291
if (isset($columnMeta['Type'])) {
@@ -527,4 +494,50 @@ public static function displayForm(
527494
'disable_is' => $cfg['Server']['DisableIS'],
528495
];
529496
}
497+
498+
/**
499+
* Set default type and default value according to the column metadata
500+
*
501+
* @param array $columnMeta Column Metadata
502+
* @phpstan-param array<string, string|null> $columnMeta
503+
*
504+
* @return array<string, string>
505+
*/
506+
public static function decorateColumnMetaDefault(array $columnMeta): array
507+
{
508+
$metaDefault['DefaultType'] = 'USER_DEFINED';
509+
$metaDefault['DefaultValue'] = '';
510+
511+
switch ($columnMeta['Default']) {
512+
case null:
513+
if ($columnMeta['Null'] === 'YES') {
514+
$metaDefault['DefaultType'] = 'NULL';
515+
} else {
516+
$metaDefault['DefaultType'] = 'NONE';
517+
}
518+
519+
break;
520+
case 'CURRENT_TIMESTAMP':
521+
case 'current_timestamp()':
522+
$metaDefault['DefaultType'] = 'CURRENT_TIMESTAMP';
523+
524+
break;
525+
case 'UUID':
526+
case 'uuid()':
527+
$metaDefault['DefaultType'] = 'UUID';
528+
529+
break;
530+
default:
531+
$metaDefault['DefaultValue'] = $columnMeta['Default'];
532+
533+
if (substr($columnMeta['Type'], -4) === 'text') {
534+
$textDefault = substr($columnMeta['Default'], 1, -1);
535+
$metaDefault['Default'] = stripcslashes($textDefault);
536+
}
537+
538+
break;
539+
}
540+
541+
return $metaDefault;
542+
}
530543
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Tests\Table;
6+
7+
use PhpMyAdmin\Table\ColumnsDefinition;
8+
use PhpMyAdmin\Tests\AbstractTestCase;
9+
10+
/**
11+
* @covers \PhpMyAdmin\Table\ColumnsDefinition
12+
*/
13+
class ColumnsDefinitionTest extends AbstractTestCase
14+
{
15+
/**
16+
* test for ColumnsDefinition::decorateColumnMetaDefault
17+
*
18+
* @param array $columnMeta column metadata
19+
* @param array $expected expected result
20+
* @phpstan-param array<string, string|null> $columnMeta
21+
* @phpstan-param array<string, string> $expected
22+
*
23+
* @dataProvider providerColumnMetaDefault
24+
*/
25+
public function testDecorateColumnMetaDefault(array $columnMeta, array $expected): void
26+
{
27+
$result = ColumnsDefinition::decorateColumnMetaDefault($columnMeta);
28+
29+
$this->assertEquals($expected, $result);
30+
}
31+
32+
/**
33+
* Data provider for testDecorateColumnMetaDefault
34+
*
35+
* @return array
36+
* @psalm-return array<string, array{array<string, string|null>, array<string, string>}>
37+
*/
38+
public function providerColumnMetaDefault(): array
39+
{
40+
return [
41+
'when Default is null and Null is YES' => [
42+
[
43+
'Default' => null,
44+
'Null' => 'YES',
45+
],
46+
[
47+
'DefaultType' => 'NULL',
48+
'DefaultValue' => '',
49+
],
50+
],
51+
'when Default is null and Null is NO' => [
52+
[
53+
'Default' => null,
54+
'Null' => 'NO',
55+
],
56+
[
57+
'DefaultType' => 'NONE',
58+
'DefaultValue' => '',
59+
],
60+
],
61+
'when Default is CURRENT_TIMESTAMP' => [
62+
['Default' => 'CURRENT_TIMESTAMP'],
63+
[
64+
'DefaultType' => 'CURRENT_TIMESTAMP',
65+
'DefaultValue' => '',
66+
],
67+
],
68+
'when Default is current_timestamp' => [
69+
['Default' => 'current_timestamp()'],
70+
[
71+
'DefaultType' => 'CURRENT_TIMESTAMP',
72+
'DefaultValue' => '',
73+
],
74+
],
75+
'when Default is UUID' => [
76+
['Default' => 'UUID'],
77+
[
78+
'DefaultType' => 'UUID',
79+
'DefaultValue' => '',
80+
],
81+
],
82+
'when Default is uuid()' => [
83+
['Default' => 'uuid()'],
84+
[
85+
'DefaultType' => 'UUID',
86+
'DefaultValue' => '',
87+
],
88+
],
89+
'when Default is anything else and Type is text' => [
90+
[
91+
'Default' => '"some\/thing"',
92+
'Type' => 'text',
93+
],
94+
[
95+
'Default' => 'some/thing',
96+
'DefaultType' => 'USER_DEFINED',
97+
'DefaultValue' => '"some\/thing"',
98+
],
99+
],
100+
'when Default is anything else and Type is not text' => [
101+
[
102+
'Default' => '"some\/thing"',
103+
'Type' => 'something',
104+
],
105+
[
106+
'DefaultType' => 'USER_DEFINED',
107+
'DefaultValue' => '"some\/thing"',
108+
],
109+
],
110+
];
111+
}
112+
}

0 commit comments

Comments
 (0)