Skip to content

Commit 7b0b9a5

Browse files
committed
add tests and allow empty metadata in tests
1 parent f2cbf61 commit 7b0b9a5

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

databox/api/src/Documentation/InitialValuesDocumentationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getContent(): ?string
7070
$output .= "### ... with file metadata ...\n";
7171
$output .= "| metadata | value(s) |\n";
7272
$output .= "|---|---|\n";
73-
foreach ($test['metadata'] as $metadataName => $values) {
73+
foreach ($test['metadata'] ?? [] as $metadataName => $values) {
7474
$v = is_array($values) ? $values : [$values];
7575
$output .= sprintf("| %s | `%s` |\n", $metadataName, join('` ; `', $v));
7676
}

databox/api/tests/Asset/Attribute/InitialAttributeValuesResolverTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ($test) {
5050
*
5151
* @param array<string, string|string[]> $metadata
5252
*/
53-
public function testResolveInitialAttributes(array $definitions, array $metadata, array $expected): void
53+
public function testResolveInitialAttributes(array $definitions, ?array $metadata, array $expected): void
5454
{
5555
$attributeDefinitions = [];
5656
foreach ($definitions as $name => $definition) {
@@ -145,6 +145,9 @@ private function isNumericArray($a): bool
145145

146146
private function conformMetadata($data): array
147147
{
148+
if (null === $data) {
149+
return [];
150+
}
148151
$conformed = [];
149152
$data = is_array($data) ? $data : [$data];
150153
foreach ($data as $key => $value) {

databox/api/tests/fixtures/metadata/InitialAttributeValuesResolverData.yaml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,114 @@ multi_multilingue:
116116
en: ['dog', 'cat', 'bird']
117117
fr: ['chien', 'chat', 'oiseau']
118118

119+
unknown_tag:
120+
about:
121+
title: 'Unknown tag'
122+
definitions:
123+
zAttribute:
124+
initialValues: '{
125+
"type": "template",
126+
"value": "{{ (file.getMetadata(''badTag'').value ?? ''whaat ?'') }}"
127+
}'
128+
metadata: ~
129+
expected:
130+
zAttribute: 'whaat ?'
131+
132+
first_metadata:
133+
about:
134+
title: 'First metadata set, with alternate value (1)'
135+
description: 'for this test, only the metadata `IPTC:City` is set.'
136+
definitions:
137+
City:
138+
initialValues: '{
139+
"type": "template",
140+
"value": "{{ file.getMetadata(''XMP-iptcCore:CreatorCity'').value ?? file.getMetadata(''IPTC:City'').value ?? ''no-city ?'' }}"
141+
}'
142+
metadata:
143+
'IPTC:City': 'Paris'
144+
expected:
145+
City: 'Paris'
146+
147+
first_metadata_default:
148+
about:
149+
title: 'First metadata set, with alternate value (2)'
150+
description: 'for this test, no metadata is set, so the default value is used.'
151+
definitions:
152+
City:
153+
initialValues: '{
154+
"type": "template",
155+
"value": "{{ file.getMetadata(''XMP-iptcCore:CreatorCity'').value ?? file.getMetadata(''IPTC:City'').value ?? ''no-city ?'' }}"
156+
}'
157+
metadata: ~
158+
expected:
159+
City: 'no-city ?'
160+
161+
many_metadata_to_mono:
162+
about:
163+
title: 'Many metadata to a mono-value attribute'
164+
description: 'Here an array `[a, b]` is used, the `join` filter inserts a " - " only if required.'
165+
definitions:
166+
CreatorLocation:
167+
initialValues: '{
168+
"type": "template",
169+
"value": "{{ [file.getMetadata(''XMP-iptcCore:CreatorCity'').value, file.getMetadata(''XMP-iptcCore:CreatorCountry'').value] | join('' - '') }}"
170+
}'
171+
metadata:
172+
'XMP-iptcCore:CreatorCity': 'Paris'
173+
'XMP-iptcCore:CreatorCountry': 'France'
174+
expected:
175+
CreatorLocation: 'Paris - France'
176+
177+
many_metadata_to_multi:
178+
about:
179+
title: 'Many metadata to a multi-values attribute'
180+
description: "_nb_: We fill an array that will be joined by `\\n` to generate __one line per item__ (required).\n
181+
\n
182+
The `merge` filter __requires__ arrays, so `IPTC:Keywords` defaults to `[]` in case there is no keywords in metadata.\n"
183+
definitions:
184+
Keywords:
185+
isMultiple: true
186+
initialValues: '{
187+
"type": "template",
188+
"value": "{{ ( (file.getMetadata(''IPTC:Keywords'').values ?? []) | merge([file.getMetadata(''IPTC:City'').value, file.getMetadata(''XMP-photoshop:Country'').value ]) ) | join(''\n'') }}"
189+
}'
190+
metadata:
191+
'IPTC:Keywords': ['Eiffel Tower', 'Seine river']
192+
'IPTC:City': 'Paris'
193+
'XMP-photoshop:Country': 'France'
194+
expected:
195+
Keywords: ['Eiffel Tower', 'Seine river', 'Paris', 'France']
196+
197+
code_to_string:
198+
about:
199+
title: 'Transform code to human readable text'
200+
description: 'here the `ExposureProgram` metadata is a code (0...9), used to index an array of strings.'
201+
definitions:
202+
ExposureProgram:
203+
initialValues: '{
204+
"type": "template",
205+
"value": "{{ [
206+
''Not Defined'', ''Manual'', ''Program AE'',
207+
''Aperture-priority AE'', ''Shutter speed priority AE'',
208+
''Creative (Slow speed)'', ''Action (High speed)'', ''Portrait'',
209+
''Landscape'', ''Bulb''
210+
][file.getMetadata(''ExifIFD:ExposureProgram'').value]
211+
?? ''Unknown mode''
212+
}}"
213+
}'
214+
metadata:
215+
'ExifIFD:ExposureProgram': 2
216+
expected:
217+
ExposureProgram: 'Program AE'
218+
219+
code_to_string_2:
220+
definitions:
221+
ExposureProgram:
222+
initialValues: '{
223+
"type": "template",
224+
"value": "{{ [ ''a'', ''b'' ][file.getMetadata(''ExifIFD:ExposureProgram'').value] ?? ''z'' }}"
225+
}'
226+
metadata: ~
227+
expected:
228+
ExposureProgram: 'z'
229+

0 commit comments

Comments
 (0)