added a way to change Twig's options when running integration tests (updated tests accordingly)

This commit is contained in:
Fabien Potencier
2011-10-26 09:29:16 +02:00
parent 13e4b4800e
commit 996128dfce
7 changed files with 44 additions and 29 deletions
@@ -13,6 +13,8 @@ Twig supports method calls
{{ items.foo.not }}
--DATA--
return array('foo' => 'bar', 'items' => array('foo' => new Foo(), 'bar' => 'foo'))
--CONFIG--
return array('strict_variables' => false)
--EXPECT--
foo
foo
@@ -22,6 +22,8 @@ Plain values:
Precedence:
{{ 'o' ~ nullVar|default('k') }}
{{ 'o' ~ nested.nullVar|default('k') }}
Deep nested:
{{ nested.definedVar.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }}
--DATA--
return array(
'definedVar' => 'defined',
@@ -35,6 +37,8 @@ return array(
'nullVar' => null,
),
)
--CONFIG--
return array('strict_variables' => false)
--EXPECT--
Variable:
ok
@@ -56,4 +60,6 @@ ok
ok
Precedence:
ok
ok
ok
Deep nested:
ok
@@ -6,13 +6,13 @@
{{ items|join(', ') }}
{% endif %}
{% if items.3 %}
{% if items.3 is defined %}
FOO
{% else %}
{{ items.1 }}
{% endif %}
{% if items.3 %}
{% if items.3 is defined %}
FOO
{% elseif items.1 %}
{{ items.0 }}
@@ -17,5 +17,7 @@ return array('items' => array())
no item
--DATA--
return array()
--CONFIG--
return array('strict_variables' => false)
--EXPECT--
no item
@@ -3,7 +3,7 @@
--TEMPLATE--
{% for item in items %}
{% endfor %}
{% if not loop %}WORKS{% endif %}
{% if loop is not defined %}WORKS{% endif %}
--DATA--
return array('items' => array())
--EXPECT--
+2 -2
View File
@@ -1,9 +1,9 @@
--TEST--
"if" creates a condition
--TEMPLATE--
{% if a %}
{% if a is defined %}
{{ a }}
{% elseif b %}
{% elseif b is defined %}
{{ b }}
{% else %}
NOTHING
+28 -23
View File
@@ -32,7 +32,7 @@ class Twig_Tests_IntegrationTest extends PHPUnit_Framework_TestCase
$message = $match[1];
$exception = false;
$templates = $this->parseTemplates($match[2]);
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
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)));
}
@@ -49,35 +49,40 @@ class Twig_Tests_IntegrationTest extends PHPUnit_Framework_TestCase
public function testIntegration($file, $message, $templates, $exception, $outputs)
{
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
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($e->getMessage().' (in '.$file.')');
}
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->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
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($e->getMessage().' (in '.$file.')');
}
try {
$output = trim($template->render(eval($match[1].';')), "\n ");
} catch (Exception $e) {
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
}
$expected = trim($match[2], "\n ");
$expected = trim($match[3], "\n ");
if ($expected != $output) {
echo 'Compiled template that failed:';