update sku mapping

This commit is contained in:
2026-04-14 15:45:29 +08:00
parent b1cd4ea0eb
commit 417ac5e1f9
11 changed files with 1454 additions and 75 deletions
@@ -48,6 +48,7 @@ class SkuMappingController extends AbstractController
new OA\Parameter(name: 'store_id', in: 'query', required: false, description: '店铺 ID 精确筛选', schema: new OA\Schema(type: 'integer')),
new OA\Parameter(name: 'origin_sku', in: 'query', required: false, description: '原始 SKU 模糊搜索', schema: new OA\Schema(type: 'string')),
new OA\Parameter(name: 'platform_outer_sku', in: 'query', required: false, description: '平台侧 SKU 模糊搜索', schema: new OA\Schema(type: 'string')),
new OA\Parameter(name: 'origin_sku_id', in: 'query', required: false, description: '内部 SKU ID 精确筛选', schema: new OA\Schema(type: 'integer')),
new OA\Parameter(name: 'enabled', in: 'query', required: false, description: '启用状态', schema: new OA\Schema(type: 'boolean')),
],
responses: [
@@ -85,6 +86,7 @@ class SkuMappingController extends AbstractController
'company_id' => 'exact',
'platform_id' => 'exact',
'store_id' => 'exact',
'origin_sku_id' => 'exact',
];
foreach ($filters as $field => $type) {
@@ -335,7 +337,7 @@ class SkuMappingController extends AbstractController
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
required: ['company_id', 'platform_id', 'origin_sku', 'platform_product_id'],
required: ['company_id', 'platform_id', 'origin_sku'],
properties: [
new OA\Property(property: 'company_id', type: 'integer', example: 3),
new OA\Property(property: 'platform_id', type: 'integer', example: 1),
@@ -369,7 +371,7 @@ class SkuMappingController extends AbstractController
{
$data = $this->request->all();
$required_fields = ['company_id', 'platform_id', 'origin_sku', 'platform_product_id'];
$required_fields = ['company_id', 'platform_id', 'origin_sku'];
foreach ($required_fields as $field) {
if (!isset($data[$field]) || $data[$field] === '') {
return $this->response->json([
@@ -385,7 +387,7 @@ class SkuMappingController extends AbstractController
'store_id' => isset($data['store_id']) ? (int) $data['store_id'] : null,
'origin_sku' => $data['origin_sku'],
'origin_sku_id' => isset($data['origin_sku_id']) ? (int) $data['origin_sku_id'] : null,
'platform_product_id' => $data['platform_product_id'],
'platform_product_id' => $data['platform_product_id'] ?? null,
'platform_outer_sku' => $data['platform_outer_sku'] ?? null,
'generation_strategy' => $data['generation_strategy'] ?? null,
'warehouse_id' => isset($data['warehouse_id']) ? (int) $data['warehouse_id'] : null,
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('skus_mapping', function (Blueprint $table) {
$table->text('platform_product_id')->nullable()->change();
});
// 删除原有唯一约束(如果存在)并创建部分唯一索引
$connection = Schema::getConnection();
$connection->statement('DROP INDEX IF EXISTS uk_platform_product');
$connection->statement(
'CREATE UNIQUE INDEX uk_platform_product ON skus_mapping (platform_id, platform_product_id) WHERE platform_product_id IS NOT NULL'
);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
$connection = Schema::getConnection();
$connection->statement('DROP INDEX IF EXISTS uk_platform_product');
$connection->statement(
'CREATE UNIQUE INDEX uk_platform_product ON skus_mapping (platform_id, platform_product_id)'
);
Schema::table('skus_mapping', function (Blueprint $table) {
$table->text('platform_product_id')->nullable(false)->change();
});
}
};
@@ -0,0 +1,273 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Sku;
use App\Model\SkuMapping;
use App\Model\SkuOrigin;
use HyperfTest\TestCase;
use HyperfTest\Traits\AuthenticatedTestTrait;
/**
* SkuMappingController 集成测试
*
* 覆盖列表筛选、origin_sku_id 过滤、创建校验(platform_product_id 可选)、CRUD、认证拦截
*
* @internal
* @coversNothing
*/
class SkuMappingControllerTest extends TestCase
{
use AuthenticatedTestTrait;
protected function hasData(): bool
{
return $this->runInCoroutine(static function (): bool {
return SkuMapping::query()->exists();
});
}
protected function getFirstRecord(): ?array
{
return $this->runInCoroutine(static function (): ?array {
$record = SkuMapping::query()->first();
return $record ? $record->toArray() : null;
});
}
protected function getFirstOrigin(): ?array
{
return $this->runInCoroutine(static function (): ?array {
$record = SkuOrigin::query()->first();
return $record ? $record->toArray() : null;
});
}
// ========== 列表接口 ==========
public function test_list_returns_paginated_data(): void
{
$response = $this->get('/api/v1/sku-mappings', [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonStructure([
'code',
'message',
'data' => [
'items',
'total',
'page',
'per_page',
],
]);
}
public function test_list_filter_by_origin_sku_id(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Mapping 数据');
}
$first = $this->getFirstRecord();
if (!$first['origin_sku_id']) {
$this->markTestSkipped('首条记录无 origin_sku_id');
}
$response = $this->get('/api/v1/sku-mappings', [
'origin_sku_id' => $first['origin_sku_id'],
], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertSame($first['origin_sku_id'], $item['origin_sku_id']);
}
}
public function test_list_filter_by_company_and_platform(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Mapping 数据');
}
$first = $this->getFirstRecord();
$response = $this->get('/api/v1/sku-mappings', [
'company_id' => $first['company_id'],
'platform_id' => $first['platform_id'],
], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertSame($first['company_id'], $item['company_id']);
$this->assertSame($first['platform_id'], $item['platform_id']);
}
}
public function test_list_filter_by_enabled(): void
{
$response = $this->get('/api/v1/sku-mappings', ['enabled' => 'true'], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertTrue($item['enabled']);
}
}
// ========== 创建校验 ==========
public function test_create_without_platform_product_id_succeeds(): void
{
$origin = $this->getFirstOrigin();
if (!$origin) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$response = $this->post('/api/v1/sku-mappings', [
'company_id' => $origin['company_id'],
'platform_id' => 1,
'origin_sku' => $origin['sku'],
'origin_sku_id' => $origin['id'],
'platform_outer_sku' => 'TEST-' . uniqid(),
], $this->authHeaders());
$response->assertStatus(201);
$response->assertJsonPath('code', 0);
// 清理测试数据
$body = json_decode($response->getContent(), true);
if (isset($body['data']['id'])) {
$this->runInCoroutine(static function () use ($body): void {
SkuMapping::query()->where('id', $body['data']['id'])->delete();
});
}
}
public function test_create_with_platform_product_id_succeeds(): void
{
$origin = $this->getFirstOrigin();
if (!$origin) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$response = $this->post('/api/v1/sku-mappings', [
'company_id' => $origin['company_id'],
'platform_id' => 1,
'origin_sku' => $origin['sku'],
'origin_sku_id' => $origin['id'],
'platform_product_id' => 'PROD-TEST-' . uniqid(),
'platform_outer_sku' => 'TEST-' . uniqid(),
], $this->authHeaders());
$response->assertStatus(201);
$response->assertJsonPath('code', 0);
// 清理测试数据
$body = json_decode($response->getContent(), true);
if (isset($body['data']['id'])) {
$this->runInCoroutine(static function () use ($body): void {
SkuMapping::query()->where('id', $body['data']['id'])->delete();
});
}
}
public function test_create_requires_company_id(): void
{
$response = $this->post('/api/v1/sku-mappings', [
'platform_id' => 1,
'origin_sku' => 'TEST-SKU',
], $this->authHeaders());
$response->assertStatus(422);
}
public function test_create_requires_platform_id(): void
{
$response = $this->post('/api/v1/sku-mappings', [
'company_id' => 1,
'origin_sku' => 'TEST-SKU',
], $this->authHeaders());
$response->assertStatus(422);
}
public function test_create_requires_origin_sku(): void
{
$response = $this->post('/api/v1/sku-mappings', [
'company_id' => 1,
'platform_id' => 1,
], $this->authHeaders());
$response->assertStatus(422);
}
// ========== 详情接口 ==========
public function test_detail_not_found_returns_404(): void
{
$response = $this->get('/api/v1/sku-mappings/999999999', [], $this->authHeaders());
$response->assertStatus(404);
$response->assertJsonPath('code', 404);
}
// ========== 更新 & 删除 ==========
public function test_update_mapping(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Mapping 数据');
}
$first = $this->getFirstRecord();
$response = $this->put('/api/v1/sku-mappings/' . $first['id'], [
'note' => 'integration-test-update-' . uniqid(),
], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
}
public function test_delete_mapping(): void
{
// 创建一条临时记录然后删除
$origin = $this->getFirstOrigin();
if (!$origin) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$createResponse = $this->post('/api/v1/sku-mappings', [
'company_id' => $origin['company_id'],
'platform_id' => 1,
'origin_sku' => $origin['sku'],
'origin_sku_id' => $origin['id'],
'platform_outer_sku' => 'DELETE-TEST-' . uniqid(),
], $this->authHeaders());
$createResponse->assertStatus(201);
$body = json_decode($createResponse->getContent(), true);
$newId = $body['data']['id'];
$deleteResponse = $this->delete('/api/v1/sku-mappings/' . $newId, [], $this->authHeaders());
$deleteResponse->assertStatus(200);
$deleteResponse->assertJsonPath('code', 0);
}
// ========== 认证拦截 ==========
public function test_list_without_token_returns_401(): void
{
$response = $this->get('/api/v1/sku-mappings');
$response->assertStatus(401);
}
}
@@ -0,0 +1,200 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Sku;
use App\Model\SkuOrigin;
use HyperfTest\TestCase;
use HyperfTest\Traits\AuthenticatedTestTrait;
/**
* SkuOriginController 集成测试
*
* 覆盖列表分页、筛选、详情、创建校验、认证拦截
*
* @internal
* @coversNothing
*/
class SkuOriginControllerTest extends TestCase
{
use AuthenticatedTestTrait;
protected function hasData(): bool
{
return $this->runInCoroutine(static function (): bool {
return SkuOrigin::query()->exists();
});
}
protected function getFirstRecord(): ?array
{
return $this->runInCoroutine(static function (): ?array {
$record = SkuOrigin::query()->first();
return $record ? $record->toArray() : null;
});
}
// ========== 列表接口 ==========
public function test_list_returns_paginated_data(): void
{
$response = $this->get('/api/v1/sku-origins', [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonStructure([
'code',
'message',
'data' => [
'items',
'total',
'page',
'per_page',
],
]);
}
public function test_list_respects_per_page(): void
{
$response = $this->get('/api/v1/sku-origins', ['per_page' => 5], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('data.per_page', 5);
}
public function test_list_filter_by_company_id(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$company_id = $this->runInCoroutine(static function (): mixed {
return SkuOrigin::query()->value('company_id');
});
$response = $this->get('/api/v1/sku-origins', ['company_id' => $company_id], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertSame($company_id, $item['company_id']);
}
}
public function test_list_filter_by_sku(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$first = $this->getFirstRecord();
$sku_fragment = substr($first['sku'], 0, 3);
$response = $this->get('/api/v1/sku-origins', ['sku' => $sku_fragment], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertStringContainsStringIgnoringCase($sku_fragment, $item['sku']);
}
}
public function test_list_filter_by_barcode(): void
{
if (!$this->hasData()) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$first = $this->getFirstRecord();
if (empty($first['barcode'])) {
$this->markTestSkipped('首条记录无 barcode');
}
$barcode_fragment = substr($first['barcode'], 0, 4);
$response = $this->get('/api/v1/sku-origins', ['barcode' => $barcode_fragment], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertStringContainsStringIgnoringCase($barcode_fragment, $item['barcode']);
}
}
// ========== 详情接口 ==========
public function test_detail_returns_sku_origin(): void
{
$first = $this->getFirstRecord();
if (!$first) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$response = $this->get('/api/v1/sku-origins/' . $first['id'], [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonPath('data.id', $first['id']);
}
public function test_detail_not_found_returns_404(): void
{
$response = $this->get('/api/v1/sku-origins/999999999', [], $this->authHeaders());
$response->assertStatus(404);
$response->assertJsonPath('code', 404);
}
// ========== 创建校验 ==========
public function test_create_requires_barcode(): void
{
$response = $this->post('/api/v1/sku-origins', [
'company_id' => 1,
'sku' => 'TEST-NO-BARCODE-' . uniqid(),
'name' => 'Test Product',
], $this->authHeaders());
$response->assertStatus(422);
}
public function test_create_requires_sku(): void
{
$response = $this->post('/api/v1/sku-origins', [
'company_id' => 1,
'barcode' => '6901234567890',
'name' => 'Test Product',
], $this->authHeaders());
$response->assertStatus(422);
}
public function test_create_duplicate_company_sku_returns_422(): void
{
$first = $this->getFirstRecord();
if (!$first) {
$this->markTestSkipped('没有 SKU Origin 数据');
}
$response = $this->post('/api/v1/sku-origins', [
'company_id' => $first['company_id'],
'sku' => $first['sku'],
'barcode' => '0000000000000',
'name' => 'Duplicate Test',
], $this->authHeaders());
$response->assertStatus(422);
}
// ========== 认证拦截 ==========
public function test_list_without_token_returns_401(): void
{
$response = $this->get('/api/v1/sku-origins');
$response->assertStatus(401);
}
}