CodeCleanerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. );
  63. }
  64. /**
  65. * @dataProvider moreUnclosedStatementsProvider
  66. */
  67. public function testMoreUnclosedStatements(array $lines)
  68. {
  69. if (defined('HHVM_VERSION')) {
  70. $this->markTestSkipped('HHVM not supported.');
  71. }
  72. $cc = new CodeCleaner();
  73. $res = $cc->clean($lines);
  74. $this->assertFalse($res);
  75. }
  76. public function moreUnclosedStatementsProvider()
  77. {
  78. return array(
  79. array(array("\$content = <<<EOS\n")),
  80. array(array("\$content = <<<'EOS'\n")),
  81. array(array('/* unclosed comment')),
  82. array(array('/** unclosed comment')),
  83. );
  84. }
  85. /**
  86. * @dataProvider invalidStatementsProvider
  87. * @expectedException \Psy\Exception\ParseErrorException
  88. */
  89. public function testInvalidStatementsThrowParseErrors($code)
  90. {
  91. $cc = new CodeCleaner();
  92. $cc->clean(array($code));
  93. }
  94. public function invalidStatementsProvider()
  95. {
  96. return array(
  97. array('function "what'),
  98. array("function 'what"),
  99. array('echo }'),
  100. array('echo {'),
  101. array('if (1) }'),
  102. array('echo """'),
  103. array("echo '''"),
  104. array('$foo "bar'),
  105. array('$foo \'bar'),
  106. );
  107. }
  108. }