103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Console;
|
||
|
||
use Illuminate\Support\Facades\DB;
|
||
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();
|
||
}
|
||
|
||
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_settlement_rows_by_backend_store_and_replaces_the_period(): void
|
||
{
|
||
$map = [
|
||
'亿家达家具特卖旗舰店' => '唯品会MP-亿家达家具旗舰店',
|
||
'朴美家具特卖旗舰店' => '唯品会MP-朴美家具特卖旗舰店',
|
||
];
|
||
$first = $this->xlsx('yijiada.xlsx', [
|
||
['ST1', '亿家达家具特卖旗舰店', '100.00'],
|
||
['ST1', '亿家达家具特卖旗舰店', '20.00'],
|
||
['ST1', '亿家达家具特卖旗舰店', '-5.50'],
|
||
]);
|
||
$second = $this->xlsx('pumei.xlsx', [
|
||
['ST2', '朴美家具特卖旗舰店', '10.25'],
|
||
]);
|
||
|
||
$result = app(VipshopMpOpeningBalanceImporter::class)->import(
|
||
[$first, $second],
|
||
'2026-06-30',
|
||
$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']);
|
||
$rows = DB::connection('business_mirror')
|
||
->table(VipshopMpOpeningBalanceImporter::TABLE)
|
||
->orderBy('store_name')
|
||
->get(['store_name', 'balance']);
|
||
$this->assertCount(2, $rows);
|
||
$this->assertSame('唯品会MP-亿家达家具旗舰店', $rows[0]->store_name);
|
||
$this->assertSame(114.5, (float) $rows[0]->balance);
|
||
$this->assertSame('唯品会MP-朴美家具特卖旗舰店', $rows[1]->store_name);
|
||
$this->assertSame(10.25, (float) $rows[1]->balance);
|
||
|
||
$again = app(VipshopMpOpeningBalanceImporter::class)->import(
|
||
[$first, $second],
|
||
'2026-06-30',
|
||
$map,
|
||
);
|
||
$this->assertSame('succeeded', $again['status']);
|
||
$this->assertSame(2, DB::connection('business_mirror')->table(VipshopMpOpeningBalanceImporter::TABLE)->count());
|
||
}
|
||
|
||
private function xlsx(string $name, array $rows): string
|
||
{
|
||
$spreadsheet = new Spreadsheet();
|
||
$spreadsheet->getActiveSheet()->fromArray(array_merge([[
|
||
'店铺ID', '店铺名称', '待结算待付款(F2=F-F1)',
|
||
]], $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;
|
||
}
|
||
}
|