Repository: twigphp/Twig (php)

Issue:
`IntegrationTestCase` does not trigger deprecation checks for multiple `--DATA--`
Only reporting this so that you are aware that recent change to deprecation triggering - welcome in general - can cause issues under certain circumstances.

New code that makes use of `trigger_deprecation` causes some [`IntegrationTestCase`](https://github.com/twigphp/Twig/blob/3.x/src/Test/IntegrationTestCase.php) to fail, if they expect deprecation to be emitted for more than one `--DATA--` set.

A sample of test that we had that started failing in 3.11:

```
--TEST--
"ez_data_attributes_serialize" filter
--DEPRECATION--
Twig Filter "ez_data_attributes_serialize" is deprecated since version 4.0. Use "ibexa_data_attributes_serialize" instead in index.twig at line 2.
--TEMPLATE--
<a href="/article" {{ data_attributes|ez_data_attributes_serialize }}>Article</a>
--DATA--
return [
    'data_attributes' => [
        'my-attr1' => 'value1',
        'my-attr2' => 'value2,value3',
    ]
];
--EXPECT--
<a href="/article" data-my-attr1="value1" data-my-attr2="value2,value3">Article</a>
--DATA--
return [
    'data_attributes' => [
        'attr' => 'foo" style="background: red',
    ]
];
--EXPECT--
<a href="/article" data-attr="foo&quot; style=&quot;background: red">Article</a>
--DATA--
return [
    'data_attributes' => [
        'attr' => true,
    ]
];
--EXPECT--
<a href="/article" data-attr="true">Article</a>
--DATA--
return [
    'data_attributes' => [
        'attr' => ['key1' => 'value1', 'key2' => 'value2'],
    ]
];
--EXPECT--
<a href="/article" data-attr="{&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}">Article</a>
```

[source](https://github.com/ibexa/core/blob/e766878c07435ee29997e9d4e704fd8235c9cce6/tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/filters/ez_data_attributes_serialize.test)

[a different template](https://github.com/ibexa/core/blob/b6c03baa05d943a3ff6282914711d9f336c60503/tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/functions/ez_file_size/ez_file_size.test)

Error report we had in CI:

```diff
1) Ibexa\Tests\Core\MVC\Symfony\Templating\Twig\Extension\DataAttributesExtensionTest::testIntegration with data set #1 ('ez_data_attributes_serialize.test', '"ez_data_attributes_serialize" filter', '', array('\n<a href="/article" {{ data_...le</a>'), false, array(array('--DATA--\nreturn [\n    'data...</a>\n', '\nreturn [\n    'data_attribu...\n];\n', '', '\n<a href="/article" data-my-...</a>\n'), array('--DATA--\nreturn [\n    'data...</a>\n', '\nreturn [\n    'data_attribu...\n];\n', '', '\n<a href="/article" data-att...</a>\n'), array('--DATA--\nreturn [\n    'data...</a>\n', '\nreturn [\n    'data_attribu...\n];\n', '', '\n<a href="/article" data-att...</a>\n'), array('--DATA--\nreturn [\n    'data...le</a>', '\nreturn [\n    'data_attribu...\n];\n', '', '\n<a href="/article" data-att...le</a>')), 'Since  4.0: Twig Filter "ez_d...ine 2.')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'Since  4.0: Twig Filter "ez_data_attributes_serialize" is deprecated. Use "ibexa_data_attributes_serialize" instead in index.twig at line 2.'
+''
```

```diff
/home/runner/work/core/core/vendor/twig/twig/src/Test/IntegrationTestCase.php:82

2) Ibexa\Tests\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtensionTest::testIntegration with data set #1 ('ez_file_size.test', '"twig" filter', '', array('\n{{ 10|ez_file_size( 2 ) }}\...4 ) }}'), false, array(array('--DATA--\n$this->setConfigura...here\n', '\n$this->setConfigurationLoca...ay()\n', '', '\n10 B wrong local so we take...here\n'), array('--DATA--\nreturn array()\n--C...sion\n', '\nreturn array()\n', '\n$this->locale = "fre-FR"; r...y();\n', '\n10 B French version\n1 kB F...sion\n'), array('--DATA--\nreturn array()\n--C...ersion', '\nreturn array()\n', '\n$this->locale = "eng-GB"; r...y();\n', '\n10 B English version\n1 kB ...ersion')), 'Since  4.0: Twig Filter "ez_f...ne 13.')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 2.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 3.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 4.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 5.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 6.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 7.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 8.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 9.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 10.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 11.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 12.
-Since  4.0: Twig Filter "ez_file_size" is deprecated. Use "ibexa_file_size" instead in index.twig at line 13.'
+''
```

The issue we've seen is that only one `--DATA--` can be checked against `--DEPRECATION--`. Every `--DATA--` after the first will fail, because there no longer is deprecation to catch.

Additionally, only the first instance of deprecation being triggered is emitted.

We can work around this by removing additional `--DATA--`, since we have other tests that cover new variant of the filter, but that might not be the case for everyone.

Pull request:
Fix integration tests when a test has more than on data/expect section and deprecations
Fixes #4186

As deprecations are emitted at compile time, we need to make sure templates are always compiled even when we have more than one data/expect section (in which case the templates are the same). The trick here is to add some whitespace at the end of the template.


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 1eaccd6c..ff4cb5bc 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,6 @@
 # 3.11.1 (2024-XX-XX)
 
- * n/a
+ * Fix integration tests when a test has more than on data/expect section and deprecations
 
 # 3.11.0 (2024-08-08)
 


Test patch:
diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php
index 5519dd0a..88b3349c 100644
--- a/src/Test/IntegrationTestCase.php
+++ b/src/Test/IntegrationTestCase.php
@@ -156,13 +156,16 @@ abstract class IntegrationTestCase extends TestCase
             }
         }
 
-        $loader = new ArrayLoader($templates);
-
         foreach ($outputs as $i => $match) {
             $config = array_merge([
                 'cache' => false,
                 'strict_variables' => true,
             ], $match[2] ? eval($match[2].';') : []);
+            // make sure that template are always compiled even if they are the same (useful when testing with more than one data/expect sections)
+            foreach ($templates as $j => $template) {
+                $templates[$j] = $template.str_repeat(' ', $i);
+            }
+            $loader = new ArrayLoader($templates);
             $twig = new Environment($loader, $config);
             $twig->addGlobal('global', 'global');
             foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
diff --git a/tests/Fixtures/functions/deprecated.test b/tests/Fixtures/functions/deprecated.test
new file mode 100644
index 00000000..355e4330
--- /dev/null
+++ b/tests/Fixtures/functions/deprecated.test
@@ -0,0 +1,21 @@
+--TEST--
+Functions can be deprecated_function
+--DEPRECATION--
+Since foo/bar 1.1: Twig Function "deprecated_function" is deprecated. Use "not_deprecated_function" instead in index.twig at line 2.
+Since foo/bar 1.1: Twig Function "deprecated_function" is deprecated. Use "not_deprecated_function" instead in index.twig at line 4.
+--TEMPLATE--
+{{ deprecated_function() }}
+
+{{ deprecated_function() }}
+--DATA--
+return []
+--EXPECT--
+foo
+
+foo
+--DATA--
+return []
+--EXPECT--
+foo
+
+foo
diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php
index 76dc98de..f68ac15c 100644
--- a/tests/IntegrationTest.php
+++ b/tests/IntegrationTest.php
@@ -185,6 +185,7 @@ class TwigTestExtension extends AbstractExtension
             new TwigFunction('*_path', [$this, 'dynamic_path']),
             new TwigFunction('*_foo_*_bar', [$this, 'dynamic_foo']),
             new TwigFunction('anon_foo', function ($name) { return '*'.$name.'*'; }),
+            new TwigFunction('deprecated_function', function () { return 'foo'; }, ['deprecated' => '1.1', 'deprecating_package' => 'foo/bar', 'alternative' => 'not_deprecated_function']),
         ];
     }