PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nikic/php-parser/test/PhpParser/LexerTest.php

https://gitlab.com/judielsm/Handora
PHP | 246 lines | 205 code | 18 blank | 23 comment | 4 complexity | f0b32173aa86435e8fdd44fb85c1924e MD5 | raw file
  1. <?php
  2. namespace PhpParser;
  3. class LexerTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /* To allow overwriting in parent class */
  6. protected function getLexer(array $options = array()) {
  7. return new Lexer($options);
  8. }
  9. /**
  10. * @dataProvider provideTestError
  11. */
  12. public function testError($code, $message) {
  13. if (defined('HHVM_VERSION')) {
  14. $this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
  15. }
  16. $lexer = $this->getLexer();
  17. try {
  18. $lexer->startLexing($code);
  19. } catch (Error $e) {
  20. $this->assertSame($message, $e->getMessage());
  21. return;
  22. }
  23. $this->fail('Expected PhpParser\Error');
  24. }
  25. public function provideTestError() {
  26. return array(
  27. array('<?php /*', 'Unterminated comment on line 1'),
  28. array('<?php ' . "\1", 'Unexpected character "' . "\1" . '" (ASCII 1) on unknown line'),
  29. array('<?php ' . "\0", 'Unexpected null byte on unknown line'),
  30. );
  31. }
  32. /**
  33. * @dataProvider provideTestLex
  34. */
  35. public function testLex($code, $options, $tokens) {
  36. $lexer = $this->getLexer($options);
  37. $lexer->startLexing($code);
  38. while ($id = $lexer->getNextToken($value, $startAttributes, $endAttributes)) {
  39. $token = array_shift($tokens);
  40. $this->assertSame($token[0], $id);
  41. $this->assertSame($token[1], $value);
  42. $this->assertEquals($token[2], $startAttributes);
  43. $this->assertEquals($token[3], $endAttributes);
  44. }
  45. }
  46. public function provideTestLex() {
  47. return array(
  48. // tests conversion of closing PHP tag and drop of whitespace and opening tags
  49. array(
  50. '<?php tokens ?>plaintext',
  51. array(),
  52. array(
  53. array(
  54. Parser::T_STRING, 'tokens',
  55. array('startLine' => 1), array('endLine' => 1)
  56. ),
  57. array(
  58. ord(';'), '?>',
  59. array('startLine' => 1), array('endLine' => 1)
  60. ),
  61. array(
  62. Parser::T_INLINE_HTML, 'plaintext',
  63. array('startLine' => 1), array('endLine' => 1)
  64. ),
  65. )
  66. ),
  67. // tests line numbers
  68. array(
  69. '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
  70. array(),
  71. array(
  72. array(
  73. ord('$'), '$',
  74. array('startLine' => 2), array('endLine' => 2)
  75. ),
  76. array(
  77. Parser::T_STRING, 'token',
  78. array('startLine' => 2), array('endLine' => 2)
  79. ),
  80. array(
  81. ord('$'), '$',
  82. array(
  83. 'startLine' => 3,
  84. 'comments' => array(new Comment\Doc('/** doc' . "\n" . 'comment */', 2))
  85. ),
  86. array('endLine' => 3)
  87. ),
  88. )
  89. ),
  90. // tests comment extraction
  91. array(
  92. '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
  93. array(),
  94. array(
  95. array(
  96. Parser::T_STRING, 'token',
  97. array(
  98. 'startLine' => 2,
  99. 'comments' => array(
  100. new Comment('/* comment */', 1),
  101. new Comment('// comment' . "\n", 1),
  102. new Comment\Doc('/** docComment 1 */', 2),
  103. new Comment\Doc('/** docComment 2 */', 2),
  104. ),
  105. ),
  106. array('endLine' => 2)
  107. ),
  108. )
  109. ),
  110. // tests differing start and end line
  111. array(
  112. '<?php "foo' . "\n" . 'bar"',
  113. array(),
  114. array(
  115. array(
  116. Parser::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
  117. array('startLine' => 1), array('endLine' => 2)
  118. ),
  119. )
  120. ),
  121. // tests exact file offsets
  122. array(
  123. '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
  124. array('usedAttributes' => array('startFilePos', 'endFilePos')),
  125. array(
  126. array(
  127. Parser::T_CONSTANT_ENCAPSED_STRING, '"a"',
  128. array('startFilePos' => 6), array('endFilePos' => 8)
  129. ),
  130. array(
  131. ord(';'), ';',
  132. array('startFilePos' => 9), array('endFilePos' => 9)
  133. ),
  134. array(
  135. Parser::T_CONSTANT_ENCAPSED_STRING, '"b"',
  136. array('startFilePos' => 18), array('endFilePos' => 20)
  137. ),
  138. array(
  139. ord(';'), ';',
  140. array('startFilePos' => 21), array('endFilePos' => 21)
  141. ),
  142. )
  143. ),
  144. // tests token offsets
  145. array(
  146. '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
  147. array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
  148. array(
  149. array(
  150. Parser::T_CONSTANT_ENCAPSED_STRING, '"a"',
  151. array('startTokenPos' => 1), array('endTokenPos' => 1)
  152. ),
  153. array(
  154. ord(';'), ';',
  155. array('startTokenPos' => 2), array('endTokenPos' => 2)
  156. ),
  157. array(
  158. Parser::T_CONSTANT_ENCAPSED_STRING, '"b"',
  159. array('startTokenPos' => 5), array('endTokenPos' => 5)
  160. ),
  161. array(
  162. ord(';'), ';',
  163. array('startTokenPos' => 6), array('endTokenPos' => 6)
  164. ),
  165. )
  166. ),
  167. // tests all attributes being disabled
  168. array(
  169. '<?php /* foo */ $bar;',
  170. array('usedAttributes' => array()),
  171. array(
  172. array(
  173. Parser::T_VARIABLE, '$bar',
  174. array(), array()
  175. ),
  176. array(
  177. ord(';'), ';',
  178. array(), array()
  179. )
  180. )
  181. )
  182. );
  183. }
  184. /**
  185. * @dataProvider provideTestHaltCompiler
  186. */
  187. public function testHandleHaltCompiler($code, $remaining) {
  188. $lexer = $this->getLexer();
  189. $lexer->startLexing($code);
  190. while (Parser::T_HALT_COMPILER !== $lexer->getNextToken());
  191. $this->assertSame($remaining, $lexer->handleHaltCompiler());
  192. $this->assertSame(0, $lexer->getNextToken());
  193. }
  194. public function provideTestHaltCompiler() {
  195. return array(
  196. array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
  197. array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
  198. array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'),
  199. //array('<?php ... __halt_compiler();' . "\0", "\0"),
  200. //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
  201. );
  202. }
  203. /**
  204. * @expectedException \PhpParser\Error
  205. * @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
  206. */
  207. public function testHandleHaltCompilerError() {
  208. $lexer = $this->getLexer();
  209. $lexer->startLexing('<?php ... __halt_compiler invalid ();');
  210. while (Parser::T_HALT_COMPILER !== $lexer->getNextToken());
  211. $lexer->handleHaltCompiler();
  212. }
  213. public function testGetTokens() {
  214. $code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";';
  215. $expectedTokens = array(
  216. array(T_OPEN_TAG, '<?php ', 1),
  217. array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1),
  218. ';',
  219. array(T_WHITESPACE, "\n", 1),
  220. array(T_COMMENT, '// foo' . "\n", 2),
  221. array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
  222. ';',
  223. );
  224. $lexer = $this->getLexer();
  225. $lexer->startLexing($code);
  226. $this->assertSame($expectedTokens, $lexer->getTokens());
  227. }
  228. }