-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathForViewHelperTest.php
More file actions
358 lines (319 loc) · 14.4 KB
/
Copy pathForViewHelperTest.php
File metadata and controls
358 lines (319 loc) · 14.4 KB
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
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Tests\Functional\ViewHelpers;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3Fluid\Fluid\Core\ViewHelper\InvalidArgumentValueException;
use TYPO3Fluid\Fluid\Tests\Functional\AbstractFunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;
final class ForViewHelperTest extends AbstractFunctionalTestCase
{
#[Test]
public function renderThrowsExceptionIfSubjectIsNotTraversable(): void
{
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionCode(1256475113);
$view = new TemplateView();
$view->assignMultiple(['value' => new \stdClass()]);
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource('<f:for each="{value}" as="item">{item}</f:for>');
$view->render();
}
#[Test]
public function renderThrowsExceptionIfSubjectIsInvalid(): void
{
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionCode(1256475113);
$view = new TemplateView();
$view->assignMultiple(['value' => new \stdClass()]);
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource('<f:for each="{value}" as="item">{item}</f:for>');
$view->render();
}
public static function renderDataProvider(): \Generator
{
$value = new \ArrayObject();
yield 'empty for empty object' => [
'<f:for each="{value}" as="item">{item}</f:for>',
['value' => $value],
'',
];
$value = [];
yield 'empty for empty array' => [
'<f:for each="{value}" as="item">{item}</f:for>',
['value' => $value],
'',
];
$value = [0, 1, 2, 3];
yield 'items are displayed' => [
'<f:for each="{value}" as="item">{item} </f:for>',
['value' => $value],
'0 1 2 3 ',
];
$value = [0, 1, 2, 3];
yield 'reverse is respected for array' => [
'<f:for each="{value}" as="item" reverse="true">{item} </f:for>',
['value' => $value],
'3 2 1 0 ',
];
$value = ['key1' => 'value1', 'key2' => 'value2'];
yield 'reverse is respected for associative array' => [
'<f:for each="{value}" as="item" reverse="true">{item} </f:for>',
['value' => $value],
'value2 value1 ',
];
$value = new \ArrayIterator([0, 1, 2, 3]);
yield 'reverse is respected for object' => [
'<f:for each="{value}" as="item" reverse="true">{item} </f:for>',
['value' => $value],
'3 2 1 0 ',
];
$value = new \ArrayObject(['key1' => 'value1', 'key2' => 'value2']);
yield 'object gets traversed' => [
'<f:for each="{value}" as="item">{item} </f:for>',
['value' => $value],
'value1 value2 ',
];
yield 'iterator contains information' => [
'<ul>'
. '<f:for each="{0:1, 1:2, 2:3, 3:4}" as="item" iteration="iterator">'
. '<li>Index: {iterator.index} Cycle: {iterator.cycle} Total: {iterator.total}{f:if(condition: iterator.isEven, then: \' Even\')}{f:if(condition: iterator.isOdd, then: \' Odd\')}{f:if(condition: iterator.isFirst, then: \' First\')}{f:if(condition: iterator.isLast, then: \' Last\')}</li>'
. '</f:for>'
. '</ul>',
[],
'<ul>'
. '<li>Index: 0 Cycle: 1 Total: 4 Odd First</li>'
. '<li>Index: 1 Cycle: 2 Total: 4 Even</li>'
. '<li>Index: 2 Cycle: 3 Total: 4 Odd</li>'
. '<li>Index: 3 Cycle: 4 Total: 4 Even Last</li>'
. '</ul>',
];
$value = new class () implements \IteratorAggregate {
public function getIterator(): \Traversable
{
return new \ArrayIterator([
'first' => 'foo',
'second' => 'bar',
]);
}
};
yield 'iterator total works for non countable traversable' => [
'<f:for each="{value}" as="item" iteration="iterator">{item}: {iterator.total}, </f:for>',
['value' => $value],
'foo: 2, bar: 2, ',
];
yield 'generator gets traversed' => [
'<f:for each="{value}" as="item">{item} </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'first' => 'foo';
yield 'second' => 'bar';
})();
return ['value' => $value];
},
'foo bar ',
];
yield 'iterator total works for generator' => [
'<f:for each="{value}" as="item" iteration="iterator">{item}: {iterator.total}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'first' => 'foo';
yield 'second' => 'bar';
})();
return ['value' => $value];
},
'foo: 2, bar: 2, ',
];
yield 'iterator last works for generator' => [
'<f:for each="{value}" as="item" iteration="iterator">{item}{f:if(condition: iterator.isLast, then: \'!\', else: \', \')}</f:for>',
static function () {
$value = (static function (): \Generator {
yield 'Lucas';
yield 'Helene';
yield 'Thomas';
})();
return ['value' => $value];
},
'Lucas, Helene, Thomas!',
];
yield 'generator preserves duplicate keys without key argument' => [
'<f:for each="{value}" as="item">{item} </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'duplicate' => 'foo';
yield 'duplicate' => 'bar';
})();
return ['value' => $value];
},
'foo bar ',
];
yield 'iterator preserves duplicate keys from generator' => [
'<f:for each="{value}" as="item" key="key" iteration="iterator">{key}: {item} / {iterator.total}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'duplicate' => 'foo';
yield 'duplicate' => 'bar';
})();
return ['value' => $value];
},
'duplicate: foo / 2, duplicate: bar / 2, ',
];
yield 'empty generator with iteration returns empty string' => [
'<f:for each="{value}" as="item" iteration="iterator">{item}: {iterator.total}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield from [];
})();
return ['value' => $value];
},
'',
];
yield 'empty generator with reverse returns empty string' => [
'<f:for each="{value}" as="item" reverse="true">{item} </f:for>',
static function () {
$value = (static function (): \Generator {
yield from [];
})();
return ['value' => $value];
},
'',
];
yield 'reverse preserves duplicate keys from generator' => [
'<f:for each="{value}" as="item" key="key" reverse="true">{key}: {item}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'duplicate' => 'foo';
yield 'duplicate' => 'bar';
})();
return ['value' => $value];
},
'duplicate: bar, duplicate: foo, ',
];
yield 'reverse and iteration work for generator' => [
'<f:for each="{value}" as="item" reverse="true" iteration="iterator">{item}: {iterator.total} / {iterator.isFirst} / {iterator.isLast}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'first' => 'foo';
yield 'second' => 'bar';
})();
return ['value' => $value];
},
'bar: 2 / 1 / , foo: 2 / / 1, ',
];
yield 'reverse and iteration preserve duplicate keys from generator' => [
'<f:for each="{value}" as="item" key="key" reverse="true" iteration="iterator">{key}: {item} / {iterator.total}, </f:for>',
static function () {
$value = (static function (): \Generator {
yield 'duplicate' => 'foo';
yield 'duplicate' => 'bar';
})();
return ['value' => $value];
},
'duplicate: bar / 2, duplicate: foo / 2, ',
];
$value = new \ArrayObject(['foo', 'bar']);
yield 'iterator metadata works for countable traversable' => [
'<f:for each="{value}" as="item" iteration="iterator">{item}: {iterator.total} / {iterator.isFirst} / {iterator.isLast}, </f:for>',
['value' => $value],
'foo: 2 / 1 / , bar: 2 / / 1, ',
];
$value = ['item'];
yield 'iterator not available if not requested' => [
'<f:for each="{value}" as="item">Total: {iterator.total}</f:for>',
['value' => $value],
'Total: ',
];
$value = ['item' => 'bar', 'baz' => 2];
yield 'key attribute is respected' => [
'<f:for each="{value}" key="key" as="item">{key}: {item}, </f:for>',
['value' => $value],
'item: bar, baz: 2, ',
];
$value = ['bar', 2];
yield 'key contains numerical index' => [
'<f:for each="{value}" key="key" as="item">{key}: {item}, </f:for>',
['value' => $value],
'0: bar, 1: 2, ',
];
$value = new \ArrayIterator(['key1' => 'value1', 'key2' => 'value2']);
yield 'keys are preserved with objects implementing iterator interface' => [
'<f:for each="{value}" key="key" as="item">{key}: {item}, </f:for>',
['value' => $value],
'key1: value1, key2: value2, ',
];
$value = new \SplObjectStorage();
$object1 = new \stdClass();
$value->offsetSet($object1);
$object2 = new \stdClass();
$value->offsetSet($object2, 'foo');
$object3 = new \stdClass();
$value->offsetSet($object3, 'bar');
yield 'keys are a numerical index with objects of type SplObjectStorage' => [
'<f:for each="{value}" key="key" as="item">{key}</f:for>',
['value' => $value],
'012',
];
$value = ['foo' => 'fooValue', 'Fluid' => 'FluidStandalone', 'TYPO3' => 'rocks'];
yield 'iterator works' => [
'<f:for each="{value}" key="key" as="item" iteration="myIterator">'
. 'key: {key}, item: {item}, '
. 'index: {myIterator.index}, cycle: {myIterator.cycle}, total: {myIterator.total}, '
. 'isFirst: {myIterator.isFirst}, isLast: {myIterator.isLast}, '
. 'isEven: {myIterator.isEven}, isOdd: {myIterator.isOdd}' . chr(10)
. '</f:for>',
['value' => $value],
'key: foo, item: fooValue, index: 0, cycle: 1, total: 3, isFirst: 1, isLast: , isEven: , isOdd: 1' . chr(10)
. 'key: Fluid, item: FluidStandalone, index: 1, cycle: 2, total: 3, isFirst: , isLast: , isEven: 1, isOdd: ' . chr(10)
. 'key: TYPO3, item: rocks, index: 2, cycle: 3, total: 3, isFirst: , isLast: 1, isEven: , isOdd: 1' . chr(10),
];
$value = ['bar', 2];
yield 'variables are restored after loop' => [
'{key} {item} <f:for each="{value}" key="key" as="item">{key}: {item}, </f:for> {key} {item}',
['value' => $value, 'key' => '[key before]', 'item' => '[item before]'],
'[key before] [item before] 0: bar, 1: 2, [key before] [item before]',
];
$value = ['bar', 2];
yield 'variables are restored after loop if overwritten in loop' => [
'<f:for each="{value}" as="item"><f:variable name="item" value="overwritten" /></f:for>{item}',
['value' => $value],
'overwritten',
];
$value = ['bar', 2];
yield 'local variables can be converted to global variables inside loop' => [
'<f:for each="{value}" as="item">{item}|<f:variable name="item" value="overwritten" />{item}|</f:for>{item}',
['value' => $value],
'bar|overwritten|2|overwritten|overwritten',
];
$value = ['bar', 2];
yield 'variables set inside loop can be used after loop' => [
'<f:for each="{value}" key="key" as="item"><f:variable name="foo" value="bar" /></f:for>{foo}',
['value' => $value],
'bar',
];
$value = ['bar', 2];
yield 'existing variables can be modified in loop and retain the value set in the loop' => [
'<f:for each="{value}" key="key" as="item"><f:variable name="foo" value="bar" /></f:for>{foo}',
['value' => $value, 'foo' => 'fallback'],
'bar',
];
}
#[DataProvider('renderDataProvider')]
#[Test]
public function render(string $template, array|callable $variables, string $expected): void
{
$view = new TemplateView();
$view->assignMultiple(is_callable($variables) ? $variables() : $variables);
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);
self::assertSame($expected, $view->render());
$view = new TemplateView();
$view->assignMultiple(is_callable($variables) ? $variables() : $variables);
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);
self::assertSame($expected, $view->render());
}
}