Files
2026-08-27 18:13:43 +08:00

353 lines
15 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\CartModel;
use App\Models\CustomerLevelModel;
use App\Models\ProductModel;
use App\Models\StoreModel;
/**
* 小程序购物车:加购合并、等级上浮价/金额服务端计算、归属校验、数据隔离、清空
* (门店即用户:购物车按 store_id 归属)
*/
class CartTest extends ProcurementTestCase
{
/**
* 造一家门店(含等级)+ 一个上架商品(成本价即售价,等级上浮 0%)
*
* @return array{0: StoreModel, 1: ProductModel}
*/
private function makeStoreWithProduct(string $price = '5.00'): array
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => $price,
]);
return [$store, $product];
}
/** 加购成功:数量入库、返回购物车项 */
public function test_add_to_cart_succeeds(): void
{
[$store, $product] = $this->makeStoreWithProduct('5.50');
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', [
'product_id' => $product->id,
'quantity' => 2.5,
])->assertOk()
->assertJsonPath('success', true)
->assertJsonPath('data.quantity', '2.50')
->assertJsonStructure(['data' => ['id', 'quantity']]);
$cart = CartModel::first();
$this->assertNotNull($cart);
$this->assertSame($store->id, $cart->store_id);
$this->assertSame('2.50', (string) $cart->quantity);
}
/** 重复加购同一商品合并累加(同商品唯一行) */
public function test_duplicate_add_merges_quantity(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 2]);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 3])
->assertJsonPath('data.quantity', '5.00');
$this->assertSame(1, CartModel::count());
$this->assertSame('5.00', (string) CartModel::first()->quantity);
}
/** 已下架商品拒绝加购 */
public function test_off_shelf_product_rejected(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$product->update(['status' => ProductModel::STATUS_OFF]);
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1])
->assertOk()
->assertJsonPath('success', false);
$this->assertSame(0, CartModel::count());
}
/** 门店未设置客户等级时拒绝加购 */
public function test_store_without_level_rejected(): void
{
[, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore(StoreModel::factory()->create(['level_id' => 0]));
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1])
->assertJsonPath('success', false)
->assertJsonPath('msg', '门店未设置客户等级,无法加购,请联系客服');
$this->assertSame(0, CartModel::count());
}
/** 列表:实时等级上浮价、服务端金额、汇总(同店两商品,新加入在前) */
public function test_list_with_prices_amounts_and_totals(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$p1 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.50']);
$p2 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '3.00']);
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 3]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 2]);
$this->getJson('/mini/cart')->assertOk()
->assertJsonPath('success', true)
// items 按 id 倒序:后加入的 p2 在前
->assertJsonPath('data.items.0.product_id', $p2->id)
->assertJsonPath('data.items.0.price', '3.00')
->assertJsonPath('data.items.0.quantity', '2.00')
->assertJsonPath('data.items.0.amount', '6.00')
->assertJsonPath('data.items.0.status', 1)
->assertJsonPath('data.items.1.product_id', $p1->id)
->assertJsonPath('data.items.1.price', '5.50')
->assertJsonPath('data.items.1.amount', '16.50')
->assertJsonPath('data.items.1.status', 1)
->assertJsonPath('data.total_count', 2)
->assertJsonPath('data.total_quantity', '5.00')
->assertJsonPath('data.total_amount', '22.50');
}
/** 列表:下架商品标记不可购,不计入汇总 */
public function test_list_marks_off_shelf_item_unbuyable(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$p1 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.50']);
$p2 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '3.00']);
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 3]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 2]);
$p1->update(['status' => ProductModel::STATUS_OFF]);
$this->getJson('/mini/cart')->assertOk()
->assertJsonPath('data.items.1.status', 0)
->assertJsonPath('data.items.1.amount', null)
->assertJsonPath('data.total_count', 2)
->assertJsonPath('data.total_quantity', '2.00')
->assertJsonPath('data.total_amount', '6.00');
}
/** 空购物车返回空列表 */
public function test_empty_cart_returns_empty_items(): void
{
$store = StoreModel::factory()->create(['level_id' => CustomerLevelModel::factory()->create()->id]);
$this->actingAsMiniStore($store);
$this->getJson('/mini/cart')->assertOk()
->assertJsonPath('data.items', [])
->assertJsonPath('data.total_count', 0)
->assertJsonPath('data.total_amount', '0.00');
}
/** 修改数量 */
public function test_update_quantity(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$cart = CartModel::first();
$this->putJson("/mini/cart/{$cart->id}", ['quantity' => 7])
->assertJsonPath('success', true)
->assertJsonPath('data.quantity', '7.00');
$this->assertSame('7.00', (string) $cart->refresh()->quantity);
}
/** 修改他店购物车项拒绝 */
public function test_update_other_stores_item_rejected(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$cart = CartModel::first();
$this->actingAsMiniStore(StoreModel::factory()->create());
$this->putJson("/mini/cart/{$cart->id}", ['quantity' => 9])
->assertJsonPath('success', false)
->assertJsonPath('msg', '购物车项不存在');
$this->assertSame('1.00', (string) $cart->refresh()->quantity, '他店改数量不应生效');
}
/** 修改不存在的购物车项拒绝 */
public function test_update_missing_item_rejected(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$this->putJson('/mini/cart/999999', ['quantity' => 1])->assertJsonPath('success', false);
}
/** 数量校验:0 与负值拒绝 */
public function test_invalid_quantity_rejected(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 0])
->assertOk()
->assertJsonPath('success', false)
->assertJsonPath('msg', '订货数量必须大于 0');
$this->assertSame(0, CartModel::count());
}
/** 删除单项 */
public function test_delete_item(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$cart = CartModel::first();
$this->deleteJson("/mini/cart/{$cart->id}")->assertJsonPath('success', true);
$this->assertSame(0, CartModel::count());
}
/** 删除他店购物车项拒绝 */
public function test_delete_other_stores_item_rejected(): void
{
[$store, $product] = $this->makeStoreWithProduct();
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$cart = CartModel::first();
$this->actingAsMiniStore(StoreModel::factory()->create());
$this->deleteJson("/mini/cart/{$cart->id}")
->assertJsonPath('success', false)
->assertJsonPath('msg', '购物车项不存在');
$this->assertSame(1, CartModel::count());
}
/** 清空购物车仅影响本店 */
public function test_clear_only_own_cart(): void
{
$level = CustomerLevelModel::factory()->create();
$storeA = StoreModel::factory()->create(['level_id' => $level->id]);
$storeB = StoreModel::factory()->create(['level_id' => $level->id]);
$p1 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.00']);
$p2 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.00']);
$this->actingAsMiniStore($storeA);
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 1]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 1]);
$this->actingAsMiniStore($storeB);
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 1]);
$this->actingAsMiniStore($storeA);
$this->deleteJson('/mini/cart')->assertJsonPath('success', true);
$this->assertSame(0, CartModel::where('store_id', $storeA->id)->count());
$this->assertSame(1, CartModel::where('store_id', $storeB->id)->count(), '他店购物车不受影响');
}
/** 未登录访问购物车 → 401 */
public function test_unauthenticated_returns_401(): void
{
$this->getJson('/mini/cart')->assertStatus(401);
}
/** 悬浮球汇总:种数/总数量/总金额(下架商品不计入数量与金额但仍计种数) */
public function test_cart_summary_endpoint(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$p1 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.00']);
$p2 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '3.00']);
$this->actingAsMiniStore($store);
// 空购物车返回零值结构
$this->getJson('/mini/cart/summary')->assertOk()
->assertJsonPath('data.total_count', 0)
->assertJsonPath('data.total_quantity', '0.00')
->assertJsonPath('data.total_amount', '0.00');
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 2]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 1]);
$this->getJson('/mini/cart/summary')->assertOk()
->assertJsonPath('data.total_count', 2)
->assertJsonPath('data.total_quantity', '3.00')
->assertJsonPath('data.total_amount', '13.00');
// 下架商品:数量/金额不计入(与购物车列表口径一致),种数仍计
$p2->update(['status' => ProductModel::STATUS_OFF]);
$this->getJson('/mini/cart/summary')->assertOk()
->assertJsonPath('data.total_count', 2)
->assertJsonPath('data.total_quantity', '2.00')
->assertJsonPath('data.total_amount', '10.00');
}
/** 未登录访问悬浮球汇总 → 401 */
public function test_cart_summary_requires_login(): void
{
$this->getJson('/mini/cart/summary')->assertStatus(401);
}
/** 下单成功后自动清空购物车中已下单的商品(未下单商品保留) */
public function test_place_order_clears_ordered_items_from_cart(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$p1 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.00']);
$p2 = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '3.00']);
$this->actingAsMiniStore($store);
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 2]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 1]);
$this->assertSame(2, CartModel::where('store_id', $store->id)->count());
// 下单两个商品 → 购物车清空
$this->postJson('/mini/order', ['items' => [
['product_id' => $p1->id, 'quantity' => 2],
['product_id' => $p2->id, 'quantity' => 1],
]])->assertOk()->assertJsonPath('success', true);
$this->assertSame(0, CartModel::where('store_id', $store->id)->count(), '下单后购物车应自动清空');
// 部分下单:仅清除已下单商品,未下单商品保留
$this->postJson('/mini/cart', ['product_id' => $p1->id, 'quantity' => 2]);
$this->postJson('/mini/cart', ['product_id' => $p2->id, 'quantity' => 1]);
$this->postJson('/mini/order', ['items' => [['product_id' => $p1->id, 'quantity' => 2]]])
->assertOk()->assertJsonPath('success', true);
$remaining = CartModel::where('store_id', $store->id)->get();
$this->assertSame(1, $remaining->count(), '未下单商品应保留在购物车');
$this->assertSame($p2->id, $remaining->first()->product_id);
}
/** 跨门店列表隔离:B 店看不到 A 店的购物车 */
public function test_cart_isolated_between_stores(): void
{
$level = CustomerLevelModel::factory()->create();
$storeA = StoreModel::factory()->create(['level_id' => $level->id]);
$storeB = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON, 'cost_price' => '5.00']);
$this->actingAsMiniStore($storeA);
$this->postJson('/mini/cart', ['product_id' => $product->id, 'quantity' => 1]);
$this->actingAsMiniStore($storeB);
$this->getJson('/mini/cart')->assertOk()
->assertJsonPath('data.items', [])
->assertJsonPath('data.total_count', 0);
}
}