forked from leftypol/leftypol
Upgrade Twig library
This commit is contained in:
parent
22f3a95e0e
commit
0fe5528574
133 changed files with 5080 additions and 1386 deletions
|
@ -12,15 +12,19 @@
|
|||
/**
|
||||
* Represents a function template test.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @deprecated since 1.12 (to be removed in 2.0)
|
||||
*/
|
||||
class Twig_Test_Function implements Twig_TestInterface
|
||||
class Twig_Test_Function extends Twig_Test
|
||||
{
|
||||
protected $function;
|
||||
|
||||
public function __construct($function)
|
||||
public function __construct($function, array $options = array())
|
||||
{
|
||||
$options['callable'] = $function;
|
||||
|
||||
parent::__construct($options);
|
||||
|
||||
$this->function = $function;
|
||||
}
|
||||
|
||||
|
|
154
inc/lib/Twig/Test/IntegrationTestCase.php
Normal file
154
inc/lib/Twig/Test/IntegrationTestCase.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2010 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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 protected function getExtensions();
|
||||
abstract protected function getFixturesDir();
|
||||
|
||||
/**
|
||||
* @dataProvider getTests
|
||||
*/
|
||||
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
|
||||
{
|
||||
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
$fixturesDir = realpath($this->getFixturesDir());
|
||||
$tests = array();
|
||||
|
||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
|
||||
if (!preg_match('/\.test$/', $file)) {
|
||||
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)) {
|
||||
$message = $match[1];
|
||||
$condition = $match[2];
|
||||
$templates = $this->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]);
|
||||
$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)));
|
||||
}
|
||||
|
||||
$tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
|
||||
}
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
|
||||
{
|
||||
if ($condition) {
|
||||
eval('$ret = '.$condition.';');
|
||||
if (!$ret) {
|
||||
$this->markTestSkipped($condition);
|
||||
}
|
||||
}
|
||||
|
||||
$loader = new Twig_Loader_Array($templates);
|
||||
|
||||
foreach ($outputs as $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->getExtensions() as $extension) {
|
||||
$twig->addExtension($extension);
|
||||
}
|
||||
|
||||
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())));
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
try {
|
||||
$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())));
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
$expected = trim($match[3], "\n ");
|
||||
|
||||
if ($expected != $output) {
|
||||
echo 'Compiled template that failed:';
|
||||
|
||||
foreach (array_keys($templates) as $name) {
|
||||
echo "Template: $name\n";
|
||||
$source = $loader->getSource($name);
|
||||
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
|
||||
}
|
||||
}
|
||||
$this->assertEquals($expected, $output, $message.' (in '.$file.')');
|
||||
}
|
||||
}
|
||||
|
||||
protected static function parseTemplates($test)
|
||||
{
|
||||
$templates = array();
|
||||
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
|
||||
}
|
||||
|
||||
return $templates;
|
||||
}
|
||||
}
|
|
@ -12,15 +12,20 @@
|
|||
/**
|
||||
* Represents a method template test.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @deprecated since 1.12 (to be removed in 2.0)
|
||||
*/
|
||||
class Twig_Test_Method implements Twig_TestInterface
|
||||
class Twig_Test_Method extends Twig_Test
|
||||
{
|
||||
protected $extension, $method;
|
||||
protected $extension;
|
||||
protected $method;
|
||||
|
||||
public function __construct(Twig_ExtensionInterface $extension, $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;
|
||||
}
|
||||
|
|
37
inc/lib/Twig/Test/Node.php
Normal file
37
inc/lib/Twig/Test/Node.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2010 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
}
|
||||
}
|
58
inc/lib/Twig/Test/NodeTestCase.php
Normal file
58
inc/lib/Twig/Test/NodeTestCase.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?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.
|
||||
*/
|
||||
abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
abstract public function getTests();
|
||||
|
||||
/**
|
||||
* @dataProvider getTests
|
||||
*/
|
||||
public function testCompile($node, $source, $environment = null)
|
||||
{
|
||||
$this->assertNodeCompilation($source, $node, $environment);
|
||||
}
|
||||
|
||||
public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null)
|
||||
{
|
||||
$compiler = $this->getCompiler($environment);
|
||||
$compiler->compile($node);
|
||||
|
||||
$this->assertEquals($source, trim($compiler->getSource()));
|
||||
}
|
||||
|
||||
protected function getCompiler(Twig_Environment $environment = null)
|
||||
{
|
||||
return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
|
||||
}
|
||||
|
||||
protected function getEnvironment()
|
||||
{
|
||||
return new Twig_Environment();
|
||||
}
|
||||
|
||||
protected function getVariableGetter($name)
|
||||
{
|
||||
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
|
||||
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
|
||||
}
|
||||
|
||||
return sprintf('$this->getContext($context, "%s")', $name);
|
||||
}
|
||||
|
||||
protected function getAttributeGetter()
|
||||
{
|
||||
if (function_exists('twig_template_get_attributes')) {
|
||||
return 'twig_template_get_attributes($this, ';
|
||||
}
|
||||
|
||||
return '$this->getAttribute(';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue