diff --git a/src/veryfi/Client.php b/src/veryfi/Client.php index ab520e6..02bad26 100644 --- a/src/veryfi/Client.php +++ b/src/veryfi/Client.php @@ -3,8 +3,6 @@ declare(strict_types=1); namespace veryfi; - -use CurlHandle; use Exception; /** @@ -21,7 +19,7 @@ class Client * * @var array static. */ - const array CATEGORIES = [ + const CATEGORIES = [ 'Advertising & Marketing', 'Automotive', 'Bank Charges & Fees', @@ -132,7 +130,7 @@ public function __construct(string $client_id, private function get_headers(): array { return array( - 'User-Agent' => 'php veryfi-php/1.0.3', + 'User-Agent' => 'php veryfi-php/1.0.4', 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Client-ID' => $this->client_id, @@ -213,10 +211,10 @@ private function request(string $http_verb, /**\internal * Exec the curl, needed for mock it. * - * @param CurlHandle $curl Curl handle of request. + * @param $curl Curl handle of request. * @return string A JSON response. */ - protected function exec_curl(CurlHandle $curl): string + protected function exec_curl($curl): string { return curl_exec($curl); } @@ -570,6 +568,19 @@ public function get_w2_documents(int $page = null): string return $this->request('GET', $endpoint_name, $params); } + /** + * Get a W2 document. + * + * @param string $document_id The ID of the document you'd like to retrieve. + * @return string Data extracted from the document. + */ + public function get_w2_document(string $document_id): string + { + $endpoint_name = "/w2s/{$document_id}/"; + $request_arguments = ['id' => $document_id]; + return $this->request('GET', $endpoint_name, $request_arguments); + } + /** * Process a W2 document from a file path and extract all fields from it. * diff --git a/tests/ClientTest.php b/tests/ClientTest.php index d46a685..a8bea04 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -16,7 +16,7 @@ final class ClientTest extends TestCase 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; + private bool $mock_responses = true; protected function setUp(): void { @@ -558,7 +558,7 @@ public function test_process_any_document(): void $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()) + $veryfi_client->expects($this->atLeastOnce()) ->method('exec_curl') ->willReturn($file_data); @@ -584,7 +584,7 @@ public function test_process_bank_statement(): void $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()) + $veryfi_client->expects($this->atLeastOnce()) ->method('exec_curl') ->willReturn($file_data); @@ -607,7 +607,7 @@ public function test_process_any_document_url(): void ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) ->getMock(); - $file_path = __DIR__ . '/resources/processAnyDocumentUrl.json'; + $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()) @@ -631,7 +631,7 @@ public function test_process_bank_statement_url(): void ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) ->getMock(); - $file_path = __DIR__ . '/resources/processBankStatementUrl.json'; + $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()) @@ -654,7 +654,7 @@ public function test_process_w2_document_from_url(): void ->onlyMethods(['exec_curl']) ->setConstructorArgs([$this->client_id, $this->client_secret, $this->username, $this->api_key]) ->getMock(); - $file_path = __DIR__ . '/resources/processW2DocumentFromUrl.json'; + $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()) @@ -684,7 +684,7 @@ public function test_get_w2_documents(): void $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()) + $veryfi_client->expects($this->atLeastOnce()) ->method('exec_curl') ->willReturn($file_data); @@ -694,6 +694,9 @@ public function test_get_w2_documents(): void $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 @@ -707,7 +710,7 @@ public function test_get_bank_statements(): void $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()) + $veryfi_client->expects($this->atLeastOnce()) ->method('exec_curl') ->willReturn($file_data); @@ -718,6 +721,9 @@ public function test_get_bank_statements(): void $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 @@ -731,7 +737,7 @@ public function test_get_any_documents(): void $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()) + $veryfi_client->expects($this->atLeastOnce()) ->method('exec_curl') ->willReturn($file_data); @@ -742,5 +748,8 @@ public function test_get_any_documents(): void $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); } }