summaryrefslogtreecommitdiff
path: root/inc/lib/Twig/Test
diff options
context:
space:
mode:
Diffstat (limited to 'inc/lib/Twig/Test')
-rw-r--r--inc/lib/Twig/Test/Function.php5
-rw-r--r--inc/lib/Twig/Test/IntegrationTestCase.php153
-rw-r--r--inc/lib/Twig/Test/Method.php7
-rw-r--r--inc/lib/Twig/Test/Node.php5
-rw-r--r--inc/lib/Twig/Test/NodeTestCase.php37
5 files changed, 164 insertions, 43 deletions
diff --git a/inc/lib/Twig/Test/Function.php b/inc/lib/Twig/Test/Function.php
index 4be6b9b9..9e83c3f8 100644
--- a/inc/lib/Twig/Test/Function.php
+++ b/inc/lib/Twig/Test/Function.php
@@ -3,16 +3,19 @@
/*
* This file is part of Twig.
*
- * (c) 2010 Fabien Potencier
+ * (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
diff --git a/inc/lib/Twig/Test/IntegrationTestCase.php b/inc/lib/Twig/Test/IntegrationTestCase.php
index 724f0941..016951aa 100644
--- a/inc/lib/Twig/Test/IntegrationTestCase.php
+++ b/inc/lib/Twig/Test/IntegrationTestCase.php
@@ -3,24 +3,68 @@
/*
* This file is part of Twig.
*
- * (c) 2010 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+use PHPUnit\Framework\TestCase;
+
/**
- * Integration test helper
+ * Integration test helper.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Karma Dordrak <drak@zikula.org>
*/
-abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
+abstract class Twig_Test_IntegrationTestCase extends TestCase
{
- abstract protected function getExtensions();
+ /**
+ * @return string
+ */
abstract protected function getFixturesDir();
/**
+ * @return Twig_RuntimeLoaderInterface[]
+ */
+ protected function getRuntimeLoaders()
+ {
+ return array();
+ }
+
+ /**
+ * @return Twig_ExtensionInterface[]
+ */
+ protected function getExtensions()
+ {
+ return array();
+ }
+
+ /**
+ * @return Twig_SimpleFilter[]
+ */
+ protected function getTwigFilters()
+ {
+ return array();
+ }
+
+ /**
+ * @return Twig_SimpleFunction[]
+ */
+ protected function getTwigFunctions()
+ {
+ return array();
+ }
+
+ /**
+ * @return Twig_SimpleTest[]
+ */
+ protected function getTwigTests()
+ {
+ return array();
+ }
+
+ /**
* @dataProvider getTests
*/
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
@@ -28,7 +72,16 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
}
- public function getTests()
+ /**
+ * @dataProvider getLegacyTests
+ * @group legacy
+ */
+ public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
+ {
+ $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
+ }
+
+ public function getTests($name, $legacyTests = false)
{
$fixturesDir = realpath($this->getFixturesDir());
$tests = array();
@@ -38,19 +91,22 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
continue;
}
+ if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
+ continue;
+ }
+
$test = file_get_contents($file->getRealpath());
- if (preg_match('/
- --TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
+ if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
$message = $match[1];
$condition = $match[2];
- $templates = $this->parseTemplates($match[3]);
+ $templates = self::parseTemplates($match[3]);
$exception = $match[5];
$outputs = array(array(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];
- $templates = $this->parseTemplates($match[3]);
+ $templates = self::parseTemplates($match[3]);
$exception = false;
preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
} else {
@@ -60,11 +116,25 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
$tests[] = array(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 $tests;
}
+ public function getLegacyTests()
+ {
+ return $this->getTests('testLegacyIntegration', true);
+ }
+
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
{
+ if (!$outputs) {
+ $this->markTestSkipped('no legacy tests to run');
+ }
+
if ($condition) {
eval('$ret = '.$condition.';');
if (!$ret) {
@@ -74,32 +144,53 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
$loader = new Twig_Loader_Array($templates);
- foreach ($outputs as $match) {
+ foreach ($outputs as $i => $match) {
$config = array_merge(array(
'cache' => false,
'strict_variables' => true,
), $match[2] ? eval($match[2].';') : array());
$twig = new Twig_Environment($loader, $config);
$twig->addGlobal('global', 'global');
+ foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
+ $twig->addRuntimeLoader($runtimeLoader);
+ }
+
foreach ($this->getExtensions() as $extension) {
$twig->addExtension($extension);
}
+ foreach ($this->getTwigFilters() as $filter) {
+ $twig->addFilter($filter);
+ }
+
+ foreach ($this->getTwigTests() as $test) {
+ $twig->addTest($test);
+ }
+
+ foreach ($this->getTwigFunctions() as $function) {
+ $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).'_');
+ }
+
try {
$template = $twig->loadTemplate('index.twig');
} catch (Exception $e) {
if (false !== $exception) {
- $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
+ $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.');
return;
}
- if ($e instanceof Twig_Error_Syntax) {
- $e->setTemplateFile($file);
-
- throw $e;
- }
-
throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
}
@@ -107,34 +198,36 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
$output = trim($template->render(eval($match[1].';')), "\n ");
} catch (Exception $e) {
if (false !== $exception) {
- $this->assertEquals(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;
}
- if ($e instanceof Twig_Error_Syntax) {
- $e->setTemplateFile($file);
- } else {
- $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
- }
+ $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
}
if (false !== $exception) {
- list($class, ) = explode(':', $exception);
- $this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
+ list($class) = explode(':', $exception);
+ $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
+ $this->assertThat(null, new $constraintClass($class));
}
$expected = trim($match[3], "\n ");
- if ($expected != $output) {
- echo 'Compiled template that failed:';
+ if ($expected !== $output) {
+ printf("Compiled templates that failed on case %d:\n", $i + 1);
foreach (array_keys($templates) as $name) {
echo "Template: $name\n";
- $source = $loader->getSource($name);
- echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
+ $loader = $twig->getLoader();
+ if (!$loader instanceof Twig_SourceContextLoaderInterface) {
+ $source = new Twig_Source($loader->getSource($name), $name);
+ } else {
+ $source = $loader->getSourceContext($name);
+ }
+ echo $twig->compile($twig->parse($twig->tokenize($source)));
}
}
$this->assertEquals($expected, $output, $message.' (in '.$file.')');
@@ -152,3 +245,5 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
return $templates;
}
}
+
+class_alias('Twig_Test_IntegrationTestCase', 'Twig\Test\IntegrationTestCase', false);
diff --git a/inc/lib/Twig/Test/Method.php b/inc/lib/Twig/Test/Method.php
index 17c6c041..feccd5db 100644
--- a/inc/lib/Twig/Test/Method.php
+++ b/inc/lib/Twig/Test/Method.php
@@ -3,16 +3,19 @@
/*
* This file is part of Twig.
*
- * (c) 2010 Fabien Potencier
+ * (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
@@ -32,6 +35,6 @@ class Twig_Test_Method extends Twig_Test
public function compile()
{
- return sprintf('$this->env->getExtension(\'%s\')->%s', $this->extension->getName(), $this->method);
+ return sprintf('$this->env->getExtension(\'%s\')->%s', get_class($this->extension), $this->method);
}
}
diff --git a/inc/lib/Twig/Test/Node.php b/inc/lib/Twig/Test/Node.php
index c832a57b..6098a527 100644
--- a/inc/lib/Twig/Test/Node.php
+++ b/inc/lib/Twig/Test/Node.php
@@ -3,16 +3,19 @@
/*
* This file is part of Twig.
*
- * (c) 2010 Fabien Potencier
+ * (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
diff --git a/inc/lib/Twig/Test/NodeTestCase.php b/inc/lib/Twig/Test/NodeTestCase.php
index b15c85ff..47942675 100644
--- a/inc/lib/Twig/Test/NodeTestCase.php
+++ b/inc/lib/Twig/Test/NodeTestCase.php
@@ -8,24 +8,31 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
-abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
+
+use PHPUnit\Framework\TestCase;
+
+abstract class Twig_Test_NodeTestCase extends TestCase
{
abstract public function getTests();
/**
* @dataProvider getTests
*/
- public function testCompile($node, $source, $environment = null)
+ public function testCompile($node, $source, $environment = null, $isPattern = false)
{
- $this->assertNodeCompilation($source, $node, $environment);
+ $this->assertNodeCompilation($source, $node, $environment, $isPattern);
}
- public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null)
+ public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);
- $this->assertEquals($source, trim($compiler->getSource()));
+ if ($isPattern) {
+ $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
+ } else {
+ $this->assertEquals($source, trim($compiler->getSource()));
+ }
}
protected function getCompiler(Twig_Environment $environment = null)
@@ -35,16 +42,22 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
protected function getEnvironment()
{
- return new Twig_Environment();
+ return new Twig_Environment(new Twig_Loader_Array(array()));
}
- protected function getVariableGetter($name)
+ protected function getVariableGetter($name, $line = false)
{
- if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
- return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
+ $line = $line > 0 ? "// line {$line}\n" : '';
+
+ if (PHP_VERSION_ID >= 70000) {
+ return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
}
- return sprintf('$this->getContext($context, "%s")', $name);
+ if (PHP_VERSION_ID >= 50400) {
+ return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
+ }
+
+ return sprintf('%s$this->getContext($context, "%s")', $line, $name);
}
protected function getAttributeGetter()
@@ -56,3 +69,7 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
return '$this->getAttribute(';
}
}
+
+class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
+class_exists('Twig_Environment');
+class_exists('Twig_Node');