EmulativeTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. class PHPParser_Tests_Lexer_EmulativeTest extends PHPUnit_Framework_TestCase
  3. {
  4. /** @var PHPParser_Lexer_Emulative */
  5. protected $lexer;
  6. protected function setUp() {
  7. $this->lexer = new PHPParser_Lexer_Emulative;
  8. }
  9. /**
  10. * @dataProvider provideTestReplaceKeywords
  11. */
  12. public function testReplaceKeywords($keyword, $expectedToken) {
  13. $this->lexer->startLexing('<?php ' . $keyword);
  14. $this->assertEquals($expectedToken, $this->lexer->getNextToken());
  15. $this->assertEquals(0, $this->lexer->getNextToken());
  16. }
  17. /**
  18. * @dataProvider provideTestReplaceKeywords
  19. */
  20. public function testNoReplaceKeywordsAfterObjectOperator($keyword) {
  21. $this->lexer->startLexing('<?php ->' . $keyword);
  22. $this->assertEquals(PHPParser_Parser::T_OBJECT_OPERATOR, $this->lexer->getNextToken());
  23. $this->assertEquals(PHPParser_Parser::T_STRING, $this->lexer->getNextToken());
  24. $this->assertEquals(0, $this->lexer->getNextToken());
  25. }
  26. public function provideTestReplaceKeywords() {
  27. return array(
  28. // PHP 5.5
  29. array('finally', PHPParser_Parser::T_FINALLY),
  30. array('yield', PHPParser_Parser::T_YIELD),
  31. // PHP 5.4
  32. array('callable', PHPParser_Parser::T_CALLABLE),
  33. array('insteadof', PHPParser_Parser::T_INSTEADOF),
  34. array('trait', PHPParser_Parser::T_TRAIT),
  35. array('__TRAIT__', PHPParser_Parser::T_TRAIT_C),
  36. // PHP 5.3
  37. array('__DIR__', PHPParser_Parser::T_DIR),
  38. array('goto', PHPParser_Parser::T_GOTO),
  39. array('namespace', PHPParser_Parser::T_NAMESPACE),
  40. array('__NAMESPACE__', PHPParser_Parser::T_NS_C),
  41. );
  42. }
  43. /**
  44. * @dataProvider provideTestLexNewFeatures
  45. */
  46. public function testLexNewFeatures($code, array $expectedTokens) {
  47. $this->lexer->startLexing('<?php ' . $code);
  48. foreach ($expectedTokens as $expectedToken) {
  49. list($expectedTokenType, $expectedTokenText) = $expectedToken;
  50. $this->assertEquals($expectedTokenType, $this->lexer->getNextToken($text));
  51. $this->assertEquals($expectedTokenText, $text);
  52. }
  53. $this->assertEquals(0, $this->lexer->getNextToken());
  54. }
  55. /**
  56. * @dataProvider provideTestLexNewFeatures
  57. */
  58. public function testLeaveStuffAloneInStrings($code) {
  59. $stringifiedToken = '"' . addcslashes($code, '"\\') . '"';
  60. $this->lexer->startLexing('<?php ' . $stringifiedToken);
  61. $this->assertEquals(PHPParser_Parser::T_CONSTANT_ENCAPSED_STRING, $this->lexer->getNextToken($text));
  62. $this->assertEquals($stringifiedToken, $text);
  63. $this->assertEquals(0, $this->lexer->getNextToken());
  64. }
  65. public function provideTestLexNewFeatures() {
  66. return array(
  67. array('0b1010110', array(
  68. array(PHPParser_Parser::T_LNUMBER, '0b1010110'),
  69. )),
  70. array('0b1011010101001010110101010010101011010101010101101011001110111100', array(
  71. array(PHPParser_Parser::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'),
  72. )),
  73. array('\\', array(
  74. array(PHPParser_Parser::T_NS_SEPARATOR, '\\'),
  75. )),
  76. array("<<<'NOWDOC'\nNOWDOC;\n", array(
  77. array(PHPParser_Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
  78. array(PHPParser_Parser::T_END_HEREDOC, 'NOWDOC'),
  79. array(ord(';'), ';'),
  80. )),
  81. array("<<<'NOWDOC'\nFoobar\nNOWDOC;\n", array(
  82. array(PHPParser_Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
  83. array(PHPParser_Parser::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"),
  84. array(PHPParser_Parser::T_END_HEREDOC, 'NOWDOC'),
  85. array(ord(';'), ';'),
  86. )),
  87. );
  88. }
  89. }