cache = new CoreCacheDriver('array');
$this->app->instance(CacheInterface::class, $this->cache);
// SEO 캐시 활성화
$this->app['config']->set('g7_settings.core.seo.cache_enabled', true);
$this->app['config']->set('g7_settings.core.seo.cache_ttl', 7200);
$this->manager = new SeoCacheManager($this->cache);
}
protected function tearDown(): void
{
Cache::flush();
parent::tearDown();
}
// ========================================================================
// C-1. SeoCacheManager
// ========================================================================
/**
* C-1-1: SEO 페이지 캐시 생성 — put() 호출 시 새 키 형식.
*/
#[Test]
public function c_1_1_seo_page_cache_uses_new_prefix(): void
{
$this->manager->put('/board/notice/123', 'ko', 'Hello');
$expectedKey = 'seo.page.'.md5('/board/notice/123|ko');
$this->assertSame('Hello', $this->cache->get($expectedKey));
$this->assertSame(
'g7:core:'.$expectedKey,
$this->cache->resolveKey($expectedKey)
);
}
/**
* C-1-2: SEO 페이지 캐시 히트.
*/
#[Test]
public function c_1_2_seo_page_cache_hit(): void
{
$this->manager->put('/page', 'ko', 'Cached');
$this->assertSame('Cached', $this->manager->get('/page', 'ko'));
$this->assertSame('Cached', $this->manager->get('/page', 'ko'));
}
/**
* C-1-3: URL 패턴 기반 무효화 (와일드카드).
*/
#[Test]
public function c_1_3_invalidate_by_url_pattern_wildcards(): void
{
$this->manager->put('/board/notice/1', 'ko', '1');
$this->manager->put('/board/notice/2', 'ko', '2');
$this->manager->put('/board/free/1', 'ko', 'free');
$count = $this->manager->invalidateByUrl('/board/notice/*');
$this->assertSame(2, $count);
$this->assertNull($this->manager->get('/board/notice/1', 'ko'));
$this->assertNull($this->manager->get('/board/notice/2', 'ko'));
$this->assertSame('free', $this->manager->get('/board/free/1', 'ko'));
}
/**
* C-1-5: clearAll() — 모든 SEO 페이지 캐시 + 인덱스 삭제.
*/
#[Test]
public function c_1_5_clear_all_removes_pages_and_index(): void
{
$this->manager->put('/page1', 'ko', '');
$this->manager->put('/page2', 'ko', '');
$this->assertNotEmpty($this->manager->getCachedUrls());
$this->manager->clearAll();
$this->assertEmpty($this->manager->getCachedUrls());
$this->assertNull($this->manager->get('/page1', 'ko'));
$this->assertNull($this->manager->get('/page2', 'ko'));
}
/**
* C-1-7: SEO 캐시 비활성 시 저장 안 함.
*/
#[Test]
public function c_1_7_disabled_cache_does_not_store(): void
{
$this->app['config']->set('g7_settings.core.cache.seo_enabled', false);
$this->app['config']->set('g7_settings.core.seo.cache_enabled', false);
$this->manager->put('/page', 'ko', '
');
$this->assertNull($this->manager->get('/page', 'ko'));
}
/**
* C-1-extra: invalidateByLayout — 레이아웃 정보 매칭 삭제.
*/
#[Test]
public function c_1_extra_invalidate_by_layout(): void
{
$this->manager->putWithLayout('/board/list', 'ko', '
', 'board.list');
$this->manager->putWithLayout('/board/show/1', 'ko', '', 'board.show');
$count = $this->manager->invalidateByLayout('board.list');
$this->assertSame(1, $count);
$this->assertNull($this->manager->get('/board/list', 'ko'));
$this->assertSame('', $this->manager->get('/board/show/1', 'ko'));
}
// ========================================================================
// C-3. Sitemap key format (GenerateSitemapJob, SitemapController)
// ========================================================================
/**
* C-3-1: Sitemap 캐시 키는 'seo.sitemap' 으로 통일.
*/
#[Test]
public function c_3_1_sitemap_cache_uses_new_key(): void
{
$this->cache->put('seo.sitemap', '', 86400);
$this->assertSame('', $this->cache->get('seo.sitemap'));
$this->assertSame(
'g7:core:seo.sitemap',
$this->cache->resolveKey('seo.sitemap')
);
}
/**
* C-1-4: 게시글 삭제 시 URL 패턴 + 목록 페이지 동시 무효화.
*/
#[Test]
public function c_1_4_post_delete_invalidates_detail_and_list(): void
{
$this->manager->put('/board/notice/123', 'ko', '');
$this->manager->put('/board/notice', 'ko', '
');
// 패턴 무효화: /board/notice* 모두 삭제
$count = $this->manager->invalidateByUrl('/board/notice*');
$this->assertSame(2, $count);
$this->assertNull($this->manager->get('/board/notice/123', 'ko'));
$this->assertNull($this->manager->get('/board/notice', 'ko'));
}
/**
* C-1-6: 모듈 설치 시 SeoCacheManager.clearAll() 동작 검증.
*/
#[Test]
public function c_1_6_module_install_clears_all(): void
{
$this->manager->put('/page1', 'ko', 'a');
$this->manager->put('/page2', 'ko', 'b');
$this->manager->put('/page3', 'ko', 'c');
// 모듈 설치 → SeoExtensionCacheListener 가 clearAll 호출
$this->manager->clearAll();
$this->assertNull($this->manager->get('/page1', 'ko'));
$this->assertNull($this->manager->get('/page2', 'ko'));
$this->assertNull($this->manager->get('/page3', 'ko'));
$this->assertEmpty($this->manager->getCachedUrls());
}
// ========================================================================
// C-2. SeoConfigMerger 병합 캐시 키 형식
// ========================================================================
/**
* C-2-1: SEO config 병합 캐시 키 형식 — 'seo.config.{templateId}'.
*/
#[Test]
public function c_2_1_seo_config_cache_key_format(): void
{
$this->cache->put('seo.config.sirsoft-admin_basic', ['merged' => true]);
$this->assertSame(
'g7:core:seo.config.sirsoft-admin_basic',
$this->cache->resolveKey('seo.config.sirsoft-admin_basic')
);
$this->assertTrue($this->cache->has('seo.config.sirsoft-admin_basic'));
}
/**
* C-2-2: SEO config 캐시 히트.
*/
#[Test]
public function c_2_2_seo_config_cache_hit(): void
{
$this->cache->put('seo.config.sirsoft-basic', ['v' => 1]);
$this->assertSame(['v' => 1], $this->cache->get('seo.config.sirsoft-basic'));
$this->assertSame(['v' => 1], $this->cache->get('seo.config.sirsoft-basic'));
}
/**
* C-2-3: 확장 변경 시 config 캐시 무효화.
*/
#[Test]
public function c_2_3_extension_change_invalidates_config_cache(): void
{
$this->cache->put('seo.config.sirsoft-basic', ['old' => true]);
$this->cache->forget('seo.config.sirsoft-basic');
$this->assertFalse($this->cache->has('seo.config.sirsoft-basic'));
}
/**
* C-3-3: 확장 변경 시 Sitemap 캐시 무효화 (SeoExtensionCacheListener 동작).
*/
#[Test]
public function c_3_3_extension_change_invalidates_sitemap(): void
{
$this->cache->put('seo.sitemap', '');
$this->cache->forget('seo.sitemap');
$this->assertFalse($this->cache->has('seo.sitemap'));
}
/**
* C-3-4: SEO 설정 변경 시 Sitemap 캐시 무효화 (SeoSettingsCacheListener 동작).
*/
#[Test]
public function c_3_4_seo_settings_change_invalidates_sitemap(): void
{
$this->cache->put('seo.sitemap', '');
// SeoSettingsCacheListener 동작 시뮬레이션
$this->cache->forget('seo.sitemap');
$this->assertFalse($this->cache->has('seo.sitemap'));
}
}