| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace Tests\Feature;
- use App\Models\OCRRecord;
- use App\Models\Student;
- use App\Models\Teacher;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Storage;
- use Tests\TestCase;
- class OCRRecordResourceTest extends TestCase
- {
- use RefreshDatabase;
- protected User $adminUser;
- protected Student $student;
- protected function setUp(): void
- {
- parent::setUp();
- // 创建管理员用户
- $this->adminUser = User::factory()->create([
- 'role' => 'admin',
- 'is_active' => true,
- ]);
- // 创建测试教师
- $teacher = Teacher::create([
- 'teacher_id' => 'teacher_test_001',
- 'name' => '测试教师',
- 'user_id' => $this->adminUser->id,
- ]);
- // 创建测试学生
- $this->student = Student::create([
- 'student_id' => 'stu_test_001',
- 'name' => '测试学生',
- 'grade' => '七年级',
- 'class_name' => '1班',
- 'teacher_id' => $teacher->teacher_id,
- ]);
- Storage::fake('public');
- }
- /** @test */
- public function it_can_list_ocr_records()
- {
- // 创建测试OCR记录
- OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'image_path' => 'ocr-uploads/test.jpg',
- 'status' => 'pending',
- 'total_questions' => 10,
- 'processed_questions' => 0,
- ]);
- $response = $this->actingAs($this->adminUser)
- ->get('/admin/ocr-records');
- $response->assertStatus(200);
- }
- /** @test */
- public function it_can_access_create_page()
- {
- $response = $this->actingAs($this->adminUser)
- ->get('/admin/ocr-records/create');
- $response->assertStatus(200);
- }
- /** @test */
- public function it_can_create_ocr_record()
- {
- $file = UploadedFile::fake()->image('exam.jpg', 800, 600);
- $response = $this->actingAs($this->adminUser)
- ->post('/admin/ocr-records', [
- 'student_id' => $this->student->student_id,
- 'image_path' => $file,
- 'status' => 'pending',
- 'total_questions' => 0,
- 'processed_questions' => 0,
- ]);
- $this->assertDatabaseHas('ocr_records', [
- 'student_id' => $this->student->student_id,
- 'status' => 'pending',
- ]);
- }
- /** @test */
- public function it_can_view_ocr_record()
- {
- $record = OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'image_path' => 'ocr-uploads/test.jpg',
- 'status' => 'completed',
- 'total_questions' => 10,
- 'processed_questions' => 10,
- 'confidence_avg' => 0.85,
- ]);
- $response = $this->actingAs($this->adminUser)
- ->get("/admin/ocr-records/{$record->id}");
- $response->assertStatus(200);
- }
- /** @test */
- public function it_shows_student_information_in_list()
- {
- OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'status' => 'pending',
- 'total_questions' => 10,
- ]);
- $response = $this->actingAs($this->adminUser)
- ->get('/admin/ocr-records');
- $response->assertStatus(200)
- ->assertSee($this->student->name)
- ->assertSee($this->student->grade)
- ->assertSee($this->student->class_name);
- }
- /** @test */
- public function it_requires_student_id()
- {
- $response = $this->actingAs($this->adminUser)
- ->post('/admin/ocr-records', [
- 'image_path' => UploadedFile::fake()->image('exam.jpg'),
- 'status' => 'pending',
- ]);
- $response->assertSessionHasErrors(['student_id']);
- }
- /** @test */
- public function it_requires_image_file()
- {
- $response = $this->actingAs($this->adminUser)
- ->post('/admin/ocr-records', [
- 'student_id' => $this->student->student_id,
- 'status' => 'pending',
- ]);
- $response->assertSessionHasErrors(['image_path']);
- }
- /** @test */
- public function it_calculates_progress_percentage_correctly()
- {
- $record = OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'status' => 'processing',
- 'total_questions' => 10,
- 'processed_questions' => 7,
- ]);
- $this->assertEquals(70, $record->progress_percentage);
- }
- /** @test */
- public function it_returns_zero_progress_when_no_questions()
- {
- $record = OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'status' => 'pending',
- 'total_questions' => 0,
- 'processed_questions' => 0,
- ]);
- $this->assertEquals(0, $record->progress_percentage);
- }
- /** @test */
- public function it_has_correct_student_relationship()
- {
- $record = OCRRecord::create([
- 'student_id' => $this->student->student_id,
- 'image_filename' => 'test.jpg',
- 'status' => 'pending',
- 'total_questions' => 10,
- ]);
- $this->assertInstanceOf(Student::class, $record->student);
- $this->assertEquals($this->student->student_id, $record->student->student_id);
- $this->assertEquals($this->student->name, $record->student->name);
- }
- }
|