92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\User;
|
|
use App\Services\DocumentPdfPreviewService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
|
|
class DocumentPreviewServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected DocumentPdfPreviewService $previewService;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->previewService = new DocumentPdfPreviewService();
|
|
Storage::fake('local');
|
|
Storage::fake('previews');
|
|
config(['scout.driver' => 'null']);
|
|
}
|
|
|
|
public function test_可以检查文档是否支持_pdf_预览(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$document = Document::factory()->create([
|
|
'conversion_status' => 'completed',
|
|
'file_path' => 'documents/test.pdf',
|
|
'file_name' => 'test.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'uploaded_by' => $user->id,
|
|
]);
|
|
|
|
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
|
|
|
|
$this->assertTrue($this->previewService->canPreview($document));
|
|
|
|
$document2 = Document::factory()->create([
|
|
'conversion_status' => 'pending',
|
|
'file_path' => 'documents/pending.pdf',
|
|
'file_name' => 'pending.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'uploaded_by' => $user->id,
|
|
]);
|
|
|
|
Storage::disk('local')->put($document2->file_path, '%PDF-1.4 test');
|
|
|
|
$this->assertFalse($this->previewService->canPreview($document2));
|
|
}
|
|
|
|
public function test_文档不存在时抛出异常(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$document = Document::factory()->create([
|
|
'conversion_status' => 'completed',
|
|
'file_path' => 'documents/2024/01/01/nonexistent.pdf',
|
|
'file_name' => 'nonexistent.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'uploaded_by' => $user->id,
|
|
]);
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
$this->expectExceptionMessage('文档尚未完成转换或原文件不存在');
|
|
|
|
$this->previewService->getPreviewPath($document);
|
|
}
|
|
|
|
public function test_非_pdf_文件在缺少_libreoffice_时给出明确错误(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$document = Document::factory()->create([
|
|
'conversion_status' => 'completed',
|
|
'file_path' => 'documents/test.docx',
|
|
'file_name' => 'test.docx',
|
|
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'uploaded_by' => $user->id,
|
|
]);
|
|
|
|
Storage::disk('local')->put($document->file_path, 'fake-docx-content');
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
$this->expectExceptionMessage('LibreOffice');
|
|
|
|
$this->previewService->getPreviewPath($document);
|
|
}
|
|
}
|