/
Beautifier_Bugs.phpt
executable file
·614 lines (595 loc) · 13.7 KB
/
Beautifier_Bugs.phpt
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Claudio Bustos <cdx@users.sourceforge.net> |
// | Jens Bierkandt <schtorch@users.sourceforge.net> |
// +----------------------------------------------------------------------+
//
// $Id:
require_once "PHPUnit.php";
if (file_exists('../Beautifier.php')) {
include_once '../Beautifier.php';
} else {
include_once "PHP/Beautifier.php";
}
class Beautifier_Bugs extends PHPUnit_TestCase {
function Beautifier_Bugs($name)
{
$this->PHPUnit_TestCase($name);
}
function setUp()
{
$this->oBeaut = new PHP_Beautifier();
}
function setText($sText)
{
$this->oBeaut->setInputString($sText);
$this->oBeaut->process();
}
/**
* HeredocBeforeCloseTag
* Close tag after heredoc remove whitespace,
* breaking the script.
*
*/
function testBugInternal1()
{
$sText = <<<SCRIPT
<?php
\$a = <<<HEREDOC
sdsdsds
HEREDOC;
?>
SCRIPT;
$this->setText($sText);
$sActual = $this->oBeaut->get();
$this->assertTrue(preg_match("/HEREDOC;\n\s*?\?>/ms", $sActual));
}
/**
* Bug 1597
* Brace after short comment in new line was appended to
* the comment, breaking the code
*/
function testBug1597()
{
$sText = <<<SCRIPT
<?php
if (\$_POST["url"] != "") //inserting data
{
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
if (\$_POST["url"] != "") //inserting data
{
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Bug 2301
* When I try to beautify PHP5 code with the 'throw new Exception'
* statement, the code is not formatted correctly. The
* whitespace between throw AND new is deleted.
*/
function testBug2301()
{
$sText = <<<SCRIPT
<?php
throw new AccountFindException();
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
throw new AccountFindException();
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Bug from Pavel Chtchevaev, 2004-11-17
* There's one more issue about the default filter, try beautifying with
* it the following string:
* "<?php\n\$o->_test1(\$c->test2()->test3())\n?>"
* It will return:
* "<?php\n \$o->_test1(\$c->test2() ->test3())\n?>"
*/
function testBugChtchevaev_2004_11_17()
{
$sText = <<<SCRIPT
<?php
\$o->_test1(\$c-> test2()-> test3());
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
\$o->_test1(\$c->test2()->test3());
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Bug 3257
* Comments between if and elseif screws up formatting.
* The beautifier will cascade and start moving the indentations over
* if there is a comment between if {} and elseif {}
*/
function testBug3257()
{
$sText = <<<SCRIPT
<?php
class Foo {
var \$foobar = 0;
function Foo(\$a, \$b) {
if (\$a) {
dostuff();
}
// \$a no good
elseif {
dootherstuff();
}
// \$c maybe
elseif {
yea();
}
}
function bar() {
echo "Hello";
}
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
class Foo {
var \$foobar = 0;
function Foo(\$a, \$b) {
if (\$a) {
dostuff();
}
// \$a no good
elseif {
dootherstuff();
}
// \$c maybe
elseif {
yea();
}
}
function bar() {
echo "Hello";
}
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Bug from Daniel Convissor, 2005-06-20
* Switch statements aren't coming out right. I, and most PEAR developers
* I've asked, partial to them looking like this:
* <code>
* switch ($subId) {
* case "myevents";
* $myeventsOn = "on";
* break;
* case "publicevents";
* $publiceventsOn = "on";
* break;
* }
* </code>
*/
function testBugConvissor_2005_06_20()
{
$this->oBeaut->addFilter("Pear");
$sText = <<<SCRIPT
<?php
switch (\$subId) {
case "myevents":
\$myeventsOn = "on";
break;
case "publicevents":
\$publiceventsOn = "on";
break;
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
switch (\$subId) {
case "myevents":
\$myeventsOn = "on";
break;
case "publicevents":
\$publiceventsOn = "on";
break;
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
function testBugJustinh_2005_07_26()
{
$sText = <<<SCRIPT
<?php
switch (\$var) {
case 1:
print "hi";
break;
case 2:
default:
break;
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
switch (\$var) {
case 1:
print "hi";
break;
case 2:
default:
break;
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
function testBugjuancarlos2005_09_13() {
$this->oBeaut->addFilter("ArrayNested");
$this->oBeaut->addFilter('IndentStyles',array('style'=>'allman'));
$sText = <<<SCRIPT
<?php include_once ("turnos.conf.php")
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php include_once ("turnos.conf.php")
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
function testBug5711() {
$this->oBeaut->addFilter("Pear");
$sText = <<<SCRIPT
<?php
class CampaignManagerConfig {
const BLOCKSIZE_ALL = 9999999;
public static function getStagingUrl(\$liveUrl) {
return true;
}
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
class CampaignManagerConfig
{
const BLOCKSIZE_ALL = 9999999;
public static function getStagingUrl(\$liveUrl)
{
return true;
}
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
function testBug6237() {
$this->oBeaut->addFilter("Pear");
$sText = <<<SCRIPT
<?php
\$_SESSION["test\$i"];
\$_SESSION["test_\$i"];
\$_SESSION['test_\$i'];
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
\$_SESSION["test\$i"];
\$_SESSION["test_\$i"];
\$_SESSION['test_\$i'];
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* according to: http://pear.php.net/manual/en/standards.control.php
* control strutures should be indented in K&R style
*
* if (<cond>) {
* <body>
* }
* however, are getting indented in Allman style
* all control structures are affected.
*/
function testBug7347() {
$this->oBeaut->addFilter("Pear");
$sText = <<<SCRIPT
<?php
class Foo {
public function __construct() {
if(\$foo && \$bar) { echo "FUBAR"; }
}
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
class Foo
{
public function __construct()
{
if (\$foo && \$bar) {
echo "FUBAR";
}
}
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Bad code to detect tokens
*/
function testInternal2() {
$this->assertTrue(array_key_exists(T_COMMENT,$this->oBeaut->aTokenFunctions));
}
/**
* Adding a comment after a case statement in a switch causes
* the indenting to be wrong.
*/
function testBug7759() {
$sText = <<<SCRIPT
<?php
echo 0;
switch(1) {
case 1:
case 5: // 5
echo 1;
break;
case 2: //2
echo "something";
echo "something";
case 3: /*3 */ /* 3? */
case 4:
default:
echo '2';
break;
}
echo 1;
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
echo 0;
switch (1) {
case 1:
case 5: // 5
echo 1;
break;
case 2: //2
echo "something";
echo "something";
case 3: /*3 */ /* 3? */
case 4:
default:
echo '2';
break;
}
echo 1;
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
function testBug7818() {
$this->oBeaut->startLog();
$sText=<<<SCRIPT
<?php
\$field->createElement(\$form, \$this->_table->{\$field->id}, \$defaults);
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
\$field->createElement(\$form, \$this->_table->{\$field->id}, \$defaults);
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* Will be great if you can rewrite T_OPEN_TAG_WITH_ECHO in the default
filter, specially "<?=" because it will be removed in
PHP6.
*/
function testBug7854() {
$this->oBeaut->addFilter("Pear");
$sText=<<<SCRIPT
<?= \$var ?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php echo \$var ?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* the first lines are intended if -l "ListClassFunction()"
* is enabled
*/
function testBug7307() {
// $this->oBeaut->startLog();
$this->oBeaut->addFilter("ListClassFunction");
$sText=<<<SCRIPT
<?php
/**
* Class and Function List:
* Function list:
* Classes list:
*/
require_once 'dbobject.class.php';
require_once 'kfp-globals.inc.php';
class test {
function m1() {}
function m2() {}
}
function f1() {
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
/**
* Class and Function List:
* Function list:
* - m1()
* - m2()
* - f1()
* Classes list:
* - test
*/
require_once 'dbobject.class.php';
require_once 'kfp-globals.inc.php';
class test {
function m1() {
}
function m2() {
}
}
function f1() {
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* When using the "break" command, the command takes an optional parameter, see http://de.php.net/break for details. But this doesn't work when using the beautifier, because, for example "break 2;" morphs to "break2;" (notice the missing space, which makes the PHP interpreter quite sour :-(
*/
function testBug_rolfhub_2007_02_07_1() {
$sText=<<<SCRIPT
<?php
\$i = 0;
while (++\$i) {
switch (\$i) {
case 5:
echo "At 5<br />";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
\$i = 0;
while (++\$i) {
switch (\$i) {
case 5:
echo "At 5<br />";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* When using the "break" command, the command takes an optional parameter, see http://de.php.net/break for details. But this doesn't work when using the beautifier, because, for example "break 2;" morphs to "break2;" (notice the missing space, which makes the PHP interpreter quite sour :-(
*/
function testBug_rolfhub_2007_02_07_1_pear() {
$this->oBeaut->addFilter("Pear");
$sText=<<<SCRIPT
<?php
\$i = 0;
while (++\$i) {
switch (\$i) {
case 5:
echo "At 5<br />";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
\$i = 0;
while (++\$i) {
switch (\$i) {
case 5:
echo "At 5<br />";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
/**
* The beautifer removes the whitespaces left and right of the operator, so for example "echo 2 . 1 . 0 . "\n";" becomes "echo 2.1.0."\n";"
*/
function testBug_rolfhub_2007_02_07_2() {
$sText=<<<SCRIPT
<?php
echo (1.0 . " " . 2 . 3);
?>
SCRIPT;
$this->setText($sText);
$sExpected = <<<SCRIPT
<?php
echo (1.0 . " " . 2 . 3);
?>
SCRIPT;
$this->assertEquals($sExpected, $this->oBeaut->get());
}
}
$suite = new PHPUnit_TestSuite('Beautifier_Bugs');
$result = PHPUnit::run($suite);
echo $result->toString();
?>