From 996128dfceaea2c7fbe5ceb4f15311ba65a8f115 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 26 Oct 2011 09:29:16 +0200 Subject: [PATCH] added a way to change Twig's options when running integration tests (updated tests accordingly) --- .../Tests/Fixtures/expressions/method_call.test | 2 + test/Twig/Tests/Fixtures/filters/default.test | 8 +++- .../Tests/Fixtures/tags/filter/with_if_tag.test | 4 +- test/Twig/Tests/Fixtures/tags/for/else.test | 2 + .../Fixtures/tags/for/loop_context_local.test | 2 +- test/Twig/Tests/Fixtures/tags/if/basic.test | 4 +- test/Twig/Tests/integrationTest.php | 43 +++++++++++--------- 7 files changed, 40 insertions(+), 25 deletions(-) diff --git a/test/Twig/Tests/Fixtures/expressions/method_call.test b/test/Twig/Tests/Fixtures/expressions/method_call.test index 0957d18..9d84a4c 100644 --- a/test/Twig/Tests/Fixtures/expressions/method_call.test +++ b/test/Twig/Tests/Fixtures/expressions/method_call.test @@ -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 diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index f17b96b..f6bfb8f 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -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 \ No newline at end of file +ok +Deep nested: +ok diff --git a/test/Twig/Tests/Fixtures/tags/filter/with_if_tag.test b/test/Twig/Tests/Fixtures/tags/filter/with_if_tag.test index 02381dd..afd95b2 100644 --- a/test/Twig/Tests/Fixtures/tags/filter/with_if_tag.test +++ b/test/Twig/Tests/Fixtures/tags/filter/with_if_tag.test @@ -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 }} diff --git a/test/Twig/Tests/Fixtures/tags/for/else.test b/test/Twig/Tests/Fixtures/tags/for/else.test index 8c6a7d2..20ccc88 100644 --- a/test/Twig/Tests/Fixtures/tags/for/else.test +++ b/test/Twig/Tests/Fixtures/tags/for/else.test @@ -17,5 +17,7 @@ return array('items' => array()) no item --DATA-- return array() +--CONFIG-- +return array('strict_variables' => false) --EXPECT-- no item diff --git a/test/Twig/Tests/Fixtures/tags/for/loop_context_local.test b/test/Twig/Tests/Fixtures/tags/for/loop_context_local.test index 4ec5440..58af2c3 100644 --- a/test/Twig/Tests/Fixtures/tags/for/loop_context_local.test +++ b/test/Twig/Tests/Fixtures/tags/for/loop_context_local.test @@ -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-- diff --git a/test/Twig/Tests/Fixtures/tags/if/basic.test b/test/Twig/Tests/Fixtures/tags/if/basic.test index 2482ddf..c1c3d27 100644 --- a/test/Twig/Tests/Fixtures/tags/if/basic.test +++ b/test/Twig/Tests/Fixtures/tags/if/basic.test @@ -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 diff --git a/test/Twig/Tests/integrationTest.php b/test/Twig/Tests/integrationTest.php index ef682f6..4f26665 100644 --- a/test/Twig/Tests/integrationTest.php +++ b/test/Twig/Tests/integrationTest.php @@ -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()))); + 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()); - return; - } + 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()))); - if ($e instanceof Twig_Error_Syntax) { - $e->setTemplateFile($file); + return; + } - throw $e; - } + if ($e instanceof Twig_Error_Syntax) { + $e->setTemplateFile($file); - throw new Twig_Error($e->getMessage().' (in '.$file.')'); - } + throw $e; + } + + throw new Twig_Error($e->getMessage().' (in '.$file.')'); + } - foreach ($outputs as $match) { 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:'; -- 1.7.2.5