e00b0678e2ab45e36e2537e02229035666d9fa33
[yaffs-website] / vendor / psy / psysh / test / CodeCleanerTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test;
13
14 use Psy\CodeCleaner;
15
16 class CodeCleanerTest extends \PHPUnit\Framework\TestCase
17 {
18     /**
19      * @dataProvider semicolonCodeProvider
20      */
21     public function testAutomaticSemicolons(array $lines, $requireSemicolons, $expected)
22     {
23         $cc = new CodeCleaner();
24         $this->assertSame($expected, $cc->clean($lines, $requireSemicolons));
25     }
26
27     public function semicolonCodeProvider()
28     {
29         return [
30             [['true'],  false, 'return true;'],
31             [['true;'], false, 'return true;'],
32             [['true;'], true,  'return true;'],
33             [['true'],  true,  false],
34
35             [['echo "foo";', 'true'], true,  false],
36
37             [['echo "foo";', 'true'], false, "echo \"foo\";\nreturn true;"],
38         ];
39     }
40
41     /**
42      * @dataProvider unclosedStatementsProvider
43      */
44     public function testUnclosedStatements(array $lines, $isUnclosed)
45     {
46         $cc  = new CodeCleaner();
47         $res = $cc->clean($lines);
48
49         if ($isUnclosed) {
50             $this->assertFalse($res);
51         } else {
52             $this->assertNotFalse($res);
53         }
54     }
55
56     public function unclosedStatementsProvider()
57     {
58         return [
59             [['echo "'],   true],
60             [['echo \''],  true],
61             [['if (1) {'], true],
62
63             [['echo ""'],   false],
64             [["echo ''"],   false],
65             [['if (1) {}'], false],
66
67             [['// closed comment'],    false],
68             [['function foo() { /**'], true],
69
70             [['var_dump(1, 2,'], true],
71             [['var_dump(1, 2,', '3)'], false],
72         ];
73     }
74
75     /**
76      * @dataProvider moreUnclosedStatementsProvider
77      */
78     public function testMoreUnclosedStatements(array $lines)
79     {
80         if (defined('HHVM_VERSION')) {
81             $this->markTestSkipped('HHVM not supported.');
82         }
83
84         $cc  = new CodeCleaner();
85         $res = $cc->clean($lines);
86
87         $this->assertFalse($res);
88     }
89
90     public function moreUnclosedStatementsProvider()
91     {
92         return [
93             [["\$content = <<<EOS\n"]],
94             [["\$content = <<<'EOS'\n"]],
95
96             [['/* unclosed comment']],
97             [['/** unclosed comment']],
98         ];
99     }
100
101     /**
102      * @dataProvider invalidStatementsProvider
103      * @expectedException \Psy\Exception\ParseErrorException
104      */
105     public function testInvalidStatementsThrowParseErrors($code)
106     {
107         $cc = new CodeCleaner();
108         $cc->clean([$code]);
109     }
110
111     public function invalidStatementsProvider()
112     {
113         // n.b. We used to check that `var_dump(1,2,)` failed, but PHP Parser
114         // 4.x backported trailing comma function calls from PHP 7.3 for free!
115         // so we're not going to spend too much time worrying about it :)
116
117         return [
118             ['function "what'],
119             ["function 'what"],
120             ['echo }'],
121             ['echo {'],
122             ['if (1) }'],
123             ['echo """'],
124             ["echo '''"],
125             ['$foo "bar'],
126             ['$foo \'bar'],
127         ];
128     }
129 }