8
8
use Yoti \ActivityDetails ;
9
9
use Yoti \Util \Profile \AttributeConverter ;
10
10
use Yoti \Entity \MultiValue ;
11
+ use Yoti \Entity \Attribute ;
11
12
12
13
/**
13
14
* @coversDefaultClass \Yoti\Util\Profile\AttributeConverter
@@ -19,7 +20,9 @@ class AttributeConverterTest extends TestCase
19
20
*/
20
21
const CONTENT_TYPE_STRING = 1 ;
21
22
const CONTENT_TYPE_JPEG = 2 ;
23
+ const CONTENT_TYPE_DATE = 3 ;
22
24
const CONTENT_TYPE_PNG = 4 ;
25
+ const CONTENT_TYPE_BYTES = 5 ;
23
26
const CONTENT_TYPE_MULTI_VALUE = 6 ;
24
27
25
28
/**
@@ -84,10 +87,30 @@ public function testConvertToYotiAttribute()
84
87
/**
85
88
* @covers ::convertToYotiAttribute
86
89
*/
87
- public function testConvertToYotiAttributeNullValue ()
90
+ public function testConvertToYotiAttributeEmptyStringValue ()
88
91
{
89
- $ attr = AttributeConverter::convertToYotiAttribute ($ this ->getMockForProtobufAttribute ('test_attr ' , '' ));
90
- $ this ->assertNull ($ attr );
92
+ $ attr = AttributeConverter::convertToYotiAttribute ($ this ->getMockForProtobufAttribute (
93
+ 'test_attr ' ,
94
+ '' ,
95
+ self ::CONTENT_TYPE_STRING
96
+ ));
97
+ $ this ->assertEquals ('test_attr ' , $ attr ->getName ());
98
+ $ this ->assertEquals ('' , $ attr ->getValue ());
99
+ }
100
+
101
+ /**
102
+ * @covers ::convertToYotiAttribute
103
+ */
104
+ public function testConvertToYotiAttributeEmptyNonStringValue ()
105
+ {
106
+ foreach ($ this ->getNonStringContentTypes () as $ contentType ) {
107
+ $ attr = AttributeConverter::convertToYotiAttribute ($ this ->getMockForProtobufAttribute (
108
+ 'test_attr ' ,
109
+ '' ,
110
+ $ contentType
111
+ ));
112
+ $ this ->assertNull ($ attr );
113
+ }
91
114
}
92
115
93
116
/**
@@ -215,11 +238,41 @@ public function testConvertToYotiAttributeMultiValue()
215
238
}
216
239
217
240
/**
218
- * Check that empty MultiValue Values result in no attribute being returned.
241
+ * Check that empty non-string MultiValue Values result in no attribute being returned.
242
+ *
243
+ * @covers ::convertToYotiAttribute
244
+ */
245
+ public function testEmptyNonStringAttributeMultiValueValue ()
246
+ {
247
+ foreach ($ this ->getNonStringContentTypes () as $ contentType ) {
248
+ // Get MultiValue values.
249
+ $ values = $ this ->createMultiValueValues ();
250
+
251
+ // Add an empty MultiValue.
252
+ $ values [] = $ this ->createMultiValueValue ('' , $ contentType );
253
+
254
+ // Create top-level MultiValue.
255
+ $ protoMultiValue = new \Attrpubapi \MultiValue ();
256
+ $ protoMultiValue ->setValues ($ values );
257
+
258
+ // Create mock Attribute that will return MultiValue as the value.
259
+ $ protobufAttribute = $ this ->getMockForProtobufAttribute (
260
+ 'test_attr ' ,
261
+ $ protoMultiValue ->serializeToString (),
262
+ self ::CONTENT_TYPE_MULTI_VALUE
263
+ );
264
+
265
+ $ attr = AttributeConverter::convertToYotiAttribute ($ protobufAttribute );
266
+ $ this ->assertNull ($ attr );
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Check that empty string MultiValue Values are allowed.
219
272
*
220
273
* @covers ::convertToYotiAttribute
221
274
*/
222
- public function testEmptyAttributeMultiValueValue ()
275
+ public function testEmptyStringAttributeMultiValueValue ()
223
276
{
224
277
// Get MultiValue values.
225
278
$ values = $ this ->createMultiValueValues ();
@@ -239,7 +292,8 @@ public function testEmptyAttributeMultiValueValue()
239
292
);
240
293
241
294
$ attr = AttributeConverter::convertToYotiAttribute ($ protobufAttribute );
242
- $ this ->assertNull ($ attr );
295
+ $ this ->assertInstanceOf (Attribute::class, $ attr );
296
+ $ this ->assertEquals ('' , $ attr ->getValue ()[3 ]);
243
297
}
244
298
245
299
/**
@@ -292,4 +346,20 @@ private function createMultiValueValue($data, $contentType)
292
346
$ protoMultiValueValue ->setContentType ($ contentType );
293
347
return $ protoMultiValueValue ;
294
348
}
349
+
350
+ /**
351
+ * List of non-string content types.
352
+ *
353
+ * @return array
354
+ */
355
+ private function getNonStringContentTypes ()
356
+ {
357
+ return [
358
+ self ::CONTENT_TYPE_JPEG ,
359
+ self ::CONTENT_TYPE_DATE ,
360
+ self ::CONTENT_TYPE_PNG ,
361
+ self ::CONTENT_TYPE_BYTES ,
362
+ self ::CONTENT_TYPE_MULTI_VALUE ,
363
+ ];
364
+ }
295
365
}
0 commit comments