|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\BiogMain; |
| 6 | +use App\Repositories\BiogMainRepository; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Pagination\LengthAwarePaginator; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use Illuminate\Support\Facades\Schema; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class BiogMainNameSearchTest extends TestCase |
| 14 | +{ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + // 設定使用 SQLite in-memory 資料庫 |
| 20 | + config()->set('database.default', 'sqlite'); |
| 21 | + config()->set('database.connections.sqlite', [ |
| 22 | + 'driver' => 'sqlite', |
| 23 | + 'database' => ':memory:', |
| 24 | + 'prefix' => '', |
| 25 | + ]); |
| 26 | + |
| 27 | + // 設置快取為陣列驅動 |
| 28 | + config(['cache.default' => 'array']); |
| 29 | + |
| 30 | + // 使用陣列驅動避免檔案權限問題 |
| 31 | + config(['session.driver' => 'array']); |
| 32 | + |
| 33 | + // 創建必要的測試表結構 |
| 34 | + $this->createTestTables(); |
| 35 | + $this->seedTestData(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function createTestTables(): void |
| 39 | + { |
| 40 | + Schema::create('BIOG_MAIN', function ($table) { |
| 41 | + $table->integer('c_personid')->primary(); |
| 42 | + $table->string('c_name_chn')->nullable(); |
| 43 | + $table->string('c_name')->nullable(); |
| 44 | + $table->string('c_surname')->nullable(); |
| 45 | + $table->string('c_mingzi')->nullable(); |
| 46 | + $table->string('c_name_proper')->nullable(); |
| 47 | + $table->string('c_name_rm')->nullable(); |
| 48 | + $table->string('c_surname_proper')->nullable(); |
| 49 | + $table->string('c_mingzi_proper')->nullable(); |
| 50 | + $table->string('c_surname_rm')->nullable(); |
| 51 | + $table->string('c_mingzi_rm')->nullable(); |
| 52 | + $table->integer('c_index_year')->nullable(); |
| 53 | + $table->integer('c_dy')->nullable(); |
| 54 | + $table->integer('c_index_addr_id')->nullable(); |
| 55 | + }); |
| 56 | + |
| 57 | + Schema::create('DYNASTIES', function ($table) { |
| 58 | + $table->integer('c_dy')->primary(); |
| 59 | + $table->string('c_dynasty_chn')->nullable(); |
| 60 | + }); |
| 61 | + |
| 62 | + Schema::create('ADDR_CODES', function ($table) { |
| 63 | + $table->integer('c_addr_id')->primary(); |
| 64 | + $table->string('c_name_chn')->nullable(); |
| 65 | + }); |
| 66 | + |
| 67 | + Schema::create('ALTNAME_DATA', function ($table) { |
| 68 | + $table->integer('c_personid'); |
| 69 | + $table->integer('c_alt_name_type_code'); |
| 70 | + $table->string('c_alt_name_chn')->nullable(); |
| 71 | + $table->primary(['c_personid', 'c_alt_name_type_code']); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + protected function seedTestData(): void |
| 76 | + { |
| 77 | + // 插入測試數據 |
| 78 | + DB::table('BIOG_MAIN')->insert([ |
| 79 | + [ |
| 80 | + 'c_personid' => 1001, |
| 81 | + 'c_name_chn' => '蘇軾', |
| 82 | + 'c_name' => 'Su Shi', |
| 83 | + 'c_surname' => '蘇', |
| 84 | + 'c_mingzi' => '軾', |
| 85 | + 'c_index_year' => 1050, |
| 86 | + 'c_dy' => 15, |
| 87 | + 'c_index_addr_id' => 100, |
| 88 | + 'c_name_proper' => null, |
| 89 | + 'c_name_rm' => null, |
| 90 | + 'c_surname_proper' => null, |
| 91 | + 'c_mingzi_proper' => null, |
| 92 | + 'c_surname_rm' => null, |
| 93 | + 'c_mingzi_rm' => null, |
| 94 | + ], |
| 95 | + [ |
| 96 | + 'c_personid' => 1002, |
| 97 | + 'c_name_chn' => '蘇轍', |
| 98 | + 'c_name' => 'Su Zhe', |
| 99 | + 'c_surname' => '蘇', |
| 100 | + 'c_mingzi' => '轍', |
| 101 | + 'c_index_year' => 1045, |
| 102 | + 'c_dy' => 15, |
| 103 | + 'c_index_addr_id' => 100, |
| 104 | + 'c_name_proper' => null, |
| 105 | + 'c_name_rm' => null, |
| 106 | + 'c_surname_proper' => null, |
| 107 | + 'c_mingzi_proper' => null, |
| 108 | + 'c_surname_rm' => null, |
| 109 | + 'c_mingzi_rm' => null, |
| 110 | + ], |
| 111 | + [ |
| 112 | + 'c_personid' => 2001, |
| 113 | + 'c_name_chn' => '王安石', |
| 114 | + 'c_name' => 'Wang Anshi', |
| 115 | + 'c_surname' => '王', |
| 116 | + 'c_mingzi' => '安石', |
| 117 | + 'c_index_year' => 1030, |
| 118 | + 'c_dy' => 15, |
| 119 | + 'c_index_addr_id' => 101, |
| 120 | + 'c_name_proper' => null, |
| 121 | + 'c_name_rm' => null, |
| 122 | + 'c_surname_proper' => null, |
| 123 | + 'c_mingzi_proper' => null, |
| 124 | + 'c_surname_rm' => null, |
| 125 | + 'c_mingzi_rm' => null, |
| 126 | + ], |
| 127 | + ]); |
| 128 | + |
| 129 | + DB::table('DYNASTIES')->insert([ |
| 130 | + ['c_dy' => 15, 'c_dynasty_chn' => '宋'], |
| 131 | + ]); |
| 132 | + |
| 133 | + DB::table('ADDR_CODES')->insert([ |
| 134 | + ['c_addr_id' => 100, 'c_name_chn' => '眉州'], |
| 135 | + ['c_addr_id' => 101, 'c_name_chn' => '臨川'], |
| 136 | + ]); |
| 137 | + |
| 138 | + DB::table('ALTNAME_DATA')->insert([ |
| 139 | + ['c_personid' => 1001, 'c_alt_name_type_code' => 4, 'c_alt_name_chn' => '子瞻'], |
| 140 | + ['c_personid' => 1001, 'c_alt_name_type_code' => 5, 'c_alt_name_chn' => '東坡居士'], |
| 141 | + ['c_personid' => 1002, 'c_alt_name_type_code' => 4, 'c_alt_name_chn' => '子由'], |
| 142 | + ]); |
| 143 | + } |
| 144 | + |
| 145 | + public function test_numeric_query_uses_personid_direct_lookup(): void |
| 146 | + { |
| 147 | + $request = new Request(['q' => '1001']); |
| 148 | + |
| 149 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 150 | + |
| 151 | + $this->assertInstanceOf(LengthAwarePaginator::class, $result); |
| 152 | + $this->assertCount(1, $result); |
| 153 | + |
| 154 | + $person = $result->items()[0]; |
| 155 | + $this->assertEquals(1001, $person->c_personid); |
| 156 | + $this->assertEquals('蘇軾', $person->c_name_chn); |
| 157 | + $this->assertEquals('宋', $person->c_dynasty_chn); |
| 158 | + $this->assertEquals('眉州', $person->ADDR_c_name_chn); |
| 159 | + $this->assertEquals('子瞻', $person->c_alt_name_chn_zi); |
| 160 | + $this->assertEquals('東坡居士', $person->c_alt_name_chn_hao); |
| 161 | + } |
| 162 | + |
| 163 | + public function test_numeric_query_returns_empty_for_nonexistent_id(): void |
| 164 | + { |
| 165 | + $request = new Request(['q' => '9999']); |
| 166 | + |
| 167 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 168 | + |
| 169 | + $this->assertInstanceOf(LengthAwarePaginator::class, $result); |
| 170 | + $this->assertCount(0, $result); |
| 171 | + } |
| 172 | + |
| 173 | + public function test_text_query_uses_complex_search(): void |
| 174 | + { |
| 175 | + // 注意:此測試在 SQLite 中會因為 FIELD() 函數不存在而失敗 |
| 176 | + // 在實際的 MySQL/MariaDB 環境中可以正常運行 |
| 177 | + // 因此我們只測試基本的查詢行為,不深入測試 FIELD() 排序 |
| 178 | + |
| 179 | + $this->markTestSkipped('SQLite 不支持 MySQL FIELD() 函數,此測試需要在 MySQL/MariaDB 環境中執行'); |
| 180 | + } |
| 181 | + |
| 182 | + public function test_mixed_alphanumeric_query_uses_complex_search(): void |
| 183 | + { |
| 184 | + $request = new Request(['q' => 'Su1001']); |
| 185 | + |
| 186 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 187 | + |
| 188 | + // 混合字母數字應該使用複雜搜尋,而非純數字優化 |
| 189 | + $this->assertInstanceOf(LengthAwarePaginator::class, $result); |
| 190 | + } |
| 191 | + |
| 192 | + public function test_leading_zeros_not_treated_as_numeric(): void |
| 193 | + { |
| 194 | + $request = new Request(['q' => '01001']); |
| 195 | + |
| 196 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 197 | + |
| 198 | + // 前導零會被 ctype_digit 判定為 true,但 addslashes 可能會移除 |
| 199 | + // 這個測試驗證行為的一致性 |
| 200 | + $this->assertInstanceOf(LengthAwarePaginator::class, $result); |
| 201 | + } |
| 202 | + |
| 203 | + public function test_numeric_query_includes_all_join_data(): void |
| 204 | + { |
| 205 | + $request = new Request(['q' => '1002']); |
| 206 | + |
| 207 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 208 | + |
| 209 | + $this->assertCount(1, $result); |
| 210 | + |
| 211 | + $person = $result->items()[0]; |
| 212 | + $this->assertEquals(1002, $person->c_personid); |
| 213 | + $this->assertEquals('蘇轍', $person->c_name_chn); |
| 214 | + $this->assertEquals('宋', $person->c_dynasty_chn); |
| 215 | + $this->assertEquals('眉州', $person->ADDR_c_name_chn); |
| 216 | + $this->assertEquals('子由', $person->c_alt_name_chn_zi); |
| 217 | + // 蘇轍沒有號,應該為 null |
| 218 | + $this->assertNull($person->c_alt_name_chn_hao); |
| 219 | + } |
| 220 | + |
| 221 | + public function test_empty_query_returns_paginated_list(): void |
| 222 | + { |
| 223 | + $request = new Request(['q' => '']); |
| 224 | + |
| 225 | + $result = BiogMainRepository::namesByQuery($request, 20); |
| 226 | + |
| 227 | + // 空查詢應該返回 JSON 字串(原有邏輯) |
| 228 | + $this->assertInternalType('string', $result); |
| 229 | + |
| 230 | + $decoded = json_decode($result, true); |
| 231 | + $this->assertInternalType('array', $decoded); |
| 232 | + $this->assertArrayHasKey('data', $decoded); |
| 233 | + $this->assertGreaterThanOrEqual(3, count($decoded['data'])); |
| 234 | + } |
| 235 | +} |
0 commit comments