CaiWuXiTong/tests/Unit/EcommerceFinance/PinduoduoPlatformBalanceImporterTest.php
qiuxiang 55f3e7d10f feat: 拼多多账户按平台余额滚动计算期初期末
以6月平台实际期末余额为基准,按财务系统每月收支滚动计算期初余额;期末余额=期初余额+收入合计-支出合计。
2026-11-06 11:37:23 +08:00

83 lines
3.1 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\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']);
});
}
protected function tearDown(): void
{
if ($this->file !== null && is_file($this->file)) {
unlink($this->file);
}
parent::tearDown();
}
public function test_it_imports_platform_actual_balances_as_the_month_end_anchor(): 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, ['拼多多', '拼多多货款账户', '拼多多-测试一店', '100.25']);
fputcsv($handle, ['拼多多', '拼多多货款账户', '拼多多-测试二店', '23.20']);
fputcsv($handle, ['京东', '京东钱包', '京东-不导入', '999.00']);
fclose($handle);
$result = app(PinduoduoPlatformBalanceImporter::class)->import($this->file, '2026-06-30');
$this->assertSame('2026-06-30', $result['period_end']);
$this->assertSame(2, $result['total']);
$this->assertSame('123.45', $result['balance_total']);
$this->assertSame(2, DB::connection('business_mirror')->table(PinduoduoPlatformBalanceImporter::TABLE)->count());
$this->assertEquals(
'100.25',
DB::connection('business_mirror')->table(PinduoduoPlatformBalanceImporter::TABLE)
->where('store_name', '拼多多-测试一店')->value('balance'),
);
}
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');
}
}