LexerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class PHPParser_Tests_LexerTest extends PHPUnit_Framework_TestCase
  3. {
  4. /** @var PHPParser_Lexer */
  5. protected $lexer;
  6. protected function setUp() {
  7. $this->lexer = new PHPParser_Lexer;
  8. }
  9. /**
  10. * @dataProvider provideTestError
  11. */
  12. public function testError($code, $message) {
  13. try {
  14. $this->lexer->startLexing($code);
  15. } catch (PHPParser_Error $e) {
  16. $this->assertEquals($message, $e->getMessage());
  17. return;
  18. }
  19. $this->fail('Expected PHPParser_Error');
  20. }
  21. public function provideTestError() {
  22. return array(
  23. array('<?php /*', 'Unterminated comment on line 1'),
  24. array('<?php ' . "\1", 'Unexpected character "' . "\1" . '" (ASCII 1) on unknown line'),
  25. array('<?php ' . "\0", 'Unexpected null byte on unknown line'),
  26. );
  27. }
  28. /**
  29. * @dataProvider provideTestLex
  30. */
  31. public function testLex($code, $tokens) {
  32. $this->lexer->startLexing($code);
  33. while ($id = $this->lexer->getNextToken($value, $startAttributes, $endAttributes)) {
  34. $token = array_shift($tokens);
  35. $this->assertEquals($token[0], $id);
  36. $this->assertEquals($token[1], $value);
  37. $this->assertEquals($token[2], $startAttributes);
  38. $this->assertEquals($token[3], $endAttributes);
  39. }
  40. }
  41. public function provideTestLex() {
  42. return array(
  43. // tests conversion of closing PHP tag and drop of whitespace and opening tags
  44. array(
  45. '<?php tokens ?>plaintext',
  46. array(
  47. array(
  48. PHPParser_Parser::T_STRING, 'tokens',
  49. array('startLine' => 1), array('endLine' => 1)
  50. ),
  51. array(
  52. ord(';'), '?>',
  53. array('startLine' => 1), array('endLine' => 1)
  54. ),
  55. array(
  56. PHPParser_Parser::T_INLINE_HTML, 'plaintext',
  57. array('startLine' => 1), array('endLine' => 1)
  58. ),
  59. )
  60. ),
  61. // tests line numbers
  62. array(
  63. '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
  64. array(
  65. array(
  66. ord('$'), '$',
  67. array('startLine' => 2), array('endLine' => 2)
  68. ),
  69. array(
  70. PHPParser_Parser::T_STRING, 'token',
  71. array('startLine' => 2), array('endLine' => 2)
  72. ),
  73. array(
  74. ord('$'), '$',
  75. array(
  76. 'startLine' => 3,
  77. 'comments' => array(new PHPParser_Comment_Doc('/** doc' . "\n" . 'comment */', 2))
  78. ),
  79. array('endLine' => 3)
  80. ),
  81. )
  82. ),
  83. // tests comment extraction
  84. array(
  85. '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
  86. array(
  87. array(
  88. PHPParser_Parser::T_STRING, 'token',
  89. array(
  90. 'startLine' => 2,
  91. 'comments' => array(
  92. new PHPParser_Comment('/* comment */', 1),
  93. new PHPParser_Comment('// comment' . "\n", 1),
  94. new PHPParser_Comment_Doc('/** docComment 1 */', 2),
  95. new PHPParser_Comment_Doc('/** docComment 2 */', 2),
  96. ),
  97. ),
  98. array('endLine' => 2)
  99. ),
  100. )
  101. ),
  102. // tests differing start and end line
  103. array(
  104. '<?php "foo' . "\n" . 'bar"',
  105. array(
  106. array(
  107. PHPParser_Parser::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
  108. array('startLine' => 1), array('endLine' => 2)
  109. ),
  110. )
  111. ),
  112. );
  113. }
  114. /**
  115. * @dataProvider provideTestHaltCompiler
  116. */
  117. public function testHandleHaltCompiler($code, $remaining) {
  118. $this->lexer->startLexing($code);
  119. while (PHPParser_Parser::T_HALT_COMPILER !== $this->lexer->getNextToken());
  120. $this->assertEquals($this->lexer->handleHaltCompiler(), $remaining);
  121. $this->assertEquals(0, $this->lexer->getNextToken());
  122. }
  123. public function provideTestHaltCompiler() {
  124. return array(
  125. array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
  126. array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
  127. array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'),
  128. //array('<?php ... __halt_compiler();' . "\0", "\0"),
  129. //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
  130. );
  131. }
  132. }