4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
16 class CodeCleanerTest extends \PHPUnit\Framework\TestCase
19 * @dataProvider semicolonCodeProvider
21 public function testAutomaticSemicolons(array $lines, $requireSemicolons, $expected)
23 $cc = new CodeCleaner();
24 $this->assertSame($expected, $cc->clean($lines, $requireSemicolons));
27 public function semicolonCodeProvider()
30 [['true'], false, 'return true;'],
31 [['true;'], false, 'return true;'],
32 [['true;'], true, 'return true;'],
33 [['true'], true, false],
35 [['echo "foo";', 'true'], true, false],
37 [['echo "foo";', 'true'], false, "echo \"foo\";\nreturn true;"],
42 * @dataProvider unclosedStatementsProvider
44 public function testUnclosedStatements(array $lines, $isUnclosed)
46 $cc = new CodeCleaner();
47 $res = $cc->clean($lines);
50 $this->assertFalse($res);
52 $this->assertNotFalse($res);
56 public function unclosedStatementsProvider()
65 [['if (1) {}'], false],
67 [['// closed comment'], false],
68 [['function foo() { /**'], true],
70 [['var_dump(1, 2,'], true],
71 [['var_dump(1, 2,', '3)'], false],
76 * @dataProvider moreUnclosedStatementsProvider
78 public function testMoreUnclosedStatements(array $lines)
80 if (defined('HHVM_VERSION')) {
81 $this->markTestSkipped('HHVM not supported.');
84 $cc = new CodeCleaner();
85 $res = $cc->clean($lines);
87 $this->assertFalse($res);
90 public function moreUnclosedStatementsProvider()
93 [["\$content = <<<EOS\n"]],
94 [["\$content = <<<'EOS'\n"]],
96 [['/* unclosed comment']],
97 [['/** unclosed comment']],
102 * @dataProvider invalidStatementsProvider
103 * @expectedException \Psy\Exception\ParseErrorException
105 public function testInvalidStatementsThrowParseErrors($code)
107 $cc = new CodeCleaner();
111 public function invalidStatementsProvider()
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 :)