菜谱项目

CodeCleanerTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 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->assertEquals($expected, $cc->clean($lines, $requireSemicolons));
  21. }
  22. public function semicolonCodeProvider()
  23. {
  24. $values = array(
  25. array(array('true'), false, 'return true;'),
  26. array(array('true;'), false, 'return true;'),
  27. array(array('true;'), true, 'return true;'),
  28. array(array('true'), true, false),
  29. array(array('echo "foo";', 'true'), true, false),
  30. );
  31. if (version_compare(PHP_VERSION, '5.4', '<')) {
  32. $values[] = array(array('echo "foo";', 'true'), false, "echo 'foo';\nreturn true;");
  33. } else {
  34. $values[] = array(array('echo "foo";', 'true'), false, "echo \"foo\";\nreturn true;");
  35. }
  36. return $values;
  37. }
  38. /**
  39. * @dataProvider unclosedStatementsProvider
  40. */
  41. public function testUnclosedStatements(array $lines, $isUnclosed)
  42. {
  43. $cc = new CodeCleaner();
  44. $res = $cc->clean($lines);
  45. if ($isUnclosed) {
  46. $this->assertFalse($res);
  47. } else {
  48. $this->assertNotFalse($res);
  49. }
  50. }
  51. public function unclosedStatementsProvider()
  52. {
  53. return array(
  54. array(array('echo "'), true),
  55. array(array('echo \''), true),
  56. array(array('if (1) {'), true),
  57. array(array('echo ""'), false),
  58. array(array("echo ''"), false),
  59. array(array('if (1) {}'), false),
  60. array(array('// closed comment'), false),
  61. array(array('function foo() { /**'), true),
  62. array(array('var_dump(1, 2,'), true),
  63. array(array('var_dump(1, 2,', '3)'), false),
  64. );
  65. }
  66. /**
  67. * @dataProvider moreUnclosedStatementsProvider
  68. */
  69. public function testMoreUnclosedStatements(array $lines)
  70. {
  71. if (defined('HHVM_VERSION')) {
  72. $this->markTestSkipped('HHVM not supported.');
  73. }
  74. $cc = new CodeCleaner();
  75. $res = $cc->clean($lines);
  76. $this->assertFalse($res);
  77. }
  78. public function moreUnclosedStatementsProvider()
  79. {
  80. return array(
  81. array(array("\$content = <<<EOS\n")),
  82. array(array("\$content = <<<'EOS'\n")),
  83. array(array('/* unclosed comment')),
  84. array(array('/** unclosed comment')),
  85. );
  86. }
  87. /**
  88. * @dataProvider invalidStatementsProvider
  89. * @expectedException \Psy\Exception\ParseErrorException
  90. */
  91. public function testInvalidStatementsThrowParseErrors($code)
  92. {
  93. $cc = new CodeCleaner();
  94. $cc->clean(array($code));
  95. }
  96. public function invalidStatementsProvider()
  97. {
  98. return array(
  99. array('function "what'),
  100. array("function 'what"),
  101. array('echo }'),
  102. array('echo {'),
  103. array('if (1) }'),
  104. array('echo """'),
  105. array("echo '''"),
  106. array('$foo "bar'),
  107. array('$foo \'bar'),
  108. array('var_dump(1,2,)'),
  109. );
  110. }
  111. }