[ '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'); } }