No Description

CodeCleanerTest.php 3.2KB

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