推荐商品

This commit is contained in:
liu
2026-08-31 22:21:59 +08:00
parent 6c809d893b
commit 9c59f3bc99
191 changed files with 2095 additions and 1 deletions
+111
View File
@@ -0,0 +1,111 @@
<?php
namespace Tests\Feature;
use App\Models\CartModel;
use App\Models\CustomerLevelModel;
use App\Models\HomeSpecialModel;
use App\Models\ProductModel;
use App\Models\StoreModel;
/**
* 小程序特价推荐列表:免登录浏览 price=null、登录门店按等级上浮换算价、
* 停用推荐/下架商品不展示、按推荐排序升序、购物车数量随行输出
*/
class MiniSpecialTest extends ProcurementTestCase
{
/**
* 造门店 + 上架商品 + 正常推荐
*
* @return array{0: StoreModel, 1: ProductModel, 2: CustomerLevelModel, 3: HomeSpecialModel}
*/
private function makeSpecial(string $price = '5.00', int $sort = 0): array
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create([
'status' => ProductModel::STATUS_ON,
'cost_price' => $price,
]);
$special = HomeSpecialModel::create([
'product_id' => $product->id,
'sort' => $sort,
'status' => HomeSpecialModel::STATUS_NORMAL,
]);
return [$store, $product, $level, $special];
}
/** 游客:返回推荐商品,price=null,成本价不输出 */
public function test_guest_sees_specials_with_null_price(): void
{
[, $product] = $this->makeSpecial();
$data = $this->getJson('/mini/special/list')->assertOk()->json('data');
$this->assertSame(1, $data['total']);
$row = $data['data'][0];
$this->assertSame($product->name, $row['name']);
$this->assertNull($row['price'], '未登录不显示价格');
$this->assertArrayNotHasKey('cost_price', $row, '成本价不得输出到小程序端');
}
/** 登录门店:按等级上浮比例换算价(成本价 20 上浮 30% = 26.00 */
public function test_store_sees_level_price(): void
{
[$store, $product, $level] = $this->makeSpecial('20.00');
$level->update(['percent' => 30]);
$this->actingAsMiniStore($store);
$row = $this->getJson('/mini/special/list')->assertOk()->json('data.data.0');
$this->assertSame($product->id, $row['id']);
$this->assertSame('26.00', $row['price']);
}
/** 停用推荐与下架商品的推荐不展示 */
public function test_disabled_special_and_off_shelf_product_excluded(): void
{
[, $productOn] = $this->makeSpecial();
$offProduct = ProductModel::factory()->create(['status' => ProductModel::STATUS_OFF]);
HomeSpecialModel::create(['product_id' => $offProduct->id, 'sort' => 0, 'status' => 1]);
$disabledProduct = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON]);
HomeSpecialModel::create(['product_id' => $disabledProduct->id, 'sort' => 0, 'status' => 0]);
$data = $this->getJson('/mini/special/list')->assertOk()->json('data');
$this->assertSame(1, $data['total']);
$this->assertSame($productOn->id, $data['data'][0]['id']);
}
/** 按推荐排序 sort 升序(越小越靠前) */
public function test_specials_sorted_by_sort_asc(): void
{
[, $productB] = $this->makeSpecial('5.00', 5);
[, $productA] = $this->makeSpecial('6.00', 1);
$rows = $this->getJson('/mini/special/list')->assertOk()->json('data.data');
$this->assertSame($productA->id, $rows[0]['id'], 'sort=1 应排在 sort=5 前');
$this->assertSame($productB->id, $rows[1]['id']);
}
/** 登录门店:购物车行 ID 与数量随行输出(供列表直接加减) */
public function test_cart_quantity_attached_for_store(): void
{
[$store, $product] = $this->makeSpecial();
CartModel::create([
'store_id' => $store->id,
'product_id' => $product->id,
'quantity' => '2.00',
]);
$this->actingAsMiniStore($store);
$row = $this->getJson('/mini/special/list')->assertOk()->json('data.data.0');
$this->assertGreaterThan(0, $row['cart_id']);
$this->assertSame('2.00', $row['cart_quantity']);
}
}