From 18f4203827008ca836ce820b0947817146f800e0 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 3 Sep 2024 08:58:43 +0200 Subject: [PATCH 1/4] Prepare IntegrationTestCase for static data providers --- CHANGELOG | 2 ++ doc/deprecated.rst | 7 +++++ extra/cache-extra/Tests/IntegrationTest.php | 2 +- extra/cache-extra/composer.json | 2 +- .../Tests/IntegrationTest.php | 2 +- extra/cssinliner-extra/composer.json | 2 +- extra/html-extra/Tests/IntegrationTest.php | 2 +- extra/html-extra/composer.json | 2 +- extra/inky-extra/Tests/IntegrationTest.php | 2 +- extra/inky-extra/composer.json | 2 +- extra/intl-extra/Tests/IntegrationTest.php | 2 +- extra/intl-extra/composer.json | 2 +- .../markdown-extra/Tests/IntegrationTest.php | 2 +- extra/markdown-extra/composer.json | 2 +- extra/string-extra/Tests/IntegrationTest.php | 2 +- extra/string-extra/composer.json | 2 +- src/Test/IntegrationTestCase.php | 26 +++++++++++++++++-- tests/IntegrationTest.php | 2 +- 18 files changed, 48 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 19fe5863c..0b3cca744 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,8 @@ * Mark `Twig\Test\NodeTestCase::getEnvironment()` as final, override `createEnvironment()` instead. * Deprecate `Twig\Test\NodeTestCase::getVariableGetter()`, call `createVariableGetter()` instead. * Deprecate `Twig\Test\NodeTestCase::getAttributeGetter()`, call `createAttributeGetter()` instead. + * Deprecate not overriding `Twig\Test\IntegrationTestCase::getFixturesDirectory()`, this method will be abstract in 4.0 + * Marked `Twig\Test\IntegrationTestCase::getTests()` and `getLegacyTests()` as final # 3.12.0 (2024-08-29) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 71a17996f..e28ecc62b 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -216,3 +216,10 @@ Testing Utilities * The method ``Twig\Test\NodeTestCase::getEnvironment()`` is considered final as of Twig 3.13. If you want to override how the Twig environment is constructed, override ``createEnvironment()`` instead. + +* The method ``getFixturesDir()`` on ``Twig\Test\IntegrationTestCase`` is + deprecated, implement the new static method ``getFixturesDirectory()`` + instead, which will be abstract in 4.0. + +* The data providers ``getTests()`` and ``getLegacyTests()`` on + ``Twig\Test\IntegrationTestCase`` are considered final als of Twig 3.13. diff --git a/extra/cache-extra/Tests/IntegrationTest.php b/extra/cache-extra/Tests/IntegrationTest.php index 4f597b0aa..c439976f9 100644 --- a/extra/cache-extra/Tests/IntegrationTest.php +++ b/extra/cache-extra/Tests/IntegrationTest.php @@ -38,7 +38,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/cache-extra/composer.json b/extra/cache-extra/composer.json index ab354ddc9..a36504995 100644 --- a/extra/cache-extra/composer.json +++ b/extra/cache-extra/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=8.0.2", "symfony/cache": "^5.4|^6.4|^7.0", - "twig/twig": "^3.12|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/extra/cssinliner-extra/Tests/IntegrationTest.php b/extra/cssinliner-extra/Tests/IntegrationTest.php index 5ab6ec9b4..7004b5e99 100644 --- a/extra/cssinliner-extra/Tests/IntegrationTest.php +++ b/extra/cssinliner-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/cssinliner-extra/composer.json b/extra/cssinliner-extra/composer.json index 96e59f2ff..229843f50 100644 --- a/extra/cssinliner-extra/composer.json +++ b/extra/cssinliner-extra/composer.json @@ -18,7 +18,7 @@ "php": ">=8.0.2", "symfony/deprecation-contracts": "^2.5|^3", "tijsverkoyen/css-to-inline-styles": "^2.0", - "twig/twig": "^3.0|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/extra/html-extra/Tests/IntegrationTest.php b/extra/html-extra/Tests/IntegrationTest.php index 8f464c152..8e2f94e38 100644 --- a/extra/html-extra/Tests/IntegrationTest.php +++ b/extra/html-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/html-extra/composer.json b/extra/html-extra/composer.json index 46ec29e69..d902b3967 100644 --- a/extra/html-extra/composer.json +++ b/extra/html-extra/composer.json @@ -18,7 +18,7 @@ "php": ">=8.0.2", "symfony/deprecation-contracts": "^2.5|^3", "symfony/mime": "^5.4|^6.4|^7.0", - "twig/twig": "^3.0|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/extra/inky-extra/Tests/IntegrationTest.php b/extra/inky-extra/Tests/IntegrationTest.php index 317d36476..d9420dd09 100644 --- a/extra/inky-extra/Tests/IntegrationTest.php +++ b/extra/inky-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/inky-extra/composer.json b/extra/inky-extra/composer.json index c4192d420..cb630b96e 100644 --- a/extra/inky-extra/composer.json +++ b/extra/inky-extra/composer.json @@ -18,7 +18,7 @@ "php": ">=8.0.2", "symfony/deprecation-contracts": "^2.5|^3", "lorenzo/pinky": "^1.0.5", - "twig/twig": "^3.0|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/extra/intl-extra/Tests/IntegrationTest.php b/extra/intl-extra/Tests/IntegrationTest.php index 7b191bacd..fa22b5708 100644 --- a/extra/intl-extra/Tests/IntegrationTest.php +++ b/extra/intl-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/intl-extra/composer.json b/extra/intl-extra/composer.json index 0c5ba1cc0..8355df43c 100644 --- a/extra/intl-extra/composer.json +++ b/extra/intl-extra/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": ">=8.0.2", - "twig/twig": "^3.10|^4.0", + "twig/twig": "^3.13|^4.0", "symfony/intl": "^5.4|^6.4|^7.0" }, "require-dev": { diff --git a/extra/markdown-extra/Tests/IntegrationTest.php b/extra/markdown-extra/Tests/IntegrationTest.php index 7474ec769..7db95c919 100644 --- a/extra/markdown-extra/Tests/IntegrationTest.php +++ b/extra/markdown-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/markdown-extra/composer.json b/extra/markdown-extra/composer.json index 91101e2c1..cf5458368 100644 --- a/extra/markdown-extra/composer.json +++ b/extra/markdown-extra/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=8.0.2", "symfony/deprecation-contracts": "^2.5|^3", - "twig/twig": "^3.0|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0", diff --git a/extra/string-extra/Tests/IntegrationTest.php b/extra/string-extra/Tests/IntegrationTest.php index 032c9a9d9..ddf6abfe5 100644 --- a/extra/string-extra/Tests/IntegrationTest.php +++ b/extra/string-extra/Tests/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } diff --git a/extra/string-extra/composer.json b/extra/string-extra/composer.json index d6c0bfa64..becf9de89 100644 --- a/extra/string-extra/composer.json +++ b/extra/string-extra/composer.json @@ -18,7 +18,7 @@ "php": ">=8.0.2", "symfony/string": "^5.4|^6.4|^7.0", "symfony/translation-contracts": "^1.1|^2|^3", - "twig/twig": "^3.0|^4.0" + "twig/twig": "^3.13|^4.0" }, "require-dev": { "symfony/phpunit-bridge": "^6.4|^7.0" diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 88b3349c4..78b0171e1 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -30,9 +30,18 @@ use Twig\TwigTest; abstract class IntegrationTestCase extends TestCase { /** + * @deprecated since Twig 3.13, use getFixturesDirectory() instead. * @return string */ - abstract protected function getFixturesDir(); + protected function getFixturesDir() + { + throw new \BadMethodCallException('Not implemented.'); + } + + protected static function getFixturesDirectory(): string + { + throw new \BadMethodCallException('Not implemented.'); + } /** * @return RuntimeLoaderInterface[] @@ -92,9 +101,19 @@ abstract class IntegrationTestCase extends TestCase $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation); } + /** + * @final since Twig 3.13 + */ public function getTests($name, $legacyTests = false) { - $fixturesDir = realpath($this->getFixturesDir()); + try { + $fixturesDir = static::getFixturesDirectory(); + } catch (\BadMethodCallException) { + trigger_deprecation('twig/twig', '3.13', 'Not overriding "%s::getFixturesDirectory()" in "%s" is deprecated. This method will be abstract in 4.0.', self::class, static::class); + $fixturesDir = $this->getFixturesDir(); + } + + $fixturesDir = realpath($fixturesDir); $tests = []; foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { @@ -137,6 +156,9 @@ abstract class IntegrationTestCase extends TestCase return $tests; } + /** + * @final since Twig 3.13 + */ public function getLegacyTests() { return $this->getTests('testLegacyIntegration', true); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index f68ac15cf..65f207d90 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -48,7 +48,7 @@ class IntegrationTest extends IntegrationTestCase ]; } - public function getFixturesDir() + protected static function getFixturesDirectory(): string { return __DIR__.'/Fixtures/'; } From 6ddb76bb760e828155c31944f2d25b56b6b09f1c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 2 Sep 2024 17:29:38 +0200 Subject: [PATCH 2/4] Make data providers static --- extra/markdown-extra/Tests/FunctionalTest.php | 2 +- tests/Cache/FilesystemTest.php | 2 +- tests/CustomExtensionTest.php | 2 +- tests/ErrorTest.php | 2 +- tests/ExpressionParserTest.php | 18 +++++++++--------- tests/Extension/CoreTest.php | 14 +++++++------- tests/Extension/EscaperTest.php | 2 +- tests/Extension/SandboxTest.php | 8 ++++---- tests/FileExtensionEscapingStrategyTest.php | 2 +- tests/LexerTest.php | 8 ++++---- tests/Loader/FilesystemTest.php | 6 +++--- tests/NodeVisitor/OptimizerTest.php | 2 +- tests/ParserTest.php | 6 +++--- tests/Runtime/EscaperRuntimeTest.php | 4 ++-- tests/TemplateTest.php | 8 ++++---- tests/TokenParser/TypesTokenParserTest.php | 2 +- 16 files changed, 44 insertions(+), 44 deletions(-) diff --git a/extra/markdown-extra/Tests/FunctionalTest.php b/extra/markdown-extra/Tests/FunctionalTest.php index 62c7928fe..154d75a9c 100644 --- a/extra/markdown-extra/Tests/FunctionalTest.php +++ b/extra/markdown-extra/Tests/FunctionalTest.php @@ -57,7 +57,7 @@ EOF } } - public function getMarkdownTests() + public static function getMarkdownTests() { return [ [<<assertMatchesRegularExpression($expected, $cache->generateKey('_test_', static::class)); } - public function provideDirectories() + public static function provideDirectories() { $pattern = '#a/b/[a-zA-Z0-9]+/[a-zA-Z0-9]+.php$#'; diff --git a/tests/CustomExtensionTest.php b/tests/CustomExtensionTest.php index a2ac0dbed..fab7813b1 100644 --- a/tests/CustomExtensionTest.php +++ b/tests/CustomExtensionTest.php @@ -31,7 +31,7 @@ class CustomExtensionTest extends TestCase $env->getUnaryOperators(); } - public function provideInvalidExtensions() + public static function provideInvalidExtensions() { return [ [new InvalidOperatorExtension([1, 2, 3]), '"Twig\Tests\InvalidOperatorExtension::getOperators()" must return an array of 2 elements, got 3.'], diff --git a/tests/ErrorTest.php b/tests/ErrorTest.php index 423a1a58d..d29112cd7 100644 --- a/tests/ErrorTest.php +++ b/tests/ErrorTest.php @@ -234,7 +234,7 @@ EOHTML } } - public function getErroredTemplates() + public static function getErroredTemplates() { return [ // error occurs in a template diff --git a/tests/ExpressionParserTest.php b/tests/ExpressionParserTest.php index 3f28cca1e..e94f8d184 100644 --- a/tests/ExpressionParserTest.php +++ b/tests/ExpressionParserTest.php @@ -48,7 +48,7 @@ class ExpressionParserTest extends TestCase $parser->parse($env->tokenize(new Source($template, 'index'))); } - public function getFailingTestsForAssignment() + public static function getFailingTestsForAssignment() { return [ ['{% set false = "foo" %}'], @@ -91,7 +91,7 @@ class ExpressionParserTest extends TestCase $parser->parse($env->tokenize(new Source($template, 'index'))); } - public function getFailingTestsForSequence() + public static function getFailingTestsForSequence() { return [ ['{{ [1, "a": "b"] }}'], @@ -100,7 +100,7 @@ class ExpressionParserTest extends TestCase ]; } - public function getTestsForSequence() + public static function getTestsForSequence() { return [ // simple sequence @@ -190,7 +190,7 @@ class ExpressionParserTest extends TestCase new ConstantExpression(2, 1), new ConstantExpression(2, 1), - $this->createNameExpression('foo', ['spread' => true]), + self::createNameExpression('foo', ['spread' => true]), ], 1)], // mapping with spread operator @@ -203,7 +203,7 @@ class ExpressionParserTest extends TestCase new ConstantExpression('c', 1), new ConstantExpression(0, 1), - $this->createNameExpression('otherLetters', ['spread' => true]), + self::createNameExpression('otherLetters', ['spread' => true]), ], 1)], ]; } @@ -232,7 +232,7 @@ class ExpressionParserTest extends TestCase $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode('0')->getNode('expr')); } - public function getTestsForString() + public static function getTestsForString() { return [ [ @@ -321,7 +321,7 @@ class ExpressionParserTest extends TestCase $parser->parse($env->tokenize(new Source($template, 'index'))); } - public function getMacroDefinitionDoesNotSupportNonConstantDefaultValues() + public static function getMacroDefinitionDoesNotSupportNonConstantDefaultValues() { return [ ['{% macro foo(name = "a #{foo} a") %}{% endmacro %}'], @@ -344,7 +344,7 @@ class ExpressionParserTest extends TestCase $this->addToAssertionCount(1); } - public function getMacroDefinitionSupportsConstantDefaultValues() + public static function getMacroDefinitionSupportsConstantDefaultValues() { return [ ['{% macro foo(name = "aa") %}{% endmacro %}'], @@ -584,7 +584,7 @@ class ExpressionParserTest extends TestCase $this->doesNotPerformAssertions(); } - private function createNameExpression(string $name, array $attributes) + private static function createNameExpression(string $name, array $attributes): NameExpression { $expression = new NameExpression($name, 1); foreach ($attributes as $key => $value) { diff --git a/tests/Extension/CoreTest.php b/tests/Extension/CoreTest.php index f61980bf9..2272bb4d0 100644 --- a/tests/Extension/CoreTest.php +++ b/tests/Extension/CoreTest.php @@ -67,7 +67,7 @@ class CoreTest extends TestCase } } - public function getRandomFunctionTestData() + public static function getRandomFunctionTestData() { return [ 'array' => [ @@ -165,7 +165,7 @@ class CoreTest extends TestCase $this->assertSame($expected, CoreExtension::first('UTF-8', $input)); } - public function provideTwigFirstCases() + public static function provideTwigFirstCases() { $i = [1 => 'a', 2 => 'b', 3 => 'c']; @@ -186,7 +186,7 @@ class CoreTest extends TestCase $this->assertSame($expected, CoreExtension::last('UTF-8', $input)); } - public function provideTwigLastCases() + public static function provideTwigLastCases() { $i = [1 => 'a', 2 => 'b', 3 => 'c']; @@ -207,7 +207,7 @@ class CoreTest extends TestCase $this->assertSame($expected, CoreExtension::keys($input)); } - public function provideArrayKeyCases() + public static function provideArrayKeyCases() { $array = ['a' => 'a1', 'b' => 'b1', 'c' => 'c1']; $keys = array_keys($array); @@ -230,7 +230,7 @@ class CoreTest extends TestCase $this->assertSame($expected, CoreExtension::inFilter($value, $compare)); } - public function provideInFilterCases() + public static function provideInFilterCases() { $array = [1, 2, 'a' => 3, 5, 6, 7]; $keys = array_keys($array); @@ -258,7 +258,7 @@ class CoreTest extends TestCase $this->assertSame($expected, CoreExtension::slice('UTF-8', $input, $start, $length, $preserveKeys)); } - public function provideSliceFilterCases() + public static function provideSliceFilterCases() { $i = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; $keys = array_keys($i); @@ -296,7 +296,7 @@ class CoreTest extends TestCase $this->assertSame(1, CoreExtension::compare('foo', \NAN)); } - public function provideCompareCases() + public static function provideCompareCases() { return [ [0, 'a', 'a'], diff --git a/tests/Extension/EscaperTest.php b/tests/Extension/EscaperTest.php index 4f7071840..436d1790f 100644 --- a/tests/Extension/EscaperTest.php +++ b/tests/Extension/EscaperTest.php @@ -32,7 +32,7 @@ class EscaperTest extends TestCase $this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy, 'ISO-8859-1')); } - public function provideCustomEscaperCases() + public static function provideCustomEscaperCases() { return [ ['foo**ISO-8859-1**UTF-8', 'foo', 'foo'], diff --git a/tests/Extension/SandboxTest.php b/tests/Extension/SandboxTest.php index d193e7ef9..810bbf74e 100644 --- a/tests/Extension/SandboxTest.php +++ b/tests/Extension/SandboxTest.php @@ -79,7 +79,7 @@ class SandboxTest extends TestCase $twig->createTemplate($template, 'index')->render([]); } - public function getSandboxedForCoreTagsTests() + public static function getSandboxedForCoreTagsTests() { yield ['apply', '{% apply upper %}foo{% endapply %}']; yield ['autoescape', '{% autoescape %}foo{% endautoescape %}']; @@ -116,7 +116,7 @@ class SandboxTest extends TestCase $twig->createTemplate($template, 'index')->render([]); } - public function getSandboxedForExtendsAndUseTagsTests() + public static function getSandboxedForExtendsAndUseTagsTests() { yield ['extends', '{% extends "1_empty" %}']; yield ['use', '{% use "1_empty" %}']; @@ -253,7 +253,7 @@ class SandboxTest extends TestCase } } - public function getSandboxUnallowedToStringTests() + public static function getSandboxUnallowedToStringTests() { return [ 'simple' => ['{{ obj }}'], @@ -281,7 +281,7 @@ class SandboxTest extends TestCase $this->assertEquals($output, $twig->load('index')->render(self::$params)); } - public function getSandboxAllowedToStringTests() + public static function getSandboxAllowedToStringTests() { return [ 'constant_test' => ['{{ obj is constant("PHP_INT_MAX") }}', ''], diff --git a/tests/FileExtensionEscapingStrategyTest.php b/tests/FileExtensionEscapingStrategyTest.php index 883aa882c..ed5ce060b 100644 --- a/tests/FileExtensionEscapingStrategyTest.php +++ b/tests/FileExtensionEscapingStrategyTest.php @@ -24,7 +24,7 @@ class FileExtensionEscapingStrategyTest extends TestCase $this->assertSame($strategy, FileExtensionEscapingStrategy::guess($filename)); } - public function getGuessData() + public static function getGuessData() { return [ // default diff --git a/tests/LexerTest.php b/tests/LexerTest.php index f07a08684..476373130 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -193,7 +193,7 @@ class LexerTest extends TestCase $this->assertSame($expected, $token->getValue()); } - public function getStringWithEscapedDelimiter() + public static function getStringWithEscapedDelimiter() { yield '{{ \'\x6\' }} => \x6' => [ '{{ \'\x6\' }}', @@ -247,7 +247,7 @@ class LexerTest extends TestCase $this->addToAssertionCount(1); } - public function getStringWithEscapedDelimiterProducingDeprecation() + public static function getStringWithEscapedDelimiterProducingDeprecation() { yield '{{ \'App\Test\' }} => AppTest' => [ '{{ \'App\\Test\' }}', @@ -465,7 +465,7 @@ bar } } - public function getTemplateForErrorsAtTheEndOfTheStream() + public static function getTemplateForErrorsAtTheEndOfTheStream() { yield ['{{ =']; yield ['{{ ..']; @@ -493,7 +493,7 @@ bar $this->addToAssertionCount(1); } - public function getTemplateForStrings() + public static function getTemplateForStrings() { yield ['日本では、春になると桜の花が咲きます。多くの人々は、公園や川の近くに集まり、お花見を楽しみます。桜の花びらが風に舞い、まるで雪のように見える瞬間は、とても美しいです。']; yield ['في العالم العربي، يُعتبر الخط العربي أحد أجمل أشكال الفن. يُستخدم الخط في تزيين المساجد والكتب والمخطوطات القديمة. يتميز الخط العربي بجماله وتناسقه، ويُعتبر رمزًا للثقافة الإسلامية.']; diff --git a/tests/Loader/FilesystemTest.php b/tests/Loader/FilesystemTest.php index 44b3c170d..c7315ea80 100644 --- a/tests/Loader/FilesystemTest.php +++ b/tests/Loader/FilesystemTest.php @@ -42,7 +42,7 @@ class FilesystemTest extends TestCase } } - public function getSecurityTests() + public static function getSecurityTests() { return [ ["AutoloaderTest\0.php"], @@ -105,7 +105,7 @@ class FilesystemTest extends TestCase $this->assertEquals("named path (final)\n", $loader->getSourceContext('@named/index.html')->getCode()); } - public function getBasePaths() + public static function getBasePaths() { return [ [ @@ -197,7 +197,7 @@ class FilesystemTest extends TestCase $this->assertSame('block from theme 2', $template->renderBlock('b2', [])); } - public function getArrayInheritanceTests() + public static function getArrayInheritanceTests() { return [ 'valid array inheritance' => ['array_inheritance_valid_parent.html.twig'], diff --git a/tests/NodeVisitor/OptimizerTest.php b/tests/NodeVisitor/OptimizerTest.php index 12dc1214d..12bdc12d1 100644 --- a/tests/NodeVisitor/OptimizerTest.php +++ b/tests/NodeVisitor/OptimizerTest.php @@ -92,7 +92,7 @@ class OptimizerTest extends TestCase } } - public function getTestsForForLoopOptimizer() + public static function getTestsForForLoopOptimizer() { return [ ['{% for i in foo %}{% endfor %}', ['i' => false]], diff --git a/tests/ParserTest.php b/tests/ParserTest.php index e0c0de7fe..727092837 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -69,7 +69,7 @@ class ParserTest extends TestCase $this->assertEquals($expected, $m->invoke($parser, $input)); } - public function getFilterBodyNodesData() + public static function getFilterBodyNodesData() { return [ [ @@ -102,7 +102,7 @@ class ParserTest extends TestCase $m->invoke($parser, $input); } - public function getFilterBodyNodesDataThrowsException() + public static function getFilterBodyNodesDataThrowsException() { return [ [new TextNode('foo', 1)], @@ -122,7 +122,7 @@ class ParserTest extends TestCase $this->assertNull($m->invoke($parser, new TextNode(\chr(0xEF).\chr(0xBB).\chr(0xBF).$emptyNode, 1))); } - public function getFilterBodyNodesWithBOMData() + public static function getFilterBodyNodesWithBOMData() { return [ [' '], diff --git a/tests/Runtime/EscaperRuntimeTest.php b/tests/Runtime/EscaperRuntimeTest.php index 593563a3b..706c0074f 100644 --- a/tests/Runtime/EscaperRuntimeTest.php +++ b/tests/Runtime/EscaperRuntimeTest.php @@ -357,7 +357,7 @@ class EscaperRuntimeTest extends TestCase $this->assertSame($expected, $escaper->escape($string, $strategy, $charset)); } - public function provideCustomEscaperCases() + public static function provideCustomEscaperCases() { return [ ['foo**ISO-8859-1', 'foo', 'foo', 'ISO-8859-1'], @@ -378,7 +378,7 @@ class EscaperRuntimeTest extends TestCase $this->assertSame($escapedJs, $escaper->escape($obj, 'js', null, true)); } - public function provideObjectsForEscaping() + public static function provideObjectsForEscaping() { return [ ['<br />', '
', ['\Twig\Tests\Runtime\Extension_TestClass' => ['js']]], diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 2756c5774..1f2a8d281 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -61,7 +61,7 @@ class TemplateTest extends TestCase } } - public function getAttributeExceptions() + public static function getAttributeExceptions() { return [ ['{{ string["a"] }}', 'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1.'], @@ -113,7 +113,7 @@ class TemplateTest extends TestCase } } - public function getGetAttributeWithSandbox() + public static function getGetAttributeWithSandbox() { return [ [new TemplatePropertyObject(), 'defined', false], @@ -132,7 +132,7 @@ class TemplateTest extends TestCase $this->assertSame('', $twig->render('index')); } - public function getRenderTemplateWithoutOutputData() + public static function getRenderTemplateWithoutOutputData() { return [ [''], @@ -265,7 +265,7 @@ class TemplateTest extends TestCase $this->assertNull(CoreExtension::getAttribute($twig, $template->getSourceContext(), $object, 'foo')); } - public function getGetAttributeTests() + public static function getGetAttributeTests() { $array = [ 'defined' => 'defined', diff --git a/tests/TokenParser/TypesTokenParserTest.php b/tests/TokenParser/TypesTokenParserTest.php index 67794c097..57ccb77cb 100644 --- a/tests/TokenParser/TypesTokenParserTest.php +++ b/tests/TokenParser/TypesTokenParserTest.php @@ -22,7 +22,7 @@ class TypesTokenParserTest extends TestCase self::assertEquals($expected, $typesNode->getAttribute('mapping')); } - public function getMappingTests(): array + public static function getMappingTests(): array { return [ // empty mapping From fc20997c9e94060a83a4c5eeadad2096df0d2706 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 2 Sep 2024 17:25:25 +0200 Subject: [PATCH 3/4] Rename AbstractTest to ProfilerTestCase --- tests/Profiler/Dumper/BlackfireTest.php | 2 +- tests/Profiler/Dumper/HtmlTest.php | 2 +- .../Profiler/Dumper/{AbstractTest.php => ProfilerTestCase.php} | 2 +- tests/Profiler/Dumper/TextTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename tests/Profiler/Dumper/{AbstractTest.php => ProfilerTestCase.php} (98%) diff --git a/tests/Profiler/Dumper/BlackfireTest.php b/tests/Profiler/Dumper/BlackfireTest.php index 3a33d9403..ecf709a3d 100644 --- a/tests/Profiler/Dumper/BlackfireTest.php +++ b/tests/Profiler/Dumper/BlackfireTest.php @@ -13,7 +13,7 @@ namespace Twig\Tests\Profiler\Dumper; use Twig\Profiler\Dumper\BlackfireDumper; -class BlackfireTest extends AbstractTest +class BlackfireTest extends ProfilerTestCase { public function testDump() { diff --git a/tests/Profiler/Dumper/HtmlTest.php b/tests/Profiler/Dumper/HtmlTest.php index 2dcbb9aec..64e459bab 100644 --- a/tests/Profiler/Dumper/HtmlTest.php +++ b/tests/Profiler/Dumper/HtmlTest.php @@ -13,7 +13,7 @@ namespace Twig\Tests\Profiler\Dumper; use Twig\Profiler\Dumper\HtmlDumper; -class HtmlTest extends AbstractTest +class HtmlTest extends ProfilerTestCase { public function testDump() { diff --git a/tests/Profiler/Dumper/AbstractTest.php b/tests/Profiler/Dumper/ProfilerTestCase.php similarity index 98% rename from tests/Profiler/Dumper/AbstractTest.php rename to tests/Profiler/Dumper/ProfilerTestCase.php index 1891c2750..aa5b05109 100644 --- a/tests/Profiler/Dumper/AbstractTest.php +++ b/tests/Profiler/Dumper/ProfilerTestCase.php @@ -14,7 +14,7 @@ namespace Twig\Tests\Profiler\Dumper; use PHPUnit\Framework\TestCase; use Twig\Profiler\Profile; -abstract class AbstractTest extends TestCase +abstract class ProfilerTestCase extends TestCase { protected function getProfile() { diff --git a/tests/Profiler/Dumper/TextTest.php b/tests/Profiler/Dumper/TextTest.php index ba19c2c90..e488edc06 100644 --- a/tests/Profiler/Dumper/TextTest.php +++ b/tests/Profiler/Dumper/TextTest.php @@ -13,7 +13,7 @@ namespace Twig\Tests\Profiler\Dumper; use Twig\Profiler\Dumper\TextDumper; -class TextTest extends AbstractTest +class TextTest extends ProfilerTestCase { public function testDump() { From a035204afb9530c3acaabbf0cecc060c5ee9327e Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 3 Sep 2024 17:18:47 +0200 Subject: [PATCH 4/4] Validate the input of CoreExtension::map() --- src/Extension/CoreExtension.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 6d1a0ed54..9dc6d6e6a 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1833,6 +1833,10 @@ final class CoreExtension extends AbstractExtension */ public static function map(Environment $env, $array, $arrow) { + if (!is_iterable($array)) { + throw new RuntimeError(\sprintf('The "map" filter expects a sequence/mapping or "Traversable", got "%s".', get_debug_type($array))); + } + self::checkArrowInSandbox($env, $arrow, 'map', 'filter'); $r = [];