Skip to content

Commit 0fb2bcb

Browse files
Moussa Sidibesidibos
authored andcommitted
SDK-603: Add test case for SandboxClient
1 parent 30c2a1c commit 0fb2bcb

13 files changed

+269
-45
lines changed

sandbox/src/Entity/SandboxAgeVerification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace YotiSandbox\Entity;
44

5-
use Yoti\Entity\Attribute;
5+
use Yoti\Entity\Profile;
66

77
class SandboxAgeVerification extends SandboxAttribute
88
{
99
public function __construct(\DateTime $dateObj, $derivation, array $anchors = [])
1010
{
1111
parent::__construct(
12-
Attribute::DATE_OF_BIRTH,
12+
Profile::ATTR_DATE_OF_BIRTH,
1313
$dateObj->format('Y-m-d'),
1414
$derivation,
1515
'true',

sandbox/src/Entity/SandboxAnchor.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@
44

55
class SandboxAnchor
66
{
7+
/**
8+
* @var string
9+
*/
710
private $type;
11+
12+
/**
13+
* @var string
14+
*/
815
private $value;
16+
17+
/**
18+
* @var string
19+
*/
920
private $subtype;
21+
22+
/**
23+
* @var int
24+
*/
1025
private $timestamp;
1126

1227
public function __construct($type, $value, $subType, $timestamp)

sandbox/src/Http/RequestBuilder.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class RequestBuilder
2121
/**
2222
* @param string $value
2323
*/
24-
public function includeRememberMeId($value)
24+
public function setRememberMeId($value)
2525
{
2626
$this->rememberMeId = $value;
2727
}
2828

29-
public function includeFullName($value, $optional = 'false', array $anchors = [])
29+
public function setFullName($value, $optional = 'false', array $anchors = [])
3030
{
3131
$this->addAttribute($this->createAttribute(
3232
Profile::ATTR_FULL_NAME,
@@ -37,7 +37,7 @@ public function includeFullName($value, $optional = 'false', array $anchors = []
3737
));
3838
}
3939

40-
public function includeFamilyName($value, $optional = 'false', array $anchors = [])
40+
public function setFamilyName($value, $optional = 'false', array $anchors = [])
4141
{
4242
$this->addAttribute($this->createAttribute(
4343
Profile::ATTR_FAMILY_NAME,
@@ -48,7 +48,7 @@ public function includeFamilyName($value, $optional = 'false', array $anchors =
4848
));
4949
}
5050

51-
public function includeGivenNames($value, $optional = 'false', array $anchors = [])
51+
public function setGivenNames($value, $optional = 'false', array $anchors = [])
5252
{
5353
$this->addAttribute($this->createAttribute(
5454
Profile::ATTR_GIVEN_NAMES,
@@ -59,7 +59,7 @@ public function includeGivenNames($value, $optional = 'false', array $anchors =
5959
));
6060
}
6161

62-
public function includeDateOfBirth($value, $optional = 'false', array $anchors = [])
62+
public function setDateOfBirth($value, $optional = 'false', array $anchors = [])
6363
{
6464
$this->addAttribute($this->createAttribute(
6565
Profile::ATTR_DATE_OF_BIRTH,
@@ -70,7 +70,7 @@ public function includeDateOfBirth($value, $optional = 'false', array $anchors =
7070
));
7171
}
7272

73-
public function includeGender($value, $optional = 'false', array $anchors = [])
73+
public function setGender($value, $optional = 'false', array $anchors = [])
7474
{
7575
$this->addAttribute($this->createAttribute(
7676
Profile::ATTR_GENDER,
@@ -81,7 +81,7 @@ public function includeGender($value, $optional = 'false', array $anchors = [])
8181
));
8282
}
8383

84-
public function includeNationality($value, $optional = 'false', array $anchors = [])
84+
public function setNationality($value, $optional = 'false', array $anchors = [])
8585
{
8686
$this->addAttribute($this->createAttribute(
8787
Profile::ATTR_NATIONALITY,
@@ -92,7 +92,7 @@ public function includeNationality($value, $optional = 'false', array $anchors =
9292
));
9393
}
9494

95-
public function includePhoneNumber($value, $optional = 'false', array $anchors = [])
95+
public function setPhoneNumber($value, $optional = 'false', array $anchors = [])
9696
{
9797
$this->addAttribute($this->createAttribute(
9898
Profile::ATTR_PHONE_NUMBER,
@@ -103,13 +103,13 @@ public function includePhoneNumber($value, $optional = 'false', array $anchors =
103103
));
104104
}
105105

106-
public function includeSelfie($value, $optional = 'false', array $anchors = [])
106+
public function setSelfie($value, $optional = 'false', array $anchors = [])
107107
{
108108
$base64Selfie = base64_encode($value);
109-
$this->includeBase64Selfie($base64Selfie, $optional, $anchors);
109+
$this->setBase64Selfie($base64Selfie, $optional, $anchors);
110110
}
111111

112-
public function includeBase64Selfie($value, $optional = 'true', array $anchors = [])
112+
public function setBase64Selfie($value, $optional = 'true', array $anchors = [])
113113
{
114114
$this->addAttribute($this->createAttribute(
115115
Profile::ATTR_SELFIE,
@@ -120,7 +120,7 @@ public function includeBase64Selfie($value, $optional = 'true', array $anchors =
120120
));
121121
}
122122

123-
public function includeEmailAddress($value, $optional = 'false', array $anchors = [])
123+
public function setEmailAddress($value, $optional = 'false', array $anchors = [])
124124
{
125125
$this->addAttribute($this->createAttribute(
126126
Profile::ATTR_EMAIL_ADDRESS,
@@ -131,7 +131,7 @@ public function includeEmailAddress($value, $optional = 'false', array $anchors
131131
));
132132
}
133133

134-
public function includePostalAddress($value, $optional = 'false', array $anchors = [])
134+
public function setPostalAddress($value, $optional = 'false', array $anchors = [])
135135
{
136136
$this->addAttribute($this->createAttribute(
137137
Profile::ATTR_POSTAL_ADDRESS,
@@ -142,7 +142,7 @@ public function includePostalAddress($value, $optional = 'false', array $anchors
142142
));
143143
}
144144

145-
public function includeStructuredPostalAddress($value, $optional = 'false', array $anchors = [])
145+
public function setStructuredPostalAddress($value, $optional = 'false', array $anchors = [])
146146
{
147147
$this->addAttribute($this->createAttribute(
148148
Profile::ATTR_STRUCTURED_POSTAL_ADDRESS,
@@ -153,7 +153,7 @@ public function includeStructuredPostalAddress($value, $optional = 'false', arra
153153
));
154154
}
155155

156-
public function includeDocumentDetails($value, $optional = 'true', array $anchors = [])
156+
public function setDocumentDetails($value, $optional = 'true', array $anchors = [])
157157
{
158158
$this->addAttribute($this->createAttribute(
159159
Profile::ATTR_DOCUMENT_DETAILS,
@@ -164,7 +164,7 @@ public function includeDocumentDetails($value, $optional = 'true', array $anchor
164164
));
165165
}
166166

167-
public function includeAgeVerification(\DateTime $dateObj, $derivation, array $anchors = [])
167+
public function setAgeVerification(\DateTime $dateObj, $derivation, array $anchors = [])
168168
{
169169
$this->addAttribute(new SandboxAgeVerification(
170170
$dateObj,

sandbox/src/Http/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Response
2020
*/
2121
public function __construct(array $result)
2222
{
23-
$responseArr = $this->processResult($result);
23+
$responseArr = $this->processData($result);
2424
$this->token = $responseArr['token'];
2525
}
2626

@@ -39,12 +39,12 @@ public function getToken()
3939
*
4040
* @throws ResponseException
4141
*/
42-
private function processResult(array $result)
42+
private function processData(array $result)
4343
{
4444
$this->checkResponseStatus($result['http_code']);
4545

4646
// Get decoded response data
47-
$responseArr = json_decode($result['response'], TRUE);
47+
$responseArr = $result['response'];
4848

4949
$this->checkJsonError();
5050

sandbox/src/Http/TokenRequest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
class TokenRequest
88
{
9+
/**
10+
* @var string
11+
*/
912
private $rememberMeId;
13+
14+
/**
15+
* @var array
16+
*/
1017
private $sandboxAttributes;
1118

1219
public function __construct($rememberMeId, array $sandboxAttrs)

sandbox/src/SandboxClient.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
namespace YotiSandbox;
33

44
use Yoti\YotiClient;
5-
use Yoti\Http\CurlRequestHandler;
65
use YotiSandbox\Http\Response;
6+
use Yoti\Http\CurlRequestHandler;
77
use YotiSandbox\Http\RequestBuilder;
88

99
class SandboxClient
1010
{
11+
const TOKEN_REQUEST_ENDPOINT_FORMAT = "/app/%s/tokens";
1112
const DEFAULT_SANDBOX_API_URL = "https://api.yoti.com/sandbox/v1";
1213

1314
/**
@@ -20,24 +21,27 @@ class SandboxClient
2021
*/
2122
private $requestHandler;
2223

24+
/**
25+
* @var YotiClient
26+
*/
2327
private $yotiClient;
2428

2529
/**
2630
* SandboxClient constructor.
2731
*
2832
* @param string $sdkId
2933
* @param string $pem
30-
* @param string $connectApi
34+
* @param string $sandboxApi
3135
* @param string $sdkIdentifier
3236
*
3337
* @throws \Yoti\Exception\RequestException
3438
* @throws \Yoti\Exception\YotiClientException
3539
*/
36-
public function __construct($sdkId, $pem, $connectApi = self::DEFAULT_SANDBOX_API_URL, $sdkIdentifier = 'PHP')
40+
public function __construct($sdkId, $pem, $sandboxApi = self::DEFAULT_SANDBOX_API_URL, $sdkIdentifier = 'PHP')
3741
{
3842
$this->sdkId = $sdkId;
3943
$this->requestHandler = new CurlRequestHandler(
40-
$connectApi,
44+
$sandboxApi,
4145
$pem,
4246
$sdkId,
4347
$sdkIdentifier
@@ -47,6 +51,8 @@ public function __construct($sdkId, $pem, $connectApi = self::DEFAULT_SANDBOX_AP
4751
}
4852

4953
/**
54+
* Return shared ActivityDetails.
55+
*
5056
* @param string $token
5157
*
5258
* @return \Yoti\ActivityDetails
@@ -60,25 +66,42 @@ public function getActivityDetails($token)
6066
}
6167

6268
/**
63-
* @param string $httpMethod
6469
* @param RequestBuilder $requestBuilder
6570
*
71+
* @param string $httpMethod
72+
*
6673
* @return string
6774
*
68-
* @throws \Yoti\Exception\RequestException
6975
* @throws Exception\ResponseException
76+
* @throws \Yoti\Exception\RequestException
7077
*/
71-
public function getTokenFromSandbox($httpMethod, RequestBuilder $requestBuilder)
78+
public function getToken(RequestBuilder $requestBuilder, $httpMethod)
7279
{
7380
// Request endpoint
74-
$endpoint = "/app/{$this->sdkId}/tokens";
81+
$endpoint = sprintf(self::TOKEN_REQUEST_ENDPOINT_FORMAT, $this->sdkId);
82+
$resultArr = $this->sendRequest($requestBuilder, $endpoint, $httpMethod);
7583

84+
return (new Response($resultArr))->getToken();
85+
}
86+
87+
/**
88+
* @param RequestBuilder $requestBuilder
89+
*
90+
* @param string $endpoint
91+
* @param string $httpMethod
92+
*
93+
* @return array
94+
*
95+
* @throws \Yoti\Exception\RequestException
96+
*/
97+
protected function sendRequest(RequestBuilder $requestBuilder, $endpoint, $httpMethod)
98+
{
7699
$resultArr = $this->requestHandler->sendRequest(
77100
$endpoint,
78101
$httpMethod,
79-
$requestBuilder->getPayload()
102+
$requestBuilder->getRequest()->getPayload()
80103
);
81104

82-
return (new Response($resultArr))->getToken();
105+
return $resultArr;
83106
}
84107
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace SandboxTest\Entity;
4+
5+
use Yoti\Entity\Profile;
6+
use YotiTest\TestCase;
7+
use YotiSandbox\Entity\SandboxAgeVerification;
8+
9+
class SandboxAgeVerificationTest extends TestCase
10+
{
11+
/**
12+
* @var SandboxAgeVerification
13+
*/
14+
public $ageVerification;
15+
16+
public function setUp()
17+
{
18+
$dateTime = (new \DateTime())->setTimestamp(1171502725);
19+
$this->ageVerification = new SandboxAgeVerification(
20+
$dateTime,
21+
'age_under'
22+
);
23+
}
24+
25+
public function testGetName()
26+
{
27+
$this->assertEquals(
28+
Profile::ATTR_DATE_OF_BIRTH,
29+
$this->ageVerification->getName()
30+
);
31+
}
32+
33+
public function testGetValue()
34+
{
35+
$this->assertEquals('2007-02-15', $this->ageVerification->getValue());
36+
}
37+
38+
public function testGetDerivation()
39+
{
40+
$this->assertEquals('age_under', $this->ageVerification->getDerivation());
41+
}
42+
43+
public function testGetOptional()
44+
{
45+
$this->assertEquals('true', $this->ageVerification->getOptional());
46+
}
47+
48+
public function testGetAnchors()
49+
{
50+
$this->assertEquals(
51+
json_encode([]),
52+
json_encode($this->ageVerification->getAnchors())
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)