/
CompileForeachTest.php
389 lines (362 loc) · 16.5 KB
/
CompileForeachTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* Smarty PHPunit tests compilation of {foreach} tag
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class for {foreach} tag tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class CompileForeachTest extends PHPUnit_Smarty
{
public function setUp()
{
$this->setUpSmarty(dirname(__FILE__));
$this->smarty->addPluginsDir("../../../__shared/PHPunitplugins/");
$this->smarty->addTemplateDir("./templates_tmp");
}
public function testInit()
{
$this->cleanDirs();
}
/**
* Test foreach tags
*
*
* @preserveGlobalState disabled
* @dataProvider dataTestForeach
*/
public function testForeach($code, $foo, $result, $testName, $testNumber)
{
$file = "testForeach_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->assign('x', 'x');
$this->smarty->assign('y', 'y');
if ($foo !== null) {
$this->smarty->assign('foo', $foo);
} else {
// unassigned $from parameter
$this->smarty->setErrorReporting(error_reporting() & ~(E_NOTICE | E_USER_NOTICE));
}
$this->assertEquals($result, $this->smarty->fetch($file), "testForeach - {$code} - {$testName}");
}
/*
* Data provider für testForeach
*/
public function dataTestForeach()
{
$i = 0;
/*
* Code
* $foo value
* result
* test name
*/
return array(
array('{foreach item=x from=$foo}{$x}{/foreach}', array(1,2,3), '123', '', $i ++),
array('{foreach $foo as $x}{$x}{/foreach}', array(1,2,3), '123', '', $i ++),
array('{foreach item=x from=$foo}{if $x == 2}{break}{/if}{$x}{/foreach}', array(0,1,2,3,4), '01', '', $i ++),
array('{foreach item=x from=$foo}{if $x == 2}{continue}{/if}{$x}{/foreach}', array(0,1,2,3,4), '0134', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}', array(1,2,3), '123', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}', array(), 'else', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}', null, 'else', '', $i ++),
array('{foreach item=x key=y from=$foo}{$y}=>{$x},{foreachelse}else{/foreach}', array(1,2,3), '0=>1,1=>2,2=>3,', '', $i ++),
array('{foreach $foo as $y => $x}{$y}=>{$x},{foreachelse}else{/foreach}', array(1,2,3), '0=>1,1=>2,2=>3,', '', $i ++),
array('{foreach $foo as $y => $x}{$y}=>{$x},{/foreach}-{$x}-{$y}', array(1,2,3), '0=>1,1=>2,2=>3,-x-y', 'saved loop variables', $i ++),
array('{foreach $foo as $y => $x}{$y}=>{$x},{foreachelse}else{/foreach}-{$x}-{$y}', array(1,2,3), '0=>1,1=>2,2=>3,-x-y', 'saved loop variables', $i ++),
array('{foreach $foo as $y => $x}{$y}=>{$x},{foreachelse}else{/foreach}-{$x}-{$y}', array(), 'else-x-y', 'saved loop variables', $i ++),
array('{foreach $foo as $x}{$x@key}=>{$x},{foreachelse}else{/foreach}', array(1,2,3), '0=>1,1=>2,2=>3,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{$x}{foreachelse}else{/foreach}total{$smarty.foreach.foo.total}', array(1,2,3), '123total3', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}total{$x@total}', array(1,2,3), '123total3', '', $i ++),
array('{foreach item=x name=foo from=$foo}{$smarty.foreach.foo.index}.{$x},{/foreach}', array(9,10,11), '0.9,1.10,2.11,', '', $i ++),
array('{foreach item=x from=$foo}{$x@index}.{$x},{/foreach}', array(9,10,11), '0.9,1.10,2.11,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{$smarty.foreach.foo.iteration}.{$x},{/foreach}', array(9,10,11), '1.9,2.10,3.11,', '', $i ++),
array('{foreach item=x from=$foo}{$x@iteration}.{$x},{/foreach}', array(9,10,11), '1.9,2.10,3.11,', '', $i ++),
array('{foreach item=x from=$foo}{$x@iteration}.{$x}-{$x=\'foo\'}{$x},{/foreach}', array(9,10,11), '1.9-foo,2.10-foo,3.11-foo,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{if $smarty.foreach.foo.first}first{/if}{$x},{/foreach}', array(9,10,11), 'first9,10,11,', '', $i ++),
array('{foreach item=x from=$foo}{if $x@first}first{/if}{$x},{/foreach}', array(9,10,11), 'first9,10,11,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{if $smarty.foreach.foo.last}last{/if}{$x},{/foreach}', array(9,10,11), '9,10,last11,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{if $smarty.foreach.foo.last}last{/if}{$smarty.foreach.foo.iteration}.{$x},{/foreach}', array(9,10,11), '1.9,2.10,last3.11,', '', $i ++),
array('{foreach item=x from=$foo}{if $x@last}last{/if}{$x},{/foreach}', array(9,10,11), '9,10,last11,', '', $i ++),
array('{foreach item=x name=foo from=$foo}{$x}{foreachelse}else{/foreach}{if $smarty.foreach.foo.show}-show{else}-noshow{/if}', array(9,10,11), '91011-show', '', $i ++),
array('{foreach item=x name=foo from=$foo}{$x}{foreachelse}else{/foreach}{if $smarty.foreach.foo.show}-show{else}-noshow{/if}', array(), 'else-noshow', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}{if $x@show}-show{else}-noshow{/if}', array(9,10,11), '91011-show', '', $i ++),
array('{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}{if $x@show}-show{else}-noshow{/if}', array(), 'else-noshow', '', $i ++),
array('{foreach $foo x y foo}{$y}.{$x},{foreachelse}else{/foreach}total{$smarty.foreach.foo.total}', array(9,10,11), '0.9,1.10,2.11,total3', '', $i ++),
array('{$x = "hallo"}{$bar=[1,2,3]}{foreach $foo as $x}outer={$x@index}.{$x}#{foreach $bar as $x}inner={$x@index}.{$x}{/foreach}##{/foreach}###{$x}', array(9,10,11), 'outer=0.9#inner=0.1inner=1.2inner=2.3##outer=1.10#inner=0.1inner=1.2inner=2.3##outer=2.11#inner=0.1inner=1.2inner=2.3#####hallo', '', $i ++),
array('{foreach $foo as $x}{$x}{foreachelse}else{/foreach}', null, 'else', '', $i ++),
array('{foreach $foo as $x}{$x}{foreachelse}else{/foreach}', [], 'else', '', $i ++),
array('{foreach $foo as $x}{$x}{foreachelse}else{/foreach}', new \ArrayIterator(), 'else', '', $i ++),
);
}
/**
* Test foreach tags caching
*
*
* @preserveGlobalState disabled
* @dataProvider dataTestForeachNocache
*/
public function testForeachCaching($code, $new, $assignNocache, $foo, $result, $testName, $testNumber)
{
$this->smarty->caching = true;
$file = "testForeachNocache_{$testNumber}.tpl";
if ($new) {
$this->makeTemplateFile($file, $code);
}
if ($foo !== null) {
$this->smarty->assign('foo', $foo, $assignNocache);
} else {
// unassigned $from parameter
$this->smarty->setErrorReporting(error_reporting() & ~(E_NOTICE | E_USER_NOTICE));
}
$this->assertEquals($result, $this->smarty->fetch($file), "testForeach - {$code} - {$testName}");
}
/*
* Data provider für testForeachNocache
*/
public function dataTestForeachNocache()
{
$i = 0;
/*
* Code
* new name new file
* assign nocache
* $foo value
* result
* test name
*/
return array(
array('{foreach item=x from=$foo}{$x}{/foreach}', true, true, array(1, 2, 3), '123', '', $i),
array('{foreach item=x from=$foo}{$x}{/foreach}', false, true, array(4, 5, 6), '456', '', $i ++),
array('{foreach item=x from=$foo}{$x}{/foreach}', true, false, array(1, 2, 3), '123', '', $i),
array('{foreach item=x from=$foo}{$x}{/foreach}', false, false, array(4, 5, 6), '123', '', $i ++),
array('{nocache}{foreach item=x from=$foo}{$x}{/foreach}{/nocache}', true, false, array(1, 2, 3), '123', '', $i),
array('{nocache}{foreach item=x from=$foo}{$x}{/foreach}{/nocache}', false, false, array(4, 5, 6), '456', '', $i ++),
);
}
/*
* test foreach and nocache
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testForeachNocacheVar1_024()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(1, 2), true);
$this->assertFalse($this->smarty->isCached('024_foreach.tpl'));
$this->assertEquals("1 2 ", $this->smarty->fetch('024_foreach.tpl'));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testForeachNocacheVar2_024()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(9, 8), true);
$this->assertTrue($this->smarty->isCached('024_foreach.tpl'));
$this->assertEquals("9 8 ", $this->smarty->fetch('024_foreach.tpl'));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testForeachNocacheTag1_025()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(1, 2));
$this->assertFalse($this->smarty->isCached('025_foreach.tpl'));
$this->assertEquals("1 2 ", $this->smarty->fetch('025_foreach.tpl'));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testForeachNocacheTag2_25()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(9, 8));
$this->assertTrue($this->smarty->isCached('025_foreach.tpl'));
$this->assertEquals("9 8 ", $this->smarty->fetch('025_foreach.tpl'));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testForeachCache1_26()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(1, 2));
$this->assertFalse($this->smarty->isCached('026_foreach.tpl'));
$this->assertEquals("1 2 ", $this->smarty->fetch('026_foreach.tpl'));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testForeachCache2_26()
{
$this->smarty->caching = true;
$this->smarty->assign('foo', array(9, 8));
$this->assertTrue($this->smarty->isCached('026_foreach.tpl'));
$this->assertEquals("1 2 ", $this->smarty->fetch('026_foreach.tpl'));
}
public function testForeachNested_27()
{
$this->smarty->assign('foo', array(9, 8));
$this->smarty->assign('bar', array(4, 10));
$this->assertEquals("outer=0#9inner=0#4inner=1#10##outer=1#8inner=0#4inner=1#10#####hallo",
$this->smarty->fetch('027_foreach.tpl'));
}
public function testForeachNestedNamed_28()
{
$this->smarty->assign('foo', array(9, 8));
$this->smarty->assign('bar', array(4, 10));
$this->assertEquals("outer=0#0-9inner=1#0-4inner=2#0-10##outer=1#1-8inner=1#1-4inner=2#1-10#####hallo",
$this->smarty->fetch('028_foreach.tpl'));
}
public function testForeachBreak_29()
{
$this->assertEquals("12",
$this->smarty->fetch('029_foreach.tpl'));
}
public function testForeachBreak_30()
{
$this->assertEquals("a1a2b1b2for20a1a2b1b2for21",
$this->smarty->fetch('030_foreach.tpl'));
}
public function testForeachBreak_31()
{
$this->assertEquals("a1a2for20a1a2for21",
$this->smarty->fetch('031_foreach.tpl'));
}
public function testForeachContinue_32()
{
$this->assertEquals("1245",
$this->smarty->fetch('032_foreach.tpl'));
}
public function testForeachContinue_33()
{
$this->assertEquals("a1a2a4a5b1b2b4b5for20a1a2a4a5b1b2b4b5for21",
$this->smarty->fetch('033_foreach.tpl'));
}
public function testForeachContinue_34()
{
$this->assertEquals("a1a2b1b2for20a1a2b1b2for21",
$this->smarty->fetch('034_foreach.tpl'));
}
public function testForeachContinue_35()
{
$this->assertEquals("a1a2a1a2",
$this->smarty->fetch('035_foreach.tpl'));
}
public function testForeachIsset_36()
{
$this->assertEquals("false",
$this->smarty->fetch('036_foreach.tpl'));
}
public function testForeachIsset_37()
{
$this->assertEquals("false",
$this->smarty->fetch('037_foreach.tpl'));
}
/**
* Test spacings
*
* @preserveGlobalState disabled
* @dataProvider dataTestSpacing
* @runInSeparateProcess
*/
public function testSpacing($code, $result, $testName, $testNumber)
{
$name = empty($testName) ? $testNumber : $testName;
$file = "Spacing_{$name}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->setTemplateDir('./templates_tmp');
$this->smarty->assign('foo', array(1,2));
$this->assertEquals($result,
$this->smarty->fetch($file),
"Spacing - {$file}");
}
/*
* Data provider für testSpacing
*/
public function dataTestSpacing()
{
$i = 1;
/*
* Code
* result
* test name
* test number
*/
return array(array("A{foreach from=\$foo item='bar'}{\$bar}{/foreach}C", "A12C", 'Newline1', $i++),
array("A{foreach from=\$foo item='bar'}\n{\$bar}{/foreach}C", "A12C", 'Newline2', $i++),
array("A{foreach from=\$foo item='bar'}{\$bar}\n{/foreach}C", "A1\n2\nC", 'Newline3', $i++),
array("A{foreach from=\$foo item='bar'}\n{\$bar}\n{/foreach}C", "A1\n2\nC", 'Newline4', $i++),
array("A\n{foreach from=\$foo item='bar'}{\$bar}{/foreach}C", "A\n12C", 'Newline5', $i++),
array("A{foreach from=\$foo item='bar'}{\$bar}{/foreach}\nC", "A12C", 'Newline6', $i++),
array("A{foreach from=\$foo item='bar'}{\$bar}{foreachelse}D{/foreach}C", "A12C", 'Newline7', $i++),
array("A{foreach from=\$foo item='bar'}{\$bar}\n{foreachelse}D{/foreach}C", "A1\n2\nC", 'Newline8', $i++),
array("{foreach from=\$foo item='bar' name='buh'}{\$bar}{/foreach}A{\$smarty.foreach.buh.total}C", "12A2C", 'Newline9', $i++),
array("{foreach from=\$foo item='bar' name='buh'}{\$bar}{/foreach}A\n{\$smarty.foreach.buh.total}C", "12A\n2C", 'Newline10', $i++),
array("{foreach from=\$foo item='bar' name='buh'}{\$bar}{/foreach}A{\$smarty.foreach.buh.total}\nC", "12A2\nC", 'Newline11', $i++),
);
}
/**
* Test spacings
*
* @preserveGlobalState disabled
* @dataProvider dataTestElseSpacing
* @runInSeparateProcess
*/
public function testElseSpacing($code, $result, $testName, $testNumber)
{
$name = empty($testName) ? $testNumber : $testName;
$file = "Spacing_Else_{$name}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->setTemplateDir('./templates_tmp');
$this->smarty->assign('foo', array());
$this->smarty->assign('buh', 'buh');
$this->assertEquals($result,
$this->smarty->fetch($file),
"Spacing - {$file}");
}
/*
* Data provider für testSpacing
*/
public function dataTestElseSpacing()
{
$i = 1;
/*
* Code
* result
* test name
* test number
*/
return array(
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}A{\$buh}B{/foreach}", "AbuhB", 'Newline1', $i++),
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}\nA{\$buh}B{/foreach}", "AbuhB", 'Newline2', $i++),
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}A{\$buh}\nB{/foreach}", "Abuh\nB", 'Newline3', $i++),
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}\nA{\$buh}\nB{/foreach}", "Abuh\nB", 'Newline4', $i++),
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}{\$buh}\nB{/foreach}", "buh\nB", 'Newline5', $i++),
array("{foreach from=\$foo item='bar'}{\$bar}{foreachelse}{\$buh}{/foreach}", "buh", 'Newline6', $i++),
);
}
}