Skip to content

Commit

Permalink
Merge pull request #45 from onfido/test/add-tests
Browse files Browse the repository at this point in the history
test: add tests for onfido-php library
  • Loading branch information
sofia-gomes-onfido authored May 23, 2024
2 parents b9b54e4 + 6720c79 commit ed72f03
Show file tree
Hide file tree
Showing 17 changed files with 1,000 additions and 18 deletions.
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);
}

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

0 comments on commit ed72f03

Please sign in to comment.