diff --git a/.travis.yml b/.travis.yml index c19ff53b8..4bf5bfeb8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,28 +1,33 @@ language: php php: - - 5.5 - - 5.6 - - hhvm - - nightly + - 5.5 + - 5.6 + - hhvm + - nightly allow_failures: - php: nightly env: - - TWIG_EXT=no - - TWIG_EXT=yes + - TWIG_EXT=no + - TWIG_EXT=yes before_script: - - if [ "$TWIG_EXT" == "yes" ]; then sh -c "cd ext/twig && phpize && ./configure --enable-twig && make && sudo make install"; fi - - if [ "$TWIG_EXT" == "yes" ]; then echo "extension=twig.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi + - if [ "$TWIG_EXT" == "yes" ]; then sh -c "cd ext/twig && phpize && ./configure --enable-twig && make && sudo make install"; fi + - if [ "$TWIG_EXT" == "yes" ]; then echo "extension=twig.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi + - if [ ${TRAVIS_PHP_VERSION:0:3} == "5.2" ]; then sed -i.bak "s|vendor/autoload.php|test/bootstrap.php|" phpunit.xml.dist; fi + +install: + # Composer is not available on PHP 5.2 + - if [ ${TRAVIS_PHP_VERSION:0:3} != "5.2" ]; then travis_retry composer install; fi install: - travis_retry composer install --no-interaction matrix: - exclude: - - php: hhvm - env: TWIG_EXT=yes - - php: nightly - env: TWIG_EXT=yes + exclude: + - php: hhvm + env: TWIG_EXT=yes + - php: nightly + env: TWIG_EXT=yes diff --git a/CHANGELOG b/CHANGELOG index 6b70cf2c1..9d19f24d5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,9 +13,10 @@ * improved the performance of the filesystem loader * removed features that were deprecated in 1.x -* 1.20.1 (2015-XX-XX) +* 1.21.0 (2015-XX-XX) - * n/a + * added deprecation notices for deprecated features + * added a deprecation "framework" for filters/functions/tests and test fixtures * 1.20.0 (2015-08-12) diff --git a/composer.json b/composer.json index 16c356e3b..ba17fb135 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,10 @@ "require": { "php": ">=5.5.0" }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7", + "symfony/debug": "~2.7" + }, "autoload": { "psr-0" : { "Twig_" : "lib/" diff --git a/doc/advanced.rst b/doc/advanced.rst index 7114a025e..45a08b289 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -260,6 +260,23 @@ arguments, but after the environment and the context. For instance, a call to ``'foo'|a_path_b()`` will result in the following arguments to be passed to the filter: ``('a', 'b', 'foo')``. +Deprecated Filters +~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 1.21 + Support for deprecated filters was added in Twig 1.21. + +You can mark a filter as being deprecated by setting the ``deprecated`` option +to ``true``. You can also give an alternative filter that replaces the +deprecated one when that makes sense:: + + $filter = new Twig_SimpleFilter('obsolete', function () { + // ... + }, array('deprecated' => true, 'alternative' => 'new_one')); + +When a filter is deprecated, Twig emits a deprecation notice when compiling a +template using it. See :ref:`deprecation-notices` for more information. + Functions --------- diff --git a/doc/recipes.rst b/doc/recipes.rst index 344ca1f80..2fbf39939 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -1,6 +1,64 @@ Recipes ======= +.. _deprecation-notices: + +Displaying Deprecation Notices +------------------------------ + +.. versionadded:: 1.21 + This works as of Twig 1.21. + +Deprecated features generate deprecation notices (via a call to the +``trigger_error()`` PHP function). By default, they are silenced and never +displayed nor logged. + +To easily remove all deprecated feature usages from your templates, write and +run a script along the lines of the following:: + + require_once __DIR__.'/vendor/autoload.php'; + + $twig = create_your_twig_env(); + + $deprecations = new Twig_Util_DeprecationCollector($twig); + + print_r($deprecations->collectDir(__DIR__.'/templates')); + +The ``collectDir()`` method compiles all templates found in a directory, +catches deprecation notices, and return them. + +.. tip:: + + If your templates are not stored on the filesystem, use the ``collect()`` + method instead which takes an ``Iterator``; the iterator must return + template names as keys and template contents as values (as done by + ``Twig_Util_TemplateDirIterator``). + +However, this code won't find all deprecations (like using deprecated some Twig +classes). To catch all notices, register a custom error handler like the one +below:: + + $deprecations = array(); + set_error_handler(function ($type, $msg) use (&$deprecations) { + if (E_USER_DEPRECATED === $type) { + $deprecations[] = $msg; + } + }); + + // run your application + + print_r($deprecations); + +Note that most deprecation notices are triggered during **compilation**, so +they won't be generated when templates are already cached. + +.. tip:: + + If you want to manage the deprecation notices from your PHPUnit tests, have + a look at the `symfony/phpunit-bridge + `_ package, which eases the + process a lot. + Making a Layout conditional --------------------------- diff --git a/lib/Twig/BaseNodeVisitor.php b/lib/Twig/BaseNodeVisitor.php index d90801c4e..9ba334293 100644 --- a/lib/Twig/BaseNodeVisitor.php +++ b/lib/Twig/BaseNodeVisitor.php @@ -35,8 +35,8 @@ abstract class Twig_BaseNodeVisitor implements Twig_NodeVisitorInterface /** * Called before child nodes are visited. * - * @param Twig_Node $node The node to visit - * @param Twig_Environment $env The Twig environment instance + * @param Twig_Node $node The node to visit + * @param Twig_Environment $env The Twig environment instance * * @return Twig_Node The modified node */ diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 85090344f..f070e5284 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -296,18 +296,17 @@ class Twig_Extension_Core extends Twig_Extension public function parseTestExpression(Twig_Parser $parser, Twig_Node $node) { $stream = $parser->getStream(); - $name = $this->getTestName($parser, $node->getLine()); - $testMap = $parser->getEnvironment()->getTests(); - $class = $testMap[$name]->getNodeClass(); + $test = $this->getTest($parser, $node->getLine()); + $class = $test->getNodeClass(); $arguments = null; if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { $arguments = $parser->getExpressionParser()->parseArguments(true); } - return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine()); + return new $class($node, $test->getName(), $arguments, $parser->getCurrentToken()->getLine()); } - private function getTestName(Twig_Parser $parser, $line) + private function getTest(Twig_Parser $parser, $line) { $stream = $parser->getStream(); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); @@ -315,7 +314,7 @@ class Twig_Extension_Core extends Twig_Extension $testMap = $env->getTests(); if (isset($testMap[$name])) { - return $name; + return $testMap[$name]; } if ($stream->test(Twig_Token::NAME_TYPE)) { @@ -325,7 +324,7 @@ class Twig_Extension_Core extends Twig_Extension if (isset($testMap[$name])) { $parser->getStream()->next(); - return $name; + return $testMap[$name]; } } @@ -371,8 +370,8 @@ function twig_cycle($values, $position) * - a random character from a string * - a random integer between 0 and the integer parameter. * - * @param Twig_Environment $env A Twig_Environment instance - * @param Traversable|array|int|string $values The values to pick a random item from + * @param Twig_Environment $env A Twig_Environment instance + * @param Traversable|array|int|string $values The values to pick a random item from * * @throws Twig_Error_Runtime When $values is an empty array (does not apply to an empty string which is returned as is). * @@ -459,9 +458,9 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ * {{ post.published_at|date_modify("-1day")|date("m/d/Y") }} * * - * @param Twig_Environment $env A Twig_Environment instance - * @param DateTime|string $date A date - * @param string $modifier A modifier string + * @param Twig_Environment $env A Twig_Environment instance + * @param DateTime|string $date A date + * @param string $modifier A modifier string * * @return DateTime A new date object */ @@ -532,11 +531,11 @@ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = nu /** * Rounds a number. * - * @param int|float $value The value to round - * @param int|float $precision The rounding precision - * @param string $method The method to use for rounding + * @param int|float $value The value to round + * @param int|float $precision The rounding precision + * @param string $method The method to use for rounding * - * @return int|float The rounded number + * @return int|float The rounded number */ function twig_round($value, $precision = 0, $method = 'common') { @@ -558,11 +557,11 @@ function twig_round($value, $precision = 0, $method = 'common') * be used. Supplying any of the parameters will override the defaults set in the * environment object. * - * @param Twig_Environment $env A Twig_Environment instance - * @param mixed $number A float/int/string of the number to format - * @param int $decimal The number of decimal points to display. - * @param string $decimalPoint The character(s) to use for the decimal point. - * @param string $thousandSep The character(s) to use for the thousands separator. + * @param Twig_Environment $env A Twig_Environment instance + * @param mixed $number A float/int/string of the number to format + * @param int $decimal The number of decimal points to display. + * @param string $decimalPoint The character(s) to use for the decimal point. + * @param string $thousandSep The character(s) to use for the thousands separator. * * @return string The formatted number */ @@ -768,9 +767,9 @@ function twig_join_filter($value, $glue = '') * {# returns [aa, bb, cc] #} * * - * @param string $value A string - * @param string $delimiter The delimiter - * @param int $limit The limit + * @param string $value A string + * @param string $delimiter The delimiter + * @param int $limit The limit * * @return array The split string as an array */ @@ -1319,7 +1318,7 @@ function twig_ensure_traversable($seq) * * @param mixed $value A variable * - * @return bool true if the value is empty, false otherwise + * @return bool true if the value is empty, false otherwise */ function twig_test_empty($value) { @@ -1342,7 +1341,7 @@ function twig_test_empty($value) * * @param mixed $value A variable * - * @return bool true if the value is traversable + * @return bool true if the value is traversable */ function twig_test_iterable($value) { @@ -1436,9 +1435,9 @@ function twig_constant($constant, $object = null) /** * Batches item. * - * @param array $items An array of items - * @param int $size The size of the batch - * @param mixed $fill A value used to fill missing items + * @param array $items An array of items + * @param int $size The size of the batch + * @param mixed $fill A value used to fill missing items * * @return array */ diff --git a/lib/Twig/Filter.php b/lib/Twig/Filter.php index cf9f7d83b..3bb2c5f1b 100644 --- a/lib/Twig/Filter.php +++ b/lib/Twig/Filter.php @@ -34,6 +34,8 @@ class Twig_Filter 'pre_escape' => null, 'preserves_safety' => null, 'node_class' => 'Twig_Node_Expression_Filter', + 'deprecated' => false, + 'alternative' => null, ), $options); } @@ -97,4 +99,14 @@ class Twig_Filter { return $this->options['is_variadic']; } + + public function isDeprecated() + { + return $this->options['deprecated']; + } + + public function getAlternative() + { + return $this->options['alternative']; + } } diff --git a/lib/Twig/Function.php b/lib/Twig/Function.php index dff943d71..d673a2940 100644 --- a/lib/Twig/Function.php +++ b/lib/Twig/Function.php @@ -32,6 +32,8 @@ class Twig_Function 'is_safe' => null, 'is_safe_callback' => null, 'node_class' => 'Twig_Node_Expression_Function', + 'deprecated' => false, + 'alternative' => null, ), $options); } @@ -87,4 +89,14 @@ class Twig_Function { return $this->options['is_variadic']; } + + public function isDeprecated() + { + return $this->options['deprecated']; + } + + public function getAlternative() + { + return $this->options['alternative']; + } } diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 5504608ff..0397061d0 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -63,7 +63,7 @@ class Twig_Parser { // push all variables into the stack to keep the current state of the parser $vars = get_object_vars($this); - unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser']); + unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']); $this->stack[] = $vars; // tag handlers diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index a00ef7218..6a947fc10 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -145,7 +145,7 @@ abstract class Twig_Template if (null !== $template) { // avoid RCEs when sandbox is enabled - if (!$template instanceof Twig_Template) { + if (!$template instanceof self) { throw new \LogicException('A block must be a method on a Twig_Template instance.'); } diff --git a/lib/Twig/Test.php b/lib/Twig/Test.php index c3918027f..44e109d28 100644 --- a/lib/Twig/Test.php +++ b/lib/Twig/Test.php @@ -27,6 +27,8 @@ class Twig_Test $this->options = array_merge(array( 'is_variadic' => false, 'node_class' => 'Twig_Node_Expression_Test', + 'deprecated' => false, + 'alternative' => null, ), $options); } @@ -49,4 +51,14 @@ class Twig_Test { return $this->options['is_variadic']; } + + public function isDeprecated() + { + return $this->options['deprecated']; + } + + public function getAlternative() + { + return $this->options['alternative']; + } } diff --git a/lib/Twig/Test/IntegrationTestCase.php b/lib/Twig/Test/IntegrationTestCase.php index 92501569d..8549c9298 100644 --- a/lib/Twig/Test/IntegrationTestCase.php +++ b/lib/Twig/Test/IntegrationTestCase.php @@ -28,7 +28,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->testIntegration($file, $message, $condition, $templates, $exception, $outputs); + } + + public function getTests($name, $legacyTests = false) { $fixturesDir = realpath($this->getFixturesDir()); $tests = array(); @@ -38,10 +47,13 @@ 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]); @@ -60,9 +72,19 @@ abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs); } + if (!$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 ($condition) { diff --git a/lib/Twig/Util/DeprecationCollector.php b/lib/Twig/Util/DeprecationCollector.php new file mode 100644 index 000000000..e406f0aa7 --- /dev/null +++ b/lib/Twig/Util/DeprecationCollector.php @@ -0,0 +1,82 @@ + + */ +class Twig_Util_DeprecationCollector +{ + private $twig; + private $deprecations; + + public function __construct(Twig_Environment $twig) + { + $this->twig = $twig; + } + + /** + * Returns deprecations for templates contained in a directory. + * + * @param string $dir A directory where templates are stored + * @param string $ext Limit the loaded templates by extension + * + * @return array() An array of deprecations + */ + public function collectDir($dir, $ext = '.twig') + { + $iterator = new RegexIterator( + new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY + ), '{'.preg_quote($ext).'$}' + ); + + return $this->collect(new Twig_Util_TemplateDirIterator($iterator)); + } + + /** + * Returns deprecations for passed templates. + * + * @param Iterator $iterator An iterator of templates (where keys are template names and values the contents of the template) + * + * @return array() An array of deprecations + */ + public function collect(Iterator $iterator) + { + $this->deprecations = array(); + + set_error_handler(array($this, 'errorHandler')); + + foreach ($iterator as $name => $contents) { + try { + $this->twig->parse($this->twig->tokenize($contents, $name)); + } catch (Twig_Error_Syntax $e) { + // ignore templates containing syntax errors + } + } + + restore_error_handler(); + + $deprecations = $this->deprecations; + $this->deprecations = array(); + + return $deprecations; + } + + /** + * @internal + */ + public function errorHandler($type, $msg) + { + if (E_USER_DEPRECATED === $type) { + $this->deprecations[] = $msg; + } + } +} diff --git a/lib/Twig/Util/TemplateDirIterator.php b/lib/Twig/Util/TemplateDirIterator.php new file mode 100644 index 000000000..3fb893278 --- /dev/null +++ b/lib/Twig/Util/TemplateDirIterator.php @@ -0,0 +1,26 @@ + + */ +class Twig_Util_TemplateDirIterator extends IteratorIterator +{ + public function current() + { + return file_get_contents(parent::current()); + } + + public function key() + { + return (string) parent::key(); + } +} diff --git a/test/Twig/Tests/CompilerTest.php b/test/Twig/Tests/CompilerTest.php index 7aa2708c3..bc25f117b 100644 --- a/test/Twig/Tests/CompilerTest.php +++ b/test/Twig/Tests/CompilerTest.php @@ -13,7 +13,7 @@ class Twig_Tests_CompilerTest extends PHPUnit_Framework_TestCase { public function testReprNumericValueWithLocale() { - $compiler = new Twig_Compiler(new Twig_Environment(new Twig_Loader_Array(array()))); + $compiler = new Twig_Compiler(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $locale = setlocale(LC_NUMERIC, 0); if (false === $locale) { diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index 0ad3490ee..a4692c267 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -16,7 +16,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase */ public function testRandomFunction($value, $expectedInArray) { - $env = new Twig_Environment(new Twig_Loader_Array(array())); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface')); for ($i = 0; $i < 100; ++$i) { $this->assertTrue(in_array(twig_random($env, $value), $expectedInArray, true)); // assertContains() would not consider the type @@ -62,18 +62,18 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase $max = mt_getrandmax(); for ($i = 0; $i < 100; ++$i) { - $val = twig_random(new Twig_Environment(new Twig_Loader_Array(array()))); + $val = twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $this->assertTrue(is_int($val) && $val >= 0 && $val <= $max); } } public function testRandomFunctionReturnsAsIs() { - $this->assertSame('', twig_random(new Twig_Environment(new Twig_Loader_Array(array())), '')); - $this->assertSame('', twig_random(new Twig_Environment(new Twig_Loader_Array(array()), array('charset' => null)), '')); + $this->assertSame('', twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), '')); + $this->assertSame('', twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('charset' => null)), '')); $instance = new stdClass(); - $this->assertSame($instance, twig_random(new Twig_Environment(new Twig_Loader_Array(array())), $instance)); + $this->assertSame($instance, twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $instance)); } /** @@ -81,7 +81,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase */ public function testRandomFunctionOfEmptyArrayThrowsException() { - twig_random(new Twig_Environment(new Twig_Loader_Array(array())), array()); + twig_random(new Twig_Environment($this->getMock('Twig_LoaderInterface')), array()); } public function testRandomFunctionOnNonUTF8String() @@ -90,7 +90,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase $this->markTestSkipped('needs iconv or mbstring'); } - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->setCharset('ISO-8859-1'); $text = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8'); @@ -106,7 +106,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase $this->markTestSkipped('needs iconv or mbstring'); } - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->setCharset('ISO-8859-1'); $input = twig_convert_encoding('Äé', 'ISO-8859-1', 'UTF-8'); @@ -117,7 +117,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase public function testCustomEscaper() { - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test'); $this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo')); @@ -128,12 +128,12 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase */ public function testUnknownCustomEscaper() { - twig_escape_filter(new Twig_Environment(new Twig_Loader_Array(array())), 'foo', 'bar'); + twig_escape_filter(new Twig_Environment($this->getMock('Twig_LoaderInterface')), 'foo', 'bar'); } public function testTwigFirst() { - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $this->assertEquals('a', twig_first($twig, 'abc')); $this->assertEquals(1, twig_first($twig, array(1, 2, 3))); $this->assertSame('', twig_first($twig, null)); @@ -142,7 +142,7 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase public function testTwigLast() { - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $this->assertEquals('c', twig_last($twig, 'abc')); $this->assertEquals(3, twig_last($twig, array(1, 2, 3))); $this->assertSame('', twig_last($twig, null)); diff --git a/test/Twig/Tests/LexerTest.php b/test/Twig/Tests/LexerTest.php index 05fc8f079..1bb7c6b85 100644 --- a/test/Twig/Tests/LexerTest.php +++ b/test/Twig/Tests/LexerTest.php @@ -14,7 +14,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{% § %}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::BLOCK_START_TYPE); @@ -25,7 +25,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ §() }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); @@ -42,7 +42,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase protected function countToken($template, $type, $value = null) { - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $count = 0; @@ -67,7 +67,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase ."baz\n" ."}}\n"; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); // foo\nbar\n @@ -87,7 +87,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase ."baz\n" ."}}\n"; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); // foo\nbar @@ -102,17 +102,17 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{# '.str_repeat('*', 100000).' #}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); // should not throw an exception } - public function testLongRaw() + public function testLongVerbatim() { - $template = '{% raw %}'.str_repeat('*', 100000).'{% endraw %}'; + $template = '{% verbatim %}'.str_repeat('*', 100000).'{% endverbatim %}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); // should not throw an exception @@ -122,7 +122,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ '.str_repeat('x', 100000).' }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); // should not throw an exception @@ -132,7 +132,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{% '.str_repeat('x', 100000).' %}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); // should not throw an exception @@ -142,7 +142,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ 922337203685477580700 }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->next(); $node = $stream->next(); @@ -155,7 +155,8 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase "{{ 'foo \' bar' }}" => 'foo \' bar', '{{ "foo \" bar" }}' => 'foo " bar', ); - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); foreach ($tests as $template => $expected) { $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); @@ -167,7 +168,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = 'foo {{ "bar #{ baz + 1 }" }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::TEXT_TYPE, 'foo '); $stream->expect(Twig_Token::VAR_START_TYPE); @@ -184,7 +185,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ "bar \#{baz+1}" }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); $stream->expect(Twig_Token::STRING_TYPE, 'bar #{baz+1}'); @@ -195,7 +196,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ "bar # baz" }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); $stream->expect(Twig_Token::STRING_TYPE, 'bar # baz'); @@ -210,7 +211,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ "bar #{x" }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); } @@ -218,7 +219,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{{ "bar #{ "foo#{bar}" }" }}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); $stream->expect(Twig_Token::STRING_TYPE, 'bar '); @@ -235,7 +236,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = '{% foo "bar #{ "foo#{bar}" }" %}'; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::BLOCK_START_TYPE); $stream->expect(Twig_Token::NAME_TYPE, 'foo'); @@ -253,7 +254,7 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase { $template = "{{ 1 and\n0}}"; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $stream = $lexer->tokenize($template); $stream->expect(Twig_Token::VAR_START_TYPE); $stream->expect(Twig_Token::NUMBER_TYPE, 1); @@ -275,7 +276,7 @@ bar '; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); } @@ -294,7 +295,7 @@ bar '; - $lexer = new Twig_Lexer(new Twig_Environment(new Twig_Loader_Array(array()))); + $lexer = new Twig_Lexer(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $lexer->tokenize($template); } } diff --git a/test/Twig/Tests/Node/Expression/FilterTest.php b/test/Twig/Tests/Node/Expression/FilterTest.php index dd21af39d..48f929bda 100644 --- a/test/Twig/Tests/Node/Expression/FilterTest.php +++ b/test/Twig/Tests/Node/Expression/FilterTest.php @@ -25,7 +25,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase public function getTests() { - $environment = new Twig_Environment(new Twig_Loader_Array(array())); + $environment = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $environment->addFilter(new Twig_SimpleFilter('bar', 'bar', array('needs_environment' => true))); $environment->addFilter(new Twig_SimpleFilter('barbar', 'twig_tests_filter_barbar', array('needs_context' => true, 'is_variadic' => true))); diff --git a/test/Twig/Tests/Node/Expression/FunctionTest.php b/test/Twig/Tests/Node/Expression/FunctionTest.php index 957039e14..f13c3b6c2 100644 --- a/test/Twig/Tests/Node/Expression/FunctionTest.php +++ b/test/Twig/Tests/Node/Expression/FunctionTest.php @@ -23,7 +23,7 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase public function getTests() { - $environment = new Twig_Environment(new Twig_Loader_Array(array())); + $environment = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $environment->addFunction(new Twig_Function('foo', 'foo', array())); $environment->addFunction(new Twig_Function('bar', 'bar', array('needs_environment' => true))); $environment->addFunction(new Twig_Function('foofoo', 'foofoo', array('needs_context' => true))); diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index 41fd3c748..10f51a1ca 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -23,8 +23,8 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase $node = new Twig_Node_Expression_Name('foo', 1); $context = new Twig_Node_Expression_Name('_context', 1); - $env = new Twig_Environment(new Twig_Loader_Array(array()), array('strict_variables' => true)); - $env1 = new Twig_Environment(new Twig_Loader_Array(array()), array('strict_variables' => false)); + $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => true)); + $env1 = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => false)); return array( array($node, "// line 1\n".'(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : $this->notFound("foo", 1))', $env), diff --git a/test/Twig/Tests/Node/Expression/TestTest.php b/test/Twig/Tests/Node/Expression/TestTest.php index 5a364e643..2b90a10cd 100644 --- a/test/Twig/Tests/Node/Expression/TestTest.php +++ b/test/Twig/Tests/Node/Expression/TestTest.php @@ -25,7 +25,7 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase public function getTests() { - $environment = new Twig_Environment(new Twig_Loader_Array(array())); + $environment = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $environment->addTest(new Twig_SimpleTest('barbar', 'twig_tests_test_barbar', array('is_variadic' => true, 'need_context' => true))); $tests = array(); diff --git a/test/Twig/Tests/ParserTest.php b/test/Twig/Tests/ParserTest.php index 120ca1e0d..398173b67 100644 --- a/test/Twig/Tests/ParserTest.php +++ b/test/Twig/Tests/ParserTest.php @@ -22,7 +22,7 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', 1), new Twig_Token(Twig_Token::EOF_TYPE, '', 1), )); - $parser = new Twig_Parser(new Twig_Environment(new Twig_Loader_Array(array()))); + $parser = new Twig_Parser(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $parser->parse($stream); } @@ -93,7 +93,7 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase public function testParseIsReentrant() { - $twig = new Twig_Environment(new Twig_Loader_Array(array()), array( + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array( 'autoescape' => false, 'optimizations' => 0, )); @@ -119,7 +119,7 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase // see https://github.com/symfony/symfony/issues/4218 public function testGetVarName() { - $twig = new Twig_Environment(new Twig_Loader_Array(array()), array( + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array( 'autoescape' => false, 'optimizations' => 0, )); @@ -136,7 +136,7 @@ EOF protected function getParser() { - $parser = new Twig_Parser(new Twig_Environment(new Twig_Loader_Array(array()))); + $parser = new Twig_Parser(new Twig_Environment($this->getMock('Twig_LoaderInterface'))); $parser->setParent(new Twig_Node()); $p = new ReflectionProperty($parser, 'stream'); $p->setAccessible(true); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 447e87741..c028b00ab 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -89,7 +89,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeWithSandbox($object, $item, $allowed, $useExt) { - $twig = new Twig_Environment(new Twig_Loader_Array(array())); + $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface')); $policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(/*method*/), array(/*prop*/), array()); $twig->addExtension(new Twig_Extension_Sandbox($policy, !$allowed)); $template = new Twig_TemplateTest($twig, $useExt); @@ -133,8 +133,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeWithTemplateAsObject($useExt) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array())), $useExt); - $template1 = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array())), false); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $useExt); + $template1 = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), false); $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string')); $this->assertEquals('some_string', $template->getAttribute($template1, 'string')); @@ -173,7 +173,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase public function testGetAttributeOnArrayWithConfusableKey($useExt = false) { $template = new Twig_TemplateTest( - new Twig_Environment(new Twig_Loader_Array(array())), + new Twig_Environment($this->getMock('Twig_LoaderInterface')), $useExt ); @@ -212,7 +212,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttribute($defined, $value, $object, $item, $arguments, $type, $useExt = false) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array())), $useExt); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $useExt); $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type)); } @@ -222,7 +222,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false, $exceptionMessage = null) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array()), array('strict_variables' => true)), $useExt); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => true)), $useExt); if ($defined) { $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type)); @@ -244,7 +244,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type, $useExt = false) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array())), $useExt); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $useExt); $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true)); } @@ -254,7 +254,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array()), array('strict_variables' => true)), $useExt); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => true)), $useExt); $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true)); } @@ -264,7 +264,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase */ public function testGetAttributeCallExceptions($useExt = false) { - $template = new Twig_TemplateTest(new Twig_Environment(new Twig_Loader_Array(array())), $useExt); + $template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), $useExt); $object = new Twig_TemplateMagicMethodExceptionObject(); diff --git a/test/Twig/Tests/escapingTest.php b/test/Twig/Tests/escapingTest.php index ccd547003..7b765caee 100644 --- a/test/Twig/Tests/escapingTest.php +++ b/test/Twig/Tests/escapingTest.php @@ -146,7 +146,7 @@ class Twig_Test_EscapingTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->env = new Twig_Environment(new Twig_Loader_Array(array())); + $this->env = new Twig_Environment($this->getMock('Twig_LoaderInterface')); } public function testHtmlEscapingConvertsSpecialChars()