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()
63 [['echo "foo",'], true],
67 [['if (1) {}'], false],
69 [['// closed comment'], false],
70 [['function foo() { /**'], true],
72 [['var_dump(1, 2,'], true],
73 [['var_dump(1, 2,', '3)'], false],
78 * @dataProvider moreUnclosedStatementsProvider
80 public function testMoreUnclosedStatements(array $lines)
82 if (\defined('HHVM_VERSION')) {
83 $this->markTestSkipped('HHVM not supported.');
86 $cc = new CodeCleaner();
87 $res = $cc->clean($lines);
89 $this->assertFalse($res);
92 public function moreUnclosedStatementsProvider()
95 [["\$content = <<<EOS\n"]],
96 [["\$content = <<<'EOS'\n"]],
98 [['/* unclosed comment']],
99 [['/** unclosed comment']],
104 * @dataProvider invalidStatementsProvider
105 * @expectedException \Psy\Exception\ParseErrorException
107 public function testInvalidStatementsThrowParseErrors($code)
109 $cc = new CodeCleaner();
113 public function invalidStatementsProvider()
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 :)