mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
made name required for Twig_Source
This commit is contained in:
+1
-1
@@ -25,7 +25,7 @@ class Twig_Source
|
||||
* @param string $name The template logical name
|
||||
* @param string $path The filesystem path of the template if any
|
||||
*/
|
||||
public function __construct($code, $name = null, $path = null)
|
||||
public function __construct($code, $name, $path = '')
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->name = $name;
|
||||
|
||||
@@ -47,7 +47,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
||||
public function testArrayExpression($template, $expected)
|
||||
{
|
||||
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
|
||||
$stream = $env->tokenize(new Twig_Source($template));
|
||||
$stream = $env->tokenize(new Twig_Source($template, ''));
|
||||
$parser = new Twig_Parser($env);
|
||||
|
||||
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
|
||||
@@ -167,7 +167,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
||||
public function testStringExpression($template, $expected)
|
||||
{
|
||||
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
|
||||
$stream = $env->tokenize(new Twig_Source($template));
|
||||
$stream = $env->tokenize(new Twig_Source($template, ''));
|
||||
$parser = new Twig_Parser($env);
|
||||
|
||||
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
|
||||
|
||||
@@ -26,7 +26,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{% § %}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
$stream->expect(Twig_Token::BLOCK_START_TYPE);
|
||||
$this->assertSame('§', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
|
||||
@@ -37,7 +37,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ §() }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$this->assertSame('§', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
|
||||
@@ -54,7 +54,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
protected function countToken($template, $type, $value = null)
|
||||
{
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
$count = 0;
|
||||
while (!$stream->isEOF()) {
|
||||
@@ -79,7 +79,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
."}}\n";
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// foo\nbar\n
|
||||
$this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
|
||||
@@ -99,7 +99,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
."}}\n";
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// foo\nbar
|
||||
$this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
|
||||
@@ -114,7 +114,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{# '.str_repeat('*', 100000).' #}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// should not throw an exception
|
||||
}
|
||||
@@ -124,7 +124,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{% verbatim %}'.str_repeat('*', 100000).'{% endverbatim %}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// should not throw an exception
|
||||
}
|
||||
@@ -134,7 +134,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ '.str_repeat('x', 100000).' }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// should not throw an exception
|
||||
}
|
||||
@@ -144,7 +144,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{% '.str_repeat('x', 100000).' %}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
|
||||
// should not throw an exception
|
||||
}
|
||||
@@ -154,7 +154,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ 922337203685477580700 }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->next();
|
||||
$node = $stream->next();
|
||||
$this->assertEquals('922337203685477580700', $node->getValue());
|
||||
@@ -168,7 +168,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
foreach ($tests as $template => $expected) {
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, $expected);
|
||||
}
|
||||
@@ -179,7 +179,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = 'foo {{ "bar #{ baz + 1 }" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::TEXT_TYPE, 'foo ');
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
@@ -196,7 +196,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ "bar \#{baz+1}" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar #{baz+1}');
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
@@ -207,7 +207,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ "bar # baz" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar # baz');
|
||||
$stream->expect(Twig_Token::VAR_END_TYPE);
|
||||
@@ -222,7 +222,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ "bar #{x" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
}
|
||||
|
||||
public function testStringWithNestedInterpolations()
|
||||
@@ -230,7 +230,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{{ "bar #{ "foo#{bar}" }" }}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
$stream->expect(Twig_Token::INTERPOLATION_START_TYPE);
|
||||
@@ -247,7 +247,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = '{% foo "bar #{ "foo#{bar}" }" %}';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::BLOCK_START_TYPE);
|
||||
$stream->expect(Twig_Token::NAME_TYPE, 'foo');
|
||||
$stream->expect(Twig_Token::STRING_TYPE, 'bar ');
|
||||
@@ -265,7 +265,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
$template = "{{ 1 and\n0}}";
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template));
|
||||
$stream = $lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
$stream->expect(Twig_Token::VAR_START_TYPE);
|
||||
$stream->expect(Twig_Token::NUMBER_TYPE, 1);
|
||||
$stream->expect(Twig_Token::OPERATOR_TYPE, 'and');
|
||||
@@ -273,7 +273,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage Unclosed "variable" at line 3
|
||||
* @expectedExceptionMessage Unclosed "variable" in "index" at line 3
|
||||
*/
|
||||
public function testUnterminatedVariable()
|
||||
{
|
||||
@@ -287,12 +287,12 @@ bar
|
||||
';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage Unclosed "block" at line 3
|
||||
* @expectedExceptionMessage Unclosed "block" in "index" at line 3
|
||||
*/
|
||||
public function testUnterminatedBlock()
|
||||
{
|
||||
@@ -306,6 +306,6 @@ bar
|
||||
';
|
||||
|
||||
$lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$lexer->tokenize(new Twig_Source($template));
|
||||
$lexer->tokenize(new Twig_Source($template, 'index'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class Twig_Tests_Loader_ChainTest extends PHPUnit_Framework_TestCase
|
||||
));
|
||||
|
||||
$this->assertEquals('foo', $loader->getSourceContext('foo')->getName());
|
||||
$this->assertNull($loader->getSourceContext('foo')->getPath());
|
||||
$this->assertSame('', $loader->getSourceContext('foo')->getPath());
|
||||
|
||||
$this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName());
|
||||
$this->assertNull($loader->getSourceContext('errors/index.html')->getPath());
|
||||
|
||||
@@ -148,7 +148,7 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
|
||||
{{ foo }}
|
||||
{% endmacro %}
|
||||
EOF
|
||||
)));
|
||||
, 'index')));
|
||||
}
|
||||
|
||||
protected function getParser()
|
||||
@@ -156,7 +156,7 @@ EOF
|
||||
$parser = new TestParser(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
|
||||
$parser->setParent(new Twig_Node());
|
||||
$parser->stream = $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock();
|
||||
$parser->stream->expects($this->any())->method('getSourceContext')->will($this->returnValue(new Twig_Source('')));
|
||||
$parser->stream->expects($this->any())->method('getSourceContext')->will($this->returnValue(new Twig_Source('', '')));
|
||||
|
||||
return $parser;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user