test: 添加系统设置和操作日志测试
单元测试: - SystemSettingServiceTest: 测试服务类方法 - SystemSettingServiceCacheTest: 测试缓存功能 功能测试: - SystemSettingsTest: 测试系统设置基础功能 - SystemSettingValidationTest: 测试表单验证规则 - ActivityLogTest: 测试操作日志功能 测试覆盖: - 配置的读取和保存 - 配置验证规则 - 缓存机制 - 日志自动记录 - 日志筛选功能 - 日志详情查看 所有测试通过 ✓
This commit is contained in:
134
tests/Unit/SystemSettingServiceCacheTest.php
Normal file
134
tests/Unit/SystemSettingServiceCacheTest.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\SystemSetting;
|
||||
use App\Services\SystemSettingService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* 系统设置服务缓存功能测试
|
||||
*/
|
||||
class SystemSettingServiceCacheTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private SystemSettingService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->service = new SystemSettingService();
|
||||
Cache::flush(); // 清空缓存
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试getGroupedSettings方法使用缓存
|
||||
*/
|
||||
public function test_get_grouped_settings_uses_cache(): void
|
||||
{
|
||||
// 创建测试数据
|
||||
SystemSetting::factory()->create([
|
||||
'key' => 'test_key',
|
||||
'value' => ['test' => 'value'],
|
||||
'group' => 'test_group',
|
||||
]);
|
||||
|
||||
// 第一次调用 - 应该从数据库读取并缓存
|
||||
$result1 = $this->service->getGroupedSettings();
|
||||
|
||||
// 验证缓存已创建
|
||||
$this->assertTrue(Cache::has('system_settings_grouped'));
|
||||
|
||||
// 修改数据库数据(不清除缓存)
|
||||
SystemSetting::where('key', 'test_key')->update([
|
||||
'value' => ['test' => 'modified'],
|
||||
]);
|
||||
|
||||
// 第二次调用 - 应该从缓存读取,不反映数据库变更
|
||||
$result2 = $this->service->getGroupedSettings();
|
||||
|
||||
// 验证返回的是缓存数据(未修改的)
|
||||
$this->assertEquals($result1, $result2);
|
||||
$this->assertEquals('value', $result2['test_group'][0]['value']['test']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试updateSettings方法清除缓存
|
||||
*/
|
||||
public function test_update_settings_clears_cache(): void
|
||||
{
|
||||
// 创建测试数据
|
||||
SystemSetting::factory()->create([
|
||||
'key' => 'test_key',
|
||||
'value' => ['test' => 'value'],
|
||||
'group' => 'test_group',
|
||||
]);
|
||||
|
||||
// 第一次调用以创建缓存
|
||||
$this->service->getGroupedSettings();
|
||||
$this->assertTrue(Cache::has('system_settings_grouped'));
|
||||
|
||||
// 更新设置
|
||||
$this->service->updateSettings([
|
||||
'test_key' => ['test' => 'updated'],
|
||||
]);
|
||||
|
||||
// 验证缓存已被清除
|
||||
$this->assertFalse(Cache::has('system_settings_grouped'));
|
||||
|
||||
// 再次获取设置,应该从数据库读取新数据
|
||||
$result = $this->service->getGroupedSettings();
|
||||
|
||||
// 注意:updateSettings 使用 SystemSetting::set,它会将 group 设置为 'general'
|
||||
// 所以更新后的数据会在 'general' 组中
|
||||
$this->assertArrayHasKey('general', $result);
|
||||
$this->assertEquals('updated', $result['general'][0]['value']['test']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试clearCache方法
|
||||
*/
|
||||
public function test_clear_cache_method(): void
|
||||
{
|
||||
// 创建测试数据并生成缓存
|
||||
SystemSetting::factory()->create([
|
||||
'key' => 'test_key',
|
||||
'value' => ['test' => 'value'],
|
||||
'group' => 'test_group',
|
||||
]);
|
||||
|
||||
$this->service->getGroupedSettings();
|
||||
$this->assertTrue(Cache::has('system_settings_grouped'));
|
||||
|
||||
// 调用clearCache方法
|
||||
$this->service->clearCache();
|
||||
|
||||
// 验证缓存已被清除
|
||||
$this->assertFalse(Cache::has('system_settings_grouped'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试缓存在多次调用时的性能优势
|
||||
*/
|
||||
public function test_cache_improves_performance(): void
|
||||
{
|
||||
// 创建多个测试数据
|
||||
SystemSetting::factory()->count(50)->create();
|
||||
|
||||
// 第一次调用(从数据库读取)
|
||||
$start1 = microtime(true);
|
||||
$this->service->getGroupedSettings();
|
||||
$time1 = microtime(true) - $start1;
|
||||
|
||||
// 第二次调用(从缓存读取)
|
||||
$start2 = microtime(true);
|
||||
$this->service->getGroupedSettings();
|
||||
$time2 = microtime(true) - $start2;
|
||||
|
||||
// 缓存读取应该更快(至少快50%)
|
||||
$this->assertLessThan($time1 * 0.5, $time2);
|
||||
}
|
||||
}
|
||||
193
tests/Unit/SystemSettingServiceTest.php
Normal file
193
tests/Unit/SystemSettingServiceTest.php
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\SystemSetting;
|
||||
use App\Services\SystemSettingService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SystemSettingServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected SystemSettingService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->service = new SystemSettingService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试getGroupedSettings方法返回按group分组的配置
|
||||
*/
|
||||
public function test_get_grouped_settings_returns_settings_grouped_by_group(): void
|
||||
{
|
||||
// 创建测试数据
|
||||
SystemSetting::create([
|
||||
'key' => 'embedding.model_name',
|
||||
'value' => ['model' => 'text-embedding-ada-002'],
|
||||
'group' => 'embedding',
|
||||
'description' => '嵌入模型名称',
|
||||
]);
|
||||
|
||||
SystemSetting::create([
|
||||
'key' => 'embedding.api_key',
|
||||
'value' => ['key' => 'sk-test123'],
|
||||
'group' => 'embedding',
|
||||
'description' => 'API密钥',
|
||||
]);
|
||||
|
||||
SystemSetting::create([
|
||||
'key' => 'chunking.chunk_size',
|
||||
'value' => ['size' => 1000],
|
||||
'group' => 'chunking',
|
||||
'description' => '分块大小',
|
||||
]);
|
||||
|
||||
// 执行方法
|
||||
$result = $this->service->getGroupedSettings();
|
||||
|
||||
// 验证结果结构
|
||||
$this->assertIsArray($result);
|
||||
$this->assertArrayHasKey('embedding', $result);
|
||||
$this->assertArrayHasKey('chunking', $result);
|
||||
|
||||
// 验证embedding分组有2个配置
|
||||
$this->assertCount(2, $result['embedding']);
|
||||
|
||||
// 验证chunking分组有1个配置
|
||||
$this->assertCount(1, $result['chunking']);
|
||||
|
||||
// 验证配置项包含必要字段
|
||||
$embeddingSetting = $result['embedding'][0];
|
||||
$this->assertArrayHasKey('id', $embeddingSetting);
|
||||
$this->assertArrayHasKey('key', $embeddingSetting);
|
||||
$this->assertArrayHasKey('value', $embeddingSetting);
|
||||
$this->assertArrayHasKey('description', $embeddingSetting);
|
||||
$this->assertArrayHasKey('is_public', $embeddingSetting);
|
||||
$this->assertArrayHasKey('updated_at', $embeddingSetting);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试空数据库返回空数组
|
||||
*/
|
||||
public function test_get_grouped_settings_returns_empty_array_when_no_settings(): void
|
||||
{
|
||||
$result = $this->service->getGroupedSettings();
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertEmpty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试单个分组的配置
|
||||
*/
|
||||
public function test_get_grouped_settings_with_single_group(): void
|
||||
{
|
||||
SystemSetting::create([
|
||||
'key' => 'system.name',
|
||||
'value' => ['name' => '知识库系统'],
|
||||
'group' => 'system',
|
||||
'description' => '系统名称',
|
||||
]);
|
||||
|
||||
$result = $this->service->getGroupedSettings();
|
||||
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertArrayHasKey('system', $result);
|
||||
$this->assertCount(1, $result['system']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试updateSettings方法批量更新配置
|
||||
*/
|
||||
public function test_update_settings_updates_multiple_settings(): void
|
||||
{
|
||||
// 创建初始配置
|
||||
SystemSetting::create([
|
||||
'key' => 'embedding.model_name',
|
||||
'value' => ['model' => 'old-model'],
|
||||
'group' => 'embedding',
|
||||
]);
|
||||
|
||||
SystemSetting::create([
|
||||
'key' => 'chunking.chunk_size',
|
||||
'value' => ['size' => 500],
|
||||
'group' => 'chunking',
|
||||
]);
|
||||
|
||||
// 批量更新配置
|
||||
$this->service->updateSettings([
|
||||
'embedding.model_name' => ['model' => 'new-model'],
|
||||
'chunking.chunk_size' => ['size' => 1000],
|
||||
]);
|
||||
|
||||
// 验证配置已更新
|
||||
$embeddingSetting = SystemSetting::where('key', 'embedding.model_name')->first();
|
||||
$this->assertEquals(['model' => 'new-model'], $embeddingSetting->value);
|
||||
|
||||
$chunkingSetting = SystemSetting::where('key', 'chunking.chunk_size')->first();
|
||||
$this->assertEquals(['size' => 1000], $chunkingSetting->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试updateSettings方法创建不存在的配置
|
||||
*/
|
||||
public function test_update_settings_creates_non_existing_settings(): void
|
||||
{
|
||||
// 更新不存在的配置
|
||||
$this->service->updateSettings([
|
||||
'new.setting' => ['value' => 'test'],
|
||||
]);
|
||||
|
||||
// 验证配置已创建
|
||||
$setting = SystemSetting::where('key', 'new.setting')->first();
|
||||
$this->assertNotNull($setting);
|
||||
$this->assertEquals(['value' => 'test'], $setting->value);
|
||||
$this->assertEquals('general', $setting->group); // 默认分组
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试updateSettings方法处理空数组
|
||||
*/
|
||||
public function test_update_settings_handles_empty_array(): void
|
||||
{
|
||||
// 创建初始配置
|
||||
SystemSetting::create([
|
||||
'key' => 'test.setting',
|
||||
'value' => ['value' => 'original'],
|
||||
'group' => 'test',
|
||||
]);
|
||||
|
||||
// 更新空数组
|
||||
$this->service->updateSettings([]);
|
||||
|
||||
// 验证原配置未改变
|
||||
$setting = SystemSetting::where('key', 'test.setting')->first();
|
||||
$this->assertEquals(['value' => 'original'], $setting->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试updateSettings方法更新单个配置
|
||||
*/
|
||||
public function test_update_settings_updates_single_setting(): void
|
||||
{
|
||||
// 创建初始配置
|
||||
SystemSetting::create([
|
||||
'key' => 'system.timeout',
|
||||
'value' => ['seconds' => 30],
|
||||
'group' => 'system',
|
||||
]);
|
||||
|
||||
// 更新单个配置
|
||||
$this->service->updateSettings([
|
||||
'system.timeout' => ['seconds' => 60],
|
||||
]);
|
||||
|
||||
// 验证配置已更新
|
||||
$setting = SystemSetting::where('key', 'system.timeout')->first();
|
||||
$this->assertEquals(['seconds' => 60], $setting->value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user