195 lines
7.2 KiB
PHP
195 lines
7.2 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\DingPaymentAccountMatcher;
|
|
use Modules\EcommerceFinance\Services\FinanceLedgerRptSynchronizer;
|
|
use Modules\EcommerceFinance\Services\FinanceLedgerTransactionWriter;
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
|
use ReflectionMethod;
|
|
use Tests\TestCase;
|
|
|
|
class FinanceLedgerRptSynchronizerDingTalkTest extends TestCase
|
|
{
|
|
private FakeFinanceLedgerRptQuery $query;
|
|
|
|
private FinanceLedgerRptSynchronizer $synchronizer;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
'finance_ledger.dingtalk_process_codes' => [
|
|
'PROC-B290696F-EC2A-4F76-87BA-E0A12E10D09F' => '日常费用报销流程',
|
|
],
|
|
]);
|
|
DB::purge('business_mirror');
|
|
|
|
Schema::connection('business_mirror')->create('fin_accounts', function (Blueprint $table): void {
|
|
$table->string('account_short');
|
|
$table->string('company_name')->nullable();
|
|
$table->string('account_type')->nullable();
|
|
$table->string('bank_name')->nullable();
|
|
});
|
|
Schema::connection('business_mirror')->create('fin_account_map', function (Blueprint $table): void {
|
|
$table->string('source');
|
|
$table->string('source_value');
|
|
$table->string('account_short')->nullable();
|
|
});
|
|
Schema::connection('business_mirror')->create('fin_transactions', function (Blueprint $table): void {
|
|
$table->string('source');
|
|
$table->string('account_short')->nullable();
|
|
});
|
|
DB::connection('business_mirror')->table('fin_accounts')->insert([
|
|
'account_short' => '锋黛招行',
|
|
'company_name' => '常州锋黛家居有限公司',
|
|
'account_type' => '公户',
|
|
'bank_name' => '招商银行常州新北支行',
|
|
]);
|
|
|
|
$this->query = new FakeFinanceLedgerRptQuery();
|
|
$factory = new class($this->query) extends UpstreamReadOnlyQueryFactory {
|
|
public function __construct(private readonly UpstreamReadOnlyQuery $query) {}
|
|
|
|
public function forSource(string $source): UpstreamReadOnlyQuery
|
|
{
|
|
return $this->query;
|
|
}
|
|
|
|
public function forSourceWithTimeout(string $source, int $timeoutSeconds): UpstreamReadOnlyQuery
|
|
{
|
|
return $this->query;
|
|
}
|
|
};
|
|
$this->synchronizer = new FinanceLedgerRptSynchronizer(
|
|
$factory,
|
|
app(FinanceLedgerTransactionWriter::class),
|
|
app(DingPaymentAccountMatcher::class),
|
|
);
|
|
}
|
|
|
|
public function test_running_workflow_with_complete_payment_details_enters_ledger(): void
|
|
{
|
|
$this->query->workflows = [[
|
|
'processInstanceId' => 'running-paid-process',
|
|
'processCode' => 'PROC-B290696F-EC2A-4F76-87BA-E0A12E10D09F',
|
|
'businessId' => 'APPROVAL-RUNNING-1',
|
|
'title' => '山时提交的日常报销',
|
|
'status' => 'RUNNING',
|
|
'originatorUserId' => 'user-shan',
|
|
'originatorDeptName' => '财务部',
|
|
'joinAt' => '2026-11-10 09:00:00',
|
|
'finishTime' => null,
|
|
]];
|
|
$this->query->schemes = [[
|
|
'id' => 1,
|
|
'processInstanceId' => 'running-paid-process',
|
|
'name' => '付款明细',
|
|
'value' => json_encode([[
|
|
'rowValue' => [
|
|
['label' => '对应序号', 'value' => '1'],
|
|
['label' => '付款金额', 'value' => '100.00'],
|
|
['label' => '科目编码', 'value' => '1002.01.647'],
|
|
['label' => '科目名称', 'value' => '招商银行'],
|
|
['label' => '账户简称', 'value' => '锋黛招行'],
|
|
['label' => '打款时间', 'value' => '2026-11-10'],
|
|
],
|
|
]], JSON_UNESCAPED_UNICODE),
|
|
]];
|
|
$this->query->employees = [
|
|
['userid' => 'user-shan', 'name' => '山时'],
|
|
];
|
|
|
|
$rows = $this->dingTalkRows();
|
|
|
|
$this->assertCount(1, $rows);
|
|
$this->assertSame('2026-11-10', $rows[0]['txn_date']);
|
|
$this->assertSame(100.0, $rows[0]['expense']);
|
|
$this->assertSame('锋黛招行', $rows[0]['account_short']);
|
|
$this->assertSame('APPROVAL-RUNNING-1', $rows[0]['approval_no']);
|
|
}
|
|
|
|
public function test_running_workflow_without_payment_date_is_not_posted(): void
|
|
{
|
|
$this->query->workflows = [[
|
|
'processInstanceId' => 'running-unpaid-process',
|
|
'processCode' => 'PROC-B290696F-EC2A-4F76-87BA-E0A12E10D09F',
|
|
'businessId' => 'APPROVAL-RUNNING-2',
|
|
'title' => '山时提交的未付款报销',
|
|
'status' => 'RUNNING',
|
|
'originatorUserId' => 'user-shan',
|
|
'originatorDeptName' => '财务部',
|
|
'joinAt' => '2026-11-11 09:00:00',
|
|
'finishTime' => null,
|
|
]];
|
|
$this->query->schemes = [[
|
|
'id' => 2,
|
|
'processInstanceId' => 'running-unpaid-process',
|
|
'name' => '付款明细',
|
|
'value' => json_encode([[
|
|
'rowValue' => [
|
|
['label' => '对应序号', 'value' => '1'],
|
|
['label' => '付款金额', 'value' => '100.00'],
|
|
['label' => '科目编码', 'value' => '1002.01.647'],
|
|
['label' => '科目名称', 'value' => '招商银行'],
|
|
['label' => '账户简称', 'value' => '锋黛招行'],
|
|
['label' => '打款时间', 'value' => ''],
|
|
],
|
|
]], JSON_UNESCAPED_UNICODE),
|
|
]];
|
|
$this->query->employees = [
|
|
['userid' => 'user-shan', 'name' => '山时'],
|
|
];
|
|
|
|
$this->assertSame([], $this->dingTalkRows());
|
|
}
|
|
|
|
private function dingTalkRows(): array
|
|
{
|
|
$method = new ReflectionMethod($this->synchronizer, 'dingTalkRows');
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invoke($this->synchronizer, '2026-11-01', '2026-11-16');
|
|
}
|
|
}
|
|
|
|
class FakeFinanceLedgerRptQuery extends UpstreamReadOnlyQuery
|
|
{
|
|
public array $workflows = [];
|
|
|
|
public array $schemes = [];
|
|
|
|
public array $employees = [];
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct('operations_replica');
|
|
}
|
|
|
|
public function select(string $sql, array $bindings = []): array
|
|
{
|
|
if (str_contains($sql, 'rpt.fin_ledger_dingtalk_workflow')) {
|
|
return $this->workflows;
|
|
}
|
|
if (str_contains($sql, 'rpt.fin_ledger_dingtalk_scheme')) {
|
|
return $this->schemes;
|
|
}
|
|
if (str_contains($sql, 'rpt.hr_employee')) {
|
|
return $this->employees;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|