From 0ff5eaf8516a2bc040cbe92b8b768e0ac235a923 Mon Sep 17 00:00:00 2001 From: Hussard Date: Wed, 15 Nov 2017 15:07:54 +0100 Subject: [PATCH 1/4] Remove the use of Twig_TokenParserBroker (deprecated) + fix all deprecation warnings - #31 Conflicts: src/Asm89/Twig/Lint/StubbedEnvironment.php --- src/Asm89/Twig/Lint/Command/LintCommand.php | 27 ++++++--- src/Asm89/Twig/Lint/Extension/StubbedCore.php | 43 --------------- src/Asm89/Twig/Lint/StubbedEnvironment.php | 25 ++++++--- .../Twig/Lint/StubbedTokenParserBroker.php | 55 ------------------- .../Twig/Lint/Test/StubbedEnvironmentTest.php | 19 ++++--- 5 files changed, 48 insertions(+), 121 deletions(-) delete mode 100644 src/Asm89/Twig/Lint/Extension/StubbedCore.php delete mode 100644 src/Asm89/Twig/Lint/StubbedTokenParserBroker.php diff --git a/src/Asm89/Twig/Lint/Command/LintCommand.php b/src/Asm89/Twig/Lint/Command/LintCommand.php index 6555f7f..28301d0 100644 --- a/src/Asm89/Twig/Lint/Command/LintCommand.php +++ b/src/Asm89/Twig/Lint/Command/LintCommand.php @@ -50,6 +50,13 @@ protected function configure() InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Excludes, based on regex, paths of files and folders from parsing' ), + new InputOption( + 'stub-tag', + '', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'List of tags that the lint command has to provide stub for', + array() + ), new InputOption('only-print-errors', '', InputOption::VALUE_NONE), new InputOption('summary', '', InputOption::VALUE_NONE) )) @@ -81,12 +88,18 @@ protected function configure() protected function execute(InputInterface $input, CliOutputInterface $output) { - $twig = new StubbedEnvironment(new \Twig_Loader_String()); - $template = null; - $filename = $input->getArgument('filename'); - $exclude = $input->getOption('exclude'); - $summary = $input->getOption('summary'); - $output = $this->getOutput($output, $input->getOption('format')); + $template = null; + $filename = $input->getArgument('filename'); + $exclude = $input->getOption('exclude'); + $stubTagList = $input->getOption('stub-tag'); + $summary = $input->getOption('summary'); + $output = $this->getOutput($output, $input->getOption('format')); + $twig = new StubbedEnvironment( + new \Twig_Loader_Array(), + array( + 'stub_tags' => $stubTagList, + ) + ); if (!$filename) { if (0 !== ftell(STDIN)) { @@ -151,7 +164,7 @@ protected function validateTemplate( ) { try { - $twig->parse($twig->tokenize($template, $file ? (string) $file : null)); + $twig->parse($twig->tokenize(new \Twig_Source($template, $file ? (string) $file : null))); if (false === $onlyPrintErrors) { $output->ok($template, $file); } diff --git a/src/Asm89/Twig/Lint/Extension/StubbedCore.php b/src/Asm89/Twig/Lint/Extension/StubbedCore.php deleted file mode 100644 index bcc4b59..0000000 --- a/src/Asm89/Twig/Lint/Extension/StubbedCore.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Asm89\Twig\Lint\Extension; - -/** - * Overridden core extension to stub tests. - * - * @author Alexander - */ -class StubbedCore extends \Twig_Extension_Core -{ - /** - * Return a class name for every test name. - * - * @param \Twig_Parser $parser - * @param string $name - * @param integer $line - * - * @return string - */ - protected function getTestNodeClass(\Twig_Parser $parser, $name) - { - return 'Twig_Node_Expression_Test'; - } - - protected function getTestName(\Twig_Parser $parser, $line) - { - try { - return parent::getTestName($parser, $line); - } catch (\Twig_Error_Syntax $exception) { - return 'null'; - } - } -} diff --git a/src/Asm89/Twig/Lint/StubbedEnvironment.php b/src/Asm89/Twig/Lint/StubbedEnvironment.php index a84d146..d49ac4a 100644 --- a/src/Asm89/Twig/Lint/StubbedEnvironment.php +++ b/src/Asm89/Twig/Lint/StubbedEnvironment.php @@ -11,7 +11,7 @@ namespace Asm89\Twig\Lint; -use Asm89\Twig\Lint\Extension\StubbedCore; +use Asm89\Twig\Lint\TokenParser\CatchAll; use Twig_LoaderInterface; /** @@ -25,7 +25,7 @@ class StubbedEnvironment extends \Twig_Environment private $stubFilters; private $stubFunctions; private $stubTests; - protected $parsers; + private $stubCallable; /** * {@inheritDoc} @@ -34,11 +34,18 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra { parent::__construct($loader, $options); - $this->addExtension(new StubbedCore()); - $this->initExtensions(); + $this->stubFilters = array(); + $this->stubFunctions = array(); + $this->stubTests = array(); + $this->stubCallable = function () { + /* This will be used as stub filter, function or test */ + }; - $broker = new StubbedTokenParserBroker(); - $this->parsers->addTokenParserBroker($broker); + if (isset($options['stub_tags'])) { + foreach ($options['stub_tags'] as $tag) { + $this->addTokenParser(new CatchAll($tag)); + } + } } /** @@ -47,7 +54,7 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra public function getFilter($name) { if (!isset($this->stubFilters[$name])) { - $this->stubFilters[$name] = new \Twig_Filter_Function('stub'); + $this->stubFilters[$name] = new \Twig_SimpleFilter('stub', $this->stubCallable); } return $this->stubFilters[$name]; @@ -59,7 +66,7 @@ public function getFilter($name) public function getFunction($name) { if (!isset($this->stubFunctions[$name])) { - $this->stubFunctions[$name] = new \Twig_Function_Function('stub'); + $this->stubFunctions[$name] = new \Twig_SimpleFunction('stub', $this->stubCallable); } return $this->stubFunctions[$name]; @@ -71,7 +78,7 @@ public function getFunction($name) public function getTest($name) { if (!isset($this->stubTests[$name])) { - $this->stubTests[$name] = new \Twig_SimpleTest('stub', function(){}); + $this->stubTests[$name] = new \Twig_SimpleTest('stub', $this->stubCallable); } return $this->stubTests[$name]; diff --git a/src/Asm89/Twig/Lint/StubbedTokenParserBroker.php b/src/Asm89/Twig/Lint/StubbedTokenParserBroker.php deleted file mode 100644 index 95042d6..0000000 --- a/src/Asm89/Twig/Lint/StubbedTokenParserBroker.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Asm89\Twig\Lint; - -use Asm89\Twig\Lint\TokenParser\CatchAll; -use Twig_TokenParserBroker; - -/** - * Broker providing stubs for all tags that are not defined. - * - * @author Alexander - */ -class StubbedTokenParserBroker extends Twig_TokenParserBroker -{ - protected $parser; - protected $parsers; - - /** - * {@inheritDoc} - */ - public function getTokenParser($name) - { - if (!isset($this->parsers[$name])) { - $this->parsers[$name] = new CatchAll($name); - $this->parsers[$name]->setParser($this->parser); - } - - return $this->parsers[$name]; - } - - /** - * {@inheritDoc} - */ - public function getParser() - { - return $this->parser; - } - - /** - * {@inheritDoc} - */ - public function setParser(\Twig_ParserInterface $parser) - { - $this->parser = $parser; - } -} diff --git a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php index 7412cfd..9ccc109 100644 --- a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php +++ b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php @@ -12,7 +12,7 @@ namespace Asm89\Twig\Lint\Test; use Asm89\Twig\Lint\StubbedEnvironment; -use Twig_Error; +use \Twig_Error; /** * @author Alexander @@ -23,21 +23,26 @@ class StubbedEnvironmentTest extends \PHPUnit_Framework_TestCase public function setup() { - $this->env = new StubbedEnvironment(); + $this->env = new StubbedEnvironment( + $this->getMockBuilder('Twig_LoaderInterface')->getMock(), + array( + 'stub_tags' => array('meh', 'render', 'some_other_block', 'stylesheets', 'trans'), + ) + ); } public function testGetFilterAlwaysReturnsAFilter() { $filter = $this->env->getFilter('foo'); - $this->assertInstanceOf('Twig_Filter', $filter); + $this->assertInstanceOf('Twig_SimpleFilter', $filter); } public function testGetFunctionAlwaysReturnsAFunction() { $function = $this->env->getFunction('foo'); - $this->assertInstanceOf('Twig_Function', $function); + $this->assertInstanceOf('Twig_SimpleFunction', $function); } /** @@ -48,12 +53,12 @@ public function testParseTemplatesWithUndefinedElements($filename) $file = __DIR__ . '/Fixtures/' . $filename; $template = file_get_contents($file); try { - $this->env->parse($this->env->tokenize($template, $file)); + $this->env->parse($this->env->tokenize(new \Twig_Source($template, $file))); } catch (Twig_Error $exception) { - $this->assertTrue(false, "Was unable to parse the template."); + $this->assertTrue(false, sprintf('Was unable to parse the template: "%s"', $exception->getMessage())); } - $this->assertTrue(true, "Was able to parse the template."); + $this->assertTrue(true, 'Was able to parse the template.'); } public function templateFixtures() From fdcd69d0e923ab117e5e60b331ae09d2b97ad002 Mon Sep 17 00:00:00 2001 From: Hussard Date: Sun, 19 Nov 2017 16:06:42 +0100 Subject: [PATCH 2/4] Apply fix for travis, php 5.3 and trusty --- .travis.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ce4dcac..5939114 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ language: php +dist: trusty + sudo: false php: - - 5.3 - 5.4 - 5.5 - 5.6 @@ -13,3 +14,8 @@ php: before_script: composer install script: phpunit + +matrix: + include: # https://github.com/travis-ci/travis-ci/issues/7712#issuecomment-300553336 + - php: "5.3" + dist: precise From 0f23be99fbcb0662849bcae785f9d73eb62b947f Mon Sep 17 00:00:00 2001 From: Hussard Date: Thu, 30 Nov 2017 17:06:09 +0100 Subject: [PATCH 3/4] Fix two words tests 'same as' and 'divisible by' for example --- composer.json | 3 +++ src/Asm89/Twig/Lint/Command/LintCommand.php | 25 +++++++++++++------ src/Asm89/Twig/Lint/StubbedEnvironment.php | 24 +++++++++++++----- .../Lint/Test/Fixtures/undefined_test.twig | 4 +++ .../Twig/Lint/Test/StubbedEnvironmentTest.php | 3 ++- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 4183fce..120fc71 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,9 @@ "symfony/finder": "^2.1 || ^3.0", "twig/twig": "^1.16.2" }, + "require-dev": { + "phpunit/phpunit": "^4.8.36 || ^5.5 || ^6.2" + }, "autoload": { "psr-0": { "Asm89\\Twig\\Lint\\": "src/" } }, diff --git a/src/Asm89/Twig/Lint/Command/LintCommand.php b/src/Asm89/Twig/Lint/Command/LintCommand.php index 28301d0..2b3bffb 100644 --- a/src/Asm89/Twig/Lint/Command/LintCommand.php +++ b/src/Asm89/Twig/Lint/Command/LintCommand.php @@ -57,6 +57,13 @@ protected function configure() 'List of tags that the lint command has to provide stub for', array() ), + new InputOption( + 'stub-test', + '', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'List of tests that the lint command has to provide stub for', + array() + ), new InputOption('only-print-errors', '', InputOption::VALUE_NONE), new InputOption('summary', '', InputOption::VALUE_NONE) )) @@ -88,16 +95,18 @@ protected function configure() protected function execute(InputInterface $input, CliOutputInterface $output) { - $template = null; - $filename = $input->getArgument('filename'); - $exclude = $input->getOption('exclude'); - $stubTagList = $input->getOption('stub-tag'); - $summary = $input->getOption('summary'); - $output = $this->getOutput($output, $input->getOption('format')); - $twig = new StubbedEnvironment( + $template = null; + $filename = $input->getArgument('filename'); + $exclude = $input->getOption('exclude'); + $stubTagList = $input->getOption('stub-tag'); + $stubTestsList = $input->getOption('stub-test'); + $summary = $input->getOption('summary'); + $output = $this->getOutput($output, $input->getOption('format')); + $twig = new StubbedEnvironment( new \Twig_Loader_Array(), array( - 'stub_tags' => $stubTagList, + 'stub_tags' => $stubTagList, + 'stub_tests' => $stubTestsList, ) ); diff --git a/src/Asm89/Twig/Lint/StubbedEnvironment.php b/src/Asm89/Twig/Lint/StubbedEnvironment.php index d49ac4a..6149bde 100644 --- a/src/Asm89/Twig/Lint/StubbedEnvironment.php +++ b/src/Asm89/Twig/Lint/StubbedEnvironment.php @@ -34,18 +34,25 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra { parent::__construct($loader, $options); - $this->stubFilters = array(); - $this->stubFunctions = array(); - $this->stubTests = array(); $this->stubCallable = function () { /* This will be used as stub filter, function or test */ }; + $this->stubFilters = array(); + $this->stubFunctions = array(); + if (isset($options['stub_tags'])) { foreach ($options['stub_tags'] as $tag) { $this->addTokenParser(new CatchAll($tag)); } } + + $this->stubTests = array(); + if (isset($options['stub_tests'])) { + foreach ($options['stub_tests'] as $test) { + $this->stubTests[$test] = new \Twig_SimpleTest('stub', $this->stubCallable); + } + } } /** @@ -77,10 +84,15 @@ public function getFunction($name) */ public function getTest($name) { - if (!isset($this->stubTests[$name])) { - $this->stubTests[$name] = new \Twig_SimpleTest('stub', $this->stubCallable); + $test = parent::getTest($name); + if ($test) { + return $test; + } + + if (isset($this->stubTests[$name])) { + return $this->stubTests[$name]; } - return $this->stubTests[$name]; + return false; } } diff --git a/tests/Asm89/Twig/Lint/Test/Fixtures/undefined_test.twig b/tests/Asm89/Twig/Lint/Test/Fixtures/undefined_test.twig index b259142..20761fd 100644 --- a/tests/Asm89/Twig/Lint/Test/Fixtures/undefined_test.twig +++ b/tests/Asm89/Twig/Lint/Test/Fixtures/undefined_test.twig @@ -5,3 +5,7 @@ {% if foo is some_undefined_test_with_args(bar) %} {{ foo }} {% endif %} + +{% if foo is created by('me') %} + {{ foo }} +{% endif %} diff --git a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php index 9ccc109..30e576c 100644 --- a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php +++ b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php @@ -26,7 +26,8 @@ public function setup() $this->env = new StubbedEnvironment( $this->getMockBuilder('Twig_LoaderInterface')->getMock(), array( - 'stub_tags' => array('meh', 'render', 'some_other_block', 'stylesheets', 'trans'), + 'stub_tags' => array('meh', 'render', 'some_other_block', 'stylesheets', 'trans'), + 'stub_tests' => array('created by', 'sometest', 'some_undefined_test', 'some_undefined_test_with_args'), ) ); } From 76fe4d7d2e0f50e7f13c9408ee78dbc5e0528a7a Mon Sep 17 00:00:00 2001 From: Hussard Date: Thu, 30 Nov 2017 17:13:27 +0100 Subject: [PATCH 4/4] Forward-compatibility for phpunit and PHP7 --- tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php index 30e576c..be043d9 100644 --- a/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php +++ b/tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php @@ -12,12 +12,13 @@ namespace Asm89\Twig\Lint\Test; use Asm89\Twig\Lint\StubbedEnvironment; +use PHPUnit\Framework\TestCase; use \Twig_Error; /** * @author Alexander */ -class StubbedEnvironmentTest extends \PHPUnit_Framework_TestCase +class StubbedEnvironmentTest extends TestCase { private $env;