Files
2026-04-14 13:45:28 +08:00

128 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Unit\Service;
use App\Service\SkuService;
use HyperfTest\TestCase;
/**
* SkuService 单元测试
*
* 验证 SKU 自动生成策略和辅助方法
*
* @internal
* @coversNothing
*/
class SkuServiceTest extends TestCase
{
private SkuService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new SkuService();
}
public function test_generate_prefix_sku(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'prefix',
origin_sku: '0032',
prefix: 'C03',
);
$this->assertSame('C03_0032', $result);
}
public function test_generate_prefix_sku_without_prefix(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'prefix',
origin_sku: '0032',
prefix: '',
);
$this->assertSame('0032', $result);
}
public function test_generate_prefix_random_sku(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'prefix_random',
origin_sku: '0032',
prefix: 'C03',
random_length: 4,
);
$this->assertStringStartsWith('C03_', $result);
// prefix(3) + _(1) + random(4) = 8
$this->assertSame(8, strlen($result));
}
public function test_generate_prefix_random_sku_without_prefix(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'prefix_random',
origin_sku: '0032',
prefix: '',
random_length: 6,
);
// 纯随机部分,6 个字符
$this->assertSame(6, strlen($result));
}
public function test_generate_manual_sku(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'manual',
origin_sku: '0032',
manual_value: 'CUSTOM_SKU_001',
);
$this->assertSame('CUSTOM_SKU_001', $result);
}
public function test_generate_manual_sku_with_null_value(): void
{
$result = $this->service->generatePlatformOuterSku(
strategy: 'manual',
origin_sku: '0032',
manual_value: null,
);
$this->assertSame('', $result);
}
public function test_generate_invalid_strategy_throws_exception(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('不支持的生成策略: unknown');
$this->service->generatePlatformOuterSku(
strategy: 'unknown',
origin_sku: '0032',
);
}
public function test_build_strategy_record_prefix(): void
{
$result = $this->service->buildStrategyRecord('prefix', 'C03');
$this->assertSame('prefix:C03', $result);
}
public function test_build_strategy_record_prefix_random(): void
{
$result = $this->service->buildStrategyRecord('prefix_random', 'C03', 4);
$this->assertSame('prefix_random:C03:4', $result);
}
public function test_build_strategy_record_manual(): void
{
$result = $this->service->buildStrategyRecord('manual');
$this->assertSame('manual', $result);
}
}