CaiWuXiTong/tests/Unit/EcommerceFinance/PinduoduoPlatformBalanceImporterTest.php
qiuxiang 6e092547c0 feat(pdd-balance): automate store-month opening balances
Maintain store-month openings, pending tasks, and balance cache invalidation.
2026-11-23 18:58:16 +08:00

108 lines
4.5 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Services\PinduoduoOpeningBalanceService;
use Modules\EcommerceFinance\Services\PinduoduoPlatformBalanceImporter;
use Tests\TestCase;
class PinduoduoPlatformBalanceImporterTest extends TestCase
{
private ?string $file = null;
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');
Schema::connection('business_mirror')->create(PinduoduoPlatformBalanceImporter::TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->date('period_end');
$table->string('store_name');
$table->string('normalized_store_name');
$table->string('account_name')->nullable();
$table->decimal('balance', 20, 6);
$table->string('source_filename');
$table->char('source_sha256', 64);
$table->dateTime('imported_at');
$table->timestamps();
$table->unique(['period_end', 'normalized_store_name']);
});
Schema::connection('business_mirror')->create(PinduoduoOpeningBalanceService::OPENING_TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->date('opening_month');
$table->string('store_name');
$table->string('normalized_store_name');
$table->decimal('opening_balance', 20, 6);
$table->string('source_type', 40);
$table->string('source_filename')->nullable();
$table->char('source_sha256', 64)->nullable();
$table->dateTime('imported_at');
$table->timestamps();
$table->unique(['opening_month', 'normalized_store_name']);
});
}
protected function tearDown(): void
{
if ($this->file !== null && is_file($this->file)) {
unlink($this->file);
}
parent::tearDown();
}
public function test_it_imports_platform_month_openings_and_actual_month_end_balances(): void
{
$this->file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pdd-balance-' . bin2hex(random_bytes(6)) . '.csv';
$handle = fopen($this->file, 'wb');
fputcsv($handle, ['平台', '来源', '店铺', '期初余额', '平台实际期末余额']);
fputcsv($handle, ['拼多多', '拼多多货款账户', '拼多多-测试一店', '12.34', '100.25']);
fputcsv($handle, ['拼多多', '拼多多货款账户', '拼多多-测试二店', '0', '23.20']);
fputcsv($handle, ['拼多多', '拼多多货款账户', '拼多多-无期初店', '-', '1.00']);
fputcsv($handle, ['京东', '京东钱包', '京东-不导入', '9.00', '999.00']);
fclose($handle);
$result = app(PinduoduoPlatformBalanceImporter::class)->import($this->file, '2026-08-31');
$this->assertSame('2026-08-31', $result['period_end']);
$this->assertSame(3, $result['total']);
$this->assertSame('124.45', $result['balance_total']);
$this->assertSame(2, $result['opening_total']);
$this->assertSame('12.34', $result['opening_balance_total']);
$this->assertSame(3, DB::connection('business_mirror')->table(PinduoduoPlatformBalanceImporter::TABLE)->count());
$this->assertEquals(
'100.25',
DB::connection('business_mirror')->table(PinduoduoPlatformBalanceImporter::TABLE)
->where('store_name', '拼多多-测试一店')->value('balance'),
);
$this->assertDatabaseHas(PinduoduoOpeningBalanceService::OPENING_TABLE, [
'opening_month' => '2026-08-01',
'store_name' => '拼多多-测试一店',
'opening_balance' => 12.34,
], 'business_mirror');
$this->assertDatabaseMissing(PinduoduoOpeningBalanceService::OPENING_TABLE, [
'store_name' => '拼多多-无期初店',
], 'business_mirror');
}
public function test_period_end_must_be_the_last_day_of_the_month(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('有效月末');
app(PinduoduoPlatformBalanceImporter::class)->periodEnd('2026-06-29');
}
}