<?php /* * This file is part of Psy Shell. * * (c) 2012-2023 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Psy\Test; use Psy\CodeCleaner; class CodeCleanerTest extends TestCase { /** * @dataProvider semicolonCodeProvider */ public function testAutomaticSemicolons(array $lines, $requireSemicolons, $expected) { $cc = new CodeCleaner(); $this->assertSame($expected, $cc->clean($lines, $requireSemicolons)); } public function semicolonCodeProvider() { return [ [['true'], false, 'return true;'], [['true;'], false, 'return true;'], [['true;'], true, 'return true;'], [['true'], true, false], [['echo "foo";', 'true'], true, false], [['echo "foo";', 'true'], false, "echo \"foo\";\nreturn true;"], ]; } /** * @dataProvider unclosedStatementsProvider */ public function testUnclosedStatements(array $lines, $isUnclosed) { $cc = new CodeCleaner(); $res = $cc->clean($lines); if ($isUnclosed) { $this->assertFalse($res); } else { $this->assertNotFalse($res); } } public function unclosedStatementsProvider() { return [ [['echo "'], true], [['echo \''], true], [['if (1) {'], true], [['echo "foo",'], true], [['echo ""'], false], [["echo ''"], false], [['if (1) {}'], false], [['// closed comment'], false], [['function foo() { /**'], true], [['var_dump(1, 2,'], true], [['var_dump(1, 2,', '3)'], false], ]; } /** * @dataProvider moreUnclosedStatementsProvider */ public function testMoreUnclosedStatements(array $lines) { $cc = new CodeCleaner(); $res = $cc->clean($lines); $this->assertFalse($res); } public function moreUnclosedStatementsProvider() { return [ [["\$content = <<<EOS\n"]], [["\$content = <<<'EOS'\n"]], [['/* unclosed comment']], [['/** unclosed comment']], ]; } /** * @dataProvider invalidStatementsProvider */ public function testInvalidStatementsThrowParseErrors($code) { $this->expectException(\Psy\Exception\ParseErrorException::class); $cc = new CodeCleaner(); $cc->clean([$code]); $this->fail(); } public function invalidStatementsProvider() { // n.b. We used to check that `var_dump(1,2,)` failed, but PHP Parser // 4.x backported trailing comma function calls from PHP 7.3 for free! // so we're not going to spend too much time worrying about it :) return [ ['function "what'], ["function 'what"], ['echo }'], ['echo {'], ['if (1) }'], ['echo """'], ["echo '''"], ['$foo "bar'], ['$foo \'bar'], ]; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
CodeCleaner | Folder | 0755 |
|
|
Command | Folder | 0755 |
|
|
Exception | Folder | 0755 |
|
|
Formatter | Folder | 0755 |
|
|
Input | Folder | 0755 |
|
|
Readline | Folder | 0755 |
|
|
Reflection | Folder | 0755 |
|
|
Sudo | Folder | 0755 |
|
|
TabCompletion | Folder | 0755 |
|
|
Util | Folder | 0755 |
|
|
VersionUpdater | Folder | 0755 |
|
|
fixtures | Folder | 0755 |
|
|
tools | Folder | 0755 |
|
|
ClassWithSecretConstructor.php | File | 507 B | 0644 |
|
ClassWithSecretParentConstructor.php | File | 358 B | 0644 |
|
ClassWithSecretiveParent.php | File | 340 B | 0644 |
|
ClassWithSecrets.php | File | 928 B | 0644 |
|
CodeCleanerTest.php | File | 3.26 KB | 0644 |
|
ConfigPathsTest.php | File | 7.08 KB | 0644 |
|
ConfigurationTest.php | File | 21.81 KB | 0644 |
|
ContextTest.php | File | 9.57 KB | 0644 |
|
FakeShell.php | File | 582 B | 0644 |
|
ParserFactoryTest.php | File | 1.22 KB | 0644 |
|
ParserTestCase.php | File | 2.51 KB | 0644 |
|
ShellTest.php | File | 20.03 KB | 0644 |
|
SudoTest.php | File | 5.49 KB | 0644 |
|
TestCase.php | File | 3.15 KB | 0644 |
|
TestableEnv.php | File | 560 B | 0644 |
|