/
ConfigParserTest.php
464 lines (396 loc) · 10.7 KB
/
ConfigParserTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Test cases for config API
*
* @package Tests
* @subpackage UnitTests
* @copyright Copyright 2016 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
/**
* Includes
*/
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';
# Mantis Core required for class autoloader and constants
require_mantis_core();
use PHPUnit_Framework_Constraint_IsType as PHPUnit_Type;
/**
* Test cases for config API parser
*
* A list of test strings (as entered in adm_config_report page) will be parsed
* by MantisBT core, and the result compared to PHP's native interpretation
* using eval().
*
* @package Tests
* @subpackage ConfigParser
*/
class MantisConfigParserTest extends PHPUnit_Framework_TestCase {
/**
* @var array List of test cases for scalar types
*/
private $cases_scalar = array();
/**
* @var array List of test cases for arrays
*/
private $cases_array = array();
/**
* MantisConfigParserTest constructor.
*/
public function __construct() {
parent::__construct();
$this->initScalarTestCases();
$this->initArrayTestCases();
}
/**
* Test with empty string or null
*
* @throws Exception
*/
public function testNoTokens() {
$this->setExpectedException( 'Exception', 'No more tokens' );
$t_parser = new ConfigParser( '' );
$t_parser->parse();
$t_parser = new ConfigParser( null );
$t_parser->parse();
}
/**
* Test a list of strings representing scalar values, making sure
* the value and the type match
*
* @throws Exception
*/
public function testScalarTypes() {
foreach( $this->cases_scalar as $t_string => $t_expected_type ) {
$t_reference_result = eval( 'return ' . $t_string . ';' );
$t_parser = new ConfigParser( $t_string );
$t_parsed_result = $t_parser->parse();
$this->assertInternalType( $t_expected_type, $t_parsed_result );
$this->assertEquals( $t_parsed_result, $t_reference_result, $this->errorMessage( $t_string ) );
}
}
/**
* Test various types of arrays
*
* @see initArrayTestCases
* @throws Exception
*/
public function testArrays() {
foreach( $this->cases_array as $t_string ) {
$t_reference_result = eval( 'return ' . $t_string . ';' );
# Check that the parsed array matches the model array
$t_parser = new ConfigParser( $t_string );
$t_parsed_1 = $t_parser->parse();
$this->assertEquals( $t_parsed_1, $t_reference_result, $this->errorMessage( $t_string ) );
# Export converted array and parse again: result should match the model
$t_parser = new ConfigParser( var_export( $t_parsed_1 , true ) );
$t_parsed_2 = $t_parser->parse();
$this->assertEquals( $t_parsed_2, $t_reference_result, $this->errorMessage( $t_string ) );
}
}
/**
* Test failure if we get extra tokens when the parser is set to error
*
* @throws Exception
*/
public function testExtraTokensError() {
$this->setExpectedExceptionRegExp('Exception', '/^Extra tokens found/');
$t_parser = new ConfigParser( '1; 2' );
$t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR );
$t_parser = new ConfigParser( 'array(); 2' );
$t_parser->parse( ConfigParser::EXTRA_TOKENS_ERROR );
}
/**
* Test no errors if we get extra tokens when the parser is set to ignore
*
* @throws Exception
*/
public function testExtraTokensIgnore() {
$t_parser = new ConfigParser( '1; 2' );
$t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE );
$this->assertEquals( $t_result, 1 );
$t_parser = new ConfigParser( 'array(); 2' );
$t_result = $t_parser->parse( ConfigParser::EXTRA_TOKENS_IGNORE );
$this->assertEquals( $t_result, array() );
}
/**
* Parser should error out if given string is not syntactically correct
*
* @throws Exception
*/
public function testSyntaxError() {
$this->setExpectedExceptionRegExp('Exception', '/^syntax error/');
$t_parser = new ConfigParser( 'array(' );
$t_parser->parse();
}
/**
* Parser only accepts arrays, scalar types and constants
*
* @throws Exception
*/
public function testInvalidTokensError() {
$this->setExpectedExceptionRegExp('Exception', '/^Unexpected token/');
$t_parser = new ConfigParser( 'echo 1;' );
$t_parser->parse();
}
/**
* Use of undefined constant should trigger an error
*
* @throws Exception
*/
public function testUnknownConstantError() {
$this->setExpectedExceptionRegExp('Exception', '/^Unknown string literal/');
# Make sure we have a string that is not a defined constant
$t_constant = 'UNDEFINED_CONSTANT';
while( defined($t_constant) ) {
$t_constant .= '_' . rand(0, 9999);
}
$t_parser = new ConfigParser( $t_constant );
$t_parser->parse();
}
/**
* Display original string in case of error to help troubleshooting
*
* @param $p_text
* @return string
*/
private function errorMessage( $p_text ) {
return "Original input:\n"
. ">>>------------------------\n"
. $p_text . "\n"
. "<<<------------------------\n";
}
/**
* Adds a new test case to the list
*
* @param string $p_string
*/
private function addArrayCase( $p_string ) {
$this->cases_array[] = $p_string;
}
/**
* Adds a new scalar test case to the list
*
* @param string $p_string Value to check
* @param string $p_type Expected type
*/
private function addScalarCase( $p_string, $p_type ) {
$this->cases_scalar[$p_string] = $p_type;
}
/**
* Initialize the Scalar type test cases
*/
private function initScalarTestCases() {
# Integer
$this->addScalarCase( '0', PHPUnit_Type::TYPE_INT );
$this->addScalarCase( '1', PHPUnit_Type::TYPE_INT );
$this->addScalarCase( " 1\n", PHPUnit_Type::TYPE_INT );
# Float
$this->addScalarCase( '1.1', PHPUnit_Type::TYPE_FLOAT );
# String
$this->addScalarCase( '""', PHPUnit_Type::TYPE_STRING );
$this->addScalarCase( "''", PHPUnit_Type::TYPE_STRING );
$this->addScalarCase( '" "', PHPUnit_Type::TYPE_STRING );
$this->addScalarCase( '"1"', PHPUnit_Type::TYPE_STRING );
$this->addScalarCase( "'1'", PHPUnit_Type::TYPE_STRING );
# Built-in string literals
$this->addScalarCase( 'null', PHPUnit_Type::TYPE_NULL );
$this->addScalarCase( 'false', PHPUnit_Type::TYPE_BOOL );
$this->addScalarCase( 'true', PHPUnit_Type::TYPE_BOOL );
# Constants
$this->addScalarCase( 'VERSION_ALL', PHPUnit_Type::TYPE_NULL ); # null
$this->addScalarCase( 'VERSION_FUTURE', PHPUnit_Type::TYPE_BOOL ); # false
$this->addScalarCase( 'VERSION_RELEASED', PHPUnit_Type::TYPE_BOOL ); # true
$this->addScalarCase( 'OFF', PHPUnit_Type::TYPE_INT ); # 0
$this->addScalarCase( 'DEVELOPER', PHPUnit_Type::TYPE_INT ); # int
$this->addScalarCase( " DEVELOPER\n", PHPUnit_Type::TYPE_INT );
$this->addScalarCase( 'MANTIS_VERSION', PHPUnit_Type::TYPE_STRING ); #string
$this->addScalarCase( " MANTIS_VERSION\n", PHPUnit_Type::TYPE_STRING );
}
/**
* Initialize the array test cases list
*/
private function initArrayTestCases() {
/**
* Template for new test cases
* ---------------------------
# comment
$this->addArrayCase(
<<<'EOT'
EOT
);
* ---------------------------
*/
/**
* Simple arrays
*/
# empty
$this->addArrayCase( "array( )" );
# one element
$this->addArrayCase( "array( 1 )" );
# several elements, trailing delimiter
$this->addArrayCase( "array( 1, 2, )" );
# formatted whitespace
$this->addArrayCase( "array ( 1, 2, 3 )" );
# no whitespace
$this->addArrayCase( "array(1,2,3)" );
# arbitrary whitespace
$this->addArrayCase( " array(\n1,\t2 , 3 )\r " );
# mixed types, quotes
$this->addArrayCase(
<<<'EOT'
array( 1, 'a', "b" )
EOT
);
# nested quotes
$this->addArrayCase(
<<<'EOT'
array( '"a""b"""', "'a''b'''" )
EOT
);
/**
* Associative arrays
*/
# associative
$this->addArrayCase( "array( 0 => 'a', 1 => 'b' )" );
# associative, unordered keys
$this->addArrayCase( "array( 5 => 'a', 2 => 'b' )" );
# associative, text keys
$this->addArrayCase( "array( 'i' => 'a', 'j' => 'b' )" );
# associative, mixed keys
$this->addArrayCase( "array( 'i' => 'a', 1 => 'b', 'j' => 'c', 7 => 'd' )" );
# mixed associative, omitting some keys
$this->addArrayCase( "array( 'i' => 'a', 1 => 'b', 'c', 'j' => 'd' )" );
# mixed associative, overwriting implicit keys
$this->addArrayCase( "array( 0 => 'a0', 1 => 'a1', 'axx', 2 => 'a2' )" );
$this->addArrayCase(
<<<'EOT'
array(
array ( 1, 'a', 3 => 1, 4 => 'b', 'x' => 'y' )
)
EOT
);
/**
* Use of constants
*/
# As array values (e.g. handle_bug_threshold)
$this->addArrayCase( "array( DEVELOPER, MANAGER )" );
# As array keys (e.g. status_enum_workflow)
$this->addArrayCase(
<<<'EOT'
array (
NEW_ => '20:feedback,30:acknowledged',
ACKNOWLEDGED => '40:confirmed',
)
EOT
);
# both (e.g. set_status_threshold)
$this->addArrayCase( 'array( NEW_ => REPORTER )' );
/**
* Multidimensional arrays
*/
# notify_flags sample
$this->addArrayCase(
<<<'EOT'
array(
'updated' => array (
'reporter' => ON,
'handler' => ON,
'monitor' => ON,
'bugnotes' => OFF,
'threshold_min' => DEVELOPER,
'threshold_max' => MANAGER,
),
'owner' => array (
'reporter' => 1,
'handler' => 1,
'monitor' => 1,
'bugnotes' => 1,
'threshold_min' => 55,
),
'reopened' => array (
'reporter' => 1,
'handler' => 1,
'monitor' => 1,
'bugnotes' => 1,
'threshold_max' => ANYBODY,
),
)
EOT
);
$this->addArrayCase(
<<<'EOT'
array(
1 => array( 1, 2 => array() ),
array( 'a', 'b', array(3, 4, ) ),
'c' => array( 'd', 5 => 'e' ),
)
EOT
);
/**
* Test cases for specific issues reported on the bugtracker
*/
# Test case for issue #0020787
$this->addArrayCase(
<<<'EOT'
array (
'additional_info',
'attachments',
'category_id',
'date_submitted',
'description',
'due_date',
)
EOT
);
# Test case for issue #0020812
$this->addArrayCase(
<<<'EOT'
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
)
EOT
);
# Test case for issue #0020813
$this->addArrayCase(
<<<'EOT'
array(
0 => "aa'aa",
1 => "bb\"bb"
)
EOT
);
# Test case for issue #0020850
$this->addArrayCase(
<<<'EOT'
array ( 0 => '""a"' )
EOT
);
# Test case for issue #0020851
$this->addArrayCase(
<<<'EOT'
array (
'a' => 'x1',
'x2',
)
EOT
);
}
}