AliyunOCRDriverTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Tests\Unit\Services\OCR;
  3. use App\Services\OCR\Drivers\AliyunOCRDriver;
  4. use Tests\TestCase;
  5. use Mockery;
  6. use AlibabaCloud\SDK\Ocrapi\V20210707\Ocrapi;
  7. use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeEduPaperOcrResponse;
  8. use AlibabaCloud\SDK\Ocrapi\V20210707\Models\RecognizeEduPaperOcrResponseBody;
  9. class AliyunOCRDriverTest extends TestCase
  10. {
  11. public function test_recognize_calls_aliyun_api()
  12. {
  13. // Mock configuration
  14. $config = [
  15. 'access_key_id' => 'test_id',
  16. 'access_key_secret' => 'test_secret',
  17. 'endpoint' => 'ocr.cn-shanghai.aliyuncs.com',
  18. ];
  19. // Create a mock of the OcrApi
  20. $mockClient = Mockery::mock(Ocrapi::class);
  21. // Mock the response with EduPaperCut structure
  22. $mockData = [
  23. 'page_list' => [
  24. [
  25. 'subject_list' => [
  26. [
  27. 'ids' => ['1'],
  28. 'text' => '1. 三角形按角分类可以分为( )',
  29. 'prism_wordsInfo' => [
  30. ['word' => '1.', 'prob' => 99],
  31. ['word' => '三角形', 'prob' => 99],
  32. ]
  33. ]
  34. ]
  35. ]
  36. ]
  37. ];
  38. $responseBody = new RecognizeEduPaperOcrResponseBody([
  39. 'Data' => json_encode($mockData),
  40. 'requestId' => 'test-request-id',
  41. ]);
  42. $response = new RecognizeEduPaperOcrResponse([
  43. 'body' => $responseBody,
  44. ]);
  45. $mockClient->shouldReceive('recognizeEduPaperCutWithOptions')
  46. ->once()
  47. ->andReturn($response);
  48. // Instantiate the driver with the mock client
  49. $driver = new AliyunOCRDriver($config, $mockClient);
  50. // This will trigger the API call
  51. $result = $driver->recognize(base_path('tests/fixtures/test.jpg'), ['cutType' => 'question']);
  52. $this->assertIsArray($result);
  53. $this->assertArrayHasKey('questions', $result);
  54. $this->assertArrayHasKey('cut_type', $result);
  55. $this->assertEquals('question', $result['cut_type']);
  56. $this->assertCount(1, $result['questions']);
  57. $this->assertEquals('1', $result['questions'][0]['question_number']);
  58. $this->assertStringContainsString('三角形', $result['questions'][0]['content']);
  59. }
  60. }