CodeCleanerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 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. return 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'), false, "echo 'foo';\nreturn true;"),
  30. array(array('echo "foo";', 'true'), true, false),
  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 array(
  49. array(array('echo "'), true),
  50. array(array('echo \''), true),
  51. array(array('if (1) {'), true),
  52. array(array('echo ""'), false),
  53. array(array("echo ''"), false),
  54. array(array('if (1) {}'), false),
  55. array(array("\$content = <<<EOS\n"), true),
  56. array(array("\$content = <<<'EOS'\n"), true),
  57. );
  58. }
  59. /**
  60. * @dataProvider invalidStatementsProvider
  61. * @expectedException Psy\Exception\ParseErrorException
  62. */
  63. public function testInvalidStatementsThrowParseErrors($code)
  64. {
  65. $cc = new CodeCleaner();
  66. $cc->clean(array($code));
  67. }
  68. public function invalidStatementsProvider()
  69. {
  70. return array(
  71. array('function "what'),
  72. array("function 'what"),
  73. array('echo }'),
  74. array('echo {'),
  75. array('if (1) }'),
  76. array('echo """'),
  77. array("echo '''"),
  78. array('$foo "bar'),
  79. array('$foo \'bar'),
  80. );
  81. }
  82. }