1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12use Twig\Token; 13use Twig\TokenStream; 14 15class Twig_Tests_TokenStreamTest extends \PHPUnit\Framework\TestCase 16{ 17 protected static $tokens; 18 19 protected function setUp() 20 { 21 self::$tokens = [ 22 new Token(Token::TEXT_TYPE, 1, 1), 23 new Token(Token::TEXT_TYPE, 2, 1), 24 new Token(Token::TEXT_TYPE, 3, 1), 25 new Token(Token::TEXT_TYPE, 4, 1), 26 new Token(Token::TEXT_TYPE, 5, 1), 27 new Token(Token::TEXT_TYPE, 6, 1), 28 new Token(Token::TEXT_TYPE, 7, 1), 29 new Token(Token::EOF_TYPE, 0, 1), 30 ]; 31 } 32 33 /** 34 * @group legacy 35 */ 36 public function testLegacyConstructorSignature() 37 { 38 $stream = new TokenStream([], 'foo', '{{ foo }}'); 39 $this->assertEquals('foo', $stream->getFilename()); 40 $this->assertEquals('{{ foo }}', $stream->getSource()); 41 $this->assertEquals('foo', $stream->getSourceContext()->getName()); 42 $this->assertEquals('{{ foo }}', $stream->getSourceContext()->getCode()); 43 } 44 45 public function testNext() 46 { 47 $stream = new TokenStream(self::$tokens); 48 $repr = []; 49 while (!$stream->isEOF()) { 50 $token = $stream->next(); 51 52 $repr[] = $token->getValue(); 53 } 54 $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() advances the pointer and returns the current token'); 55 } 56 57 /** 58 * @expectedException \Twig\Error\SyntaxError 59 * @expectedExceptionMessage Unexpected end of template 60 */ 61 public function testEndOfTemplateNext() 62 { 63 $stream = new TokenStream([ 64 new Token(Token::BLOCK_START_TYPE, 1, 1), 65 ]); 66 while (!$stream->isEOF()) { 67 $stream->next(); 68 } 69 } 70 71 /** 72 * @expectedException \Twig\Error\SyntaxError 73 * @expectedExceptionMessage Unexpected end of template 74 */ 75 public function testEndOfTemplateLook() 76 { 77 $stream = new TokenStream([ 78 new Token(Token::BLOCK_START_TYPE, 1, 1), 79 ]); 80 while (!$stream->isEOF()) { 81 $stream->look(); 82 $stream->next(); 83 } 84 } 85} 86