CaiWuXiTong/tests/Feature/Console/VipshopMpOpeningBalanceImporterTest.php

128 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Console;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Modules\EcommerceFinance\Services\VipshopMpOpeningBalanceImporter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Tests\TestCase;
class VipshopMpOpeningBalanceImporterTest extends TestCase
{
private array $files = [];
private array $directories = [];
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
(require database_path('migrations/2026_11_06_180000_create_vipshop_mp_opening_balance_snapshots.php'))->up();
(require database_path('migrations/2026_12_01_180000_add_settlement_to_vipshop_mp_opening_balance_snapshots.php'))->up();
}
protected function tearDown(): void
{
foreach ($this->files as $path) {
@unlink($path);
}
foreach ($this->directories as $directory) {
@rmdir($directory);
}
parent::tearDown();
}
public function test_it_sums_pending_and_actual_settlement_rows_by_backend_store_and_replaces_the_period(): void
{
$map = [
'亿家达家具特卖旗舰店' => '唯品会MP-亿家达家具旗舰店',
'朴美家具特卖旗舰店' => '唯品会MP-朴美家具特卖旗舰店',
];
$first = $this->xlsx('yijiada.xlsx', [
['ST1', '亿家达家具特卖旗舰店', '100.00', '40.00'],
['ST1', '亿家达家具特卖旗舰店', '20.00', '10.00'],
['ST1', '亿家达家具特卖旗舰店', '-5.50', '0.00'],
]);
$second = $this->xlsx('pumei.xlsx', [
['ST2', '朴美家具特卖旗舰店', '10.25', '5.00'],
]);
$result = app(VipshopMpOpeningBalanceImporter::class)->import(
[$first, $second],
'2026-07-31',
$map,
);
$this->assertSame('succeeded', $result['status']);
$this->assertSame(2, $result['store_count']);
$this->assertSame(4, $result['row_count']);
$this->assertSame('124.750000', $result['balance_total']);
$this->assertSame('55.000000', $result['settled_total']);
$rows = DB::connection('business_mirror')
->table(VipshopMpOpeningBalanceImporter::TABLE)
->orderBy('store_name')
->get(['store_name', 'balance', 'settled_amount', 'settled_row_count']);
$this->assertCount(2, $rows);
$this->assertSame('唯品会MP-亿家达家具旗舰店', $rows[0]->store_name);
$this->assertSame(114.5, (float) $rows[0]->balance);
$this->assertSame(50.0, (float) $rows[0]->settled_amount);
$this->assertSame(2, (int) $rows[0]->settled_row_count);
$this->assertSame('唯品会MP-朴美家具特卖旗舰店', $rows[1]->store_name);
$this->assertSame(10.25, (float) $rows[1]->balance);
$this->assertSame(5.0, (float) $rows[1]->settled_amount);
$this->assertSame(1, (int) $rows[1]->settled_row_count);
$again = app(VipshopMpOpeningBalanceImporter::class)->import(
[$first, $second],
'2026-07-31',
$map,
);
$this->assertSame('succeeded', $again['status']);
$this->assertSame(2, DB::connection('business_mirror')->table(VipshopMpOpeningBalanceImporter::TABLE)->count());
}
public function test_july_and_later_files_must_include_actual_settlement(): void
{
$path = $this->xlsx('missing-settlement.xlsx', [
['ST1', '亿家达家具特卖旗舰店', '100.00'],
], false);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('缺少字段:实际结算给商家');
app(VipshopMpOpeningBalanceImporter::class)->import(
[$path],
'2026-07-31',
['亿家达家具特卖旗舰店' => '唯品会MP-亿家达家具旗舰店'],
);
}
private function xlsx(string $name, array $rows, bool $includeSettlement = true): string
{
$spreadsheet = new Spreadsheet();
$headers = ['店铺ID', '店铺名称', '待结算待付款(F2=F-F1)'];
if ($includeSettlement) {
$headers[] = '实际结算给商家';
}
$spreadsheet->getActiveSheet()->fromArray(array_merge([$headers], $rows));
$directory = sys_get_temp_dir() . '/vipshop-opening-' . uniqid('', true);
mkdir($directory, 0700, true);
$this->directories[] = $directory;
$path = $directory . '/' . $name;
(new Xlsx($spreadsheet))->save($path);
$spreadsheet->disconnectWorksheets();
$this->files[] = $path;
return $path;
}
}