getSetting('language_currency.default_currency') ?? 'KRW'); } /** * 현재 기본 통화와 "다른" 통화를 반환합니다(변경 시도가 항상 실제 변경이 되도록). */ private function differentCurrency(): string { return $this->currentCurrency() === 'USD' ? 'KRW' : 'USD'; } /** * withValidator 가드만 실행해 base 통화 에러 존재 여부를 반환합니다. */ private function hasBaseLockError(string $newCurrency): bool { $request = StoreEcommerceSettingsRequest::create('/', 'POST', [ 'language_currency' => ['default_currency' => $newCurrency], ]); $request->setContainer($this->app); $validator = ValidatorFacade::make($request->all(), $request->rules()); $request->withValidator($validator); $validator->passes(); return $validator->errors()->has('language_currency.default_currency') && str_contains( (string) $validator->errors()->first('language_currency.default_currency'), __('sirsoft-ecommerce::validation.custom.language_currency.base_locked_after_data') ); } public function test_change_allowed_when_no_product_and_no_order(): void { $this->assertFalse( app(ProductRepositoryInterface::class)->existsAny(), '사전 조건: 상품 0건이어야 함' ); $this->assertFalse(app(OrderRepositoryInterface::class)->existsAny()); // 데이터 0건 → 현재와 다른 통화로 변경 통과 $this->assertFalse($this->hasBaseLockError($this->differentCurrency())); } public function test_change_blocked_when_product_exists(): void { Product::factory()->create(); $this->assertTrue(app(ProductRepositoryInterface::class)->existsAny()); // 상품 1건 → 현재와 다른 통화로의 base 변경 차단(422 메시지) $this->assertTrue($this->hasBaseLockError($this->differentCurrency())); } public function test_change_blocked_when_soft_deleted_product_only(): void { $product = Product::factory()->create(); $product->delete(); // soft delete $this->assertTrue( app(ProductRepositoryInterface::class)->existsAny(), '소프트삭제된 상품도 existsAny 가 true 여야 함(withTrashed)' ); $this->assertTrue($this->hasBaseLockError($this->differentCurrency())); } public function test_same_currency_resave_passes_even_with_data(): void { Product::factory()->create(); // 동일 통화(현재 default) 재저장은 변경이 아니므로 통과 $this->assertFalse($this->hasBaseLockError($this->currentCurrency())); } }