CaiWuXiTong/tests/Unit/EcommerceFinance/PerformanceDetailSubjectMatcherTest.php

131 lines
5.1 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use App\Business\CompanyPlan\CompanyPlanRepository;
use App\Business\CompanyPlan\CompanyPlanShopRepository;
use Mockery;
use Modules\EcommerceFinance\Services\PerformanceDetailSubjectMatcher;
use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder;
use Tests\TestCase;
class PerformanceDetailSubjectMatcherTest extends TestCase
{
private string $databasePath;
protected function setUp(): void
{
parent::setUp();
$this->databasePath = sys_get_temp_dir() . '/caiwu-performance-subjects-' . bin2hex(random_bytes(6)) . '.sqlite';
config(['company_plan.database_path' => $this->databasePath]);
}
protected function tearDown(): void
{
Mockery::close();
foreach ([$this->databasePath, $this->databasePath . '-wal', $this->databasePath . '-shm'] as $path) {
if (is_file($path)) {
unlink($path);
}
}
parent::tearDown();
}
public function test_it_uses_the_subject_period_matching_the_business_date(): void
{
$periods = Mockery::mock(StoreReportPeriodBuilder::class);
$periods->shouldReceive('build')->once()->andReturn([
'店铺A' => [
[
'company_name' => '主体甲有限公司',
'effective_at' => '2026-07-01',
'expires_at' => '2026-09-30',
'source' => 'initial',
],
[
'company_name' => '主体乙有限公司',
'effective_at' => '2026-10-01',
'expires_at' => null,
'source' => 'initial',
],
],
]);
$plans = app(CompanyPlanRepository::class);
$shops = app(CompanyPlanShopRepository::class);
$plans->importRows(1, [
[
'company_name' => '主体甲有限公司',
'taxpayer_type' => '一般纳税人',
'registered_at' => '2025-01-01',
],
[
'company_name' => '主体乙有限公司',
'taxpayer_type' => '小规模纳税人',
'registered_at' => '2026-09-20',
],
[
'company_name' => '历史主体有限公司',
'taxpayer_type' => '小规模纳税人',
'company_status' => '正常',
'plan' => '注销',
'registered_at' => '2025-01-01',
],
[
'company_name' => '接替主体有限公司',
'taxpayer_type' => '一般纳税人',
'company_status' => '正常',
'plan' => '保留',
'registered_at' => '2026-09-01',
],
], 'update', null);
$shops->pdo()->exec(
"INSERT INTO company_plan_shops "
. "(company_id, company_name, shop_name, shop_status, successor, created_at, updated_at) VALUES "
. "(1, '历史主体有限公司', '店铺C', '正常', '接替主体有限公司', '2026-01-01', '2026-01-01')"
);
$matcher = new PerformanceDetailSubjectMatcher($periods, $plans, $shops);
$map = $matcher->map(['店铺A', '店铺C'], 1);
$historical = $matcher->apply((object) [
'store_name' => '店铺A',
'occurred_date' => '2026-09-15',
], $map);
$historicalAtExpiry = $matcher->apply((object) [
'store_name' => '店铺A',
'occurred_date' => '2026-09-30',
], $map);
$currentAtEffective = $matcher->apply((object) [
'store_name' => '店铺A',
'occurred_date' => '2026-10-01',
], $map);
$fallback = $matcher->apply((object) [
'store_name' => '店铺C',
'occurred_date' => '2026-11-05',
], $map);
$currentSmallPayment = $matcher->applyCurrent((object) [
'shop_name' => '店铺A',
'completed_at' => '2026-09-15 12:00:00',
], $map);
$this->assertSame('主体甲有限公司', $historical->company_name);
$this->assertSame('一般纳税人', $historical->taxpayer_type);
$this->assertSame('2026-07-01', $historical->company_effective_at);
$this->assertSame('2026-09-30', $historical->company_expires_at);
$this->assertSame('店铺维度报表', $historical->subject_match_source);
$this->assertSame('主体甲有限公司', $historicalAtExpiry->company_name);
$this->assertSame('主体乙有限公司', $currentAtEffective->company_name);
$this->assertSame('小规模纳税人', $currentAtEffective->taxpayer_type);
$this->assertNull($currentAtEffective->company_expires_at);
$this->assertSame('接替主体有限公司', $fallback->company_name);
$this->assertSame('一般纳税人', $fallback->taxpayer_type);
$this->assertSame('公司盘点接替主体', $fallback->subject_match_source);
$this->assertSame('主体乙有限公司', $currentSmallPayment->company_name);
$this->assertSame('小规模纳税人', $currentSmallPayment->taxpayer_type);
}
}