150 lines
6.0 KiB
PHP
150 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\EcommerceFinance\Services\DingInvoiceLocalAttachmentImporter;
|
|
use Tests\TestCase;
|
|
|
|
class ImportDingInvoiceAttachmentsTest extends TestCase
|
|
{
|
|
private string $directory;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
'foreign_key_constraints' => true,
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
Storage::fake('uploads');
|
|
$this->createTables();
|
|
$this->directory = sys_get_temp_dir() . '/invoice-attachment-import-' . bin2hex(random_bytes(6));
|
|
mkdir($this->directory . '/flow-1', 0755, true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (isset($this->directory) && is_dir($this->directory)) {
|
|
foreach (glob($this->directory . '/flow-1/*') ?: [] as $file) {
|
|
unlink($file);
|
|
}
|
|
@rmdir($this->directory . '/flow-1');
|
|
@rmdir($this->directory);
|
|
}
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_imports_historical_screenshot_as_raw_attachment(): void
|
|
{
|
|
$image = base64_decode(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='
|
|
);
|
|
file_put_contents($this->directory . '/flow-1/payment.png', $image);
|
|
$manifestPath = $this->directory . '/manifest.json';
|
|
file_put_contents($manifestPath, json_encode([
|
|
[
|
|
'business_id' => 'FLOW-1',
|
|
'files' => [[
|
|
'relative_path' => 'flow-1/payment.png',
|
|
'file_name' => '付款截图.png',
|
|
'source_field' => '评论附件',
|
|
'file_id' => 'FILE-1',
|
|
]],
|
|
],
|
|
], JSON_UNESCAPED_UNICODE));
|
|
$now = now();
|
|
DB::connection('business_mirror')->table('ding_invoice_flow_mirror')->insert([
|
|
'source_scope' => 'personal_permission',
|
|
'business_id' => 'FLOW-1',
|
|
'process_instance_id' => 'PROCESS-1',
|
|
'paid_month' => '2026-10',
|
|
'payment_amount' => 100,
|
|
'payment_total' => 100,
|
|
'invoice_count' => 0,
|
|
'invoice_total' => 0,
|
|
'scan_status' => 'no_invoice',
|
|
'attachment_count' => 1,
|
|
'scanned_file_count' => 1,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
$result = app(DingInvoiceLocalAttachmentImporter::class)->import(
|
|
$manifestPath,
|
|
'2026-10',
|
|
$this->directory,
|
|
);
|
|
|
|
$this->assertSame(1, $result['stored']);
|
|
$row = DB::connection('business_mirror')
|
|
->table('ding_invoice_attachment_mirror')
|
|
->first();
|
|
$this->assertSame('FLOW-1', $row->business_id);
|
|
$this->assertSame('2026-10', $row->paid_month);
|
|
$this->assertSame('local_import', $row->source_origin);
|
|
$this->assertSame('pending', $row->ocr_status);
|
|
$this->assertSame('付款截图.png', $row->source_file_name);
|
|
$this->assertNotNull($row->preview_path);
|
|
$this->assertStringStartsWith('invoice-attachments/2026-10/', $row->preview_path);
|
|
Storage::disk('uploads')->assertExists($row->preview_path);
|
|
$this->assertStringStartsWith('invoice-attachment-sources/2026-10/', $row->raw_path);
|
|
Storage::disk('uploads')->assertExists($row->raw_path);
|
|
}
|
|
|
|
private function createTables(): void
|
|
{
|
|
$schema = Schema::connection('business_mirror');
|
|
$schema->create('ding_invoice_flow_mirror', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('source_scope');
|
|
$table->string('business_id');
|
|
$table->string('process_instance_id')->nullable();
|
|
$table->string('paid_month');
|
|
$table->decimal('payment_amount', 18, 2)->default(0);
|
|
$table->decimal('payment_total', 18, 2)->default(0);
|
|
$table->unsignedInteger('invoice_count')->default(0);
|
|
$table->decimal('invoice_total', 18, 2)->default(0);
|
|
$table->string('scan_status')->default('imported');
|
|
$table->unsignedInteger('attachment_count')->default(0);
|
|
$table->unsignedInteger('scanned_file_count')->default(0);
|
|
$table->timestamps();
|
|
});
|
|
$schema->create('ding_invoice_attachment_mirror', function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->string('source_scope');
|
|
$table->string('business_id');
|
|
$table->string('paid_month');
|
|
$table->string('attachment_key');
|
|
$table->string('process_instance_id')->nullable();
|
|
$table->string('source_file_id')->nullable();
|
|
$table->string('source_file_name');
|
|
$table->string('source_file_type')->nullable();
|
|
$table->string('source_field')->nullable();
|
|
$table->string('source_origin')->default('form');
|
|
$table->unsignedBigInteger('source_size')->default(0);
|
|
$table->string('source_hash')->nullable();
|
|
$table->string('source_path', 1024)->nullable();
|
|
$table->string('raw_path', 1024)->nullable();
|
|
$table->string('preview_path', 1024)->nullable();
|
|
$table->string('preview_mime')->nullable();
|
|
$table->string('ocr_status')->default('pending');
|
|
$table->string('ocr_provider')->nullable();
|
|
$table->unsignedInteger('invoice_count')->default(0);
|
|
$table->text('last_error')->nullable();
|
|
$table->dateTime('downloaded_at')->nullable();
|
|
$table->timestamps();
|
|
});
|
|
}
|
|
}
|