매 요청 균일하게 발생하던 부팅 오버헤드를 4개 축에서 제거한다. - 설치 완료 상태에서 매 요청 반복되던 information_schema 조회(알림·본인인증 테이블 hasTable)를 installer_completed 가드로 스킵. 미설치 환경은 기존 hasTable 폴백 유지. - 코어+모듈+플러그인 정적 훅 매핑을 bootstrap/cache/hooks.php 에 사전 계산해 매 요청 디렉토리 스캔·리플렉션·클래스 로딩을 제거(route:cache 동형). 등록↔발화 계약·매핑 바이트 동일. 확장/코어 변경 시 자동 재생성, 캐시 부재·손상은 스캔 폴백. - 확장 소스(Modules\*/Plugins\*)를 autoload-extensions.php 의 classmap 에 편입해 findFile 파일시스템 스캔을 제거(느린 FS·cold OPcache 환경 직격). 클래스 로딩은 여전히 lazy, PSR-4 폴백 유지. - config 캐시를 변경 지점(설정 저장/코어·확장 업데이트/APP_KEY 재생성/설치 완료)에서 clear 후 즉시 재생성하도록 ConfigCacheHelper 로 일원화. clear 만 하고 방치돼 캐시가 영구 비활성으로 남던 성능 손실 제거. HookCacheManager::read 요청당 1회 로드(memo)로 중복 파싱 제거. 부팅 매핑 fingerprint 스캔↔캐시 완전 동일(229 action + 35 filter). 신규/수정 8개 스위트 54 pass. 순수 내부 부팅 인프라 — 확장 공개 표면·훅 계약 불변.
217 lines
7.1 KiB
PHP
217 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use App\Models\NotificationDefinition;
|
|
use App\Services\NotificationDefinitionService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* NotificationDefinitionService 테스트
|
|
*
|
|
* 알림 정의 조회, 캐싱, 수정 동작을 검증합니다.
|
|
*/
|
|
class NotificationDefinitionServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private NotificationDefinitionService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->service = app(NotificationDefinitionService::class);
|
|
}
|
|
|
|
/**
|
|
* resolve()가 활성 정의를 반환하는지 확인
|
|
*/
|
|
public function test_resolve_returns_active_definition(): void
|
|
{
|
|
NotificationDefinition::create([
|
|
'type' => 'welcome',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '환영', 'en' => 'Welcome'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => ['core.auth.after_register'],
|
|
'is_active' => true,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
$this->service->invalidateCache('welcome');
|
|
|
|
$result = $this->service->resolve('welcome');
|
|
|
|
$this->assertNotNull($result);
|
|
$this->assertEquals('welcome', $result->type);
|
|
}
|
|
|
|
/**
|
|
* resolve()가 비활성 정의를 반환하지 않는지 확인
|
|
*/
|
|
public function test_resolve_returns_null_for_inactive_definition(): void
|
|
{
|
|
NotificationDefinition::create([
|
|
'type' => 'inactive_type',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '비활성', 'en' => 'Inactive'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => [],
|
|
'is_active' => false,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
$this->service->invalidateCache('inactive_type');
|
|
|
|
$result = $this->service->resolve('inactive_type');
|
|
|
|
$this->assertNull($result);
|
|
}
|
|
|
|
/**
|
|
* getAllActive()가 활성 정의만 반환하는지 확인
|
|
*/
|
|
public function test_get_all_active_returns_only_active(): void
|
|
{
|
|
NotificationDefinition::create([
|
|
'type' => 'active_one',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '활성1'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => [],
|
|
'is_active' => true,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
NotificationDefinition::create([
|
|
'type' => 'inactive_one',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '비활성1'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => [],
|
|
'is_active' => false,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
$this->service->invalidateAllCache();
|
|
|
|
$result = $this->service->getAllActive();
|
|
|
|
$this->assertCount(1, $result);
|
|
$this->assertEquals('active_one', $result->first()->type);
|
|
}
|
|
|
|
/**
|
|
* toggleActive()가 활성 상태를 반전시키는지 확인
|
|
*/
|
|
public function test_toggle_active_inverts_status(): void
|
|
{
|
|
$definition = NotificationDefinition::create([
|
|
'type' => 'toggle_test',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '토글 테스트'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => [],
|
|
'is_active' => true,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
$result = $this->service->toggleActive($definition);
|
|
|
|
$this->assertFalse($result->is_active);
|
|
}
|
|
|
|
/**
|
|
* updateDefinition()이 채널과 훅을 수정하는지 확인
|
|
*/
|
|
public function test_update_definition_modifies_channels_and_hooks(): void
|
|
{
|
|
$definition = NotificationDefinition::create([
|
|
'type' => 'update_test',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '수정 테스트'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => ['core.auth.after_register'],
|
|
'is_active' => true,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
$result = $this->service->updateDefinition($definition, [
|
|
'channels' => ['mail', 'database'],
|
|
'hooks' => ['core.auth.after_register', 'core.auth.after_login'],
|
|
]);
|
|
|
|
$this->assertEquals(['mail', 'database'], $result->channels);
|
|
$this->assertEquals(['core.auth.after_register', 'core.auth.after_login'], $result->hooks);
|
|
}
|
|
|
|
/**
|
|
* getAllActive() 캐시 히트 시 notification_definitions DB 조회가 0건인지 확인.
|
|
*
|
|
* 서빙 API 부팅 비용 최적화 검증 (계획서 §2-1 나): 동적 훅(알림) 등록 경로
|
|
* (NotificationHookListener::registerDynamicHooks → getAllActive)는 이미
|
|
* `['notification']` 태그로 캐시되어 있으므로, 첫 요청(캐시 워밍) 이후에는
|
|
* 매 요청 DB 조회가 발생하지 않아야 한다 (37ms 캐시 미스는 첫 요청 1회 비용).
|
|
*
|
|
* @scenario cache_state=present_but_testing_env, listener_type=action_sync, regeneration_trigger=extension_update, registration_source=plugin
|
|
*
|
|
* @effects notification_getAllActive_cache_hit_issues_zero_db_query
|
|
*/
|
|
public function test_get_all_active_cache_hit_issues_zero_db_query(): void
|
|
{
|
|
NotificationDefinition::create([
|
|
'type' => 'cache_hit_probe',
|
|
'hook_prefix' => 'core.auth',
|
|
'extension_type' => 'core',
|
|
'extension_identifier' => 'core',
|
|
'name' => ['ko' => '캐시 히트 검증'],
|
|
'variables' => [],
|
|
'channels' => ['mail'],
|
|
'hooks' => ['core.auth.after_register'],
|
|
'is_active' => true,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
// 1차 호출: 캐시 워밍 (DB 조회 발생)
|
|
$this->service->invalidateAllCache();
|
|
$this->service->getAllActive();
|
|
|
|
// 2차 호출: 캐시 히트 → notification_definitions 조회 0건이어야 함
|
|
DB::enableQueryLog();
|
|
$this->service->getAllActive();
|
|
$queries = DB::getQueryLog();
|
|
DB::disableQueryLog();
|
|
|
|
$definitionQueries = array_filter(
|
|
$queries,
|
|
fn ($q) => str_contains($q['query'], 'notification_definitions')
|
|
);
|
|
|
|
$this->assertCount(
|
|
0,
|
|
$definitionQueries,
|
|
'캐시 히트 시 notification_definitions DB 조회가 발생하지 않아야 합니다 (동적 훅 등록 비용 제거)'
|
|
);
|
|
}
|
|
}
|