update sku mapping
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user