CaiWuXiTong/tests/Unit/EcommerceFinance/DingPaymentAccountMatcherTest.php
shanshi a7b3e051b5 feat: 接入钉钉运行中付款明细 (#1209)
运行中审批完成付款填写后进入动账明细,按账户简称、科目编码和关键字匹配账户。
2026-11-16 18:51:19 +08:00

124 lines
4.7 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 Tests\TestCase;
class DingPaymentAccountMatcherTest extends TestCase
{
private DingPaymentAccountMatcher $matcher;
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
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();
});
$this->matcher = app(DingPaymentAccountMatcher::class);
}
public function test_exact_account_short_and_learned_code_win_before_keyword_matching(): void
{
$this->account('锋黛招行', '常州锋黛家居有限公司', '招商银行常州新北支行');
$this->account('石洪明华夏', '石洪明', '华夏银行常州分行营业部');
$this->assertSame(
'锋黛招行',
$this->matcher->resolve('招商银行', '1002.01.647', '锋黛招行'),
);
$this->matcher->remember('1002.01.647', '锋黛招行');
$this->assertSame(
'锋黛招行',
$this->matcher->resolve('招商银行', '1002.01.647', ''),
);
}
public function test_saved_alias_and_unique_bank_company_keyword_are_used(): void
{
$this->account('石洪明华夏', '石洪明', '华夏银行常州分行营业部');
$this->account('欧栗中行', '常州欧栗家居有限公司', '中国银行常州经济开发区支行');
DB::connection('business_mirror')->table('fin_account_map')->insert([
'source' => 'dingtalk',
'source_value' => '华夏银行-石洪明',
'account_short' => '石洪明华夏',
]);
$this->assertSame(
'石洪明华夏',
$this->matcher->resolve('华夏银行-石洪明', '', ''),
);
$this->assertSame(
'欧栗中行',
$this->matcher->resolve('中国银行-欧栗', '', ''),
);
}
public function test_dominant_ordinary_account_resolves_same_bank_candidates(): void
{
$this->account('欧伯娜中行0884', '常州欧伯娜家居有限公司', '中国银行常州经济开发区支行');
$this->account('欧伯娜中行3233', '常州欧伯娜家居有限公司', '中国银行常州崔桥支行');
foreach (range(1, 6) as $index) {
DB::connection('business_mirror')->table('fin_transactions')->insert([
'source' => 'dingtalk',
'account_short' => '欧伯娜中行0884',
]);
}
DB::connection('business_mirror')->table('fin_transactions')->insert([
'source' => 'dingtalk',
'account_short' => '欧伯娜中行3233',
]);
$this->assertSame(
'欧伯娜中行0884',
$this->matcher->resolve('中国银行-欧伯娜', '', ''),
);
}
public function test_ambiguous_account_without_clear_usage_stays_unmatched(): void
{
$this->account('欧伯娜中行0884', '常州欧伯娜家居有限公司', '中国银行常州经济开发区支行');
$this->account('欧伯娜中行3233', '常州欧伯娜家居有限公司', '中国银行常州崔桥支行');
$this->assertSame('', $this->matcher->resolve('中国银行-欧伯娜', '', ''));
}
private function account(string $short, string $company, string $bank): void
{
DB::connection('business_mirror')->table('fin_accounts')->insert([
'account_short' => $short,
'company_name' => $company,
'account_type' => '公户',
'bank_name' => $bank,
]);
}
}