This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
<?php
/*
* 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.
*/
require_once __DIR__.'/../../../../lib/Twig/Extensions/Extension/Array.php';
class Twig_Tests_Extension_ArrayTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider getShuffleFilterTestData
*/
public function testShuffleFilter($data, $expectedElements)
{
foreach ($expectedElements as $element) {
$this->assertTrue(in_array($element, twig_shuffle_filter($data), true)); // assertContains() would not consider the type
}
}
public function testShuffleFilterOnEmptyArray()
{
$this->assertEquals(array(), twig_shuffle_filter(array()));
}
public function getShuffleFilterTestData()
{
return array(
array(
array(1, 2, 3),
array(1, 2, 3),
),
array(
array('a' => 'apple', 'b' => 'orange', 'c' => 'citrus'),
array('apple', 'orange', 'citrus'),
),
array(
new ArrayObject(array('apple', 'orange', 'citrus')),
array('apple', 'orange', 'citrus'),
),
);
}
}

View File

@@ -0,0 +1,112 @@
<?php
/*
* 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.
*/
/**
* @author Robin van der Vleuten <robinvdvleuten@gmail.com>
*/
class Twig_Tests_Extension_DateTest extends \PHPUnit\Framework\TestCase
{
/**
* @var TwigEnvironment
*/
private $env;
public function setUp()
{
$this->env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
}
/**
* @dataProvider getDiffTestData()
*/
public function testDiffWithStringsFromGivenNow($expected, $translated, $date, $now)
{
$extension = new Twig_Extensions_Extension_Date();
$this->assertEquals($expected, $extension->diff($this->env, $date, $now));
}
public function testDiffWithStringsFromNow()
{
$extension = new Twig_Extensions_Extension_Date();
$this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, '24-07-2014'));
}
/**
* @dataProvider getDiffTestData()
*/
public function testDiffWithDateTimeFromGivenNow($expected, $translated, $date, $now)
{
$extension = new Twig_Extensions_Extension_Date();
$this->assertEquals($expected, $extension->diff($this->env, new DateTime($date), new DateTime($now)));
}
public function testDiffWithDateTimeFromNow()
{
$extension = new Twig_Extensions_Extension_Date();
$this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, new DateTime('24-07-2014')));
}
/**
* @dataProvider getDiffTestData()
*/
public function testDiffCanReturnTranslatableString($expected, $translated, $date, $now)
{
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
$translator
->expects($this->once())
->method('transChoice')
->with($translated);
$extension = new Twig_Extensions_Extension_Date($translator);
$extension->diff($this->env, $date, $now);
}
public function getDiffTestData()
{
return array_merge($this->getDiffAgoTestData(), $this->getDiffInTestData());
}
public function getDiffAgoTestData()
{
return array(
array('1 second ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:02'),
array('5 seconds ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:06'),
array('1 minute ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:29:01'),
array('5 minutes ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:33:03'),
array('1 hour ago', 'diff.ago.hour', '24-07-2014 17:28:01', '24-07-2014 18:29:01'),
array('9 hours ago', 'diff.ago.hour', '24-07-2014 17:28:01', '25-07-2014 02:33:03'),
array('1 day ago', 'diff.ago.day', '23-07-2014', '24-07-2014'),
array('5 days ago', 'diff.ago.day', '19-07-2014', '24-07-2014'),
array('1 month ago', 'diff.ago.month', '23-07-2014', '24-08-2014'),
array('6 months ago', 'diff.ago.month', '19-07-2014', '24-01-2015'),
array('1 year ago', 'diff.ago.year', '19-07-2014', '20-08-2015'),
array('3 years ago', 'diff.ago.year', '19-07-2014', '20-08-2017'),
);
}
public function getDiffInTestData()
{
return array(
array('in 1 second', 'diff.in.second', '24-07-2014 17:28:02', '24-07-2014 17:28:01'),
array('in 5 seconds', 'diff.in.second', '24-07-2014 17:28:06', '24-07-2014 17:28:01'),
array('in 1 minute', 'diff.in.minute', '24-07-2014 17:29:01', '24-07-2014 17:28:01'),
array('in 5 minutes', 'diff.in.minute', '24-07-2014 17:33:03', '24-07-2014 17:28:01'),
array('in 1 hour', 'diff.in.hour', '24-07-2014 18:29:01', '24-07-2014 17:28:01'),
array('in 9 hours', 'diff.in.hour', '25-07-2014 02:33:03', '24-07-2014 17:28:01'),
array('in 1 day', 'diff.in.day', '24-07-2014', '23-07-2014'),
array('in 5 days', 'diff.in.day', '24-07-2014', '19-07-2014'),
array('in 1 month', 'diff.in.month', '24-08-2014', '23-07-2014'),
array('in 6 months', 'diff.in.month', '24-01-2015', '19-07-2014'),
array('in 1 year', 'diff.in.year', '20-08-2015', '19-07-2014'),
array('in 3 years', 'diff.in.year', '20-08-2017', '19-07-2014'),
);
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* 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.
*/
class Twig_Tests_Extension_IntlTest extends \PHPUnit\Framework\TestCase
{
/**
* @requires extension intl
* @requires PHP 5.5
*/
public function testLocalizedDateFilterWithDateTimeZone()
{
class_exists('Twig_Extensions_Extension_Intl');
$env = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$date = twig_localized_date_filter($env, new DateTime('2015-01-01T00:00:00', new DateTimeZone('UTC')), 'short', 'long', 'en', '+01:00');
$this->assertEquals('1/1/15 1:00:00 AM GMT+01:00', $date);
}
/**
* @requires extension intl
* @requires PHP 5.5
*/
public function testLocalizedDateFilterWithDateTimeZoneZ()
{
class_exists('Twig_Extensions_Extension_Intl');
$env = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$date = twig_localized_date_filter($env, new DateTime('2017-11-19T00:00:00Z'), 'short', 'long', 'fr', 'Z');
$this->assertEquals('19/11/2017 00:00:00 UTC', $date);
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* 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.
*/
require_once __DIR__.'/../../../../lib/Twig/Extensions/Extension/Text.php';
class Twig_Tests_Extension_TextTest extends \PHPUnit\Framework\TestCase
{
/** @var TwigEnvironment */
private $env;
public function setUp()
{
$this->env = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$this->env
->expects($this->any())
->method('getCharset')
->will($this->returnValue('utf-8'))
;
}
/**
* @dataProvider getTruncateTestData
*/
public function testTruncate($input, $length, $preserve, $separator, $expectedOutput)
{
$output = twig_truncate_filter($this->env, $input, $length, $preserve, $separator);
$this->assertEquals($expectedOutput, $output);
}
public function getTruncateTestData()
{
return array(
array('This is a very long sentence.', 2, false, '...', 'Th...'),
array('This is a very long sentence.', 6, false, '...', 'This i...'),
array('This is a very long sentence.', 2, true, '...', 'This...'),
array('This is a very long sentence.', 2, true, '[...]', 'This[...]'),
array('This is a very long sentence.', 23, false, '...', 'This is a very long sen...'),
array('This is a very long sentence.', 23, true, '...', 'This is a very long sentence.'),
);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_ArgumentsTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Arguments('foo');
$this->assertEquals('<foo:arguments>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_ArrayTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Array('foo');
$this->assertEquals('<foo:array>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_BodyTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Body('foo');
$this->assertEquals('<foo:body>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_BooleanTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Boolean('foo');
$this->assertEquals('<foo:boolean>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_ConstantTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Constant('foo');
$this->assertEquals('foo', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_ExpressionTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Expression('foo');
$this->assertEquals('<foo>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_NumberTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Number('foo');
$this->assertEquals('<foo:number>', (string) $grammar);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_OptionalTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Optional(new Twig_Extensions_Grammar_Constant('foo'), new Twig_Extensions_Grammar_Number('bar'));
$this->assertEquals('[foo <bar:number>]', (string) $grammar);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* 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.
*/
class Twig_Tests_Grammar_TagTest extends \PHPUnit\Framework\TestCase
{
public function testMagicToString()
{
$grammar = new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Constant('foo'),
new Twig_Extensions_Grammar_Number('bar'),
new Twig_Extensions_Grammar_Optional(new Twig_Extensions_Grammar_Constant('foo'), new Twig_Extensions_Grammar_Number('bar'))
);
$this->assertEquals('foo <bar:number> [foo <bar:number>]', (string) $grammar);
}
}

View File

@@ -0,0 +1,113 @@
<?php
/*
* 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.
*/
class Twig_Tests_Node_TransTest extends Twig_Test_NodeTestCase
{
/**
* @covers \Twig_Node_Trans::__construct
*/
public function testConstructor()
{
$count = new Twig_Node_Expression_Constant(12, 0);
$body = new Twig_Node(array(
new Twig_Node_Text('Hello', 0),
), array(), 0);
$plural = new Twig_Node(array(
new Twig_Node_Text('Hey ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
new Twig_Node_Text(', I have ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
new Twig_Node_Text(' apples', 0),
), array(), 0);
$node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
$this->assertEquals($body, $node->getNode('body'));
$this->assertEquals($count, $node->getNode('count'));
$this->assertEquals($plural, $node->getNode('plural'));
}
public function getTests()
{
$tests = array();
$body = new Twig_Node_Expression_Name('foo', 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
$tests[] = array($node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo')));
$body = new Twig_Node_Expression_Constant('Hello', 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
$tests[] = array($node, 'echo gettext("Hello");');
$body = new Twig_Node(array(
new Twig_Node_Text('Hello', 0),
), array(), 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
$tests[] = array($node, 'echo gettext("Hello");');
$body = new Twig_Node(array(
new Twig_Node_Text('J\'ai ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
new Twig_Node_Text(' pommes', 0),
), array(), 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
$tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
$count = new Twig_Node_Expression_Constant(12, 0);
$body = new Twig_Node(array(
new Twig_Node_Text('Hey ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
new Twig_Node_Text(', I have one apple', 0),
), array(), 0);
$plural = new Twig_Node(array(
new Twig_Node_Text('Hey ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
new Twig_Node_Text(', I have ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
new Twig_Node_Text(' apples', 0),
), array(), 0);
$node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
$tests[] = array($node, sprintf('echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s, "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), $this->getVariableGetter('name')));
// with escaper extension set to on
$body = new Twig_Node(array(
new Twig_Node_Text('J\'ai ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Filter(new Twig_Node_Expression_Name('foo', 0), new Twig_Node_Expression_Constant('escape', 0), new Twig_Node(), 0), 0),
new Twig_Node_Text(' pommes', 0),
), array(), 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
$tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
// with notes
$body = new Twig_Node_Expression_Constant('Hello', 0);
$notes = new Twig_Node_Text('Notes for translators', 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
$tests[] = array($node, "// notes: Notes for translators\necho gettext(\"Hello\");");
$body = new Twig_Node_Expression_Constant('Hello', 0);
$notes = new Twig_Node_Text("Notes for translators\nand line breaks", 0);
$node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
$tests[] = array($node, "// notes: Notes for translators and line breaks\necho gettext(\"Hello\");");
$count = new Twig_Node_Expression_Constant(5, 0);
$body = new Twig_Node_Text('There is 1 pending task', 0);
$plural = new Twig_Node(array(
new Twig_Node_Text('There are ', 0),
new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
new Twig_Node_Text(' pending tasks', 0),
), array(), 0);
$notes = new Twig_Node_Text('Notes for translators', 0);
$node = new Twig_Extensions_Node_Trans($body, $plural, $count, $notes, 0);
$tests[] = array($node, "// notes: Notes for translators\n".'echo strtr(ngettext("There is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));');
return $tests;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* 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.
*/
class SimpleTokenParser extends Twig_Extensions_SimpleTokenParser
{
protected $tag;
protected $grammar;
public function __construct($tag, $grammar)
{
$this->tag = $tag;
$this->grammar = $grammar;
}
public function getGrammar()
{
return $this->grammar;
}
public function getTag()
{
return $this->tag;
}
public function getNode(array $values, $line)
{
$nodes = array();
$nodes[] = new Twig_Node_Print(new Twig_Node_Expression_Constant('|', $line), $line);
foreach ($values as $value) {
if ($value instanceof Twig_Node) {
if ($value instanceof Twig_Node_Expression_Array) {
$nodes[] = new Twig_Node_Print(new Twig_Node_Expression_Function('dump', $value, $line), $line);
} else {
$nodes[] = new Twig_Node_Print($value, $line);
}
} else {
$nodes[] = new Twig_Node_Print(new Twig_Node_Expression_Constant($value, $line), $line);
}
$nodes[] = new Twig_Node_Print(new Twig_Node_Expression_Constant('|', $line), $line);
}
return new Twig_Node($nodes);
}
}

View File

@@ -0,0 +1,115 @@
<?php
/*
* 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.
*/
/**
* @group legacy
*/
class Twig_Tests_SimpleTokenParserTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider getTests
*/
public function testParseGrammar($str, $grammar)
{
$this->assertEquals($grammar, Twig_Extensions_SimpleTokenParser::parseGrammar($str), '::parseGrammar() parses a grammar');
}
public function testParseGrammarExceptions()
{
try {
Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo>');
$this->fail();
} catch (Exception $e) {
$this->assertEquals('Twig_Error_Runtime', get_class($e));
}
try {
Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo');
$this->fail();
} catch (Exception $e) {
$this->assertEquals('Twig_Error_Runtime', get_class($e));
}
try {
Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo> (with');
$this->fail();
} catch (Exception $e) {
$this->assertEquals('Twig_Error_Runtime', get_class($e));
}
}
public function getTests()
{
return array(
array('', new Twig_Extensions_Grammar_Tag()),
array('const', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Constant('const')
)),
array(' const ', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Constant('const')
)),
array('<expr>', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr')
)),
array('<expr:expression>', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr')
)),
array(' <expr:expression> ', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr')
)),
array('<nb:number>', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Number('nb')
)),
array('<bool:boolean>', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Boolean('bool')
)),
array('<content:body>', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Body('content')
)),
array('<expr:expression> [with <arguments:array>]', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant('with'),
new Twig_Extensions_Grammar_Array('arguments')
)
)),
array(' <expr:expression> [ with <arguments:array> ] ', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant('with'),
new Twig_Extensions_Grammar_Array('arguments')
)
)),
array('<expr:expression> [with <arguments:array> [or <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant('with'),
new Twig_Extensions_Grammar_Array('arguments'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant('or'),
new Twig_Extensions_Grammar_Expression('optional')
)
)
)),
array('<expr:expression> [with <arguments:array> [, <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
new Twig_Extensions_Grammar_Expression('expr'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant('with'),
new Twig_Extensions_Grammar_Array('arguments'),
new Twig_Extensions_Grammar_Optional(
new Twig_Extensions_Grammar_Constant(',', Twig_Token::PUNCTUATION_TYPE),
new Twig_Extensions_Grammar_Expression('optional')
)
)
)),
);
}
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* 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.
*/
/**
* @group legacy
*/
class grammarTest extends \PHPUnit\Framework\TestCase
{
protected function setUp()
{
require_once __DIR__.'/SimpleTokenParser.php';
}
/**
* @dataProvider getTests
*/
public function testGrammar($tag, $grammar, $template, $output, $exception)
{
$twig = new Twig_Environment(new Twig_Loader_Array(array('template' => $template)), array('cache' => false, 'autoescape' => false, 'debug' => true));
$twig->addExtension(new Twig_Extension_Debug());
$twig->addTokenParser(new SimpleTokenParser($tag, $grammar));
$ok = true;
try {
$template = $twig->loadTemplate('template');
} catch (Exception $e) {
$ok = false;
if (false === $exception) {
$this->fail('Exception not expected');
} else {
$this->assertEquals($exception, get_class($e));
}
}
if ($ok) {
if (false !== $exception) {
$this->fail(sprintf('Exception "%s" expected', $exception));
}
$actual = $template->render(array());
$this->assertEquals($output, $actual);
}
}
public function getTests()
{
return array(
array('foo1', '', '{% foo1 %}', '|', false),
array('foo2', '', '{% foo2 "bar" %}', '|', 'Twig_Error_Syntax'),
array('foo3', '<foo>', '{% foo3 "bar" %}', '|bar|', false),
array('foo4', '<foo>', '{% foo4 1 + 2 %}', '|3|', false),
array('foo5', '<foo:expression>', '{% foo5 1 + 2 %}', '|3|', false),
array('foo6', '<foo:array>', '{% foo6 1 + 2 %}', '|3|', 'Twig_Error_Syntax'),
array('foo7', '<foo>', '{% foo7 %}', '|3|', 'Twig_Error_Syntax'),
array('foo8', '<foo:array>', '{% foo8 [1, 2] %}', "|int(0)\nint(1)\nint(1)\nint(2)\n|", false),
array('foo9', '<foo> with <bar>', '{% foo9 "bar" with "foobar" %}', '|bar|with|foobar|', false),
array('foo10', '<foo> [with <bar>]', '{% foo10 "bar" with "foobar" %}', '|bar|with|foobar|', false),
array('foo11', '<foo> [with <bar>]', '{% foo11 "bar" %}', '|bar|', false),
);
}
}