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_matching_subject_period_and_company_plan_fallback(): 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' => '一般纳税人', 'company_status' => '已注销', 'plan' => '注销', 'registered_at' => '2025-01-01', ], [ 'company_name' => '主体乙有限公司', 'taxpayer_type' => '小规模纳税人', 'company_status' => '正常', 'plan' => '保留', '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); $current = $matcher->apply((object) [ 'store_name' => '店铺A', 'occurred_date' => '2026-10-15', ], $map); $fallback = $matcher->apply((object) [ 'store_name' => '店铺C', 'occurred_date' => '2026-11-05', ], $map); $this->assertSame('主体甲有限公司', $historical->company_name); $this->assertSame('一般纳税人', $historical->taxpayer_type); $this->assertSame('2026-07-01', $historical->subject_effective_at); $this->assertSame('2026-09-30', $historical->subject_expires_at); $this->assertSame('店铺维度报表', $historical->subject_match_source); $this->assertSame('主体乙有限公司', $current->company_name); $this->assertSame('小规模纳税人', $current->taxpayer_type); $this->assertSame('2026-10-01', $current->subject_effective_at); $this->assertNull($current->subject_expires_at); $this->assertSame('接替主体有限公司', $fallback->company_name); $this->assertSame('一般纳税人', $fallback->taxpayer_type); $this->assertSame('公司盘点接替主体', $fallback->subject_match_source); } }