Skip to content

Commit

Permalink
Add support for new endpoints
Browse files Browse the repository at this point in the history
Added BankStatement process documents and GET endpoints
Added AnyDocs process documents and GET endpoints
Added W2 process documents and GET endpoints
Added some unit tests
Added missing PHP types for the functions
Added support for PHP 8.3
  • Loading branch information
alejouribesanchez committed Sep 24, 2024
1 parent 3ccd837 commit e96a75e
Show file tree
Hide file tree
Showing 12 changed files with 15,793 additions and 8 deletions.
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
295 changes: 289 additions & 6 deletions src/veryfi/Client.php

Large diffs are not rendered by default.

242 changes: 240 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ 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 bool $mock_responses = true;
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 = false;

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
{
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,224 @@ 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->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->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->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->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/processAnyDocumentUrl.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/processBankStatementUrl.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/processW2DocumentFromUrl.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->once())
->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);
}

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->once())
->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);
}

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->once())
->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);
}
}
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

0 comments on commit e96a75e

Please sign in to comment.