Skip to content

Commit cc897d0

Browse files
committed
SDK-869: Support INT values
1 parent d320b4a commit cc897d0

File tree

2 files changed

+68
-41
lines changed

2 files changed

+68
-41
lines changed

src/Yoti/Util/Profile/AttributeConverter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class AttributeConverter
1919
const CONTENT_TYPE_PNG = 4;
2020
const CONTENT_TYPE_JSON = 5;
2121
const CONTENT_TYPE_MULTI_VALUE = 6;
22+
const CONTENT_TYPE_INT = 7;
2223

2324
/**
2425
* @param ProtobufAttribute $attribute
@@ -56,7 +57,7 @@ private static function convertValueBasedOnAttributeName($value, $attrName)
5657
*/
5758
private static function convertValueBasedOnContentType($value, $contentType)
5859
{
59-
if (empty($value) && ($contentType !== self::CONTENT_TYPE_STRING)) {
60+
if (strlen($value) === 0 && ($contentType !== self::CONTENT_TYPE_STRING)) {
6061
throw new AttributeException("Warning: Value is NULL");
6162
}
6263

@@ -83,6 +84,9 @@ private static function convertValueBasedOnContentType($value, $contentType)
8384
case self::CONTENT_TYPE_UNDEFINED:
8485
throw new AttributeException("Content Type is undefined");
8586

87+
case self::CONTENT_TYPE_INT:
88+
return (int) $value;
89+
8690
case self::CONTENT_TYPE_STRING:
8791
default:
8892
return $value;

tests/Util/Profile/AttributeConverterTest.php

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AttributeConverterTest extends TestCase
2424
const CONTENT_TYPE_PNG = 4;
2525
const CONTENT_TYPE_BYTES = 5;
2626
const CONTENT_TYPE_MULTI_VALUE = 6;
27+
const CONTENT_TYPE_INT = 7;
2728

2829
/**
2930
* Mocks \Attrpubapi\Attribute with provided name and value.
@@ -100,17 +101,40 @@ public function testConvertToYotiAttributeEmptyStringValue()
100101

101102
/**
102103
* @covers ::convertToYotiAttribute
104+
*
105+
* @dataProvider nonStringContentTypesDataProvider
103106
*/
104-
public function testConvertToYotiAttributeEmptyNonStringValue()
107+
public function testConvertToYotiAttributeEmptyNonStringValue($contentType)
105108
{
106-
foreach ($this->getNonStringContentTypes() as $contentType) {
107-
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute(
108-
'test_attr',
109-
'',
110-
$contentType
111-
));
112-
$this->assertNull($attr);
113-
}
109+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute(
110+
'test_attr',
111+
'',
112+
$contentType
113+
));
114+
$this->assertNull($attr);
115+
}
116+
117+
/**
118+
* @covers ::convertToYotiAttribute
119+
*
120+
* @dataProvider validIntegerDataProvider
121+
*/
122+
public function testConvertToYotiAttributeIntegerValue($int)
123+
{
124+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute(
125+
'test_attr',
126+
$int,
127+
self::CONTENT_TYPE_INT
128+
));
129+
$this->assertSame($int, $attr->getValue());
130+
}
131+
132+
/**
133+
* Provides list of valid integers.
134+
*/
135+
public function validIntegerDataProvider()
136+
{
137+
return [[0], [1], [123], [-1], [-10]];
114138
}
115139

116140
/**
@@ -259,30 +283,30 @@ public function testConvertToYotiAttributeMultiValue()
259283
* Check that empty non-string MultiValue Values result in no attribute being returned.
260284
*
261285
* @covers ::convertToYotiAttribute
286+
*
287+
* @dataProvider nonStringContentTypesDataProvider
262288
*/
263-
public function testEmptyNonStringAttributeMultiValueValue()
289+
public function testEmptyNonStringAttributeMultiValueValue($contentType)
264290
{
265-
foreach ($this->getNonStringContentTypes() as $contentType) {
266-
// Get MultiValue values.
267-
$values = $this->createMultiValueValues();
268-
269-
// Add an empty MultiValue.
270-
$values[] = $this->createMultiValueValue('', $contentType);
271-
272-
// Create top-level MultiValue.
273-
$protoMultiValue = new \Attrpubapi\MultiValue();
274-
$protoMultiValue->setValues($values);
275-
276-
// Create mock Attribute that will return MultiValue as the value.
277-
$protobufAttribute = $this->getMockForProtobufAttribute(
278-
'test_attr',
279-
$protoMultiValue->serializeToString(),
280-
self::CONTENT_TYPE_MULTI_VALUE
281-
);
282-
283-
$attr = AttributeConverter::convertToYotiAttribute($protobufAttribute);
284-
$this->assertNull($attr);
285-
}
291+
// Get MultiValue values.
292+
$values = $this->createMultiValueValues();
293+
294+
// Add an empty MultiValue.
295+
$values[] = $this->createMultiValueValue('', $contentType);
296+
297+
// Create top-level MultiValue.
298+
$protoMultiValue = new \Attrpubapi\MultiValue();
299+
$protoMultiValue->setValues($values);
300+
301+
// Create mock Attribute that will return MultiValue as the value.
302+
$protobufAttribute = $this->getMockForProtobufAttribute(
303+
'test_attr',
304+
$protoMultiValue->serializeToString(),
305+
self::CONTENT_TYPE_MULTI_VALUE
306+
);
307+
308+
$attr = AttributeConverter::convertToYotiAttribute($protobufAttribute);
309+
$this->assertNull($attr);
286310
}
287311

288312
/**
@@ -366,18 +390,17 @@ private function createMultiValueValue($data, $contentType)
366390
}
367391

368392
/**
369-
* List of non-string content types.
370-
*
371-
* @return array
393+
* Provides non-string content types.
372394
*/
373-
private function getNonStringContentTypes()
395+
public function nonStringContentTypesDataProvider()
374396
{
375397
return [
376-
self::CONTENT_TYPE_JPEG,
377-
self::CONTENT_TYPE_DATE,
378-
self::CONTENT_TYPE_PNG,
379-
self::CONTENT_TYPE_BYTES,
380-
self::CONTENT_TYPE_MULTI_VALUE,
398+
[ self::CONTENT_TYPE_JPEG ],
399+
[ self::CONTENT_TYPE_DATE ],
400+
[ self::CONTENT_TYPE_PNG ],
401+
[ self::CONTENT_TYPE_BYTES ],
402+
[ self::CONTENT_TYPE_MULTI_VALUE ],
403+
[ self::CONTENT_TYPE_INT ],
381404
];
382405
}
383406
}

0 commit comments

Comments
 (0)