tests/TokenStreamTest.php
7e8cdd7b
 <?php
 
2119e60c
 namespace Twig\Tests;
 
7e8cdd7b
 /*
  * This file is part of Twig.
  *
  * (c) Fabien Potencier
  *
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
 
34bdab4d
 use PHPUnit\Framework\TestCase;
5ebcecf2
 use Twig\Error\SyntaxError;
90d579e4
 use Twig\Token;
 use Twig\TokenStream;
 
34bdab4d
 class TokenStreamTest extends TestCase
7e8cdd7b
 {
a009821d
     protected static $tokens;
7e8cdd7b
 
6a50ca06
     protected function setUp(): void
7e8cdd7b
     {
5c55243d
         self::$tokens = [
90d579e4
             new Token(Token::TEXT_TYPE, 1, 1),
             new Token(Token::TEXT_TYPE, 2, 1),
             new Token(Token::TEXT_TYPE, 3, 1),
             new Token(Token::TEXT_TYPE, 4, 1),
             new Token(Token::TEXT_TYPE, 5, 1),
             new Token(Token::TEXT_TYPE, 6, 1),
             new Token(Token::TEXT_TYPE, 7, 1),
             new Token(Token::EOF_TYPE, 0, 1),
5c55243d
         ];
7e8cdd7b
     }
 
4d8fc511
     public function testNext()
7e8cdd7b
     {
90d579e4
         $stream = new TokenStream(self::$tokens);
5c55243d
         $repr = [];
4d8fc511
         while (!$stream->isEOF()) {
             $token = $stream->next();
7e8cdd7b
 
4d8fc511
             $repr[] = $token->getValue();
         }
648c8435
         $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() advances the pointer and returns the current token');
7e8cdd7b
     }
b58df0e1
 
     public function testEndOfTemplateNext()
     {
5ebcecf2
         $this->expectException(SyntaxError::class);
b776e41f
         $this->expectExceptionMessage('Unexpected end of template');
 
90d579e4
         $stream = new TokenStream([
             new Token(Token::BLOCK_START_TYPE, 1, 1),
5c55243d
         ]);
b58df0e1
         while (!$stream->isEOF()) {
             $stream->next();
         }
     }
 
     public function testEndOfTemplateLook()
     {
5ebcecf2
         $this->expectException(SyntaxError::class);
b776e41f
         $this->expectExceptionMessage('Unexpected end of template');
 
90d579e4
         $stream = new TokenStream([
             new Token(Token::BLOCK_START_TYPE, 1, 1),
5c55243d
         ]);
b58df0e1
         while (!$stream->isEOF()) {
             $stream->look();
             $stream->next();
         }
     }
7e8cdd7b
 }