Skip to content

Commit b7caee3

Browse files
LuborRodRodion Liuborets
andauthored
Sdk 1957 Add support for retreving instructions PDF for an In-Branch Verification session (#252)
* SDK-1956 Add main logic * SDK-1956 Add several tests * SDK-1956 Add more tests * SDK-1956 Add last tests * SDK-1957 Add logic and tests * SDK-1957 Small fixes for 7.1 and 7.2 * SDK-1957 Remove code smells Co-authored-by: Rodion Liuborets <[email protected]>
1 parent 9b5d549 commit b7caee3

File tree

12 files changed

+240
-0
lines changed

12 files changed

+240
-0
lines changed

src/DocScan/DocScanClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,15 @@ public function getIbvInstructions(string $sessionId): InstructionsResponse
204204
{
205205
return $this->docScanService->getIbvInstructions($sessionId);
206206
}
207+
208+
209+
/**
210+
* @param string $sessionId
211+
* @return Media
212+
* @throws Exception\DocScanException
213+
*/
214+
public function getIbvInstructionsPdf(string $sessionId): Media
215+
{
216+
return $this->docScanService->getIbvInstructionsPdf($sessionId);
217+
}
207218
}

src/DocScan/Service.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,29 @@ public function getIbvInstructions(string $sessionId): InstructionsResponse
332332
return new InstructionsResponse($result);
333333
}
334334

335+
/**
336+
* @param string $sessionId
337+
* @return Media
338+
* @throws DocScanException
339+
*/
340+
public function getIbvInstructionsPdf(string $sessionId): Media
341+
{
342+
$response = (new RequestBuilder($this->config))
343+
->withBaseUrl($this->apiUrl)
344+
->withPemFile($this->pemFile)
345+
->withEndpoint(sprintf('/sessions/%s/instructions/pdf', $sessionId))
346+
->withGet()
347+
->build()
348+
->execute();
349+
350+
self::assertResponseIsSuccess($response);
351+
352+
$content = (string)$response->getBody();
353+
$mimeType = $response->getHeader("Content-Type")[0] ?? '';
354+
355+
return new Media($mimeType, $content);
356+
}
357+
335358
/**
336359
* @param ResponseInterface $response
337360
*
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Yoti\DocScan\Session\Retrieve\Instructions\Branch;
4+
5+
class UnknownBranchResponse extends BranchResponse
6+
{
7+
}

src/DocScan/Session/Retrieve/Instructions/Document/DocumentProposalResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function __construct(array $documentData)
2929
case Constants::SUPPLEMENTARY_DOCUMENT:
3030
$this->document = new SelectedSupplementaryDocumentResponse($documentData['document']);
3131
break;
32+
default:
33+
$this->document = new UnknownDocumentResponse($documentData['document']);
3234
}
3335
}
3436

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Yoti\DocScan\Session\Retrieve\Instructions\Document;
4+
5+
class UnknownDocumentResponse extends SelectedDocumentResponse
6+
{
7+
}

src/DocScan/Session/Retrieve/Instructions/InstructionsResponse.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yoti\DocScan\Constants;
66
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\BranchResponse;
77
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\UkPostOfficeBranchResponse;
8+
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\UnknownBranchResponse;
89
use Yoti\DocScan\Session\Retrieve\Instructions\Document\DocumentProposalResponse;
910

1011
class InstructionsResponse
@@ -42,6 +43,8 @@ public function __construct(array $sessionData)
4243
case Constants::UK_POST_OFFICE:
4344
$this->branch = new UkPostOfficeBranchResponse($sessionData['branch']);
4445
break;
46+
default:
47+
$this->branch = new UnknownBranchResponse();
4548
}
4649
}
4750
}

tests/DocScan/DocScanClientTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,28 @@ public function testGetIbvInstructions()
404404
TestData::DOC_SCAN_SESSION_ID
405405
);
406406
}
407+
408+
/**
409+
* @test
410+
* @covers ::getIbvInstructionsPdf
411+
*/
412+
public function testGetIbvInstructionsPdf()
413+
{
414+
$response = $this->createMock(ResponseInterface::class);
415+
$response->method('getBody')->willReturn(json_encode((object)[]));
416+
$response->method('getStatusCode')->willReturn(200);
417+
418+
$httpClient = $this->createMock(ClientInterface::class);
419+
$httpClient->expects($this->exactly(1))
420+
->method('sendRequest')
421+
->willReturn($response);
422+
423+
$docScanClient = new DocScanClient(TestData::SDK_ID, TestData::PEM_FILE, [
424+
Config::HTTP_CLIENT => $httpClient,
425+
]);
426+
427+
$docScanClient->getIbvInstructionsPdf(
428+
TestData::DOC_SCAN_SESSION_ID
429+
);
430+
}
407431
}

tests/DocScan/ServiceTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,4 +1111,106 @@ function (RequestInterface $requestMessage) {
11111111
TestData::DOC_SCAN_SESSION_ID
11121112
);
11131113
}
1114+
1115+
/**
1116+
* @test
1117+
* @covers ::__construct
1118+
* @covers ::getIbvInstructionsPdf
1119+
* @covers ::assertResponseIsSuccess
1120+
*/
1121+
public function getIbvInstructionsPdfShouldReturnMediaObjectOnSuccessfulCall()
1122+
{
1123+
$httpClient = $this->createMock(ClientInterface::class);
1124+
$httpClient->expects($this->exactly(1))
1125+
->method('sendRequest')
1126+
->with(
1127+
$this->callback(
1128+
function (RequestInterface $requestMessage) {
1129+
$expectedPathPattern = sprintf(
1130+
'~^%s/sessions/%s/instructions/pdf.*?~',
1131+
TestData::DOC_SCAN_BASE_URL,
1132+
TestData::DOC_SCAN_SESSION_ID
1133+
);
1134+
1135+
$this->assertEquals('GET', $requestMessage->getMethod());
1136+
$this->assertMatchesRegularExpression($expectedPathPattern, (string)$requestMessage->getUri());
1137+
return true;
1138+
}
1139+
)
1140+
)
1141+
->willReturn(
1142+
$this->createResponse(
1143+
200,
1144+
file_get_contents(TestData::DUMMY_PDF_FILE),
1145+
[
1146+
'Content-Type' => [
1147+
'application/pdf'
1148+
]
1149+
]
1150+
)
1151+
);
1152+
1153+
$docScanService = new Service(
1154+
TestData::SDK_ID,
1155+
PemFile::fromFilePath(TestData::PEM_FILE),
1156+
new Config(
1157+
[
1158+
Config::HTTP_CLIENT => $httpClient,
1159+
]
1160+
)
1161+
);
1162+
1163+
$this->assertInstanceOf(
1164+
Media::class,
1165+
$docScanService->getIbvInstructionsPdf(TestData::DOC_SCAN_SESSION_ID)
1166+
);
1167+
}
1168+
1169+
/**
1170+
* @test
1171+
* @covers ::__construct
1172+
* @covers ::getIbvInstructionsPdf
1173+
* @covers ::assertResponseIsSuccess
1174+
*/
1175+
public function getIbvInstructionsPdfShouldThrowExceptionOnFailedCall()
1176+
{
1177+
$httpClient = $this->createMock(ClientInterface::class);
1178+
$httpClient->expects($this->exactly(1))
1179+
->method('sendRequest')
1180+
->with(
1181+
$this->callback(
1182+
function (RequestInterface $requestMessage) {
1183+
$expectedPathPattern = sprintf(
1184+
'~^%s/sessions/%s/instructions/pdf.*?~',
1185+
TestData::DOC_SCAN_BASE_URL,
1186+
TestData::DOC_SCAN_SESSION_ID
1187+
);
1188+
1189+
$this->assertEquals('GET', $requestMessage->getMethod());
1190+
$this->assertMatchesRegularExpression($expectedPathPattern, (string)$requestMessage->getUri());
1191+
return true;
1192+
}
1193+
)
1194+
)
1195+
->willReturn(
1196+
$this->createResponse(
1197+
404
1198+
)
1199+
);
1200+
1201+
$docScanService = new Service(
1202+
TestData::SDK_ID,
1203+
PemFile::fromFilePath(TestData::PEM_FILE),
1204+
new Config(
1205+
[
1206+
Config::HTTP_CLIENT => $httpClient,
1207+
]
1208+
)
1209+
);
1210+
1211+
$this->expectException(DocScanException::class);
1212+
$this->expectExceptionMessage("Server responded with 404");
1213+
1214+
$docScanService->getIbvInstructionsPdf(TestData::DOC_SCAN_SESSION_ID);
1215+
}
11141216
}

tests/DocScan/Session/Retrieve/Instructions/Document/DocumentProposalResponseTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Yoti\DocScan\Session\Retrieve\Instructions\Document\DocumentProposalResponse;
77
use Yoti\DocScan\Session\Retrieve\Instructions\Document\SelectedIdDocumentResponse;
88
use Yoti\DocScan\Session\Retrieve\Instructions\Document\SelectedSupplementaryDocumentResponse;
9+
use Yoti\DocScan\Session\Retrieve\Instructions\Document\UnknownDocumentResponse;
910
use Yoti\Test\TestCase;
1011

1112
/**
@@ -46,6 +47,15 @@ public function shouldBuildCorrectly(): void
4647
],
4748
];
4849

50+
$data3 = [
51+
'requirement_id' => self::SOME_REQUIREMENT_ID,
52+
'document' => [
53+
'type' => 'SOME_TYPE',
54+
'country_code' => self::SOME_COUNTRY_CODE,
55+
'document_type' => self::SOME_DOCUMENT_TYPE
56+
],
57+
];
58+
4959
$result = new DocumentProposalResponse($data);
5060

5161
$this->assertEquals(self::SOME_REQUIREMENT_ID, $result->getRequirementId());
@@ -54,5 +64,9 @@ public function shouldBuildCorrectly(): void
5464
$result2 = new DocumentProposalResponse($data2);
5565
$this->assertEquals(self::SOME_REQUIREMENT_ID, $result2->getRequirementId());
5666
$this->assertInstanceOf(SelectedSupplementaryDocumentResponse::class, $result2->getDocument());
67+
68+
$result3 = new DocumentProposalResponse($data3);
69+
$this->assertEquals(self::SOME_REQUIREMENT_ID, $result3->getRequirementId());
70+
$this->assertInstanceOf(UnknownDocumentResponse::class, $result3->getDocument());
5771
}
5872
}

tests/DocScan/Session/Retrieve/Instructions/InstructionsResponseTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Yoti\Test\DocScan\Session\Retrieve\Instructions;
44

55
use Yoti\DocScan\Constants;
6+
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\UkPostOfficeBranchResponse;
7+
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\UnknownBranchResponse;
68
use Yoti\DocScan\Session\Retrieve\Instructions\Branch\BranchResponse;
79
use Yoti\DocScan\Session\Retrieve\Instructions\InstructionsResponse;
810
use Yoti\Test\TestCase;
@@ -72,6 +74,50 @@ public function shouldBuildCorrectly()
7274
]
7375
];
7476

77+
$data2 = [
78+
'contact_profile_exists' => true,
79+
'documents' =>
80+
[
81+
[
82+
'requirement_id' => self::SOME_REQUIREMENT_ID,
83+
'document' => [
84+
'type' => Constants::ID_DOCUMENT,
85+
'country_code' => self::SOME_COUNTRY_CODE,
86+
'document_type' => self::SOME_DOCUMENT_TYPE
87+
],
88+
],
89+
90+
[
91+
'requirement_id' => self::SOME_REQUIREMENT_ID_2,
92+
'document' => [
93+
'type' => Constants::SUPPLEMENTARY_DOCUMENT,
94+
'country_code' => self::SOME_COUNTRY_CODE_2,
95+
'document_type' => self::SOME_DOCUMENT_TYPE_2
96+
],
97+
]
98+
],
99+
'branch' => [
100+
'type' => 'SOME_TYPE',
101+
'fad_code' => self::SOME_FAD_CODE,
102+
'name' => self::SOME_NAME,
103+
'address' => self::SOME_ADDRESS,
104+
'post_code' => self::SOME_POST_CODE,
105+
'location' => [
106+
'latitude' => self::SOME_LATITUDE,
107+
'longitude' => self::SOME_LONGITUDE,
108+
]
109+
]
110+
];
111+
112+
113+
$result = new InstructionsResponse($data);
114+
115+
$this->assertTrue($result->isContactProfileExists());
116+
$this->assertInstanceOf(UkPostOfficeBranchResponse::class, $result->getBranch());
117+
$this->assertCount(2, $result->getDocuments());
118+
119+
$result2 = new InstructionsResponse($data2);
120+
$this->assertInstanceOf(UnknownBranchResponse::class, $result2->getBranch());
75121
$result = new InstructionsResponse($data);
76122

77123
$this->assertTrue($result->isContactProfileExists());

0 commit comments

Comments
 (0)