Skip to content

Commit 14887b3

Browse files
committed
SDK-752: Further test coverage for AttributeConverter
1 parent 05ce9f1 commit 14887b3

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

tests/TestCase.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,20 @@
66

77
class TestCase extends PHPUnit_Framework_TestCase
88
{
9-
9+
/**
10+
* Call protected/private method of a class.
11+
*
12+
* @param object $class Class that we will run method on.
13+
* @param string $methodName Method name to call
14+
* @param array $parameters Array of parameters to pass into method.
15+
*
16+
* @return mixed Method return.
17+
*/
18+
public function invokeStaticMethod($class, $methodName, array $parameters = array())
19+
{
20+
$reflection = new \ReflectionClass($class);
21+
$method = $reflection->getMethod($methodName);
22+
$method->setAccessible(true);
23+
return $method->invokeArgs(new $class, $parameters);
24+
}
1025
}

tests/Util/Profile/AttributeConverterTest.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,47 @@
77
use Yoti\Entity\Receipt;
88
use Yoti\ActivityDetails;
99
use Yoti\Util\Profile\AttributeConverter;
10+
use Yoti\Exception\AttributeException;
1011

12+
/**
13+
* @coversDefaultClass \Yoti\Util\Profile\AttributeConverter
14+
*/
1115
class AttributeConverterTest extends TestCase
1216
{
17+
/**
18+
* Mocks \Attrpubapi\Attribute with provided name and value.
19+
*/
20+
private function getMockForProtobufAttribute($name, $value) {
21+
// Setup protobuf mock.
22+
$protobufAttribute = $this->getMockBuilder(\Attrpubapi\Attribute::class)
23+
->disableOriginalConstructor()
24+
->getMock();
25+
$protobufAttribute
26+
->method('getAnchors')
27+
->willReturn($this->getMockBuilder(\Traversable::class)->getMock());
28+
$protobufAttribute
29+
->method('getName')
30+
->willReturn($name);
31+
$protobufAttribute
32+
->method('getValue')
33+
->willReturn($value);
34+
35+
return $protobufAttribute;
36+
}
37+
38+
/**
39+
* @covers ::convertTimestampToDate
40+
*/
1341
public function testDateTypeShouldReturnDateTime()
1442
{
1543
$dateTime = AttributeConverter::convertTimestampToDate('1980/12/01');
1644
$this->assertInstanceOf(\DateTime::class, $dateTime);
1745
$this->assertEquals('01-12-1980', $dateTime->format('d-m-Y'));
1846
}
1947

48+
/**
49+
* @covers ::convertValueBasedOnContentType
50+
*/
2051
public function testSelfieValueShouldReturnImageObject()
2152
{
2253
$pem = file_get_contents(PEM_FILE);
@@ -27,4 +58,63 @@ public function testSelfieValueShouldReturnImageObject()
2758
$this->profile = $this->activityDetails->getProfile();
2859
$this->assertInstanceOf(Image::class, $this->profile->getSelfie()->getValue());
2960
}
30-
}
61+
62+
/**
63+
* @covers ::convertToYotiAttribute
64+
*/
65+
public function testConvertToYotiAttribute() {
66+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute('test_attr', 'my_value'));
67+
$this->assertEquals('test_attr', $attr->getName());
68+
$this->assertEquals('my_value', $attr->getValue());
69+
}
70+
71+
/**
72+
* @covers ::convertToYotiAttribute
73+
*/
74+
public function testConvertToYotiAttributeNullValue() {
75+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute('test_attr', ''));
76+
$this->assertNull($attr);
77+
}
78+
79+
/**
80+
* @covers ::convertValueBasedOnContentType
81+
* @covers ::validateInput
82+
*/
83+
public function testConvertValueBasedOnContentTypeValidation() {
84+
$this->expectException(AttributeException::class);
85+
$this->expectExceptionMessage('Warning: test_attr value is NULL');
86+
$this->invokeStaticMethod(
87+
AttributeConverter::class,
88+
'convertValueBasedOnContentType',
89+
[$this->getMockForProtobufAttribute('test_attr', '')]
90+
);
91+
}
92+
93+
/**
94+
* @covers ::convertValueBasedOnAttributeName
95+
* @covers ::validateInput
96+
*/
97+
public function testConvertValueBasedOnAttributeNameValidation() {
98+
$this->expectException(AttributeException::class);
99+
$this->expectExceptionMessage('Warning: test_attr value is NULL');
100+
$this->invokeStaticMethod(
101+
AttributeConverter::class,
102+
'convertValueBasedOnAttributeName',
103+
['', 'test_attr']
104+
);
105+
}
106+
107+
/**
108+
* @covers ::validateInput
109+
*/
110+
public function testValidateInput() {
111+
$this->expectException(AttributeException::class);
112+
$this->expectExceptionMessage('Warning: test_attr value is NULL');
113+
$this->invokeStaticMethod(
114+
AttributeConverter::class,
115+
'validateInput',
116+
['', 'test_attr']
117+
);
118+
}
119+
120+
}

0 commit comments

Comments
 (0)