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

@@ -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');