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

Feature/lp 956 update php sdk #18

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ To learn more about Veryfi visit https://www.veryfi.com/

## Tutorial

Debug project with https://xdebug.org/docs/install#pecl
pecl install xdebug
Installing '/opt/homebrew/Cellar/php/8.3.10/pecl/20230831/xdebug.so'
install ok: channel://pecl.php.net/xdebug-3.3.2
Extension xdebug enabled in php.ini

Below is an introduction to the php SDK.

Expand Down
308 changes: 301 additions & 7 deletions src/veryfi/Client.php

Large diffs are not rendered by default.

249 changes: 248 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ final class ClientTest extends TestCase
private string $username = 'your_username';
private string $api_key = 'your_api_key';
private string $receipt_path = __DIR__ . '/resources/receipt.jpg';
private string $w2_path = __DIR__ . '/resources/w2.png';
private string $any_doc_path = __DIR__ . '/resources/driver_license.png';
private string $bank_statement_path = __DIR__ . '/resources/bankstatement.pdf';
private bool $mock_responses = true;

protected function setUp(): void
{
parent::setUp();
$this->client_id = getenv('VERYFI_CLIENT_ID');
$this->client_secret = getenv('VERYFI_CLIENT_SECRET');
$this->username = getenv('VERYFI_USERNAME');
$this->api_key = getenv('VERYFI_API_KEY');
}

public function test_get_documents(): void
{
if ($this->mock_responses) {
Expand Down Expand Up @@ -154,7 +166,7 @@ public function test_delete_document(): void
}
$categories = array('Job Supplies');
$file = $this->receipt_path;
$json_response = json_decode($veryfi_client->process_document($file, $categories, false), true);
$json_response = json_decode($veryfi_client->process_document($file, $categories), true);
$id = $json_response['id'];
$delete_json_response = json_decode($veryfi_client->delete_document($id));
$this->assertEquals(json_decode('{"status": "ok", "message": "Document has been deleted"}'), $delete_json_response);
Expand Down Expand Up @@ -229,6 +241,9 @@ public function test_get_line_item(): void
$this->assertEquals($line_item_id, $json_response['id']);
}

/**
* @throws Exception
*/
public function test_update_line_item(): void
{
$document_id = 44691518;
Expand All @@ -254,6 +269,9 @@ public function test_update_line_item(): void
$this->assertEquals('TEST', $json_response['description']);
}

/**
* @throws Exception
*/
public function test_add_line_item(): void
{
$document_id = 44691518;
Expand Down Expand Up @@ -505,4 +523,233 @@ public function test_add_document_tags(): void
$json_response = json_decode($veryfi_client->add_tags($document_id, $tags), true);
$this->assertNotEmpty($json_response);
}

public function test_process_w2_document(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ .'/resources/processW2Document.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$file = $this->w2_path;
$json_response = json_decode($veryfi_client->process_w2_document($file, true), true);
$this->assertNotEmpty( $json_response['id']);
}

public function test_process_any_document(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ .'/resources/processAnyDocument.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->atLeastOnce())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$file = $this->any_doc_path;
$json_response = json_decode($veryfi_client->process_any_document($file, 'us_driver_license'), true);
$this->assertNotEmpty( $json_response['id']);
$json_response = json_decode($veryfi_client->process_any_document_from_file($file, 'us_driver_license'), true);
$this->assertNotEmpty( $json_response['id']);
}

public function test_process_bank_statement(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ .'/resources/processBankStatement.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->atLeastOnce())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$file = $this->bank_statement_path;
$json_response = json_decode($veryfi_client->process_bank_statement($file), true);
$this->assertNotEmpty( $json_response['id']);
$json_response = json_decode($veryfi_client->process_bank_statement_from_file($file), true);
$this->assertNotEmpty( $json_response['id']);
}

public function test_process_any_document_url(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/processAnyDocument.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$url = 'https://cdn-dev.veryfi.com/testing/veryfi-python/driver_license.png';
$json_response = json_decode($veryfi_client->process_any_document_url($url, 'us_driver_license'), true);
$this->assertNotEmpty( $json_response['id']);
}

public function test_process_bank_statement_url(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/processBankStatement.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$url = 'https://cdn-dev.veryfi.com/testing/veryfi-python/bankstatement.pdf';
$json_response = json_decode($veryfi_client->process_bank_statement_url($url), true);
$this->assertNotEmpty( $json_response['id']);
}

public function test_process_w2_document_from_url(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();
$file_path = __DIR__ . '/resources/processW2Document.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->once())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$file_name = 'w2_form.pdf';
$url = 'https://cdn.veryfi.com/wp-content/uploads/image.png';
$json_response = json_decode($veryfi_client->process_w2_document_from_url($file_name, $url, null, true), true);
$this->assertNotEmpty( $json_response['id']);
}

/**
* @throws Exception
*/
public function test_get_w2_documents(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();
$file_path = __DIR__ . '/resources/getW2Documents.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->atLeastOnce())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}
$json_response = json_decode($veryfi_client->get_w2_documents(), true);
$json_len = sizeof($json_response);
$this->assertTrue($json_len > 1);
$document_id = $json_response['results'][0]['id'];
$json_response = json_decode($veryfi_client->get_w2_document($document_id), true);
$this->assertNotEmpty( $json_response);
}

public function test_get_bank_statements(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/getBankStatements.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->atLeastOnce())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$json_response = json_decode($veryfi_client->get_bank_statements(), true);
$json_len = sizeof($json_response);
$this->assertTrue($json_len > 1);
$document_id = $json_response['results'][0]['id'];
$json_response = json_decode($veryfi_client->get_bank_statement($document_id), true);
$this->assertNotEmpty( $json_response);
}

public function test_get_any_documents(): void
{
if ($this->mock_responses) {
$veryfi_client = $this->getMockBuilder(Client::class)
->onlyMethods(['exec_curl'])
->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key])
->getMock();

$file_path = __DIR__ . '/resources/getAnyDocuments.json';
$file = fopen($file_path, 'r');
$file_data = mb_convert_encoding(fread($file, filesize($file_path)), 'UTF-8');
$veryfi_client->expects($this->atLeastOnce())
->method('exec_curl')
->willReturn($file_data);

} else {
$veryfi_client = new Client($this->client_id, $this->client_secret, $this->username, $this->api_key);
}

$json_response = json_decode($veryfi_client->get_any_documents(), true);
$json_len = sizeof($json_response);
$this->assertTrue($json_len > 1);
$document_id = $json_response['results'][0]['id'];
$json_response = json_decode($veryfi_client->get_any_document($document_id), true);
$this->assertNotEmpty( $json_response);
}
}
Binary file added tests/resources/bankstatement.pdf
Binary file not shown.
Binary file added tests/resources/driver_license.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading