修复一些错误

This commit is contained in:
liu
2026-08-14 22:20:13 +08:00
parent 228212f8e3
commit 4bdaf2a219
12 changed files with 454 additions and 37 deletions
+110
View File
@@ -0,0 +1,110 @@
<?php
namespace Tests\Feature;
use App\Models\CustomerLevelModel;
use App\Models\ProductModel;
use App\Models\ProductPriceModel;
use App\Models\StoreModel;
use App\Models\UserModel;
/**
* 小程序商品:商品详情接口(免登录浏览,登录门店按等级显示换算价);
* 商品列表回归:登录门店场景曾数组误用对象访问 + 价格行缺失导致 500
*/
class MiniProductTest extends ProcurementTestCase
{
/**
* 造门店 + 上架商品 + 等级价(固定价 5.00)
*
* @return array{0: StoreModel, 1: ProductModel, 2: UserModel, 3: CustomerLevelModel}
*/
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]);
ProductPriceModel::factory()->create([
'product_id' => $product->id,
'level_id' => $level->id,
'price' => $price,
]);
return [$store, $product, UserModel::factory()->forStore($store->id)->create(), $level];
}
/** 商品详情:未登录浏览 price=null,基础字段完整 */
public function test_product_detail_guest_sees_null_price(): void
{
[, $product] = $this->makeStoreWithProduct();
$data = $this->getJson("/mini/product/{$product->id}")->assertOk()->json('data');
$this->assertSame($product->name, $data['name']);
$this->assertSame($product->spec, $data['spec']);
$this->assertSame($product->unit, $data['unit']);
$this->assertNull($data['price'], '未登录不显示价格');
$this->assertArrayNotHasKey('cost_price', $data, '成本价不得输出到小程序端');
}
/** 商品详情:登录门店按等级显示固定价 */
public function test_product_detail_store_user_sees_level_price(): void
{
[, $product, $user] = $this->makeStoreWithProduct('6.50');
$this->actingAsMiniUser($user);
$data = $this->getJson("/mini/product/{$product->id}")->assertOk()->json('data');
$this->assertSame('6.50', $data['price']);
}
/** 商品详情:百分比计价按最新成本价上浮换算 */
public function test_product_detail_percent_price_calculated_from_cost(): void
{
[, $product, $user, $level] = $this->makeStoreWithProduct();
ProductPriceModel::where('product_id', $product->id)->where('level_id', $level->id)
->update(['price_type' => ProductPriceModel::PRICE_TYPE_PERCENT, 'percent' => '30']);
$product->update(['cost_price' => '20.00']);
$this->actingAsMiniUser($user);
$data = $this->getJson("/mini/product/{$product->id}")->assertOk()->json('data');
$this->assertSame('26.00', $data['price'], '20 元上浮 30% = 26.00');
}
/** 商品详情:下架/不存在拒绝 */
public function test_product_detail_off_shelf_or_missing_rejected(): void
{
[, $product] = $this->makeStoreWithProduct();
$product->update(['status' => ProductModel::STATUS_OFF]);
$this->getJson("/mini/product/{$product->id}")->assertJsonPath('success', false);
$this->getJson('/mini/product/99999')->assertJsonPath('success', false);
}
/** 商品列表:登录门店显示等级价(回归:曾数组误用对象访问导致 500) */
public function test_product_list_store_user_sees_level_price(): void
{
[, $product, $user] = $this->makeStoreWithProduct('7.00');
$this->actingAsMiniUser($user);
$rows = $this->getJson('/mini/product/list')->assertOk()->json('data.data');
$row = collect($rows)->firstWhere('id', $product->id);
$this->assertNotNull($row);
$this->assertSame('7.00', $row['price']);
$this->assertArrayNotHasKey('cost_price', $row, '成本价不得输出到小程序端');
}
/** 商品列表:未登录 price=null */
public function test_product_list_guest_sees_null_price(): void
{
[, $product] = $this->makeStoreWithProduct();
$rows = $this->getJson('/mini/product/list')->assertOk()->json('data.data');
$row = collect($rows)->firstWhere('id', $product->id);
$this->assertNotNull($row);
$this->assertNull($row['price']);
}
}