Update Twig from 1.35.0 to 1.42.4 (PHP 5.5 is now required!)

This fixes some errors on PHP 7.4 and contains even more fixes
Also bumped PHP version to 5.5 as Twig requires it.
This commit is contained in:
slawkens
2020-02-15 05:41:38 +01:00
parent d9e449b6cf
commit 8021308822
414 changed files with 9276 additions and 5531 deletions

View File

@@ -1,38 +0,0 @@
<?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.
*/
@trigger_error('The Twig_Test_Function class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleTest instead.', E_USER_DEPRECATED);
/**
* Represents a function template test.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since 1.12 (to be removed in 2.0)
*/
class Twig_Test_Function extends Twig_Test
{
protected $function;
public function __construct($function, array $options = array())
{
$options['callable'] = $function;
parent::__construct($options);
$this->function = $function;
}
public function compile()
{
return $this->function;
}
}

View File

@@ -9,7 +9,19 @@
* file that was distributed with this source code.
*/
namespace Twig\Test;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Extension\ExtensionInterface;
use Twig\Loader\ArrayLoader;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
use Twig\Source;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
/**
* Integration test helper.
@@ -17,7 +29,7 @@ use PHPUnit\Framework\TestCase;
* @author Fabien Potencier <fabien@symfony.com>
* @author Karma Dordrak <drak@zikula.org>
*/
abstract class Twig_Test_IntegrationTestCase extends TestCase
abstract class IntegrationTestCase extends TestCase
{
/**
* @return string
@@ -25,43 +37,43 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
abstract protected function getFixturesDir();
/**
* @return Twig_RuntimeLoaderInterface[]
* @return RuntimeLoaderInterface[]
*/
protected function getRuntimeLoaders()
{
return array();
return [];
}
/**
* @return Twig_ExtensionInterface[]
* @return ExtensionInterface[]
*/
protected function getExtensions()
{
return array();
return [];
}
/**
* @return Twig_SimpleFilter[]
* @return TwigFilter[]
*/
protected function getTwigFilters()
{
return array();
return [];
}
/**
* @return Twig_SimpleFunction[]
* @return TwigFunction[]
*/
protected function getTwigFunctions()
{
return array();
return [];
}
/**
* @return Twig_SimpleTest[]
* @return TwigTest[]
*/
protected function getTwigTests()
{
return array();
return [];
}
/**
@@ -84,9 +96,9 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
public function getTests($name, $legacyTests = false)
{
$fixturesDir = realpath($this->getFixturesDir());
$tests = array();
$tests = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!preg_match('/\.test$/', $file)) {
continue;
}
@@ -102,7 +114,7 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
$condition = $match[2];
$templates = self::parseTemplates($match[3]);
$exception = $match[5];
$outputs = array(array(null, $match[4], null, ''));
$outputs = [[null, $match[4], null, '']];
} elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
$message = $match[1];
$condition = $match[2];
@@ -110,15 +122,15 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
$exception = false;
preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
} else {
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
throw new \InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
}
$tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
$tests[] = [str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs];
}
if ($legacyTests && empty($tests)) {
// add a dummy test to avoid a PHPUnit message
return array(array('not', '-', '', array(), '', array()));
return [['not', '-', '', [], '', []]];
}
return $tests;
@@ -132,7 +144,7 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
{
if (!$outputs) {
$this->markTestSkipped('no legacy tests to run');
$this->markTestSkipped('no tests to run');
}
if ($condition) {
@@ -142,14 +154,14 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
}
}
$loader = new Twig_Loader_Array($templates);
$loader = new ArrayLoader($templates);
foreach ($outputs as $i => $match) {
$config = array_merge(array(
$config = array_merge([
'cache' => false,
'strict_variables' => true,
), $match[2] ? eval($match[2].';') : array());
$twig = new Twig_Environment($loader, $config);
], $match[2] ? eval($match[2].';') : []);
$twig = new Environment($loader, $config);
$twig->addGlobal('global', 'global');
foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
$twig->addRuntimeLoader($runtimeLoader);
@@ -171,41 +183,37 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
$twig->addFunction($function);
}
// avoid using the same PHP class name for different cases
// only for PHP 5.2+
if (PHP_VERSION_ID >= 50300) {
$p = new ReflectionProperty($twig, 'templateClassPrefix');
$p->setAccessible(true);
$p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
}
$p = new \ReflectionProperty($twig, 'templateClassPrefix');
$p->setAccessible(true);
$p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
try {
$template = $twig->loadTemplate('index.twig');
} catch (Exception $e) {
$template = $twig->load('index.twig');
} catch (\Exception $e) {
if (false !== $exception) {
$message = $e->getMessage();
$this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
$last = substr($message, strlen($message) - 1);
$this->assertTrue('.' === $last || '?' === $last, $message, 'Exception message must end with a dot or a question mark.');
$this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $message)));
$last = substr($message, \strlen($message) - 1);
$this->assertTrue('.' === $last || '?' === $last, 'Exception message must end with a dot or a question mark.');
return;
}
throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
throw new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
}
try {
$output = trim($template->render(eval($match[1].';')), "\n ");
} catch (Exception $e) {
} catch (\Exception $e) {
if (false !== $exception) {
$this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
$this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $e->getMessage())));
return;
}
$e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
$e = new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
$output = trim(sprintf('%s: %s', \get_class($e), $e->getMessage()));
}
if (false !== $exception) {
@@ -222,8 +230,8 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
foreach (array_keys($templates) as $name) {
echo "Template: $name\n";
$loader = $twig->getLoader();
if (!$loader instanceof Twig_SourceContextLoaderInterface) {
$source = new Twig_Source($loader->getSource($name), $name);
if (!$loader instanceof SourceContextLoaderInterface) {
$source = new Source($loader->getSource($name), $name);
} else {
$source = $loader->getSourceContext($name);
}
@@ -236,7 +244,7 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
protected static function parseTemplates($test)
{
$templates = array();
$templates = [];
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
@@ -246,4 +254,4 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase
}
}
class_alias('Twig_Test_IntegrationTestCase', 'Twig\Test\IntegrationTestCase', false);
class_alias('Twig\Test\IntegrationTestCase', 'Twig_Test_IntegrationTestCase');

View File

@@ -1,40 +0,0 @@
<?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.
*/
@trigger_error('The Twig_Test_Method class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleTest instead.', E_USER_DEPRECATED);
/**
* Represents a method template test.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since 1.12 (to be removed in 2.0)
*/
class Twig_Test_Method extends Twig_Test
{
protected $extension;
protected $method;
public function __construct(Twig_ExtensionInterface $extension, $method, array $options = array())
{
$options['callable'] = array($extension, $method);
parent::__construct($options);
$this->extension = $extension;
$this->method = $method;
}
public function compile()
{
return sprintf('$this->env->getExtension(\'%s\')->%s', get_class($this->extension), $this->method);
}
}

View File

@@ -1,40 +0,0 @@
<?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.
*/
@trigger_error('The Twig_Test_Node class is deprecated since version 1.12 and will be removed in 2.0.', E_USER_DEPRECATED);
/**
* Represents a template test as a Node.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since 1.12 (to be removed in 2.0)
*/
class Twig_Test_Node extends Twig_Test
{
protected $class;
public function __construct($class, array $options = array())
{
parent::__construct($options);
$this->class = $class;
}
public function getClass()
{
return $this->class;
}
public function compile()
{
}
}

View File

@@ -9,9 +9,15 @@
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;
namespace Twig\Test;
abstract class Twig_Test_NodeTestCase extends TestCase
use PHPUnit\Framework\TestCase;
use Twig\Compiler;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Node;
abstract class NodeTestCase extends TestCase
{
abstract public function getTests();
@@ -23,7 +29,7 @@ abstract class Twig_Test_NodeTestCase extends TestCase
$this->assertNodeCompilation($source, $node, $environment, $isPattern);
}
public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
public function assertNodeCompilation($source, Node $node, Environment $environment = null, $isPattern = false)
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);
@@ -35,25 +41,25 @@ abstract class Twig_Test_NodeTestCase extends TestCase
}
}
protected function getCompiler(Twig_Environment $environment = null)
protected function getCompiler(Environment $environment = null)
{
return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
return new Compiler(null === $environment ? $this->getEnvironment() : $environment);
}
protected function getEnvironment()
{
return new Twig_Environment(new Twig_Loader_Array(array()));
return new Environment(new ArrayLoader([]));
}
protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';
if (PHP_VERSION_ID >= 70000) {
return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
if (\PHP_VERSION_ID >= 70000) {
return sprintf('%s($context["%s"] ?? null)', $line, $name);
}
if (PHP_VERSION_ID >= 50400) {
if (\PHP_VERSION_ID >= 50400) {
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}
@@ -62,7 +68,7 @@ abstract class Twig_Test_NodeTestCase extends TestCase
protected function getAttributeGetter()
{
if (function_exists('twig_template_get_attributes')) {
if (\function_exists('twig_template_get_attributes')) {
return 'twig_template_get_attributes($this, ';
}
@@ -70,6 +76,4 @@ abstract class Twig_Test_NodeTestCase extends TestCase
}
}
class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
class_exists('Twig_Environment');
class_exists('Twig_Node');
class_alias('Twig\Test\NodeTestCase', 'Twig_Test_NodeTestCase');