erp-backend/tests/Api/PurchaseOrderTest.php
2026-04-01 17:07:04 +08:00

132 lines
4.0 KiB
PHP

<?php
namespace Tests\Api;
use App\Models\Warehouse;
use App\Models\Supplier;
use App\Models\Brand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PurchaseOrderTest extends TestCase
{
use RefreshDatabase;
public function test_create_purchase_order_draft()
{
// 创建测试数据
$warehouse = Warehouse::factory()->create(['type' => 'erp']);
$supplier = Supplier::factory()->create();
$brand = Brand::factory()->create();
$response = $this->postJson('/api/purchase-orders/draft', [
'warehouse_id' => $warehouse->id,
'supplier_id' => $supplier->id,
'brand_id' => $brand->id,
'expected_arrival' => '2026-03-20',
'shipping_method' => 'express',
'freight' => 50.00,
'category' => '电子产品',
'remark' => '测试采购单',
]);
$response->assertStatus(200)
->assertJsonStructure([
'code',
'data' => [
'id',
'po_no',
'warehouse_id',
'supplier_id',
'status',
'total_amount',
],
'message'
])
->assertJson([
'code' => 200,
'message' => '采购单草稿创建成功'
]);
}
public function test_update_purchase_order_with_items()
{
// 创建采购单草稿
$warehouse = Warehouse::factory()->create(['type' => 'erp']);
$supplier = Supplier::factory()->create();
$brand = Brand::factory()->create();
$draftResponse = $this->postJson('/api/purchase-orders/draft', [
'warehouse_id' => $warehouse->id,
'supplier_id' => $supplier->id,
'brand_id' => $brand->id,
'expected_arrival' => '2026-03-20',
'shipping_method' => 'express',
]);
$purchaseOrderId = $draftResponse->json('data.id');
// 更新采购单,添加商品项
$response = $this->putJson("/api/purchase-orders/{$purchaseOrderId}", [
'items' => [
[
'sku_code' => 'TEST001',
'sku_name' => '测试商品1',
'quantity' => 10,
'price' => 100.00,
],
[
'sku_code' => 'TEST002',
'sku_name' => '测试商品2',
'quantity' => 5,
'price' => 200.00,
]
]
]);
$response->assertStatus(200)
->assertJson([
'code' => 200,
'message' => '采购单更新成功'
]);
}
public function test_submit_purchase_order_for_review()
{
// 创建采购单草稿并添加商品
$warehouse = Warehouse::factory()->create(['type' => 'erp']);
$supplier = Supplier::factory()->create();
$brand = Brand::factory()->create();
$draftResponse = $this->postJson('/api/purchase-orders/draft', [
'warehouse_id' => $warehouse->id,
'supplier_id' => $supplier->id,
'brand_id' => $brand->id,
'expected_arrival' => '2026-03-20',
'shipping_method' => 'express',
]);
$purchaseOrderId = $draftResponse->json('data.id');
// 添加商品
$this->putJson("/api/purchase-orders/{$purchaseOrderId}", [
'items' => [
[
'sku_code' => 'TEST001',
'sku_name' => '测试商品',
'quantity' => 10,
'price' => 100.00,
]
]
]);
// 提交审核
$response = $this->postJson("/api/purchase-orders/{$purchaseOrderId}/submit-review");
$response->assertStatus(200)
->assertJson([
'code' => 200,
'message' => '采购单已提交审核'
]);
}
}