| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace Tests\Unit\Services\OCR;
- use App\Services\OCR\Drivers\AliyunOCRDriver;
- use Tests\TestCase;
- use Mockery;
- use AlibabaCloud\SDK\Ocrapi\V20210707\Ocrapi;
- use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeEduPaperOcrResponse;
- use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeEduPaperOcrResponseBody;
- class AliyunOCRDriverTest extends TestCase
- {
- public function test_recognize_calls_aliyun_api()
- {
- // Mock configuration
- $config = [
- 'access_key_id' => 'test_id',
- 'access_key_secret' => 'test_secret',
- 'endpoint' => 'ocr.cn-shanghai.aliyuncs.com',
- ];
- // Create a mock of the OcrApi
- $mockClient = Mockery::mock(Ocrapi::class);
-
- // Mock the response with EduPaperCut structure
- $mockData = [
- 'page_list' => [
- [
- 'subject_list' => [
- [
- 'ids' => ['1'],
- 'text' => '1. 三角形按角分类可以分为( )',
- 'prism_wordsInfo' => [
- ['word' => '1.', 'prob' => 99],
- ['word' => '三角形', 'prob' => 99],
- ]
- ]
- ]
- ]
- ]
- ];
- $responseBody = new RecognizeEduPaperOcrResponseBody([
- 'Data' => json_encode($mockData),
- 'requestId' => 'test-request-id',
- ]);
- $response = new RecognizeEduPaperOcrResponse([
- 'body' => $responseBody,
- ]);
- $mockClient->shouldReceive('recognizeEduPaperCutWithOptions')
- ->once()
- ->andReturn($response);
- // Instantiate the driver with the mock client
- $driver = new AliyunOCRDriver($config, $mockClient);
-
- // This will trigger the API call
- $result = $driver->recognize(base_path('tests/fixtures/test.jpg'), ['cutType' => 'question']);
-
- $this->assertIsArray($result);
- $this->assertArrayHasKey('questions', $result);
- $this->assertArrayHasKey('cut_type', $result);
- $this->assertEquals('question', $result['cut_type']);
- $this->assertCount(1, $result['questions']);
- $this->assertEquals('1', $result['questions'][0]['question_number']);
- $this->assertStringContainsString('三角形', $result['questions'][0]['content']);
- }
- }
|