130 lines
4.9 KiB
PHP
130 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use App\Console\Commands\AssignFinanceLedgerCompanyPlans;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class AssignFinanceLedgerCompanyPlansTest extends FeatureTestCase
|
|
{
|
|
public function test_assigns_company_plans_and_derived_keep_values(): void
|
|
{
|
|
$connection = DB::connection('business_mirror');
|
|
$connection->table('fin_accounts')->insert([
|
|
[
|
|
'company_name' => '保留公司有限公司',
|
|
'account_short' => '保留公司账户',
|
|
'account_type' => '公户',
|
|
'company_plan' => null,
|
|
'account_should_keep' => '否',
|
|
'updated_at' => '2026-11-01 09:00:00',
|
|
],
|
|
[
|
|
'company_name' => '暂时公司(个人独资)',
|
|
'account_short' => '暂时公司银行账户',
|
|
'account_type' => '公户',
|
|
'company_plan' => null,
|
|
'account_should_keep' => '是',
|
|
'updated_at' => '2026-11-01 09:01:00',
|
|
],
|
|
[
|
|
'company_name' => '保留公司有限公司',
|
|
'account_short' => '保留公司支付宝',
|
|
'account_type' => '支付宝',
|
|
'company_plan' => '待注销',
|
|
'account_should_keep' => '否',
|
|
'updated_at' => '2026-11-01 09:02:00',
|
|
],
|
|
[
|
|
'company_name' => '暂时公司(个人独资)',
|
|
'account_short' => '暂时公司网商',
|
|
'account_type' => '网商银行',
|
|
'company_plan' => '保留',
|
|
'account_should_keep' => '是',
|
|
'updated_at' => '2026-11-01 09:03:00',
|
|
],
|
|
]);
|
|
|
|
$path = $this->workbook([
|
|
['公司名称'],
|
|
['保留公司有限公司'],
|
|
]);
|
|
|
|
try {
|
|
$command = $this->app->make(AssignFinanceLedgerCompanyPlans::class);
|
|
$method = new \ReflectionMethod($command, 'companyNames');
|
|
$method->setAccessible(true);
|
|
$companyNames = $method->invoke($command, $path);
|
|
$this->assertArrayHasKey('保留公司有限公司', $companyNames);
|
|
|
|
$this->artisan('finance-ledger:assign-company-plans', [
|
|
'path' => $path,
|
|
'--dry-run' => true,
|
|
])->assertSuccessful();
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'company_name' => '保留公司有限公司',
|
|
'account_should_keep' => '否',
|
|
], 'business_mirror');
|
|
|
|
$this->artisan('finance-ledger:assign-company-plans', [
|
|
'path' => $path,
|
|
])->assertSuccessful();
|
|
} finally {
|
|
@unlink($path);
|
|
}
|
|
|
|
$this->assertSame([
|
|
'保留公司账户' => '保留/是',
|
|
'暂时公司银行账户' => '不保留/暂时保留',
|
|
'保留公司支付宝' => '待注销/否',
|
|
'暂时公司网商' => '保留/是',
|
|
], $connection->table('fin_accounts')
|
|
->orderBy('id')
|
|
->get(['company_name', 'account_short', 'company_plan', 'account_should_keep'])
|
|
->mapWithKeys(static fn ($row): array => [
|
|
(string) $row->account_short => $row->company_plan . '/' . $row->account_should_keep,
|
|
])
|
|
->all());
|
|
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'company_name' => '保留公司有限公司',
|
|
'company_plan' => '保留',
|
|
'account_should_keep' => '是',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'company_name' => '暂时公司(个人独资)',
|
|
'company_plan' => '不保留',
|
|
'account_should_keep' => '暂时保留',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'account_short' => '保留公司支付宝',
|
|
'company_plan' => '待注销',
|
|
'account_should_keep' => '否',
|
|
'updated_at' => '2026-11-01 09:02:00',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'account_short' => '暂时公司网商',
|
|
'company_plan' => '保留',
|
|
'account_should_keep' => '是',
|
|
'updated_at' => '2026-11-01 09:03:00',
|
|
], 'business_mirror');
|
|
}
|
|
|
|
/**
|
|
* @param list<list<string>> $rows
|
|
*/
|
|
private function workbook(array $rows): string
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->fromArray($rows, null, 'A1', true);
|
|
$path = sys_get_temp_dir() . '/company-plan-list-' . bin2hex(random_bytes(6)) . '.xlsx';
|
|
(new Xlsx($spreadsheet))->save($path);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
return $path;
|
|
}
|
|
}
|