Files
KnowledgeBase/tests/Unit/SystemSettingServiceCacheTest.php
lizhuoran 112aec6b09 test: 添加系统设置和操作日志测试
单元测试:
- SystemSettingServiceTest: 测试服务类方法
- SystemSettingServiceCacheTest: 测试缓存功能

功能测试:
- SystemSettingsTest: 测试系统设置基础功能
- SystemSettingValidationTest: 测试表单验证规则
- ActivityLogTest: 测试操作日志功能

测试覆盖:
- 配置的读取和保存
- 配置验证规则
- 缓存机制
- 日志自动记录
- 日志筛选功能
- 日志详情查看

所有测试通过 ✓
2026-03-09 10:08:57 +08:00

135 lines
4.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
}
}