274 lines
8.1 KiB
PHP
274 lines
8.1 KiB
PHP
|
|
<?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);
|
||
|
|
}
|
||
|
|
}
|