Files
star-erp/tests/Feature/InventoryTransferImportTest.php
sky121113 197df3bec4
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
[FIX] 修復所有 E2E 模組測試的標題定位器以及將測試帳號還原為 admin 權限
2026-03-09 16:53:06 +08:00

118 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Modules\Core\Models\User;
use App\Modules\Inventory\Models\InventoryTransferOrder;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Imports\InventoryTransferItemImport;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Maatwebsite\Excel\Facades\Excel;
use Tests\TestCase;
class InventoryTransferImportTest extends TestCase
{
use RefreshDatabase;
protected $user;
protected $tenant;
protected $fromWarehouse;
protected $toWarehouse;
protected $order;
protected $product;
protected function setUp(): void
{
parent::setUp();
// Create a unique tenant for this test run
$tenantId = 'test_' . str_replace('.', '', microtime(true));
$this->tenant = \App\Modules\Core\Models\Tenant::create([
'id' => $tenantId,
]);
$this->tenant->domains()->create(['domain' => $tenantId . '.test']);
tenancy()->initialize($this->tenant);
$this->user = User::create([
'name' => 'Test User',
'username' => 'testuser',
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$this->actingAs($this->user);
$this->fromWarehouse = Warehouse::create([
'code' => 'W1',
'name' => 'From Warehouse',
'type' => 'standard',
]);
$this->toWarehouse = Warehouse::create([
'code' => 'W2',
'name' => 'To Warehouse',
'type' => 'standard',
]);
$this->order = InventoryTransferOrder::create([
'doc_no' => 'TO' . time(),
'from_warehouse_id' => $this->fromWarehouse->id,
'to_warehouse_id' => $this->toWarehouse->id,
'status' => 'draft',
'created_by' => $this->user->id,
]);
$category = \App\Modules\Inventory\Models\Category::create(['name' => 'General', 'code' => 'GEN']);
$this->product = Product::create([
'code' => 'P001',
'name' => 'Test Product',
'status' => 'enabled',
'category_id' => $category->id,
]);
}
/** @test */
public function it_can_import_items_with_chinese_headers()
{
// 建立假 Excel使用中文標題
$content = [
['商品代碼', '批號', '數量', '備註'],
['P001', 'BATCH001', '10', 'Imported Via Test'],
['P001', '', '5', 'Batch should be NO-BATCH'],
];
// 這裡我們直接呼叫 Import 類別來測試,避免多層模擬
$import = new InventoryTransferItemImport($this->order);
// 我們模擬 Maatwebsite\Excel 傳入的 Collection
// 注意Excel 預設會將標題 slugify。如果 "商品代碼" 被 slugify我們的 Import 類別會在那邊掛掉。
// 所以這個測試可以幫我們確認 keys 是否如預期。
// 如果 WithHeadingRow 是用 slug 處理,那 keys 會是 slug 化的版本。
// 但如果我們在 Import 類別中直接讀取 $row['商品代碼'],我們得確定它真的在那裡。
$rows = collect([
collect(['商品代碼', '批號', '數量', '備註']),
collect(['P001', 'BATCH001', '10', 'Imported Via Test']),
collect(['P001', '', '5', 'Batch should be NO-BATCH']),
]);
$import->collection($rows);
$this->assertDatabaseHas('inventory_transfer_items', [
'transfer_order_id' => $this->order->id,
'product_id' => $this->product->id,
'batch_number' => 'BATCH001',
'quantity' => 10,
]);
$this->assertDatabaseHas('inventory_transfer_items', [
'transfer_order_id' => $this->order->id,
'product_id' => $this->product->id,
'batch_number' => 'NO-BATCH',
'quantity' => 5,
]);
}
}