Passed
Branch master (366c16)
by William
03:30
created

RoutineTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 151
c 2
b 0
f 0
dl 0
loc 276
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameterProvider() 0 81 1
A getReturnTypeProvider() 0 81 1
A testGetParameter() 0 3 1
A testGetReturnType() 0 3 1
A testGetParameters() 0 4 1
A getParametersProvider() 0 71 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Utils;
6
7
use PhpMyAdmin\SqlParser\Parser;
8
use PhpMyAdmin\SqlParser\Tests\TestCase;
9
use PhpMyAdmin\SqlParser\Utils\Routine;
10
11
class RoutineTest extends TestCase
12
{
13
    /**
14
     * @param mixed $def
15
     *
16
     * @dataProvider getReturnTypeProvider
17
     */
18
    public function testGetReturnType($def, array $expected)
19
    {
20
        $this->assertEquals($expected, Routine::getReturnType($def));
21
    }
22
23
    public function getReturnTypeProvider()
24
    {
25
        return [
26
            [
27
                '',
28
                [
29
                    '',
30
                    '',
31
                    '',
32
                    '',
33
                    '',
34
                ],
35
            ],
36
            [
37
                'TEXT',
38
                [
39
                    '',
40
                    '',
41
                    'TEXT',
42
                    '',
43
                    '',
44
                ],
45
            ],
46
            [
47
                'INT(20)',
48
                [
49
                    '',
50
                    '',
51
                    'INT',
52
                    '20',
53
                    '',
54
                ],
55
            ],
56
            [
57
                'INT UNSIGNED',
58
                [
59
                    '',
60
                    '',
61
                    'INT',
62
                    '',
63
                    'UNSIGNED',
64
                ],
65
            ],
66
            [
67
                'VARCHAR(1) CHARSET utf8',
68
                [
69
                    '',
70
                    '',
71
                    'VARCHAR',
72
                    '1',
73
                    'utf8',
74
                ],
75
            ],
76
            [
77
                'ENUM(\'a\', \'b\') CHARSET latin1',
78
                [
79
                    '',
80
                    '',
81
                    'ENUM',
82
                    '\'a\',\'b\'',
83
                    'latin1',
84
                ],
85
            ],
86
            [
87
                'DECIMAL(5,2) UNSIGNED ZEROFILL',
88
                [
89
                    '',
90
                    '',
91
                    'DECIMAL',
92
                    '5,2',
93
                    'UNSIGNED ZEROFILL',
94
                ],
95
            ],
96
            [
97
                'SET(\'test\'\'esc"\',   \'more\\\'esc\')',
98
                [
99
                    '',
100
                    '',
101
                    'SET',
102
                    '\'test\'\'esc"\',\'more\\\'esc\'',
103
                    '',
104
                ],
105
            ],
106
        ];
107
    }
108
109
    /**
110
     * @param mixed $def
111
     *
112
     * @dataProvider getParameterProvider
113
     */
114
    public function testGetParameter($def, array $expected)
115
    {
116
        $this->assertEquals($expected, Routine::getParameter($def));
117
    }
118
119
    public function getParameterProvider()
120
    {
121
        return [
122
            [
123
                '',
124
                [
125
                    '',
126
                    '',
127
                    '',
128
                    '',
129
                    '',
130
                ],
131
            ],
132
            [
133
                '`foo` TEXT',
134
                [
135
                    '',
136
                    'foo',
137
                    'TEXT',
138
                    '',
139
                    '',
140
                ],
141
            ],
142
            [
143
                '`foo` INT(20)',
144
                [
145
                    '',
146
                    'foo',
147
                    'INT',
148
                    '20',
149
                    '',
150
                ],
151
            ],
152
            [
153
                'IN `fo``fo` INT UNSIGNED',
154
                [
155
                    'IN',
156
                    'fo`fo',
157
                    'INT',
158
                    '',
159
                    'UNSIGNED',
160
                ],
161
            ],
162
            [
163
                'OUT bar VARCHAR(1) CHARSET utf8',
164
                [
165
                    'OUT',
166
                    'bar',
167
                    'VARCHAR',
168
                    '1',
169
                    'utf8',
170
                ],
171
            ],
172
            [
173
                '`"baz\'\'` ENUM(\'a\', \'b\') CHARSET latin1',
174
                [
175
                    '',
176
                    '"baz\'\'',
177
                    'ENUM',
178
                    '\'a\',\'b\'',
179
                    'latin1',
180
                ],
181
            ],
182
            [
183
                'INOUT `foo` DECIMAL(5,2) UNSIGNED ZEROFILL',
184
                [
185
                    'INOUT',
186
                    'foo',
187
                    'DECIMAL',
188
                    '5,2',
189
                    'UNSIGNED ZEROFILL',
190
                ],
191
            ],
192
            [
193
                '`foo``s func` SET(\'test\'\'esc"\',   \'more\\\'esc\')',
194
                [
195
                    '',
196
                    'foo`s func',
197
                    'SET',
198
                    '\'test\'\'esc"\',\'more\\\'esc\'',
199
                    '',
200
                ],
201
            ],
202
        ];
203
    }
204
205
    /**
206
     * @param mixed $query
207
     *
208
     * @dataProvider getParametersProvider
209
     */
210
    public function testGetParameters($query, array $expected)
211
    {
212
        $parser = new Parser($query);
213
        $this->assertEquals($expected, Routine::getParameters($parser->statements[0]));
214
    }
215
216
    public function getParametersProvider()
217
    {
218
        return [
219
            [
220
                'CREATE PROCEDURE `foo`() SET @A=0',
221
                [
222
                    'num' => 0,
223
                    'dir' => [],
224
                    'name' => [],
225
                    'type' => [],
226
                    'length' => [],
227
                    'length_arr' => [],
228
                    'opts' => [],
229
                ],
230
            ],
231
            [
232
                'CREATE DEFINER=`user\\`@`somehost``(` FUNCTION `foo```(`baz` INT) BEGIN SELECT NULL; END',
233
                [
234
                    'num' => 1,
235
                    'dir' => [0 => ''],
236
                    'name' => [0 => 'baz'],
237
                    'type' => [0 => 'INT'],
238
                    'length' => [0 => ''],
239
                    'length_arr' => [
240
                        0 => [],
241
                    ],
242
                    'opts' => [0 => ''],
243
                ],
244
            ],
245
            [
246
                'CREATE PROCEDURE `foo`(IN `baz\\)` INT(25) zerofill unsigned) BEGIN SELECT NULL; END',
247
                [
248
                    'num' => 1,
249
                    'dir' => [0 => 'IN'],
250
                    'name' => [0 => 'baz\\)'],
251
                    'type' => [0 => 'INT'],
252
                    'length' => [0 => '25'],
253
                    'length_arr' => [
254
                        0 => ['25'],
255
                    ],
256
                    'opts' => [0 => 'UNSIGNED ZEROFILL'],
257
                ],
258
            ],
259
            [
260
                'CREATE PROCEDURE `foo`(IN `baz\\` INT(001) zerofill, out bazz varchar(15) charset utf8) ' .
261
                'BEGIN SELECT NULL; END',
262
                [
263
                    'num' => 2,
264
                    'dir' => [
265
                        0 => 'IN',
266
                        1 => 'OUT',
267
                    ],
268
                    'name' => [
269
                        0 => 'baz\\',
270
                        1 => 'bazz',
271
                    ],
272
                    'type' => [
273
                        0 => 'INT',
274
                        1 => 'VARCHAR',
275
                    ],
276
                    'length' => [
277
                        0 => '1',
278
                        1 => '15',
279
                    ],
280
                    'length_arr' => [
281
                        0 => ['1'],
282
                        1 => ['15'],
283
                    ],
284
                    'opts' => [
285
                        0 => 'ZEROFILL',
286
                        1 => 'utf8',
287
                    ],
288
                ],
289
            ],
290
        ];
291
    }
292
}
293