Passed
Pull Request — master (#311)
by William
12:43
created

RoutineTest::getParameterProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 65
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 81
rs 8.7636

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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