106 lines
3.7 KiB
PHP
106 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Tests\TestCase;
|
|
|
|
class GoogleVisionOcrApiTest extends TestCase
|
|
{
|
|
/**
|
|
* Test validation requires passport_file.
|
|
*/
|
|
public function test_extract_passport_validation_fails_without_file()
|
|
{
|
|
$response = $this->postJson('/api/workers/ocr/passport-vision');
|
|
|
|
$response->assertStatus(422)
|
|
->assertJson([
|
|
'success' => false,
|
|
'message' => 'Validation error.',
|
|
])
|
|
->assertJsonValidationErrors(['passport_file']);
|
|
}
|
|
|
|
/**
|
|
* Test successful OCR extraction using mocked Google Vision API.
|
|
*/
|
|
public function test_extract_passport_success_with_mrz()
|
|
{
|
|
// Mock Google Vision API Response
|
|
Http::fake([
|
|
'https://vision.googleapis.com/*' => Http::response([
|
|
'responses' => [
|
|
[
|
|
'fullTextAnnotation' => [
|
|
'text' => "PASSPORT\nType: P Country: ARE\nPassport No: Z43R34255\nSurname: AL-HASHIMI\nGiven Names: AHMAD\nNationality: ARE\nDate of Birth: 03/07/1978\nSex: M\nPlace of Birth: DUBAI\nDate of Issue: 10/02/2015\nDate of Expiry: 10/02/2020\nMRZ:\nP<AREAL<HASHIMI<<AHMAD<<<<<<<<<<<<<<<<<<<<<<\nZ43R342558ARE7807033M2002100<<<<<<<<<<<<<<02"
|
|
]
|
|
]
|
|
]
|
|
], 200)
|
|
]);
|
|
|
|
$file = UploadedFile::fake()->image('passport.jpg');
|
|
|
|
$response = $this->postJson('/api/workers/ocr/passport-vision', [
|
|
'passport_file' => $file
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'success' => true,
|
|
'data' => [
|
|
'date_of_birth' => '03/07/1978',
|
|
'date_of_expiry' => '10/02/2020',
|
|
'date_of_issue' => '10/02/2015',
|
|
'given_names' => 'AHMAD',
|
|
'issuing_country' => 'ARE',
|
|
'nationality' => 'ARE',
|
|
'passport_number' => 'Z43R34255',
|
|
'place_of_birth' => 'DUBAI',
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test OCR extraction fallback via regex when MRZ is absent.
|
|
*/
|
|
public function test_extract_passport_success_via_regex_fallback()
|
|
{
|
|
// Mock Google Vision API Response without MRZ lines
|
|
Http::fake([
|
|
'https://vision.googleapis.com/*' => Http::response([
|
|
'responses' => [
|
|
[
|
|
'fullTextAnnotation' => [
|
|
'text' => "PASSPORT\nPassport Number: Z43R34255\nGiven Names: AHMAD\nNationality: ARE\nIssuing Country: ARE\nDate of Birth: 03/07/1978\nDate of Issue: 10/02/2015\nDate of Expiry: 10/02/2020\nPlace of Birth: DUBAI"
|
|
]
|
|
]
|
|
]
|
|
], 200)
|
|
]);
|
|
|
|
$file = UploadedFile::fake()->image('passport.jpg');
|
|
|
|
$response = $this->postJson('/api/workers/ocr/passport-vision', [
|
|
'passport_file' => $file
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'success' => true,
|
|
'data' => [
|
|
'date_of_birth' => '03/07/1978',
|
|
'date_of_expiry' => '10/02/2020',
|
|
'date_of_issue' => '10/02/2015',
|
|
'given_names' => 'AHMAD',
|
|
'issuing_country' => 'ARE',
|
|
'nationality' => 'ARE',
|
|
'passport_number' => 'Z43R34255',
|
|
'place_of_birth' => 'DUBAI',
|
|
]
|
|
]);
|
|
}
|
|
}
|