1515namespace PhpMyAdmin \Tests \Twig \Extensions \Node ;
1616
1717use PhpMyAdmin \Twig \Extensions \Node \TransNode ;
18+ use Twig \Attribute \YieldReady ;
1819use Twig \Node \Expression \ConstantExpression ;
1920use Twig \Node \Expression \FilterExpression ;
2021use Twig \Node \Expression \NameExpression ;
2324use Twig \Node \TextNode ;
2425use Twig \Test \NodeTestCase ;
2526
27+ use function class_exists ;
2628use function sprintf ;
2729
2830class TransTest extends NodeTestCase
2931{
32+ private function echoOrYield (): string
33+ {
34+ return class_exists (YieldReady::class) ? 'yield ' : 'echo ' ;
35+ }
36+
3037 public function testConstructor (): void
3138 {
3239 $ count = new ConstantExpression (12 , 0 );
@@ -89,7 +96,8 @@ public function testEnableDebugNotEnabled(): void
8996 $ this ->assertEmpty ($ compiler ->getDebugInfo ());
9097 $ sourceCode = $ compiler ->compile ($ node )->getSource ();
9198 $ this ->assertSame (
92- '// custom: Notes for translators ' . "\n" . 'echo strtr(ngettext("There is 1 pending task", '
99+ '// custom: Notes for translators ' . "\n"
100+ . $ this ->echoOrYield () . ' strtr(ngettext("There is 1 pending task", '
93101 . ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), )); ' . "\n" ,
94102 $ sourceCode
95103 );
@@ -117,7 +125,8 @@ public function testEnableDebugEnabled(): void
117125 $ this ->assertEmpty ($ compiler ->getDebugInfo ());
118126 $ sourceCode = $ compiler ->compile ($ node )->getSource ();
119127 $ this ->assertSame (
120- '// line 80 ' . "\n" . '// custom: Notes for translators ' . "\n" . 'echo strtr(ngettext("There '
128+ '// line 80 ' . "\n" . '// custom: Notes for translators ' . "\n"
129+ . $ this ->echoOrYield () . ' strtr(ngettext("There '
121130 . ' is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), )); ' . "\n" ,
122131 $ sourceCode
123132 );
@@ -138,21 +147,24 @@ public function getTests(): array
138147 new TextNode ('coredomain ' , 0 ),
139148 ], [], 0 );
140149 $ node = new TransNode ($ body , null , null , null , null , $ domain , 0 );
141- $ tests [] = [$ node , sprintf ('echo dgettext("coredomain", %s); ' , $ this ->getVariableGetter ('foo ' ))];
150+ $ tests [] = [
151+ $ node ,
152+ sprintf ($ this ->echoOrYield () . ' dgettext("coredomain", %s); ' , $ this ->getVariableGetter ('foo ' )),
153+ ];
142154
143155 $ body = new NameExpression ('foo ' , 0 );
144156 $ node = new TransNode ($ body , null , null , null , null , null , 0 );
145- $ tests [] = [$ node , sprintf (' echo gettext(%s); ' , $ this ->getVariableGetter ('foo ' ))];
157+ $ tests [] = [$ node , sprintf ($ this -> echoOrYield () . ' gettext(%s); ' , $ this ->getVariableGetter ('foo ' ))];
146158
147159 $ body = new ConstantExpression ('Hello ' , 0 );
148160 $ node = new TransNode ($ body , null , null , null , null , null , 0 );
149- $ tests [] = [$ node , ' echo gettext("Hello"); ' ];
161+ $ tests [] = [$ node , $ this -> echoOrYield () . ' gettext("Hello"); ' ];
150162
151163 $ body = new Node ([
152164 new TextNode ('Hello ' , 0 ),
153165 ], [], 0 );
154166 $ node = new TransNode ($ body , null , null , null , null , null , 0 );
155- $ tests [] = [$ node , ' echo gettext("Hello"); ' ];
167+ $ tests [] = [$ node , $ this -> echoOrYield () . ' gettext("Hello"); ' ];
156168
157169 $ body = new Node ([
158170 new TextNode ('J \'ai ' , 0 ),
@@ -163,7 +175,7 @@ public function getTests(): array
163175 $ tests [] = [
164176 $ node ,
165177 sprintf (
166- ' echo strtr(gettext("J \'ai %%foo%% pommes"), array("%%foo%%" => %s, )); ' ,
178+ $ this -> echoOrYield () . ' strtr(gettext("J \'ai %%foo%% pommes"), array("%%foo%%" => %s, )); ' ,
167179 $ this ->getVariableGetter ('foo ' )
168180 ),
169181 ];
@@ -185,7 +197,7 @@ public function getTests(): array
185197 $ tests [] = [
186198 $ node ,
187199 sprintf (
188- ' echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have '
200+ $ this -> echoOrYield () . ' strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have '
189201 . ' %%count%% apples", abs(12)), array("%%name%%" => %s, '
190202 . ' "%%name%%" => %s, "%%count%%" => abs(12), )); ' ,
191203 $ this ->getVariableGetter ('name ' ),
@@ -207,7 +219,7 @@ public function getTests(): array
207219 $ tests [] = [
208220 $ node ,
209221 sprintf (
210- ' echo strtr(gettext("J \'ai %%foo%% pommes"), array("%%foo%%" => %s, )); ' ,
222+ $ this -> echoOrYield () . ' strtr(gettext("J \'ai %%foo%% pommes"), array("%%foo%%" => %s, )); ' ,
211223 $ this ->getVariableGetter ('foo ' )
212224 ),
213225 ];
@@ -216,12 +228,16 @@ public function getTests(): array
216228 $ body = new ConstantExpression ('Hello ' , 0 );
217229 $ notes = new TextNode ('Notes for translators ' , 0 );
218230 $ node = new TransNode ($ body , null , null , null , $ notes , null , 0 );
219- $ tests [] = [$ node , "// notes: Notes for translators \n echo gettext(\ "Hello \ "); " ];
231+ $ tests [] = [$ node , "// notes: Notes for translators \n" . $ this -> echoOrYield () . ' gettext("Hello");' ];
220232
221233 $ body = new ConstantExpression ('Hello ' , 0 );
222234 $ notes = new TextNode ("Notes for translators \nand line breaks " , 0 );
223235 $ node = new TransNode ($ body , null , null , null , $ notes , null , 0 );
224- $ tests [] = [$ node , "// notes: Notes for translators and line breaks \necho gettext( \"Hello \"); " ];
236+ $ tests [] = [
237+ $ node ,
238+ "// notes: Notes for translators and line breaks \n"
239+ . $ this ->echoOrYield () . ' gettext("Hello"); ' ,
240+ ];
225241
226242 $ count = new ConstantExpression (5 , 0 );
227243 $ body = new TextNode ('There is 1 pending task ' , 0 );
@@ -234,7 +250,8 @@ public function getTests(): array
234250 $ node = new TransNode ($ body , $ plural , $ count , null , $ notes , null , 0 );
235251 $ tests [] = [
236252 $ node ,
237- '// notes: Notes for translators ' . "\n" . 'echo strtr(ngettext("There is 1 pending task", '
253+ '// notes: Notes for translators ' . "\n"
254+ . $ this ->echoOrYield () . ' strtr(ngettext("There is 1 pending task", '
238255 . ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), )); ' ,
239256 ];
240257
0 commit comments