2195a4ff874b48b953d58302bac363ee470967f5
[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 "foo",'], true],
64
65             [['echo ""'],   false],
66             [["echo ''"],   false],
67             [['if (1) {}'], false],
68
69             [['// closed comment'],    false],
70             [['function foo() { /**'], true],
71
72             [['var_dump(1, 2,'], true],
73             [['var_dump(1, 2,', '3)'], false],
74         ];
75     }
76
77     /**
78      * @dataProvider moreUnclosedStatementsProvider
79      */
80     public function testMoreUnclosedStatements(array $lines)
81     {
82         if (\defined('HHVM_VERSION')) {
83             $this->markTestSkipped('HHVM not supported.');
84         }
85
86         $cc  = new CodeCleaner();
87         $res = $cc->clean($lines);
88
89         $this->assertFalse($res);
90     }
91
92     public function moreUnclosedStatementsProvider()
93     {
94         return [
95             [["\$content = <<<EOS\n"]],
96             [["\$content = <<<'EOS'\n"]],
97
98             [['/* unclosed comment']],
99             [['/** unclosed comment']],
100         ];
101     }
102
103     /**
104      * @dataProvider invalidStatementsProvider
105      * @expectedException \Psy\Exception\ParseErrorException
106      */
107     public function testInvalidStatementsThrowParseErrors($code)
108     {
109         $cc = new CodeCleaner();
110         $cc->clean([$code]);
111     }
112
113     public function invalidStatementsProvider()
114     {
115         // n.b. We used to check that `var_dump(1,2,)` failed, but PHP Parser
116         // 4.x backported trailing comma function calls from PHP 7.3 for free!
117         // so we're not going to spend too much time worrying about it :)
118
119         return [
120             ['function "what'],
121             ["function 'what"],
122             ['echo }'],
123             ['echo {'],
124             ['if (1) }'],
125             ['echo """'],
126             ["echo '''"],
127             ['$foo "bar'],
128             ['$foo \'bar'],
129         ];
130     }
131 }