['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '']]); DB::purge('business_mirror'); Schema::connection('business_mirror')->create('business_mirror_table_states', function (Blueprint $table): void { $table->string('dataset_key')->primary(); $table->dateTime('initial_completed_at')->nullable(); }); $registry = new BusinessMirrorDatasetRegistry(); foreach ([SupplierManagementMirrorRepository::DATASET, ...array_values(SupplierManagementMirrorRepository::RELATED)] as $key) { Schema::connection('business_mirror')->create($registry->get($key)->targetTable(), function (Blueprint $table) use ($registry, $key): void { foreach ($registry->columnSelection($key) as $column) { if (strtolower($column) === 'id') $table->integer($column)->primary(); else $table->string($column)->nullable(); } }); } $this->repository = new SupplierManagementMirrorRepository($registry, new GenericBusinessMirrorRepository(app('db'))); } public function test_filters_treat_wildcards_literally_and_pages_are_stable(): void { $this->ready(SupplierManagementMirrorRepository::DATASET); $this->table('js_supplier_management')->insert([ ['Id' => 1, 'scmname' => 'A%_供应商', 'buyer' => '采购甲', 'coopersgn' => 1], ['Id' => 2, 'scmname' => 'A%_供应商', 'buyer' => '采购甲', 'coopersgn' => 1], ['Id' => 3, 'scmname' => 'A普通供应商', 'buyer' => '采购乙', 'coopersgn' => 2], ]); $filters = ['supplier_name' => '%_', 'buyer' => '采购甲', 'cooperation_status' => '1']; $this->assertSame(2, $this->repository->paginate($filters, 1, 1)['total']); $this->assertSame(2, $this->repository->paginate($filters, 2, 1)['rows'][0]->Id); $this->assertSame([1, 2], array_map(fn ($row) => $row->Id, iterator_to_array($this->repository->exportRows($filters)))); } public function test_details_do_not_mix_suppliers_or_contract_signing_companies(): void { foreach (SupplierManagementMirrorRepository::RELATED as $key) $this->ready($key); $supplier = (object) ['scmname' => '甲供应商', 'tradebody' => '甲主体', 'company' => '签订公司甲']; $this->table('js_supplier_contracts')->insert([ ['id' => 1, 'tradebody' => '甲主体', 'company' => '签订公司甲', 'title' => '正确合同'], ['id' => 2, 'tradebody' => '甲主体', 'company' => '签订公司乙', 'title' => '其他公司合同'], ]); foreach (['js_supplier_contacts', 'js_supplier_payment_accounts'] as $table) { $this->table($table)->insert([['Id' => 1, 'scmname' => '甲供应商'], ['Id' => 2, 'scmname' => '乙供应商']]); } foreach (['contracts', 'contacts', 'payments'] as $tab) { $this->assertSame(1, $this->repository->details($supplier, $tab, 1, 20)['total']); } $supplier->company = ''; $this->assertSame(0, $this->repository->details($supplier, 'contracts', 1, 20)['total']); $supplier->scmname = ''; $this->assertSame(0, $this->repository->details($supplier, 'payments', 1, 20)['total']); $this->assertFalse($this->repository->details($supplier, 'logs', 1, 20)['ready']); } public function test_partial_initial_sync_is_not_presented_as_complete(): void { $this->table('js_supplier_management')->insert(['Id' => 1]); $this->assertFalse($this->repository->ready()); $this->expectException(\RuntimeException::class); $this->repository->paginate([], 1, 20); } private function ready(string $key): void { $this->table('business_mirror_table_states')->insert(['dataset_key' => $key, 'initial_completed_at' => '2026-10-11 00:00:00']); } private function table(string $name): \Illuminate\Database\Query\Builder { return DB::connection('business_mirror')->table($name); } }