forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractComponentTest.php
More file actions
352 lines (335 loc) · 13.5 KB
/
AbstractComponentTest.php
File metadata and controls
352 lines (335 loc) · 13.5 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
<?php
declare(strict_types=1);
namespace TYPO3Fluid\Fluid\Tests\Unit\Component;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Component\AbstractComponent;
use TYPO3Fluid\Fluid\Component\Argument\ArgumentCollection;
use TYPO3Fluid\Fluid\Component\ComponentInterface;
use TYPO3Fluid\Fluid\Component\EmbeddedComponentInterface;
use TYPO3Fluid\Fluid\Component\Error\ChildNotFoundException;
use TYPO3Fluid\Fluid\Core\Parser\Exception;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ObjectAccessorNode;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\RootNode;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Tests\Unit\Component\Fixtures\AlternativeComponentFixture;
use TYPO3Fluid\Fluid\Tests\Unit\Component\Fixtures\ComponentFixture;
use TYPO3Fluid\Fluid\Tests\Unit\Component\Fixtures\TransparentComponentFixture;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
use TYPO3Fluid\Fluid\Tests\Unit\ViewHelpers\Fixtures\UserWithoutToString;
use TYPO3Fluid\Fluid\Tests\UnitTestCase;
/**
* Test for base methods on AbstractComponent
*/
class AbstractComponentTest extends UnitTestCase
{
/**
* @test
*/
public function setArgumentsSetsArguments(): void
{
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
$arguments = new ArgumentCollection();
$subject->setArguments($arguments);
$this->assertSame($arguments, $subject->getArguments());
}
/**
* @test
*/
public function castingNonStringCompatibleChildComponentsThrowsError(): void
{
$context = new RenderingContextFixture();
$context->setVariableProvider(new StandardVariableProvider(['foo' => new UserWithoutToString('user')]));
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
// Note: two identical child components is intentional; more than one must exist for casting to occur.
$subject->addChild(new ObjectAccessorNode('foo'));
$subject->addChild(new ObjectAccessorNode('foo'));
$this->setExpectedException(Exception::class);
$subject->evaluate($context);
}
/**
* @test
* @dataProvider getNamedChildErrorTestValues
* @param iterable $children
* @param string $name
* @throws \ReflectionException
*/
public function getNamedThrowsErrorWhenNotFound(iterable $children, string $name): void
{
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
foreach ($children as $child) {
$subject->addChild($child);
}
$this->expectException(ChildNotFoundException::class);
$subject->getNamedChild($name);
}
public function getNamedChildErrorTestValues(): array
{
$namedChild = new ComponentFixture('named');
$unnamedChild = new ComponentFixture();
$transparentParent = new TransparentComponentFixture();
$transparentParent->addChild($namedChild);
return [
'not-matching named child as not found' => [
[$unnamedChild],
'named',
],
'not-matching named child in transparent parent' => [
[$unnamedChild, $transparentParent],
'notMatchedName',
],
];
}
/**
* @test
* @dataProvider getNamedChildTestValues
* @param iterable $children
* @param string $name
* @param ComponentInterface $expected
* @throws \ReflectionException
*/
public function getNamedChildReturnsExpectedChild(iterable $children, string $name, ComponentInterface $expected): void
{
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
foreach ($children as $child) {
$subject->addChild($child);
}
$this->assertSame($expected, $subject->getNamedChild($name));
}
public function getNamedChildTestValues(): array
{
$namedChild = new ComponentFixture('named');
$namedRoot = new ComponentFixture('root');
$unnamedChild = new ComponentFixture();
$transparentParent = new TransparentComponentFixture();
$emptyTransparentParent = new TransparentComponentFixture();
$transparentParent->addChild($namedChild);
$namedRoot->addChild($namedChild);
return [
'named child as immediate root' => [
[$unnamedChild, $namedChild],
'named',
$namedChild,
],
'named child in transparent parent' => [
[$transparentParent],
'named',
$namedChild,
],
'named child in named child' => [
[$namedRoot],
'root.named',
$namedChild,
],
'swallows ChildNotFound in transparent parent' => [
[$emptyTransparentParent, $namedChild],
'named',
$namedChild,
],
];
}
/**
* @test
* @dataProvider getTypedChildrenTestValues
* @param iterable $children
* @param string $typeClassName
* @param string|null $name
* @param ComponentInterface $expected
* @throws \ReflectionException
*/
public function getTypedChildrenReturnsExpectedChildren(iterable $children, string $typeClassName, ?string $name, ComponentInterface $expected): void
{
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
foreach ($children as $child) {
$subject->addChild($child);
}
$this->assertEquals($expected, $subject->getTypedChildren($typeClassName, $name));
}
public function getTypedChildrenTestValues(): array
{
$unnamedTransparentWithoutChildren = new ComponentFixture();
$unnamedTransparentWithChildren = new ComponentFixture();
$namedNestedNonTransparentWithoutChildren = new AlternativeComponentFixture('nested');
$namedNonTransparentWithoutChildren = new ComponentFixture('name1');
$namedTransparentWithChildren = new TransparentComponentFixture('name2');
$namedNonTransparentWithoutChildrenAlternative = new AlternativeComponentFixture('name3');
$namedTransparentWithChildren->addChild($namedNestedNonTransparentWithoutChildren)->addChild(new ComponentFixture());
$unnamedTransparentWithChildren->addChild($namedNestedNonTransparentWithoutChildren)->addChild(new ComponentFixture());
$children = [
$unnamedTransparentWithoutChildren,
$unnamedTransparentWithChildren,
$namedNonTransparentWithoutChildrenAlternative,
$namedNonTransparentWithoutChildren,
$namedTransparentWithChildren,
];
return [
'no children gets universal type and null name then returns empty collection' => [
[],
ComponentInterface::class,
null,
new RootNode(),
],
'single child gets universal type and not-null name' => [
[$namedNonTransparentWithoutChildren],
ComponentInterface::class,
'name1',
(new RootNode())->addChild($namedNonTransparentWithoutChildren),
],
'universal type and null name' => [
$children,
ComponentInterface::class,
null,
(new RootNode())
->addChild($unnamedTransparentWithoutChildren)
->addChild($unnamedTransparentWithChildren)
->addChild($namedNonTransparentWithoutChildrenAlternative)
->addChild($namedNonTransparentWithoutChildren)
->addChild($namedTransparentWithChildren)
->addChild($namedNestedNonTransparentWithoutChildren)
->addChild(new ComponentFixture())
,
],
'universal type and not-null name' => [
$children,
ComponentInterface::class,
'name1',
(new RootNode())->addChild($namedNonTransparentWithoutChildren),
],
'specific type and null name' => [
$children,
AlternativeComponentFixture::class,
null,
(new RootNode())
->addChild($namedNonTransparentWithoutChildrenAlternative)
->addChild($namedNestedNonTransparentWithoutChildren),
],
'specific type and not-null name' => [
$children,
AlternativeComponentFixture::class,
'name3',
(new RootNode())->addChild($namedNonTransparentWithoutChildrenAlternative),
],
'specific type and not-null name that does not match' => [
$children,
AlternativeComponentFixture::class,
'notMatchingName',
new RootNode(),
],
'universal type and not-null dotted name' => [
$children,
ComponentInterface::class,
'name2.nested',
(new RootNode())->addChild($namedNestedNonTransparentWithoutChildren),
],
'universal type and not-null dotted name that does not match' => [
$children,
ComponentInterface::class,
'not.matching.name',
new RootNode(),
],
'with transparent using specific type and not-null name gets nested child' => [
$children,
AlternativeComponentFixture::class,
'nested',
(new RootNode())->addChild($namedNestedNonTransparentWithoutChildren),
],
];
}
/**
* @test
* @dataProvider getFlattenTestValues
* @param ComponentInterface $subject
* @param bool $extract
* @param mixed $expected
*/
public function flattenReturnsExpectedValue(ComponentInterface $subject, bool $extract, $expected): void
{
$this->assertSame($expected, $subject->flatten($extract));
}
/**
* @return array
* @throws \ReflectionException
*/
public function getFlattenTestValues(): array
{
$textNode = new TextNode('foo');
$selfReturn = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
return [
'returns null for empty children with extract true' => [
$this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass(),
true,
null,
],
'returns self for empty children with extract false' => [
$selfReturn,
false,
$selfReturn,
],
'returns single child text value if single child TextNode and extract true' => [
$this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass()->setChildren([new TextNode('foo')]),
true,
'foo',
],
'returns single child if single child TextNode and extract false' => [
$this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass()->setChildren([$textNode]),
false,
$textNode
],
];
}
/**
* @test
* @dataProvider getEvaluateChildrenTestValues
* @param iterable $children
* @param mixed $expected
* @throws \ReflectionException
*/
public function evaluateChildrenReturnsExpectedValue(iterable $children, $expected): void
{
/** @var ComponentInterface $subject */
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
$subject->setChildren($children);
$this->assertSame($expected, $subject->evaluate(new RenderingContextFixture()));
}
/**
* @return array
* @throws \ReflectionException
*/
public function getEvaluateChildrenTestValues(): array
{
$arrayReturningComponent = $this->getMockBuilder(ComponentInterface::class)->getMockForAbstractClass();
$arrayReturningComponent->expects($this->once())->method('evaluate')->willReturn(['foo' => 'bar']);
$embeddedComponent = $this->getMockBuilder(EmbeddedComponentInterface::class)->getMockForAbstractClass();
return [
'returns null for empty children' => [
[],
null,
],
'multiple text nodes become concatenated string' => [
[new TextNode('foo'), new TextNode('bar'), new TextNode('baz')],
'foobarbaz',
],
'skips embedded components' => [
[new TextNode('foo'), $embeddedComponent, new TextNode('baz')],
'foobaz',
],
'executes single child without cast to string' => [
[$arrayReturningComponent],
['foo' => 'bar'],
],
];
}
/**
* @test
* @throws \ReflectionException
*/
public function allowsArbitraryArgumentsByDefault(): void
{
/** @var ComponentInterface $subject */
$subject = $this->getMockBuilder(AbstractComponent::class)->getMockForAbstractClass();
$this->assertSame(true, $subject->allowUndeclaredArgument('random-argument'));
}
}