daumPostcodeExistedBefore = File::isDirectory($activePath); if (! $this->daumPostcodeExistedBefore && File::isDirectory($bundledPath)) { File::copyDirectory($bundledPath, $activePath); } // 확장 디렉토리 보호 활성화 $this->setUpExtensionProtection(); // 플러그인 매니저가 플러그인 디렉토리를 스캔하도록 초기화 $pluginManager = app(PluginManager::class); $pluginManager->loadPlugins(); } protected function tearDown(): void { // 확장 디렉토리 보호 해제 $this->tearDownExtensionProtection(); // 테스트에서 생성한 활성 디렉토리만 정리 if (! $this->daumPostcodeExistedBefore) { $activePath = base_path('plugins/sirsoft-daum_postcode'); if (File::isDirectory($activePath)) { File::deleteDirectory($activePath); } } parent::tearDown(); } // ======================================================================== // Non-DDL 테스트: 플러그인 설치 없이 실행 가능한 빠른 테스트들 // 이 테스트들은 DDL을 유발하지 않으므로 트랜잭션 격리가 유지됨 // ======================================================================== /** * plugin:list 커맨드가 플러그인 목록을 정상적으로 출력하는지 테스트 */ public function test_list_command_displays_plugins(): void { $this->artisan('plugin:list') ->assertExitCode(0); } /** * plugin:list 커맨드가 상태 필터를 정상적으로 처리하는지 테스트 */ public function test_list_command_filters_by_status(): void { // active 상태 필터 $this->artisan('plugin:list', ['--status' => 'active']) ->assertExitCode(0); // uninstalled 상태 필터 $this->artisan('plugin:list', ['--status' => 'uninstalled']) ->assertExitCode(0); // 잘못된 상태 $this->artisan('plugin:list', ['--status' => 'invalid']) ->assertExitCode(1); } /** * plugin:list 커맨드가 플러그인이 없을 때 적절한 메시지를 출력하는지 테스트 */ public function test_list_command_shows_message_when_no_plugins(): void { $this->artisan('plugin:list', ['--status' => 'installed']) ->expectsOutput(__('plugins.commands.list.no_plugins')) ->assertExitCode(0); } /** * plugin:install 커맨드가 존재하지 않는 플러그인에 대해 실패하는지 테스트 * * 이 테스트는 DDL을 유발하지 않음 (플러그인이 없어서 마이그레이션 실행 안 됨) */ public function test_install_command_fails_for_nonexistent_plugin(): void { $identifier = 'nonexistent-plugin'; $this->artisan('plugin:install', ['identifier' => $identifier]) ->assertExitCode(1); } /** * plugin:check-updates 커맨드가 존재하지 않는 플러그인에 대해 실패하는지 테스트 */ public function test_check_updates_fails_for_nonexistent_plugin(): void { $identifier = 'nonexistent-plugin'; $this->artisan('plugin:check-updates', ['identifier' => $identifier]) ->assertExitCode(1); } /** * plugin:update 커맨드가 미설치 플러그인에 대해 실패하는지 테스트 */ public function test_update_fails_for_noninstalled_plugin(): void { $identifier = 'sirsoft-daum_postcode'; $this->artisan('plugin:update', ['identifier' => $identifier]) ->assertExitCode(1); } /** * plugin:update --force 커맨드가 미설치 플러그인에 대해 실패하는지 테스트 */ public function test_update_force_fails_for_noninstalled_plugin(): void { $identifier = 'sirsoft-daum_postcode'; $this->artisan('plugin:update', ['identifier' => $identifier, '--force' => true]) ->assertExitCode(1); } /** * plugin:cache-clear 커맨드가 모든 캐시를 삭제하는지 테스트 * * 실재하는 상태 키(ext.plugins.*_identifiers)가 소멸하고 캐시 버전이 bump 되는지 * 검증한다 (#588 — 기존 forget 대상은 writer 없는 유령 키였다). */ public function test_cache_clear_command_clears_all_caches(): void { $coreCache = new CoreCacheDriver(config('cache.default', 'array')); $coreCache->put('ext.plugins.active_identifiers', ['stale-plugin'], 3600); $coreCache->put('ext.plugins.installed_identifiers', ['stale-plugin'], 3600); // 실제 writer(ExtensionMiddlewareRegistry::buildIndex)와 동형으로 태그 포함 // remember — flush() 는 taggable 스토어에서 태그 인덱스 기반으로 삭제한다 $coreCache->remember('extension.middleware_index', fn () => [['stale' => true]], 3600, [ExtensionMiddlewareRegistry::CACHE_TAG]); Cache::put('g7:core:ext.cache_version', 1000); $this->artisan('plugin:cache-clear') ->expectsOutput(__('plugins.commands.cache_clear.clearing_all')) ->assertExitCode(0); $this->assertNull( $coreCache->get('ext.plugins.active_identifiers'), 'plugin:cache-clear 후 활성 플러그인 상태 캐시가 삭제되어야 합니다.' ); $this->assertNull( $coreCache->get('ext.plugins.installed_identifiers'), 'plugin:cache-clear 후 설치 플러그인 상태 캐시가 삭제되어야 합니다.' ); $this->assertNull( $coreCache->get('extension.middleware_index'), 'plugin:cache-clear 후 확장 미들웨어 인덱스 캐시(extension.middleware_index)가 삭제되어야 합니다.' ); $this->assertGreaterThan( 1000, ClearsTemplateCaches::getExtensionCacheVersion(), 'plugin:cache-clear 후 확장 캐시 버전이 bump 되어야 합니다.' ); } /** * plugin:cache-clear 커맨드가 특정 플러그인의 캐시만 삭제하는지 테스트 */ public function test_cache_clear_command_clears_single_plugin_cache(): void { $identifier = 'sirsoft-daum_postcode'; $coreCache = new CoreCacheDriver(config('cache.default', 'array')); $coreCache->put('ext.plugins.active_identifiers', ['stale-plugin'], 3600); // 실제 writer(ExtensionMiddlewareRegistry::buildIndex)와 동형으로 태그 포함 // remember — flush() 는 taggable 스토어에서 태그 인덱스 기반으로 삭제한다 $coreCache->remember('extension.middleware_index', fn () => [['stale' => true]], 3600, [ExtensionMiddlewareRegistry::CACHE_TAG]); Cache::put('g7:core:ext.cache_version', 1000); $this->artisan('plugin:cache-clear', ['identifier' => $identifier]) ->expectsOutput(__('plugins.commands.cache_clear.clearing_single', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertNull( $coreCache->get('ext.plugins.active_identifiers'), 'plugin:cache-clear(단일) 후 플러그인 상태 캐시가 삭제되어야 합니다.' ); $this->assertNull( $coreCache->get('extension.middleware_index'), 'plugin:cache-clear(단일) 후 확장 미들웨어 인덱스 캐시(extension.middleware_index)가 삭제되어야 합니다.' ); $this->assertGreaterThan( 1000, ClearsTemplateCaches::getExtensionCacheVersion(), 'plugin:cache-clear(단일) 후 확장 캐시 버전이 bump 되어야 합니다.' ); } /** * plugin:cache-clear 커맨드가 존재하지 않는 플러그인에 대해 실패하는지 테스트 */ public function test_cache_clear_command_fails_for_nonexistent_plugin(): void { $identifier = 'nonexistent-plugin'; $this->artisan('plugin:cache-clear', ['identifier' => $identifier]) ->assertExitCode(1); } // ======================================================================== // DDL 통합 테스트: 플러그인 설치가 필요한 모든 테스트를 하나로 통합 // // ⚠️ 중요: DDL을 유발하는 테스트는 반드시 하나의 테스트 메서드로 통합 // PHPUnit은 테스트 메서드 간에 트랜잭션을 롤백하는데, DDL(CREATE TABLE 등)은 // 암시적 커밋을 유발하여 트랜잭션을 깨뜨림. 다음 테스트에서 migrate:fresh 재실행 필요. // // 개별 테스트로 분리하면 각각 12-14초 소요 (마이그레이션 재실행) // 하나의 테스트로 통합하면 전체 1-2초 내에 완료 // ======================================================================== /** * 플러그인 Artisan 커맨드 전체 워크플로우 통합 테스트 * * ⚠️ DDL 유발: 플러그인 설치 시 마이그레이션 실행 * * 이 테스트는 플러그인 설치/활성화/비활성화/삭제와 관련된 모든 시나리오를 * 하나의 테스트 메서드에서 순차적으로 실행합니다. * * 테스트 시나리오: * ───────────────────────────────────────────────────────────────────── * Part 1: 미설치 상태에서의 실패 케이스 * - 미설치 플러그인 활성화 실패 * - 미설치 플러그인 비활성화 실패 * - 미설치 플러그인 삭제 실패 * * Part 2: 플러그인 설치 워크플로우 * - 플러그인 설치 성공 * - 이미 설치된 플러그인 재설치 경고 * - 비활성 상태에서 비활성화 시도 실패 * * Part 3: 플러그인 활성화/비활성화 워크플로우 * - 플러그인 활성화 성공 * - 이미 활성화된 플러그인 재활성화 경고 * - 플러그인 비활성화 성공 * * Part 4: 플러그인 삭제 워크플로우 * - 삭제 확인에서 거절 → 플러그인 유지 * - 삭제 확인에서 승인 → 플러그인 삭제 및 권한 삭제 * * Part 5: --force 옵션 테스트 * - 플러그인 재설치 → --force 옵션으로 확인 없이 삭제 * * Part 6: 전체 라이프사이클 워크플로우 * - install → activate → deactivate → uninstall 순서 실행 * ───────────────────────────────────────────────────────────────────── */ public function test_z_plugin_commands_full_workflow(): void { $identifier = 'sirsoft-daum_postcode'; // ===================================================================== // Part 1: 미설치 상태에서의 실패 케이스 // ===================================================================== // 미설치 플러그인 활성화 실패 $this->artisan('plugin:activate', ['identifier' => $identifier]) ->assertExitCode(1); // 미설치 플러그인 비활성화 실패 $this->artisan('plugin:deactivate', ['identifier' => $identifier]) ->assertExitCode(1); // 미설치 플러그인 삭제 실패 $this->artisan('plugin:uninstall', ['identifier' => $identifier]) ->assertExitCode(1); // ===================================================================== // Part 2: 플러그인 설치 워크플로우 // ===================================================================== // 플러그인 설치 성공 $this->artisan('plugin:install', ['identifier' => $identifier]) ->expectsOutput('✅ '.__('plugins.commands.install.success', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'vendor' => 'sirsoft', 'status' => ExtensionStatus::Inactive->value, ]); // 이미 설치된 플러그인 재설치 경고 $this->artisan('plugin:install', ['identifier' => $identifier]) ->expectsOutput('⚠️ '.__('plugins.commands.install.already_installed', ['plugin' => $identifier])) ->assertExitCode(1); // 비활성 상태에서 비활성화 시도 (실패) $this->artisan('plugin:deactivate', ['identifier' => $identifier]) ->assertExitCode(1); // ===================================================================== // Part 3: 플러그인 활성화/비활성화 워크플로우 // ===================================================================== // 플러그인 활성화 성공 $this->artisan('plugin:activate', ['identifier' => $identifier]) ->expectsOutput('✅ '.__('plugins.commands.activate.success', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'status' => ExtensionStatus::Active->value, ]); // 이미 활성화된 플러그인 재활성화 경고 $this->artisan('plugin:activate', ['identifier' => $identifier]) ->expectsOutput('⚠️ '.__('plugins.commands.activate.already_active', ['plugin' => $identifier])) ->assertExitCode(1); // 플러그인 비활성화 성공 $this->artisan('plugin:deactivate', ['identifier' => $identifier]) ->expectsOutput('✅ '.__('plugins.commands.deactivate.success', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'status' => ExtensionStatus::Inactive->value, ]); // ===================================================================== // Part 4: 플러그인 삭제 워크플로우 // ===================================================================== // 삭제 확인에서 거절 → 플러그인 유지 $this->artisan('plugin:uninstall', ['identifier' => $identifier]) ->expectsQuestion(__('plugins.commands.uninstall.confirm_question').' (yes/no) [no]', 'no') ->expectsOutput(__('plugins.commands.uninstall.aborted')) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, ]); // 삭제 확인에서 승인 → 플러그인 삭제 $this->artisan('plugin:uninstall', ['identifier' => $identifier]) ->expectsQuestion(__('plugins.commands.uninstall.confirm_question').' (yes/no) [no]', 'yes') ->expectsOutput('✅ '.__('plugins.commands.uninstall.success', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertDatabaseMissing('plugins', [ 'identifier' => $identifier, ]); // 권한도 삭제되었는지 확인 $this->assertEquals(0, Permission::byExtension(ExtensionOwnerType::Plugin, $identifier)->count()); // ===================================================================== // Part 5: check-updates / update 커맨드 테스트 // ===================================================================== // 재설치 $this->artisan('plugin:install', ['identifier' => $identifier]) ->assertExitCode(0); // check-updates 전체 확인 (설치된 플러그인이 있는 상태) $this->artisan('plugin:check-updates') ->assertExitCode(0); // check-updates 단일 확인 $this->artisan('plugin:check-updates', ['identifier' => $identifier]) ->assertExitCode(0); // update 커맨드 (업데이트 없는 경우 → 최신 상태 메시지) $this->artisan('plugin:update', ['identifier' => $identifier]) ->assertExitCode(0); // 삭제 $this->artisan('plugin:uninstall', ['identifier' => $identifier, '--force' => true]) ->assertExitCode(0); // ===================================================================== // Part 6: --force 옵션 테스트 // ===================================================================== // 재설치 $this->artisan('plugin:install', ['identifier' => $identifier]) ->assertExitCode(0); // --force 옵션으로 확인 없이 삭제 $this->artisan('plugin:uninstall', ['identifier' => $identifier, '--force' => true]) ->expectsOutput('✅ '.__('plugins.commands.uninstall.success', ['plugin' => $identifier])) ->assertExitCode(0); $this->assertDatabaseMissing('plugins', [ 'identifier' => $identifier, ]); // ===================================================================== // Part 7: 전체 라이프사이클 워크플로우 // install → activate → deactivate → uninstall // ===================================================================== // Install $this->artisan('plugin:install', ['identifier' => $identifier]) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'status' => ExtensionStatus::Inactive->value, ]); // Activate $this->artisan('plugin:activate', ['identifier' => $identifier]) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'status' => ExtensionStatus::Active->value, ]); // Deactivate $this->artisan('plugin:deactivate', ['identifier' => $identifier]) ->assertExitCode(0); $this->assertDatabaseHas('plugins', [ 'identifier' => $identifier, 'status' => ExtensionStatus::Inactive->value, ]); // Uninstall $this->artisan('plugin:uninstall', ['identifier' => $identifier, '--force' => true]) ->assertExitCode(0); $this->assertDatabaseMissing('plugins', [ 'identifier' => $identifier, ]); } /** * 잘못된 식별자 형식으로 플러그인 설치 시 FAILURE 반환 */ public function test_install_fails_with_invalid_identifier_format(): void { $this->artisan('plugin:install', ['identifier' => 'sirsoftpayment']) ->assertExitCode(1); } /** * 숫자로 시작하는 단어가 포함된 식별자로 플러그인 설치 시 FAILURE 반환 */ public function test_install_fails_with_digit_starting_identifier(): void { $this->artisan('plugin:install', ['identifier' => 'sirsoft-2payment']) ->assertExitCode(1); } /** * 특수문자가 포함된 식별자로 플러그인 설치 시 FAILURE 반환 */ public function test_install_fails_with_special_char_identifier(): void { $this->artisan('plugin:install', ['identifier' => 'sirsoft-pay@ment']) ->assertExitCode(1); } }