CaiWuXiTong/tests/Feature/Business/FinanceLedgerAccountShortRenameTest.php

211 lines
8.5 KiB
PHP

<?php
namespace Tests\Feature\Business;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Modules\EcommerceFinance\Services\FinanceLedgerAccountEditor;
use Tests\Feature\FeatureTestCase;
class FinanceLedgerAccountShortRenameTest extends FeatureTestCase
{
public function test_bulk_rename_updates_bank_bills_balance_adjustments_and_snapshots(): void
{
$connection = DB::connection('business_mirror');
$oldShort = '批量简称测试' . strtoupper(substr(md5(uniqid('', true)), 0, 8));
$newShort = '华夏银行-批量简称' . strtoupper(substr(md5(uniqid('', true)), 0, 6));
$accountNo = '1105' . random_int(1000000000, 9999999999);
$accountId = (int) $connection->table('fin_accounts')->insertGetId([
'company_name' => '批量简称测试公司',
'account_short' => $oldShort,
'account_type' => '公户',
'account_no' => $accountNo,
'bank_name' => '华夏银行常州分行营业部',
'current_balance' => 100,
]);
$now = now();
$connection->table('fin_bank_direct_bills')->insert([
'bank_name' => '华夏银行',
'finance_account_id' => $accountId,
'company_name' => '批量简称测试公司',
'account_short' => $oldShort,
'account_no' => $accountNo,
'txn_date' => '2026-11-01',
'txn_at' => '2026-11-01 10:00:00',
'record_code' => 'BULK-SHORT-001',
'income' => 0,
'expense' => 10,
'balance' => 90,
'currency' => '人民币',
'source' => 'bank-monthly',
'row_key' => hash('sha256', 'bulk-short-bill-'.$accountId),
'imported_at' => $now,
'created_at' => $now,
'updated_at' => $now,
]);
$connection->table('fin_bank_balance_current_adjustments')->insert([
'account_short' => $oldShort,
'adjustment_amount' => 12.50,
'created_at' => $now,
'updated_at' => $now,
]);
$connection->table('fin_bank_direct_balance_snapshots')->insert([
'finance_account_id' => $accountId,
'bank_name' => '华夏银行',
'account_no' => $accountNo,
'account_short' => $oldShort,
'company_name' => '批量简称测试公司',
'account_balance' => 90,
'available_balance' => 90,
'balance_date' => '2026-11-01',
'balance_at' => '2026-11-01 10:00:00',
'source' => 'bank-direct',
'source_file_name' => 'bulk-short.xlsx',
'source_sha256' => hash('sha256', 'bulk-short-snapshot'),
'imported_at' => $now,
'created_at' => $now,
'updated_at' => $now,
]);
$connection->table('fin_transaction_custom_fields')->insert([
'account_short' => $oldShort,
'field_key' => (string) str()->uuid(),
'field_name' => '核对说明',
'sort_order' => 10,
'created_at' => $now,
'updated_at' => $now,
]);
$result = app(FinanceLedgerAccountEditor::class)->renameShorts([[
'id' => $accountId,
'old_short' => $oldShort,
'new_short' => $newShort,
'account_no' => $accountNo,
'company_name' => '批量简称测试公司',
]], true);
$this->assertSame(1, $result['changed']);
$this->assertFalse($result['dry_run']);
foreach ([
'fin_accounts',
'fin_bank_direct_bills',
'fin_bank_balance_current_adjustments',
'fin_bank_direct_balance_snapshots',
'fin_transaction_custom_fields',
] as $table) {
$this->assertDatabaseMissing($table, ['account_short' => $oldShort], 'business_mirror');
$this->assertDatabaseHas($table, ['account_short' => $newShort], 'business_mirror');
}
$this->assertDatabaseHas('fin_ledger_edit_events', [
'entity_type' => 'account_short_rename',
'entity_key' => (string) $accountId,
], 'business_mirror');
}
public function test_bulk_rename_rejects_an_old_short_shared_by_multiple_accounts(): void
{
$connection = DB::connection('business_mirror');
$oldShort = '重复旧简称' . strtoupper(substr(md5(uniqid('', true)), 0, 8));
$firstId = (int) $connection->table('fin_accounts')->insertGetId([
'company_name' => '重复简称甲',
'account_short' => $oldShort,
'account_type' => '网商银行',
'account_no' => '8888' . random_int(1000000000, 9999999999),
'bank_name' => '浙江网商银行',
]);
$connection->table('fin_accounts')->insert([
'company_name' => '重复简称乙',
'account_short' => $oldShort,
'account_type' => '网商银行',
'account_no' => '8888' . random_int(1000000000, 9999999999),
'bank_name' => '浙江网商银行',
]);
$this->expectException(ValidationException::class);
app(FinanceLedgerAccountEditor::class)->renameShorts([[
'id' => $firstId,
'old_short' => $oldShort,
'new_short' => '网商银行-重复简称1',
]], true);
}
public function test_bulk_rename_can_rename_all_accounts_sharing_the_same_old_short(): void
{
$connection = DB::connection('business_mirror');
$oldShort = '共享旧简称' . strtoupper(substr(md5(uniqid('', true)), 0, 8));
$newShort = '网商银行-共享公司';
$ids = [];
foreach (['共享公司甲', '共享公司乙'] as $suffix) {
$ids[] = (int) $connection->table('fin_accounts')->insertGetId([
'company_name' => '共享公司',
'account_short' => $oldShort,
'account_type' => '网商银行',
'account_no' => '8888' . random_int(1000000000, 9999999999),
'bank_name' => '浙江网商银行',
'remark' => $suffix,
]);
}
$result = app(FinanceLedgerAccountEditor::class)->renameShorts(
array_map(static fn (int $id): array => [
'id' => $id,
'old_short' => $oldShort,
'new_short' => $newShort,
], $ids),
true,
);
$this->assertSame(2, $result['changed']);
$this->assertSame(2, $connection->table('fin_accounts')
->whereIn('id', $ids)
->where('account_short', $newShort)
->count());
$this->assertSame(0, $connection->table('fin_accounts')
->where('account_short', $oldShort)
->count());
}
public function test_bulk_rename_command_defaults_to_dry_run_and_requires_apply(): void
{
$connection = DB::connection('business_mirror');
$oldShort = '命令旧简称' . strtoupper(substr(md5(uniqid('', true)), 0, 8));
$newShort = '农业银行-命令测试';
$accountNo = '1035' . random_int(1000000000, 9999999999);
$id = (int) $connection->table('fin_accounts')->insertGetId([
'company_name' => '命令测试公司',
'account_short' => $oldShort,
'account_type' => '公户',
'account_no' => $accountNo,
'bank_name' => '中国农业银行',
]);
$path = sys_get_temp_dir() . '/account-short-rename-' . bin2hex(random_bytes(6)) . '.json';
file_put_contents($path, json_encode([[
'id' => $id,
'old_short' => $oldShort,
'new_short' => $newShort,
'account_no' => $accountNo,
'company_name' => '命令测试公司',
]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
try {
$this->artisan('finance-ledger:import-account-short-renames', ['path' => $path])
->assertExitCode(0);
$this->assertDatabaseHas('fin_accounts', [
'id' => $id,
'account_short' => $oldShort,
], 'business_mirror');
$this->artisan('finance-ledger:import-account-short-renames', [
'path' => $path,
'--apply' => true,
])->assertExitCode(0);
$this->assertDatabaseHas('fin_accounts', [
'id' => $id,
'account_short' => $newShort,
], 'business_mirror');
} finally {
@unlink($path);
}
}
}