[ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ], 'upstream_mirrors.connection' => 'business_mirror', ]); DB::purge('business_mirror'); $schema = Schema::connection('business_mirror'); $schema->create('fin_accounts', function (Blueprint $table): void { $table->increments('id'); $table->string('account_short'); $table->string('account_no')->nullable(); }); $schema->create('fin_account_opening', function (Blueprint $table): void { $table->increments('id'); $table->string('account_short'); $table->date('opening_date'); $table->decimal('opening_balance', 18, 2); $table->string('remark')->nullable(); $table->timestamps(); }); $schema->create('fin_transactions', function (Blueprint $table): void { $table->increments('id'); $table->date('txn_date'); $table->string('source'); $table->string('source_value')->nullable(); $table->decimal('income', 18, 2)->default(0); $table->decimal('expense', 18, 2)->default(0); $table->decimal('balance', 18, 2)->nullable(); }); $schema->create('fin_bank_direct_bills', function (Blueprint $table): void { $table->increments('id'); $table->string('account_no'); $table->date('txn_date'); $table->timestamp('txn_at')->nullable(); $table->decimal('income', 18, 2)->default(0); $table->decimal('expense', 18, 2)->default(0); $table->decimal('balance', 18, 2)->nullable(); }); $schema->create('fin_bank_direct_balance_snapshots', function (Blueprint $table): void { $table->increments('id'); $table->string('account_no'); $table->date('balance_date'); $table->timestamp('balance_at')->nullable(); $table->decimal('account_balance', 18, 2)->nullable(); $table->decimal('available_balance', 18, 2); }); $db = DB::connection('business_mirror'); $db->table('fin_accounts')->insert([ ['account_short' => '有银行余额账户', 'account_no' => '6200000000000001'], ['account_short' => '无依据账户', 'account_no' => '6200000000000002'], ]); $db->table('fin_transactions')->insert([ [ 'txn_date' => '2026-10-31', 'source' => 'bank_statement', 'source_value' => '6200000000000001', 'income' => 0, 'expense' => 85000, 'balance' => 412.44, ], [ 'txn_date' => '2026-11-01', 'source' => 'bank_statement', 'source_value' => '6200000000000002', 'income' => 100, 'expense' => 0, 'balance' => 110, ], ]); } protected function tearDown(): void { DB::purge('business_mirror'); parent::tearDown(); } public function test_it_locks_the_previous_month_last_real_balance(): void { $this->artisan('finance-ledger:lock-monthly-opening', [ 'date' => '2026-11-01', '--apply' => true, ])->expectsOutputToContain('新增:1') ->expectsOutputToContain('无真实余额 1') ->assertSuccessful(); $this->assertDatabaseHas('fin_account_opening', [ 'account_short' => '有银行余额账户', 'opening_date' => '2026-11-01', 'opening_balance' => 412.44, ], 'business_mirror'); $this->assertDatabaseMissing('fin_account_opening', [ 'account_short' => '无依据账户', 'opening_date' => '2026-11-01', ], 'business_mirror'); } public function test_it_preserves_an_existing_manual_monthly_opening(): void { DB::connection('business_mirror')->table('fin_account_opening')->insert([ 'account_short' => '有银行余额账户', 'opening_date' => '2026-11-01', 'opening_balance' => 399.99, 'remark' => '手工导入期初', 'created_at' => '2026-11-01 08:00:00', 'updated_at' => '2026-11-01 08:00:00', ]); $this->artisan('finance-ledger:lock-monthly-opening', [ 'date' => '2026-11-01', '--apply' => true, ])->expectsOutputToContain('保留手工期初:1') ->assertSuccessful(); $opening = DB::connection('business_mirror') ->table('fin_account_opening') ->where('account_short', '有银行余额账户') ->where('opening_date', '2026-11-01') ->first(); $this->assertSame('399.99', (string) $opening->opening_balance); $this->assertSame('手工导入期初', $opening->remark); $this->assertSame('2026-11-01 08:00:00', (string) $opening->updated_at); } }