From 4beb7bfb0b1d120113e8ba6b82b2c4d8a902f8f4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 31 Jul 2018 09:55:27 +0200 Subject: [PATCH] Add support for dynamic tests --- .travis.yml | 2 +- CHANGELOG | 3 ++- composer.json | 2 +- doc/advanced.rst | 7 +++++-- ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 21 +++++++++++++++++---- lib/Twig/Node/Expression/Test.php | 3 +++ lib/Twig/SimpleTest.php | 12 ++++++++++++ test/Twig/Tests/IntegrationTest.php | 6 ++++++ 9 files changed, 48 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index d70399ca1..a7d1a4101 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ before_script: script: | if [[ $TRAVIS_PHP_VERSION = 7.* || $TRAVIS_PHP_VERSION = nightly ]]; then - SYMFONY_PHPUNIT_VERSION=6.1 ./vendor/bin/simple-phpunit + SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit else ./vendor/bin/simple-phpunit fi diff --git a/CHANGELOG b/CHANGELOG index 628904ec8..4fcfde212 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ -* 1.35.5 (2018-XX-XX) +* 1.36.0 (2018-XX-XX) + * added support for dynamically named tests * fixed filesystem loader throwing an exception instead of returning false * 1.35.4 (2018-07-13) diff --git a/composer.json b/composer.json index 0a1728f8e..650402e69 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.35-dev" + "dev-master": "1.36-dev" } } } diff --git a/doc/advanced.rst b/doc/advanced.rst index 2715cc57d..75f69f68a 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -371,9 +371,12 @@ The ``node`` sub-node will contain an expression of ``my_value``. Node-based tests also have access to the ``arguments`` node. This node will contain the various other arguments that have been provided to your test. +.. versionadded:: 1.36 + Dynamic tests support was added in Twig 1.36. + If you want to pass a variable number of positional or named arguments to the -test, set the ``is_variadic`` option to ``true``. Tests also support dynamic -name feature as filters and functions. +test, set the ``is_variadic`` option to ``true``. Tests support dynamic +names (see dynamic filters and functions for the syntax). Tags ---- diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index ca0a2837a..e2b955a3b 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.35.5-DEV" +#define PHP_TWIG_VERSION "1.36.0-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 7f6d3a707..03d1733ec 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,11 +16,11 @@ */ class Twig_Environment { - const VERSION = '1.35.5'; - const VERSION_ID = 13505; + const VERSION = '1.36.0'; + const VERSION_ID = 13600; const MAJOR_VERSION = 1; - const MINOR_VERSION = 35; - const RELEASE_VERSION = 5; + const MINOR_VERSION = 36; + const RELEASE_VERSION = 0; const EXTRA_VERSION = 'DEV'; protected $charset; @@ -1240,6 +1240,19 @@ class Twig_Environment return $this->tests[$name]; } + foreach ($this->tests as $pattern => $test) { + $pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count); + + if ($count) { + if (preg_match('#^'.$pattern.'$#', $name, $matches)) { + array_shift($matches); + $test->setArguments($matches); + + return $test; + } + } + } + return false; } diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index ad102ba60..4a369d6d8 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -28,6 +28,9 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression_Call $this->setAttribute('name', $name); $this->setAttribute('type', 'test'); $this->setAttribute('thing', $test); + if ($test instanceof Twig_SimpleTest) { + $this->setAttribute('arguments', $test->getArguments()); + } if ($test instanceof Twig_TestCallableInterface || $test instanceof Twig_SimpleTest) { $this->setAttribute('callable', $test->getCallable()); } diff --git a/lib/Twig/SimpleTest.php b/lib/Twig/SimpleTest.php index fee5778b3..79fd234ac 100644 --- a/lib/Twig/SimpleTest.php +++ b/lib/Twig/SimpleTest.php @@ -22,6 +22,8 @@ class Twig_SimpleTest protected $callable; protected $options; + private $arguments = array(); + public function __construct($name, $callable, array $options = array()) { $this->name = $name; @@ -68,6 +70,16 @@ class Twig_SimpleTest { return $this->options['alternative']; } + + public function setArguments($arguments) + { + $this->arguments = $arguments; + } + + public function getArguments() + { + return $this->arguments; + } } class_alias('Twig_SimpleTest', 'Twig\TwigTest', false); diff --git a/test/Twig/Tests/IntegrationTest.php b/test/Twig/Tests/IntegrationTest.php index 9cbb15904..a07c8aca7 100644 --- a/test/Twig/Tests/IntegrationTest.php +++ b/test/Twig/Tests/IntegrationTest.php @@ -168,6 +168,7 @@ class TwigTestExtension extends Twig_Extension { return array( new Twig_SimpleTest('multi word', array($this, 'is_multi_word')), + new Twig_SimpleTest('test_*', array($this, 'dynamic_test')), ); } @@ -209,6 +210,11 @@ class TwigTestExtension extends Twig_Extension return $foo.'/'.$bar.'/'.$item; } + public function dynamic_test($element, $item) + { + return $element === $item; + } + public function escape_something($value) { return strtoupper($value);