Passed
Push — master ( e6982c...abc21e )
by Maurício
10:04
created

TablePartitionDefinitionTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 166
c 1
b 0
f 0
dl 0
loc 249
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetDetailsWithoutPostValues() 0 19 1
A testGetDetailsWithMaxPartitions() 0 9 1
A providerGetDetailsWithMaxPartitions() 0 3 1
B testGetDetails() 0 125 6
A providerGetDetails() 0 63 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests;
6
7
use PhpMyAdmin\TablePartitionDefinition;
8
use PHPUnit\Framework\TestCase;
9
10
use function count;
11
12
/**
13
 * @covers \PhpMyAdmin\TablePartitionDefinition
14
 */
15
class TablePartitionDefinitionTest extends TestCase
16
{
17
    /**
18
     * @dataProvider providerGetDetails
19
     */
20
    public function testGetDetails(
21
        string $partitionBy,
22
        bool $canHaveSubpartitions,
23
        bool $valueEnabled,
24
        int $partitionCount,
25
        int $subPartitionCount,
26
        ?array $partitions
27
    ): void {
28
        $expected = [
29
            'partition_by' => $partitionBy,
30
            'partition_expr' => 'partition_expr',
31
            'subpartition_by' => 'subpartition_by',
32
            'subpartition_expr' => 'subpartition_expr',
33
            'partition_count' => $partitionCount > 0 ? $partitionCount : '',
34
            'subpartition_count' => $subPartitionCount > 0 ? $subPartitionCount : '',
35
            'can_have_subpartitions' => $canHaveSubpartitions,
36
            'value_enabled' => $valueEnabled,
37
            'partitions' => [
38
                [
39
                    'name' => 'part0',
40
                    'value_type' => '',
41
                    'value' => '',
42
                    'engine' => '',
43
                    'comment' => '',
44
                    'data_directory' => '',
45
                    'index_directory' => '',
46
                    'max_rows' => '',
47
                    'min_rows' => '',
48
                    'tablespace' => '',
49
                    'node_group' => '',
50
                    'prefix' => 'partitions[0]',
51
                    'subpartition_count' => 2,
52
                    'subpartitions' => [
53
                        [
54
                            'name' => 'part0_s0',
55
                            'engine' => '',
56
                            'comment' => '',
57
                            'data_directory' => '',
58
                            'index_directory' => '',
59
                            'max_rows' => '',
60
                            'min_rows' => '',
61
                            'tablespace' => '',
62
                            'node_group' => '',
63
                            'prefix' => 'partitions[0][subpartitions][0]',
64
                        ],
65
                        [
66
                            'name' => 'part0_s1',
67
                            'engine' => '',
68
                            'comment' => '',
69
                            'data_directory' => '',
70
                            'index_directory' => '',
71
                            'max_rows' => '',
72
                            'min_rows' => '',
73
                            'tablespace' => '',
74
                            'node_group' => '',
75
                            'prefix' => 'partitions[0][subpartitions][1]',
76
                        ],
77
                    ],
78
                ],
79
                [
80
                    'name' => 'p1',
81
                    'value_type' => '',
82
                    'value' => '',
83
                    'engine' => '',
84
                    'comment' => '',
85
                    'data_directory' => '',
86
                    'index_directory' => '',
87
                    'max_rows' => '',
88
                    'min_rows' => '',
89
                    'tablespace' => '',
90
                    'node_group' => '',
91
                    'prefix' => 'partitions[1]',
92
                    'subpartition_count' => 2,
93
                    'subpartitions' => [
94
                        [
95
                            'name' => 'p1_s0',
96
                            'engine' => '',
97
                            'comment' => '',
98
                            'data_directory' => '',
99
                            'index_directory' => '',
100
                            'max_rows' => '',
101
                            'min_rows' => '',
102
                            'tablespace' => '',
103
                            'node_group' => '',
104
                            'prefix' => 'partitions[1][subpartitions][0]',
105
                        ],
106
                        [
107
                            'name' => 'p1_s1',
108
                            'engine' => '',
109
                            'comment' => '',
110
                            'data_directory' => '',
111
                            'index_directory' => '',
112
                            'max_rows' => '',
113
                            'min_rows' => '',
114
                            'tablespace' => '',
115
                            'node_group' => '',
116
                            'prefix' => 'partitions[1][subpartitions][1]',
117
                        ],
118
                    ],
119
                ],
120
            ],
121
        ];
122
123
        if (! $canHaveSubpartitions && $partitionCount === 2) {
124
            unset($expected['partitions'][0]['subpartition_count']);
125
            unset($expected['partitions'][0]['subpartitions']);
126
            unset($expected['partitions'][1]['subpartition_count']);
127
            unset($expected['partitions'][1]['subpartitions']);
128
        }
129
130
        if ($partitionCount < 2) {
131
            unset($expected['partitions']);
132
        }
133
134
        $_POST['partition_by'] = $partitionBy;
135
        $_POST['partition_expr'] = 'partition_expr';
136
        $_POST['subpartition_by'] = 'subpartition_by';
137
        $_POST['subpartition_expr'] = 'subpartition_expr';
138
        $_POST['partition_count'] = (string) $partitionCount;
139
        $_POST['subpartition_count'] = (string) $subPartitionCount;
140
        $_POST['partitions'] = $partitions;
141
        $_POST['ignored_key'] = 'ignored_value';
142
143
        $actual = TablePartitionDefinition::getDetails();
144
        $this->assertEquals($expected, $actual);
145
    }
146
147
    /**
148
     * @psalm-return array<string, array{
149
     *   0: string, 1: bool, 2: bool, 3: int, 4: int, 5: array<string, string|array<string, string>[]>[]|null
150
     * }>
151
     */
152
    public function providerGetDetails(): array
153
    {
154
        return [
155
            'partition by RANGE' => ['RANGE', true, true, 2, 2, [['name' => 'part0']]],
156
            'partition by RANGE COLUMNS' => ['RANGE COLUMNS', true, true, 2, 2, [['name' => 'part0']]],
157
            'partition by LIST' => ['LIST', true, true, 2, 2, [['name' => 'part0']]],
158
            'partition by LIST COLUMNS' => ['LIST COLUMNS', true, true, 2, 2, [['name' => 'part0']]],
159
            'partition by HASH' => ['HASH', false, false, 2, 2, [['name' => 'part0']]],
160
            'partition count === 0' => ['RANGE', false, true, 0, 0, null],
161
            'partition count === 1' => ['RANGE', false, true, 1, 1, null],
162
            'more partitions than the partition count' => [
163
                'RANGE',
164
                true,
165
                true,
166
                2,
167
                2,
168
                [['name' => 'part0'], ['name' => 'p1'], ['name' => 'p2']],
169
            ],
170
            'more subpartitions than the subpartition count' => [
171
                'RANGE',
172
                true,
173
                true,
174
                2,
175
                2,
176
                [
177
                    [
178
                        'name' => 'part0',
179
                        'subpartitions' => [
180
                            [
181
                                'name' => 'part0_s0',
182
                                'engine' => '',
183
                                'comment' => '',
184
                                'data_directory' => '',
185
                                'index_directory' => '',
186
                                'max_rows' => '',
187
                                'min_rows' => '',
188
                                'tablespace' => '',
189
                                'node_group' => '',
190
                                'prefix' => 'partitions[1][subpartitions][0]',
191
                            ],
192
                            [
193
                                'name' => 'part0_s1',
194
                                'engine' => '',
195
                                'comment' => '',
196
                                'data_directory' => '',
197
                                'index_directory' => '',
198
                                'max_rows' => '',
199
                                'min_rows' => '',
200
                                'tablespace' => '',
201
                                'node_group' => '',
202
                                'prefix' => 'partitions[1][subpartitions][1]',
203
                            ],
204
                            [
205
                                'name' => 'part0_s1',
206
                                'engine' => '',
207
                                'comment' => '',
208
                                'data_directory' => '',
209
                                'index_directory' => '',
210
                                'max_rows' => '',
211
                                'min_rows' => '',
212
                                'tablespace' => '',
213
                                'node_group' => '',
214
                                'prefix' => 'partitions[1][subpartitions][1]',
215
                            ],
216
                        ],
217
                    ],
218
                ],
219
            ],
220
        ];
221
    }
222
223
    public function testGetDetailsWithoutPostValues(): void
224
    {
225
        $_POST = [];
226
        $expected = [
227
            'partition_by' => null,
228
            'partition_expr' => null,
229
            'subpartition_by' => null,
230
            'subpartition_expr' => null,
231
            'partition_count' => '',
232
            'subpartition_count' => '',
233
            'can_have_subpartitions' => false,
234
            'value_enabled' => false,
235
        ];
236
237
        $actual = TablePartitionDefinition::getDetails($expected);
238
        $this->assertEquals($expected, $actual);
239
240
        $actual = TablePartitionDefinition::getDetails();
241
        $this->assertEquals($expected, $actual);
242
    }
243
244
    /**
245
     * @dataProvider providerGetDetailsWithMaxPartitions
246
     */
247
    public function testGetDetailsWithMaxPartitions(int $partitionCount, string $partitionCountFromPost): void
248
    {
249
        $_POST = ['partition_count' => $partitionCountFromPost];
250
        $actual = TablePartitionDefinition::getDetails();
251
        $this->assertArrayHasKey('partition_count', $actual);
252
        $this->assertArrayHasKey('partitions', $actual);
253
        $this->assertSame($partitionCount, $actual['partition_count']);
254
        $this->assertIsArray($actual['partitions']);
255
        $this->assertEquals($partitionCount, count($actual['partitions']));
256
    }
257
258
    /**
259
     * @psalm-return array{0: int, 1: string}[]
260
     */
261
    public function providerGetDetailsWithMaxPartitions(): array
262
    {
263
        return ['count within the limit' => [8192, '8192'], 'count above the limit' => [8192, '8193']];
264
    }
265
}
266