From 36372c61d3992fb635eb5cc197b190985b86bff9 Mon Sep 17 00:00:00 2001 From: Drak Date: Mon, 17 Sep 2012 12:33:27 +0100 Subject: [PATCH] Made node tests reusable --- doc/advanced.rst | 46 +++ lib/Twig/Test/IntegrationTestCase.php | 155 +++++++++ lib/Twig/Test/NodeTestCase.php | 58 ++++ .../Tests/Fixtures/expressions/method_call.test | 2 +- test/Twig/Tests/Fixtures/filters/default.test | 4 +- test/Twig/Tests/Fixtures/filters/join.test | 2 +- test/Twig/Tests/Fixtures/functions/attribute.test | 2 +- test/Twig/Tests/Fixtures/tests/constant.test | 4 +- test/Twig/Tests/Fixtures/tests/defined.test | 4 +- .../Twig/Tests/Fixtures/tests/in_with_objects.test | 4 +- test/Twig/Tests/IntegrationTest.php | 216 +++++++++++++ test/Twig/Tests/Node/AutoEscapeTest.php | 3 +- test/Twig/Tests/Node/BlockReferenceTest.php | 3 +- test/Twig/Tests/Node/BlockTest.php | 3 +- test/Twig/Tests/Node/DoTest.php | 3 +- test/Twig/Tests/Node/Expression/ArrayTest.php | 4 +- test/Twig/Tests/Node/Expression/AssignNameTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/AddTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/AndTest.php | 4 +- .../Tests/Node/Expression/Binary/ConcatTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/DivTest.php | 4 +- .../Tests/Node/Expression/Binary/FloorDivTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/ModTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/MulTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/OrTest.php | 4 +- test/Twig/Tests/Node/Expression/Binary/SubTest.php | 4 +- .../Twig/Tests/Node/Expression/ConditionalTest.php | 4 +- test/Twig/Tests/Node/Expression/ConstantTest.php | 4 +- test/Twig/Tests/Node/Expression/FilterTest.php | 4 +- test/Twig/Tests/Node/Expression/FunctionTest.php | 4 +- test/Twig/Tests/Node/Expression/GetAttrTest.php | 4 +- test/Twig/Tests/Node/Expression/NameTest.php | 4 +- test/Twig/Tests/Node/Expression/ParentTest.php | 4 +- test/Twig/Tests/Node/Expression/TestTest.php | 4 +- test/Twig/Tests/Node/Expression/Unary/NegTest.php | 4 +- test/Twig/Tests/Node/Expression/Unary/NotTest.php | 4 +- test/Twig/Tests/Node/Expression/Unary/PosTest.php | 4 +- test/Twig/Tests/Node/ForTest.php | 3 +- test/Twig/Tests/Node/IfTest.php | 3 +- test/Twig/Tests/Node/ImportTest.php | 3 +- test/Twig/Tests/Node/IncludeTest.php | 3 +- test/Twig/Tests/Node/MacroTest.php | 3 +- test/Twig/Tests/Node/ModuleTest.php | 3 +- test/Twig/Tests/Node/PrintTest.php | 3 +- test/Twig/Tests/Node/SandboxTest.php | 3 +- test/Twig/Tests/Node/SandboxedModuleTest.php | 3 +- test/Twig/Tests/Node/SandboxedPrintTest.php | 3 +- test/Twig/Tests/Node/SetTest.php | 3 +- test/Twig/Tests/Node/SpacelessTest.php | 3 +- test/Twig/Tests/Node/TestCase.php | 58 ---- test/Twig/Tests/Node/TextTest.php | 3 +- test/Twig/Tests/integrationTest.php | 328 -------------------- 52 files changed, 525 insertions(+), 497 deletions(-) create mode 100644 lib/Twig/Test/IntegrationTestCase.php create mode 100644 lib/Twig/Test/NodeTestCase.php create mode 100644 test/Twig/Tests/IntegrationTest.php delete mode 100644 test/Twig/Tests/Node/TestCase.php delete mode 100644 test/Twig/Tests/integrationTest.php diff --git a/doc/advanced.rst b/doc/advanced.rst index e2d2f69..46dc6de 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -832,5 +832,51 @@ The ``getTests()`` methods allows to add new test functions:: // ... } +Functional Tests +~~~~~~~~~~~~~~~~ + +You can create functional tests for extensions simply by creating the following file structure +in your test directory:: + + Fixtures/ + filters/ + foo.test + bar.test + functions/ + foo.test + bar.test + tags/ + foo.test + bar.test + IntegrationTest.php + +The ``IntegrationTest.php`` file should look like this:: + + class Project_Tests_IntegrationTest extends Twig_Test_IntegrationTestCase + { + public function getExtensions() + { + return array( + new Project_Twig_Extension1(), + new Project_Twig_Extension2(), + ); + } + + public function getFixturesDir() + { + return dirname(__FILE__).'/Fixtures/'; + } + } + +Fixtures examples can be found within the Twig repository ``tests/Twig/Fixtures`` directory. + +Node Tests +~~~~~~~~~~ + +Testing the node visitors can be complex, so extend your test cases from +``Twig_Test_NodeTestCase``. Examples can be found in the Twig repository ``tests/Twig/Node`` directory. + .. _`spl_autoload_register()`: http://www.php.net/spl_autoload_register .. _`rot13`: http://www.php.net/manual/en/function.str-rot13.php +.. _`tests/Twig/Fixtures`: https://github.com/fabpot/Twig/tree/master/test/Twig/Tests/Fixtures +.. _`tests/Twig/Node`: https://github.com/fabpot/Twig/tree/master/test/Twig/Tests/Node diff --git a/lib/Twig/Test/IntegrationTestCase.php b/lib/Twig/Test/IntegrationTestCase.php new file mode 100644 index 0000000..3fcf4eb --- /dev/null +++ b/lib/Twig/Test/IntegrationTestCase.php @@ -0,0 +1,155 @@ + + * @author Karma Dordrak + */ +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; + } +} diff --git a/lib/Twig/Test/NodeTestCase.php b/lib/Twig/Test/NodeTestCase.php new file mode 100644 index 0000000..b15c85f --- /dev/null +++ b/lib/Twig/Test/NodeTestCase.php @@ -0,0 +1,58 @@ +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('; + } +} diff --git a/test/Twig/Tests/Fixtures/expressions/method_call.test b/test/Twig/Tests/Fixtures/expressions/method_call.test index 9d84a4c..5f801e6 100644 --- a/test/Twig/Tests/Fixtures/expressions/method_call.test +++ b/test/Twig/Tests/Fixtures/expressions/method_call.test @@ -12,7 +12,7 @@ Twig supports method calls {{ items.foo.in }} {{ items.foo.not }} --DATA-- -return array('foo' => 'bar', 'items' => array('foo' => new Foo(), 'bar' => 'foo')) +return array('foo' => 'bar', 'items' => array('foo' => new TwigTestFoo(), 'bar' => 'foo')) --CONFIG-- return array('strict_variables' => false) --EXPECT-- diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index 22e86e4..0e4404b 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -51,7 +51,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), - 'object' => new Foo(), + 'object' => new TwigTestFoo(), ) --CONFIG-- return array('strict_variables' => false) @@ -106,7 +106,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), - 'object' => new Foo(), + 'object' => new TwigTestFoo(), ) --CONFIG-- return array('strict_variables' => true) diff --git a/test/Twig/Tests/Fixtures/filters/join.test b/test/Twig/Tests/Fixtures/filters/join.test index 8feef63..b342c17 100644 --- a/test/Twig/Tests/Fixtures/filters/join.test +++ b/test/Twig/Tests/Fixtures/filters/join.test @@ -5,7 +5,7 @@ {{ foo|join(', ') }} {{ bar|join(', ') }} --DATA-- -return array('foo' => new Foo(), 'bar' => new ArrayObject(array(3, 4))) +return array('foo' => new TwigTestFoo(), 'bar' => new ArrayObject(array(3, 4))) --EXPECT-- foo, bar 1, 2 diff --git a/test/Twig/Tests/Fixtures/functions/attribute.test b/test/Twig/Tests/Fixtures/functions/attribute.test index ba7d5e8..16ae1e8 100644 --- a/test/Twig/Tests/Fixtures/functions/attribute.test +++ b/test/Twig/Tests/Fixtures/functions/attribute.test @@ -5,7 +5,7 @@ {{ attribute(array, item) }} {{ attribute(obj, "bar", ["a", "b"]) }} --DATA-- -return array('obj' => new Foo(), 'method' => 'foo', 'array' => array('foo' => 'bar'), 'item' => 'foo') +return array('obj' => new TwigTestFoo(), 'method' => 'foo', 'array' => array('foo' => 'bar'), 'item' => 'foo') --EXPECT-- foo bar diff --git a/test/Twig/Tests/Fixtures/tests/constant.test b/test/Twig/Tests/Fixtures/tests/constant.test index fb3d288..c8adb15 100644 --- a/test/Twig/Tests/Fixtures/tests/constant.test +++ b/test/Twig/Tests/Fixtures/tests/constant.test @@ -2,8 +2,8 @@ "const" test --TEMPLATE-- {{ 8 is constant('E_NOTICE') ? 'ok' : 'no' }} -{{ 'bar' is constant('Foo::BAR_NAME') ? 'ok' : 'no' }} -{{ value is constant('Foo::BAR_NAME') ? 'ok' : 'no' }} +{{ 'bar' is constant('TwigTestFoo::BAR_NAME') ? 'ok' : 'no' }} +{{ value is constant('TwigTestFoo::BAR_NAME') ? 'ok' : 'no' }} --DATA-- return array('value' => 'bar'); --EXPECT-- diff --git a/test/Twig/Tests/Fixtures/tests/defined.test b/test/Twig/Tests/Fixtures/tests/defined.test index 2fbaa86..cbfe03d 100644 --- a/test/Twig/Tests/Fixtures/tests/defined.test +++ b/test/Twig/Tests/Fixtures/tests/defined.test @@ -37,7 +37,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), - 'object' => new Foo(), + 'object' => new TwigTestFoo(), ); --EXPECT-- ok @@ -76,7 +76,7 @@ return array( 'nullVar' => null, 'definedArray' => array(0), ), - 'object' => new Foo(), + 'object' => new TwigTestFoo(), ); --CONFIG-- return array('strict_variables' => false) diff --git a/test/Twig/Tests/Fixtures/tests/in_with_objects.test b/test/Twig/Tests/Fixtures/tests/in_with_objects.test index daeb1cb..8e08061 100644 --- a/test/Twig/Tests/Fixtures/tests/in_with_objects.test +++ b/test/Twig/Tests/Fixtures/tests/in_with_objects.test @@ -5,8 +5,8 @@ Twig supports the in operator when using objects TRUE {% endif %} --DATA-- -$foo = new Foo(); -$foo1 = new Foo(); +$foo = new TwigTestFoo(); +$foo1 = new TwigTestFoo(); $foo->position = $foo1; $foo1->position = $foo; diff --git a/test/Twig/Tests/IntegrationTest.php b/test/Twig/Tests/IntegrationTest.php new file mode 100644 index 0000000..71cf62d --- /dev/null +++ b/test/Twig/Tests/IntegrationTest.php @@ -0,0 +1,216 @@ +position = 0; + } + + public function current() + { + return $this->array[$this->position]; + } + + public function key() + { + return 'a'; + } + + public function next() + { + ++$this->position; + } + + public function valid() + { + return isset($this->array[$this->position]); + } +} + +class TwigTestTokenParser_☃ extends Twig_TokenParser +{ + public function parse(Twig_Token $token) + { + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); + + return new Twig_Node_Print(new Twig_Node_Expression_Constant('☃', -1), -1); + } + + public function getTag() + { + return '☃'; + } +} + +class TwigTestExtension extends Twig_Extension +{ + public function getTokenParsers() + { + return array( + new TwigTestTokenParser_☃(), + ); + } + + public function getFilters() + { + return array( + '☃' => new Twig_Filter_Method($this, '☃Filter'), + 'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))), + 'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), + 'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))), + 'preserves_safety' => new Twig_Filter_Method($this, 'preserves_safety', array('preserves_safety' => array('html'))), + '*_path' => new Twig_Filter_Method($this, 'dynamic_path'), + '*_foo_*_bar' => new Twig_Filter_Method($this, 'dynamic_foo'), + ); + } + + public function getFunctions() + { + return array( + '☃' => new Twig_Function_Method($this, '☃Function'), + 'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))), + 'unsafe_br' => new Twig_Function_Method($this, 'br'), + '*_path' => new Twig_Function_Method($this, 'dynamic_path'), + '*_foo_*_bar' => new Twig_Function_Method($this, 'dynamic_foo'), + ); + } + + public function ☃Filter($value) + { + return "☃{$value}☃"; + } + + public function ☃Function($value) + { + return "☃{$value}☃"; + } + + /** + * nl2br which also escapes, for testing escaper filters + */ + public function escape_and_nl2br($env, $value, $sep = '
') + { + return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep); + } + + /** + * nl2br only, for testing filters with pre_escape + */ + public function nl2br($value, $sep = '
') + { + // not secure if $value contains html tags (not only entities) + // don't use + return str_replace("\n", "$sep\n", $value); + } + + public function dynamic_path($element, $item) + { + return $element.'/'.$item; + } + + public function dynamic_foo($foo, $bar, $item) + { + return $foo.'/'.$bar.'/'.$item; + } + + public function escape_something($value) + { + return strtoupper($value); + } + + public function preserves_safety($value) + { + return strtoupper($value); + } + + public function br() + { + return '
'; + } + + public function getName() + { + return 'test'; + } +} diff --git a/test/Twig/Tests/Node/AutoEscapeTest.php b/test/Twig/Tests/Node/AutoEscapeTest.php index ebfcb48..129feb2 100644 --- a/test/Twig/Tests/Node/AutoEscapeTest.php +++ b/test/Twig/Tests/Node/AutoEscapeTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_AutoEscapeTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_AutoEscapeTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_AutoEscape::__construct diff --git a/test/Twig/Tests/Node/BlockReferenceTest.php b/test/Twig/Tests/Node/BlockReferenceTest.php index f1c5ab1..d775683 100644 --- a/test/Twig/Tests/Node/BlockReferenceTest.php +++ b/test/Twig/Tests/Node/BlockReferenceTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_BlockReferenceTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_BlockReferenceTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_BlockReference::__construct diff --git a/test/Twig/Tests/Node/BlockTest.php b/test/Twig/Tests/Node/BlockTest.php index 6bc5e79..ea3e324 100644 --- a/test/Twig/Tests/Node/BlockTest.php +++ b/test/Twig/Tests/Node/BlockTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_BlockTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_BlockTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Block::__construct diff --git a/test/Twig/Tests/Node/DoTest.php b/test/Twig/Tests/Node/DoTest.php index 49c2a59..97caed4 100644 --- a/test/Twig/Tests/Node/DoTest.php +++ b/test/Twig/Tests/Node/DoTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_DoTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_DoTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Do::__construct diff --git a/test/Twig/Tests/Node/Expression/ArrayTest.php b/test/Twig/Tests/Node/Expression/ArrayTest.php index 06b30e9..7bf878a 100644 --- a/test/Twig/Tests/Node/Expression/ArrayTest.php +++ b/test/Twig/Tests/Node/Expression/ArrayTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_ArrayTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_ArrayTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Array::__construct diff --git a/test/Twig/Tests/Node/Expression/AssignNameTest.php b/test/Twig/Tests/Node/Expression/AssignNameTest.php index fc92b0d..7103f23 100644 --- a/test/Twig/Tests/Node/Expression/AssignNameTest.php +++ b/test/Twig/Tests/Node/Expression/AssignNameTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_AssignNameTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_AssignNameTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_AssignName::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/AddTest.php b/test/Twig/Tests/Node/Expression/Binary/AddTest.php index 80d318f..5066ae2 100644 --- a/test/Twig/Tests/Node/Expression/Binary/AddTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/AddTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_AddTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_AddTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Add::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/AndTest.php b/test/Twig/Tests/Node/Expression/Binary/AndTest.php index a6ed4f2..8f8bd3f 100644 --- a/test/Twig/Tests/Node/Expression/Binary/AndTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/AndTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_AndTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_AndTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_And::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/ConcatTest.php b/test/Twig/Tests/Node/Expression/Binary/ConcatTest.php index a04edc9..815c430 100644 --- a/test/Twig/Tests/Node/Expression/Binary/ConcatTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/ConcatTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_ConcatTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_ConcatTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Concat::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/DivTest.php b/test/Twig/Tests/Node/Expression/Binary/DivTest.php index 42002fc..ce27b07 100644 --- a/test/Twig/Tests/Node/Expression/Binary/DivTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/DivTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_DivTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_DivTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Div::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/FloorDivTest.php b/test/Twig/Tests/Node/Expression/Binary/FloorDivTest.php index 94ff2f6..b5720ae 100644 --- a/test/Twig/Tests/Node/Expression/Binary/FloorDivTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/FloorDivTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_FloorDivTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_FloorDivTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_FloorDiv::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/ModTest.php b/test/Twig/Tests/Node/Expression/Binary/ModTest.php index 67612a5..a16f21b 100644 --- a/test/Twig/Tests/Node/Expression/Binary/ModTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/ModTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_ModTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_ModTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Mod::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/MulTest.php b/test/Twig/Tests/Node/Expression/Binary/MulTest.php index c817d02..f46a8ca 100644 --- a/test/Twig/Tests/Node/Expression/Binary/MulTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/MulTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_MulTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_MulTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Mul::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/OrTest.php b/test/Twig/Tests/Node/Expression/Binary/OrTest.php index d8e2f8d..1d60943 100644 --- a/test/Twig/Tests/Node/Expression/Binary/OrTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/OrTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_OrTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_OrTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Or::__construct diff --git a/test/Twig/Tests/Node/Expression/Binary/SubTest.php b/test/Twig/Tests/Node/Expression/Binary/SubTest.php index 6583d09..a8487d9 100644 --- a/test/Twig/Tests/Node/Expression/Binary/SubTest.php +++ b/test/Twig/Tests/Node/Expression/Binary/SubTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Binary_SubTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Binary_SubTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Binary_Sub::__construct diff --git a/test/Twig/Tests/Node/Expression/ConditionalTest.php b/test/Twig/Tests/Node/Expression/ConditionalTest.php index 4a05e4e..3a9d1f5 100644 --- a/test/Twig/Tests/Node/Expression/ConditionalTest.php +++ b/test/Twig/Tests/Node/Expression/ConditionalTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_ConditionalTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_ConditionalTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Conditional::__construct diff --git a/test/Twig/Tests/Node/Expression/ConstantTest.php b/test/Twig/Tests/Node/Expression/ConstantTest.php index 0cf3867..4ceb53a 100644 --- a/test/Twig/Tests/Node/Expression/ConstantTest.php +++ b/test/Twig/Tests/Node/Expression/ConstantTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_ConstantTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_ConstantTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Constant::__construct diff --git a/test/Twig/Tests/Node/Expression/FilterTest.php b/test/Twig/Tests/Node/Expression/FilterTest.php index 2c52482..6c55f7a 100644 --- a/test/Twig/Tests/Node/Expression/FilterTest.php +++ b/test/Twig/Tests/Node/Expression/FilterTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_FilterTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Filter::__construct diff --git a/test/Twig/Tests/Node/Expression/FunctionTest.php b/test/Twig/Tests/Node/Expression/FunctionTest.php index 68c5c61..2733309 100644 --- a/test/Twig/Tests/Node/Expression/FunctionTest.php +++ b/test/Twig/Tests/Node/Expression/FunctionTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_FunctionTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Function::__construct diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index 6df143d..a1c36b7 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_GetAttr::__construct diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index e77b432..d333f83 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_NameTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Name::__construct diff --git a/test/Twig/Tests/Node/Expression/ParentTest.php b/test/Twig/Tests/Node/Expression/ParentTest.php index 7efbe03..0f5d7b4 100644 --- a/test/Twig/Tests/Node/Expression/ParentTest.php +++ b/test/Twig/Tests/Node/Expression/ParentTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_ParentTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_ParentTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Parent::__construct diff --git a/test/Twig/Tests/Node/Expression/TestTest.php b/test/Twig/Tests/Node/Expression/TestTest.php index 08668c0..d89db60 100644 --- a/test/Twig/Tests/Node/Expression/TestTest.php +++ b/test/Twig/Tests/Node/Expression/TestTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../TestCase.php'; - -class Twig_Tests_Node_Expression_TestTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Test::__construct diff --git a/test/Twig/Tests/Node/Expression/Unary/NegTest.php b/test/Twig/Tests/Node/Expression/Unary/NegTest.php index 218de6e..05187c2 100644 --- a/test/Twig/Tests/Node/Expression/Unary/NegTest.php +++ b/test/Twig/Tests/Node/Expression/Unary/NegTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Unary_NegTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Unary_NegTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Unary_Neg::__construct diff --git a/test/Twig/Tests/Node/Expression/Unary/NotTest.php b/test/Twig/Tests/Node/Expression/Unary/NotTest.php index ed6349c..74815a9 100644 --- a/test/Twig/Tests/Node/Expression/Unary/NotTest.php +++ b/test/Twig/Tests/Node/Expression/Unary/NotTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Unary_NotTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Unary_NotTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Unary_Not::__construct diff --git a/test/Twig/Tests/Node/Expression/Unary/PosTest.php b/test/Twig/Tests/Node/Expression/Unary/PosTest.php index 6a414bc..04cf38c 100644 --- a/test/Twig/Tests/Node/Expression/Unary/PosTest.php +++ b/test/Twig/Tests/Node/Expression/Unary/PosTest.php @@ -9,9 +9,7 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/../../TestCase.php'; - -class Twig_Tests_Node_Expression_Unary_PosTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_Expression_Unary_PosTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Expression_Unary_Pos::__construct diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index ef3be82..273df7b 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_ForTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_For::__construct diff --git a/test/Twig/Tests/Node/IfTest.php b/test/Twig/Tests/Node/IfTest.php index 04b92e7..ce68bc3 100644 --- a/test/Twig/Tests/Node/IfTest.php +++ b/test/Twig/Tests/Node/IfTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_IfTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_IfTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_If::__construct diff --git a/test/Twig/Tests/Node/ImportTest.php b/test/Twig/Tests/Node/ImportTest.php index 6a89195..64286b8 100644 --- a/test/Twig/Tests/Node/ImportTest.php +++ b/test/Twig/Tests/Node/ImportTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_ImportTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_ImportTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Import::__construct diff --git a/test/Twig/Tests/Node/IncludeTest.php b/test/Twig/Tests/Node/IncludeTest.php index d068392..7e1db64 100644 --- a/test/Twig/Tests/Node/IncludeTest.php +++ b/test/Twig/Tests/Node/IncludeTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_IncludeTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_IncludeTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Include::__construct diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index 3dd57a6..4af1005 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_MacroTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Macro::__construct diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 592949b..1a8fea8 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_ModuleTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Module::__construct diff --git a/test/Twig/Tests/Node/PrintTest.php b/test/Twig/Tests/Node/PrintTest.php index 168663c..3df6ac7 100644 --- a/test/Twig/Tests/Node/PrintTest.php +++ b/test/Twig/Tests/Node/PrintTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_PrintTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_PrintTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Print::__construct diff --git a/test/Twig/Tests/Node/SandboxTest.php b/test/Twig/Tests/Node/SandboxTest.php index 1610873..7b57f6b 100644 --- a/test/Twig/Tests/Node/SandboxTest.php +++ b/test/Twig/Tests/Node/SandboxTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_SandboxTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_SandboxTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Sandbox::__construct diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 2ce0584..24d879e 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_SandboxedModuleTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_SandboxedModuleTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_SandboxedModule::__construct diff --git a/test/Twig/Tests/Node/SandboxedPrintTest.php b/test/Twig/Tests/Node/SandboxedPrintTest.php index 85adddf..1837aa5 100644 --- a/test/Twig/Tests/Node/SandboxedPrintTest.php +++ b/test/Twig/Tests/Node/SandboxedPrintTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_SandboxedPrintTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_SandboxedPrintTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_SandboxedPrint::__construct diff --git a/test/Twig/Tests/Node/SetTest.php b/test/Twig/Tests/Node/SetTest.php index 6319fb6..6bc9ccd 100644 --- a/test/Twig/Tests/Node/SetTest.php +++ b/test/Twig/Tests/Node/SetTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_SetTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_SetTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Set::__construct diff --git a/test/Twig/Tests/Node/SpacelessTest.php b/test/Twig/Tests/Node/SpacelessTest.php index 55892f4..eef3fbe 100644 --- a/test/Twig/Tests/Node/SpacelessTest.php +++ b/test/Twig/Tests/Node/SpacelessTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_SpacelessTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_SpacelessTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Spaceless::__construct diff --git a/test/Twig/Tests/Node/TestCase.php b/test/Twig/Tests/Node/TestCase.php deleted file mode 100644 index f142529..0000000 --- a/test/Twig/Tests/Node/TestCase.php +++ /dev/null @@ -1,58 +0,0 @@ -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('; - } -} diff --git a/test/Twig/Tests/Node/TextTest.php b/test/Twig/Tests/Node/TextTest.php index 87e0337..f5e3f8c 100644 --- a/test/Twig/Tests/Node/TextTest.php +++ b/test/Twig/Tests/Node/TextTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -require_once dirname(__FILE__).'/TestCase.php'; -class Twig_Tests_Node_TextTest extends Twig_Tests_Node_TestCase +class Twig_Tests_Node_TextTest extends Twig_Test_NodeTestCase { /** * @covers Twig_Node_Text::__construct diff --git a/test/Twig/Tests/integrationTest.php b/test/Twig/Tests/integrationTest.php deleted file mode 100644 index 3171758..0000000 --- a/test/Twig/Tests/integrationTest.php +++ /dev/null @@ -1,328 +0,0 @@ -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; - } - - /** - * @dataProvider getTests - */ - public function testIntegration($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->addExtension(new TestExtension()); - $twig->addExtension(new Twig_Extension_Debug()); - $policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(), array(), array()); - $twig->addExtension(new Twig_Extension_Sandbox($policy, false)); - $twig->addGlobal('global', 'global'); - - 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 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; - } -} - -function test_foo($value = 'foo') -{ - return $value; -} - -class Foo implements Iterator -{ - const BAR_NAME = 'bar'; - - public $position = 0; - public $array = array(1, 2); - - public function bar($param1 = null, $param2 = null) - { - return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : ''); - } - - public function getFoo() - { - return 'foo'; - } - - public function getSelf() - { - return $this; - } - - public function is() - { - return 'is'; - } - - public function in() - { - return 'in'; - } - - public function not() - { - return 'not'; - } - - public function strToLower($value) - { - return strtolower($value); - } - - public function rewind() - { - $this->position = 0; - } - - public function current() - { - return $this->array[$this->position]; - } - - public function key() - { - return 'a'; - } - - public function next() - { - ++$this->position; - } - - public function valid() - { - return isset($this->array[$this->position]); - } -} - -class TestTokenParser_☃ extends Twig_TokenParser -{ - public function parse(Twig_Token $token) - { - $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - - return new Twig_Node_Print(new Twig_Node_Expression_Constant('☃', -1), -1); - } - - public function getTag() - { - return '☃'; - } -} - -class TestExtension extends Twig_Extension -{ - public function getTokenParsers() - { - return array( - new TestTokenParser_☃(), - ); - } - - public function getFilters() - { - return array( - '☃' => new Twig_Filter_Method($this, '☃Filter'), - 'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))), - 'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), - 'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))), - 'preserves_safety' => new Twig_Filter_Method($this, 'preserves_safety', array('preserves_safety' => array('html'))), - '*_path' => new Twig_Filter_Method($this, 'dynamic_path'), - '*_foo_*_bar' => new Twig_Filter_Method($this, 'dynamic_foo'), - ); - } - - public function getFunctions() - { - return array( - '☃' => new Twig_Function_Method($this, '☃Function'), - 'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))), - 'unsafe_br' => new Twig_Function_Method($this, 'br'), - '*_path' => new Twig_Function_Method($this, 'dynamic_path'), - '*_foo_*_bar' => new Twig_Function_Method($this, 'dynamic_foo'), - ); - } - - public function ☃Filter($value) - { - return "☃{$value}☃"; - } - - public function ☃Function($value) - { - return "☃{$value}☃"; - } - - /** - * nl2br which also escapes, for testing escaper filters - */ - public function escape_and_nl2br($env, $value, $sep = '
') - { - return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep); - } - - /** - * nl2br only, for testing filters with pre_escape - */ - public function nl2br($value, $sep = '
') - { - // not secure if $value contains html tags (not only entities) - // don't use - return str_replace("\n", "$sep\n", $value); - } - - public function dynamic_path($element, $item) - { - return $element.'/'.$item; - } - - public function dynamic_foo($foo, $bar, $item) - { - return $foo.'/'.$bar.'/'.$item; - } - - public function escape_something($value) - { - return strtoupper($value); - } - - public function preserves_safety($value) - { - return strtoupper($value); - } - - public function br() - { - return '
'; - } - - public function getName() - { - return 'test'; - } -} -- 1.7.2.5