markTestSkipped('HHVM does not throw warnings from token_get_all()'); } $errorHandler = new ErrorHandler\Collecting(); $lexer = $this->getLexer(['usedAttributes' => [ 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos' ]]); $lexer->startLexing($code, $errorHandler); $errors = $errorHandler->getErrors(); $this->assertCount(count($messages), $errors); for ($i = 0; $i < count($messages); $i++) { $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code)); } } public function provideTestError() { return [ ["getLexer($options); $lexer->startLexing($code); while ($id = $lexer->getNextToken($value, $startAttributes, $endAttributes)) { $token = array_shift($tokens); $this->assertSame($token[0], $id); $this->assertSame($token[1], $value); $this->assertEquals($token[2], $startAttributes); $this->assertEquals($token[3], $endAttributes); } } public function provideTestLex() { return [ // tests conversion of closing PHP tag and drop of whitespace and opening tags [ 'plaintext', [], [ [ Tokens::T_STRING, 'tokens', ['startLine' => 1], ['endLine' => 1] ], [ ord(';'), '?>', ['startLine' => 1], ['endLine' => 1] ], [ Tokens::T_INLINE_HTML, 'plaintext', ['startLine' => 1, 'hasLeadingNewline' => false], ['endLine' => 1] ], ] ], // tests line numbers [ ' 2], ['endLine' => 2] ], [ Tokens::T_STRING, 'token', ['startLine' => 2], ['endLine' => 2] ], [ ord('$'), '$', [ 'startLine' => 3, 'comments' => [ new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14, 5, 3, 31, 5), ] ], ['endLine' => 3] ], ] ], // tests comment extraction [ ' 2, 'comments' => [ new Comment('/* comment */', 1, 6, 1, 1, 18, 1), new Comment('// comment', 1, 20, 3, 1, 29, 3), new Comment\Doc('/** docComment 1 */', 2, 31, 5, 2, 49, 5), new Comment\Doc('/** docComment 2 */', 2, 50, 6, 2, 68, 6), ], ], ['endLine' => 2] ], ] ], // tests differing start and end line [ ' 1], ['endLine' => 2] ], ] ], // tests exact file offsets [ ' ['startFilePos', 'endFilePos']], [ [ Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"', ['startFilePos' => 6], ['endFilePos' => 8] ], [ ord(';'), ';', ['startFilePos' => 9], ['endFilePos' => 9] ], [ Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"', ['startFilePos' => 18], ['endFilePos' => 20] ], [ ord(';'), ';', ['startFilePos' => 21], ['endFilePos' => 21] ], ] ], // tests token offsets [ ' ['startTokenPos', 'endTokenPos']], [ [ Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"', ['startTokenPos' => 1], ['endTokenPos' => 1] ], [ ord(';'), ';', ['startTokenPos' => 2], ['endTokenPos' => 2] ], [ Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"', ['startTokenPos' => 6], ['endTokenPos' => 6] ], [ ord(';'), ';', ['startTokenPos' => 7], ['endTokenPos' => 7] ], ] ], // tests all attributes being disabled [ ' []], [ [ Tokens::T_VARIABLE, '$bar', [], [] ], [ ord(';'), ';', [], [] ] ] ], // tests no tokens [ '', [], [] ], // tests PHP 8 T_NAME_* emulation [ ' []], [ [Tokens::T_NAME_QUALIFIED, 'Foo\Bar', [], []], [Tokens::T_NAME_FULLY_QUALIFIED, '\Foo\Bar', [], []], [Tokens::T_NAME_RELATIVE, 'namespace\Foo\Bar', [], []], [Tokens::T_NAME_QUALIFIED, 'Foo\Bar', [], []], [Tokens::T_NS_SEPARATOR, '\\', [], []], ] ], // tests PHP 8 T_NAME_* emulation with reserved keywords [ ' []], [ [Tokens::T_NAME_QUALIFIED, 'fn\use', [], []], [Tokens::T_NAME_FULLY_QUALIFIED, '\fn\use', [], []], [Tokens::T_NAME_RELATIVE, 'namespace\fn\use', [], []], [Tokens::T_NAME_QUALIFIED, 'fn\use', [], []], [Tokens::T_NS_SEPARATOR, '\\', [], []], ] ], ]; } /** * @dataProvider provideTestHaltCompiler */ public function testHandleHaltCompiler($code, $remaining) { $lexer = $this->getLexer(); $lexer->startLexing($code); while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken()); $this->assertSame($remaining, $lexer->handleHaltCompiler()); $this->assertSame(0, $lexer->getNextToken()); } public function provideTestHaltCompiler() { return [ ['Remaining Text', 'Remaining Text'], //array('expectException(Error::class); $this->expectExceptionMessage('__HALT_COMPILER must be followed by "();"'); $lexer = $this->getLexer(); $lexer->startLexing('getNextToken()); $lexer->handleHaltCompiler(); } public function testGetTokens() { $code = 'getLexer(); $lexer->startLexing($code); $this->assertSame($expectedTokens, $lexer->getTokens()); } }