Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for onfido-php library #45

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions test/OnfidoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function createApplicant(): Onfido\Model\Applicant
[
'first_name' => 'Test',
'last_name' => 'Applicant',
'dob' => new DateTime("1980-01-22"),
'dob' => new DateTime('1980-01-22'),
'location' => new Onfido\Model\Location([
'ip_address' => '127.0.0.1',
'country_of_residence' => Onfido\Model\CountryCodes::DEU
Expand Down Expand Up @@ -86,6 +86,93 @@ protected static function cleanUpApplicants(): void
}
}
}

protected function uploadDocument(string $applicantId): Onfido\Model\Document
{
return self::$onfido->uploadDocument(
'passport',
$applicantId,
new \SplFileObject('test/media/sample_driving_licence.png')
);
}

protected function uploadLivePhoto(string $applicantId): Onfido\Model\LivePhoto
{
return self::$onfido->uploadLivePhoto(
$applicantId,
new \SplFileObject('test/media/sample_photo.png')
);
}

protected function createCheck(
Onfido\Model\CheckBuilder $checkBuilder = null,
string $applicantId = null,
string $documentId = null,
array $reportNames = null
): Onfido\Model\Check
{
if($checkBuilder != null) {
return self::$onfido->createCheck($checkBuilder);
}

return self::$onfido->createCheck(
new Onfido\Model\CheckBuilder([
'applicant_id' => $applicantId,
'document_ids' => [$documentId],
'report_names' => $reportNames
]
));
}

protected function createWorkflowRun(
Onfido\Model\WorkflowRunBuilder $workflowRunBuilder = null,
string $applicantId = null,
string $workflowId = null
): Onfido\Model\WorkflowRun
{
if($workflowRunBuilder != null) {
return self::$onfido->createWorkflowRun($workflowRunBuilder);
}

return self::$onfido->createWorkflowRun(
new Onfido\Model\WorkflowRunBuilder([
'applicant_id' => $applicantId,
'workflow_id' => $workflowId
])
);
}

protected function getTaskIdByPartialId($tasks, string $partialId): string {
foreach ($tasks as $task) {
if (strpos($task->getId(), $partialId) !== false) {
return $task->getId();
}
}
}

protected function waitUntilStatus(
callable $function,
string $instanceId,
string $status,
$maxRetries = 10,
$sleepTime = 1
)
{
$instance = $function($instanceId);
$iteration = 0;

while($instance->getStatus() !== $status) {
if($iteration > $maxRetries) {
$this->fail('Status did not change in time');
}

$iteration =+ 1;
sleep($sleepTime);

$instance = $function($instanceId);
}
return $instance;
}
}

?>
?>
18 changes: 18 additions & 0 deletions test/Resource/AddressPickerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Onfido\Test\Resource;

use Onfido\Test\OnfidoTestCase as OnfidoTestCase;

class AddressPickerTest extends OnfidoTestCase
{

public function testFindAddress()
{

$postCode = 'S2 2DF';

$addressesWithPostCode = self::$onfido->findAddresses($postCode)['addresses'];
$this->assertSame($postCode, $addressesWithPostCode[0]['postcode']);
}
}
68 changes: 52 additions & 16 deletions test/Resource/ApplicantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,31 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class ApplicantTest extends OnfidoTestCase
class ApplicantsTest extends OnfidoTestCase
{
private $applicant;
private $applicantId;
/**
* Setup before running each test case
*/
public function setUp(): void
{
}

/**
* Clean up after running each test case
*/
public function tearDown(): void
{
$this->applicant = $this->createApplicant();
$this->applicantId = $this->applicant->getId();
}

/**
* Test "create_applicant" operation
*/
public function testCreateApplicant()
{
$applicant = $this->createApplicant();

$this->assertSame('Test', $applicant->getFirstName());
$this->assertSame('Applicant', $applicant->getLastName());
$this->assertSame('1980-01-22', $applicant->getDob()->format('Y-m-d'));
$this->assertSame('127.0.0.1', $applicant->getLocation()->getIpAddress());
$this->assertSame('DEU', $applicant->getLocation()->getCountryOfResidence());
$this->assertSame('Test', $this->applicant->getFirstName());
$this->assertSame('Applicant', $this->applicant->getLastName());
$this->assertSame('1980-01-22', $this->applicant->getDob()->format('Y-m-d'));
$this->assertSame('127.0.0.1', $this->applicant->getLocation()->getIpAddress());
$this->assertSame('DEU', $this->applicant->getLocation()->getCountryOfResidence());

$address = $applicant->getAddress();
$address = $this->applicant->getAddress();

$this->assertSame('100', $address->getBuildingNumber());
$this->assertSame('Main Street', $address->getStreet());
Expand All @@ -53,4 +48,45 @@ public function testCreateApplicant()
$this->assertSame(Onfido\Model\CountryCodes::GBR, $address->getCountry());
$this->assertSame('My wonderful address', $address->getLine1());
}

public function testListApplicants()
{
$listOfApplicants = self::$onfido->listApplicants()['applicants'];
$this->assertGreaterThan(0, sizeOf($listOfApplicants));
}

public function testFindApplicant()
{
$getApplicant = self::$onfido->findApplicant($this->applicantId);

$this->assertSame($this->applicantId, $getApplicant->getId());
}

public function testUpdateApplicant()
{
$updatedApplicantData = new Onfido\Model\ApplicantBuilder([
'first_name' => 'Jane',
'last_name' => 'Doe',
]);
$updatedApplicant = self::$onfido->updateApplicant($this->applicantId, $updatedApplicantData);
$this->assertSame('Jane', $updatedApplicant->getFirstName());
$this->assertSame('Doe', $updatedApplicant->getLastName());
}

public function testDeleteApplicant()
{
self::$onfido->deleteApplicant($this->applicantId);

$this->expectException(Onfido\ApiException::class);
self::$onfido->findApplicant($this->applicantId);
}

public function testRestoreDeleteApplicant()
{
self::$onfido->deleteApplicant($this->applicantId);
self::$onfido->restoreApplicant($this->applicantId);
$getApplicant = self::$onfido->findApplicant($this->applicantId);

$this->assertSame($this->applicantId, $getApplicant->getId());
}
}
97 changes: 97 additions & 0 deletions test/Resource/ChecksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Onfido\Test\Resource;

use Onfido\Test\OnfidoTestCase as OnfidoTestCase;
use Onfido\Model\ReportName as ReportName;
use Onfido\Model\Check as Check;
use Onfido\Model\CheckBuilder as CheckBuilder;
use Onfido\Model\UsDrivingLicenceBuilder as UsDrivingLicenceBuilder;

class ChecksTest extends OnfidoTestCase
{
private $applicantId;
private $documentId;
private $check;
/**
* Setup before running each test case
*/
public function setUp(): void
{
$this->applicantId = $this->createApplicant()->getId();
$this->documentId = $this->uploadDocument($this->applicantId)->getId();
$this->check = $this->createCheck(
null,
$this->applicantId,
$this->documentId,
[ReportName::DOCUMENT, ReportName::IDENTITY_ENHANCED]
);
}

public function testCreateCheck(): void
{
$this->assertSame($this->applicantId, $this->check->getApplicantId());
$this->assertSame(Check::STATUS_IN_PROGRESS, $this->check->getStatus());
$this->assertSame(2, sizeOf($this->check->getReportIds()));
}

public function testCreateConsiderCheck(): void
{
$checkBuilder = new CheckBuilder([
'applicant_id' => $this->applicantId,
'document_ids' => [$this->documentId],
'report_names' => [ReportName::DOCUMENT],
'consider' => [ReportName::DOCUMENT]
]);

$check = $this->createCheck($checkBuilder);
$this->assertSame($this->applicantId, $check->getApplicantId());
}

public function testCreateDrivingLicenceCheck(): void
{
$usDrivingLicenceBuilder = new UsDrivingLicenceBuilder([
'id_number' => '12345',
'issue_state' => 'GA'
]);

$checkBuilder = new CheckBuilder([
'applicant_id' => $this->applicantId,
'document_ids' => [$this->documentId],
'report_names' => [ReportName::DOCUMENT],
'us_driving_licence' => $usDrivingLicenceBuilder
]);

$check = $this->createCheck($checkBuilder);

$this->assertSame($this->applicantId, $check->getApplicantId());
}

public function testListChecks(): void
{
$listOfChecks = self::$onfido->listChecks($this->applicantId)["checks"];

$this->assertGreaterThan(0, sizeOf($listOfChecks));
}

public function testFindCheck(): void
{
$getCheck = self::$onfido->findCheck($this->check->getId());

$this->assertSame($this->check->getId(), $getCheck->getId());
$this->assertSame($this->applicantId, $getCheck->getApplicantId());
}

public function testRestartCheck(): void
{
self::$onfido->resumeCheck($this->check->getId());
$this->assertTrue(true);
dvacca-onfido marked this conversation as resolved.
Show resolved Hide resolved
}

public function testDownloadCheck(): void
{
$file = self::$onfido->downloadCheck($this->check->getId());

$this->assertGreaterThan(0, $file->getSize());
}
}
58 changes: 58 additions & 0 deletions test/Resource/DocumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Onfido\Test\Resource;

use Onfido\Test\OnfidoTestCase as OnfidoTestCase;
use Onfido\Model\DocumentTypes as DocumentTypes;

use Onfido as Onfido;


class DocumentsTest extends OnfidoTestCase
{
private $applicantId;
private $document;
/**
* Setup before running each test case
*/
public function setUp(): void
{
$this->applicantId = $this->createApplicant()->getId();
$this->document = $this->uploadDocument($this->applicantId);
}

public function testUploadDocument(): void
{
$this->assertSame($this->applicantId, $this->document->getApplicantId());
$this->assertSame(DocumentTypes::PASSPORT, $this->document->getType());
$this->assertSame($this->applicantId, $this->document->getApplicantId());
}

public function testListDocuments(): void
{
$listOfDocuments = self::$onfido->listDocuments($this->applicantId)['documents'];
$this->assertGreaterThan(0, sizeOf($listOfDocuments));
}

public function testFindDocument(): void
{
$getDocument = self::$onfido->findDocument($this->document->getId());
$this->assertSame($this->document->getId(), $getDocument->getId());
}

public function testDownloadDocument(): void
{
$file = self::$onfido->downloadDocument($this->document->getId());

$this->assertGreaterThan(0, $file->getSize());
}

public function testDownloadInexistentDocument(): void
{
$inexistentDocumentId = '00000000-0000-0000-0000-000000000000';

$this->expectException(Onfido\ApiException::class);
self::$onfido->downloadDocument($inexistentDocumentId);
}
}
?>
Loading