-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathManifestDepthGroupTraitTest.php
More file actions
300 lines (262 loc) · 9.41 KB
/
Copy pathManifestDepthGroupTraitTest.php
File metadata and controls
300 lines (262 loc) · 9.41 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
<?php
/*
* This file is part of the Silverback API Components Bundle Project
*
* (c) Daniel West <daniel@silverback.is>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silverback\ApiComponentsBundle\Tests\Serializer\Normalizer;
use PHPUnit\Framework\TestCase;
use Silverback\ApiComponentsBundle\Serializer\Normalizer\Trait\ManifestDepthGroupTrait;
class ConcreteManifestDepthGroup
{
use ManifestDepthGroupTrait;
public function groups(array $resource): array
{
return $this->buildDepthGroups($resource);
}
}
class ManifestDepthGroupTraitTest extends TestCase
{
private ConcreteManifestDepthGroup $subject;
protected function setUp(): void
{
$this->subject = new ConcreteManifestDepthGroup();
}
/**
* Build an expected nested node { iri, children }.
*/
private function n(string $iri, array ...$children): array
{
return ['iri' => $iri, 'children' => $children];
}
public function test_flat_resource_returns_single_depth_tree(): void
{
$resource = [
'@id' => '/_/routes/home',
'page' => ['@id' => '/_/pages/abc'],
];
$this->assertSame(
[$this->n('/_/routes/home', $this->n('/_/pages/abc'))],
$this->subject->groups($resource)
);
}
public function test_resource_with_parent_page_returns_two_trees_root_first(): void
{
$resource = [
'@id' => '/_/abstract_page_data/child-uuid',
'page' => ['@id' => '/_/pages/child-page-uuid'],
'parentPage' => [
'@id' => '/_/pages/parent-uuid',
'route' => ['@id' => '/_/routes/conference'],
],
];
$this->assertSame(
[
$this->n('/_/pages/parent-uuid', $this->n('/_/routes/conference')),
$this->n('/_/abstract_page_data/child-uuid', $this->n('/_/pages/child-page-uuid')),
],
$this->subject->groups($resource)
);
}
public function test_resource_with_parent_page_data_returns_two_trees_root_first(): void
{
$resource = [
'@id' => '/_/abstract_page_data/child-uuid',
'parentPageData' => [
'@id' => '/_/abstract_page_data/parent-uuid',
'route' => ['@id' => '/_/routes/conference'],
],
];
$this->assertSame(
[
$this->n('/_/abstract_page_data/parent-uuid', $this->n('/_/routes/conference')),
$this->n('/_/abstract_page_data/child-uuid'),
],
$this->subject->groups($resource)
);
}
public function test_two_level_nesting_returns_three_trees(): void
{
$resource = [
'@id' => '/_/abstract_page_data/child-uuid',
'parentPageData' => [
'@id' => '/_/abstract_page_data/parent-uuid',
'parentPage' => [
'@id' => '/_/pages/grandparent-uuid',
],
],
];
$this->assertSame(
[
$this->n('/_/pages/grandparent-uuid'),
$this->n('/_/abstract_page_data/parent-uuid'),
$this->n('/_/abstract_page_data/child-uuid'),
],
$this->subject->groups($resource)
);
}
public function test_well_known_iris_are_filtered_out(): void
{
$resource = [
'@id' => '/_/routes/home',
'_metadata' => ['@id' => '/.well-known/genid/abc123'],
];
$this->assertSame([$this->n('/_/routes/home')], $this->subject->groups($resource));
}
public function test_resource_metadata_collection_iri_is_filtered_out(): void
{
$resource = [
'@id' => '/_/routes/home',
'something' => ['@id' => '/_/resource_metadatas'],
];
$this->assertSame([$this->n('/_/routes/home')], $this->subject->groups($resource));
}
public function test_duplicate_iris_within_depth_tree_are_deduplicated(): void
{
$resource = [
'@id' => '/_/routes/home',
'items' => [
['@id' => '/_/pages/abc'],
['@id' => '/_/pages/abc'],
],
];
$this->assertSame(
[$this->n('/_/routes/home', $this->n('/_/pages/abc'))],
$this->subject->groups($resource)
);
}
public function test_nested_arrays_of_sub_resources_are_walked(): void
{
$resource = [
'@id' => '/_/routes/home',
'componentGroups' => [
['@id' => '/_/component_groups/cg1'],
['@id' => '/_/component_groups/cg2'],
],
];
$this->assertSame(
[$this->n('/_/routes/home', $this->n('/_/component_groups/cg1'), $this->n('/_/component_groups/cg2'))],
$this->subject->groups($resource)
);
}
public function test_containment_is_preserved_as_nesting(): void
{
// route → pageData → page → componentGroup → position → component
$resource = [
'@id' => '/_/routes/home',
'pageData' => [
'@id' => '/page_data/pd1',
'page' => [
'@id' => '/_/pages/p1',
'componentGroups' => [
[
'@id' => '/_/component_groups/cg1',
'componentPositions' => [
[
'@id' => '/_/component_positions/cp1',
'component' => '/component/dummy/c1',
],
],
],
],
],
],
];
$this->assertSame(
[$this->n(
'/_/routes/home',
$this->n(
'/page_data/pd1',
$this->n(
'/_/pages/p1',
$this->n(
'/_/component_groups/cg1',
$this->n(
'/_/component_positions/cp1',
$this->n('/component/dummy/c1')
)
)
)
)
), ],
$this->subject->groups($resource)
);
}
public function test_parent_iri_does_not_appear_in_child_tree(): void
{
$resource = [
'@id' => '/_/abstract_page_data/child-uuid',
'parentPage' => ['@id' => '/_/pages/parent-uuid'],
];
$this->assertSame(
[
$this->n('/_/pages/parent-uuid'),
$this->n('/_/abstract_page_data/child-uuid'),
],
$this->subject->groups($resource)
);
}
public function test_string_iri_property_value_is_collected(): void
{
// layout is a string IRI (not an embedded object) — must appear as a child node
$resource = [
'@id' => '/_/routes/home',
'layout' => '/_/layouts/abc',
];
$this->assertSame(
[$this->n('/_/routes/home', $this->n('/_/layouts/abc'))],
$this->subject->groups($resource)
);
}
public function test_blank_node_string_iri_properties_are_not_collected(): void
{
// AP4 blank-node resources (/.well-known/genid/...) must not contribute
// their string-valued properties to the tree — only real API resources should appear
$resource = [
'@id' => '/_/routes/home',
'metadata' => [
'@id' => '/.well-known/genid/abc123',
'something' => '/_/page_data_metadatas/uuid',
],
];
$this->assertSame([$this->n('/_/routes/home')], $this->subject->groups($resource));
}
public function test_keys_after_parent_page_in_iteration_order_are_still_collected(): void
{
// parentPage must appear BEFORE 'page' in the array to verify continue (not break) is used.
// If break were used instead, 'page' would never be visited.
$resource = [
'@id' => '/_/abstract_page_data/child-uuid',
'parentPage' => ['@id' => '/_/pages/parent-uuid'],
'page' => ['@id' => '/_/pages/child-page-uuid'],
];
$this->assertSame(
[
$this->n('/_/pages/parent-uuid'),
$this->n('/_/abstract_page_data/child-uuid', $this->n('/_/pages/child-page-uuid')),
],
$this->subject->groups($resource)
);
}
public function test_at_prefixed_key_string_value_is_not_collected(): void
{
// String values under @-prefixed keys (e.g. @type) must not be collected
$resource = [
'@id' => '/_/routes/home',
'@type' => '/some-vocabulary-type',
];
$this->assertSame([$this->n('/_/routes/home')], $this->subject->groups($resource));
}
public function test_non_path_string_property_is_not_collected(): void
{
// String values that do not start with '/' are not IRIs and must not be collected
$resource = [
'@id' => '/_/routes/home',
'title' => 'My Page Title',
];
$this->assertSame([$this->n('/_/routes/home')], $this->subject->groups($resource));
}
}