v7.0.0-beta.1 release

This commit is contained in:
HeuJung committed 2026-04-01 10:30:52 +09:00
commit 6595fd0eb5
3967 files changed
+1875383

No files matched your search

@@ -0,0 +1,121 @@
<?php
namespace Tests\Unit\Notifications\Auth;
use App\Enums\ExtensionOwnerType;
use App\Mail\DbTemplateMail;
use App\Models\MailTemplate;
use App\Models\User;
use App\Notifications\Auth\PasswordChangedNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;
/**
* PasswordChangedNotification 테스트
*
* 비밀번호 변경 완료 알림의 채널 결정 및 이메일 생성을 검증합니다.
*/
class PasswordChangedNotificationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
Config::set('app.name', 'G7 Test');
Config::set('app.url', 'https://g7.test');
}
/**
* via()가 기본적으로 mail 채널을 반환하는지 확인
*/
public function test_via_returns_mail_channel_by_default(): void
{
$notification = new PasswordChangedNotification();
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$channels = $notification->via($user);
$this->assertContains('mail', $channels);
}
/**
* toMail()이 DbTemplateMail을 반환하는지 확인
*/
public function test_to_mail_returns_db_template_mail(): void
{
MailTemplate::factory()->withType('password_changed')->create();
$notification = new PasswordChangedNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
}
/**
* Mailable의 templateType이 password_changed인지 확인
*/
public function test_mailable_template_type_is_password_changed(): void
{
MailTemplate::factory()->withType('password_changed')->create();
$notification = new PasswordChangedNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals('password_changed', $mailable->getTemplateType());
}
/**
* Mailable의 module이 core인지 확인
*/
public function test_mailable_module_is_core(): void
{
MailTemplate::factory()->withType('password_changed')->create();
$notification = new PasswordChangedNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals(ExtensionOwnerType::Core, $mailable->getExtensionType());
$this->assertEquals('core', $mailable->getExtensionIdentifier());
}
/**
* 비활성 템플릿에서 스킵 인스턴스 반환
*/
public function test_to_mail_returns_skipped_when_template_inactive(): void
{
MailTemplate::factory()->withType('password_changed')->inactive()->create();
$notification = new PasswordChangedNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
$this->assertTrue($mailable->isSkipped());
}
/**
* 템플릿 미존재 시 스킵 인스턴스 반환
*/
public function test_to_mail_returns_skipped_when_no_template(): void
{
$notification = new PasswordChangedNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
$this->assertTrue($mailable->isSkipped());
}
}
@@ -0,0 +1,190 @@
<?php
namespace Tests\Unit\Notifications\Auth;
use App\Enums\ExtensionOwnerType;
use App\Mail\DbTemplateMail;
use App\Models\MailTemplate;
use App\Models\User;
use App\Notifications\Auth\ResetPasswordNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;
/**
* ResetPasswordNotification 테스트
*
* 비밀번호 재설정 알림의 채널 결정 및 이메일 생성을 검증합니다.
*/
class ResetPasswordNotificationTest extends TestCase
{
use RefreshDatabase;
private string $testToken = 'test-reset-token-12345';
protected function setUp(): void
{
parent::setUp();
Cache::flush();
Config::set('app.name', 'G7 Test');
Config::set('app.url', 'https://g7.test');
}
/**
* via()가 기본적으로 mail 채널을 반환하는지 확인
*/
public function test_via_returns_mail_channel_by_default(): void
{
$notification = new ResetPasswordNotification($this->testToken);
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$channels = $notification->via($user);
$this->assertContains('mail', $channels);
}
/**
* toMail()이 DbTemplateMail을 반환하는지 확인
*/
public function test_to_mail_returns_db_template_mail(): void
{
MailTemplate::factory()->withType('reset_password')->create();
$notification = new ResetPasswordNotification($this->testToken);
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
}
/**
* Mailable의 templateType이 reset_password인지 확인
*/
public function test_mailable_template_type_is_reset_password(): void
{
MailTemplate::factory()->withType('reset_password')->create();
$notification = new ResetPasswordNotification($this->testToken);
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals('reset_password', $mailable->getTemplateType());
}
/**
* Mailable의 module이 core인지 확인
*/
public function test_mailable_module_is_core(): void
{
MailTemplate::factory()->withType('reset_password')->create();
$notification = new ResetPasswordNotification($this->testToken);
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals(ExtensionOwnerType::Core, $mailable->getExtensionType());
$this->assertEquals('core', $mailable->getExtensionIdentifier());
}
/**
* 비활성 템플릿에서 스킵 인스턴스 반환
*/
public function test_to_mail_returns_skipped_when_template_inactive(): void
{
MailTemplate::factory()->withType('reset_password')->inactive()->create();
$notification = new ResetPasswordNotification($this->testToken);
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
$this->assertTrue($mailable->isSkipped());
}
/**
* getToken()이 토큰을 반환하는지 확인
*/
public function test_get_token_returns_token(): void
{
$notification = new ResetPasswordNotification($this->testToken);
$this->assertEquals($this->testToken, $notification->getToken());
}
/**
* action_url 변수를 포함하는 비밀번호 재설정 템플릿을 생성합니다.
*/
private function createResetPasswordTemplate(): void
{
MailTemplate::factory()->withType('reset_password')->create([
'subject' => [
'ko' => '비밀번호 재설정',
'en' => 'Reset Password',
],
'body' => [
'ko' => '<p><a href="{action_url}">비밀번호 재설정</a></p>',
'en' => '<p><a href="{action_url}">Reset Password</a></p>',
],
]);
}
/**
* redirect_prefix 없이 기본 /reset-password URL 생성
*/
public function test_to_mail_generates_default_reset_url(): void
{
$this->createResetPasswordTemplate();
$notification = new ResetPasswordNotification($this->testToken);
$user = User::factory()->create(['email' => 'user@example.com']);
$mailable = $notification->toMail($user);
// renderedBody는 생성자 파라미터이므로 Reflection으로 접근
$ref = new \ReflectionProperty($mailable, 'renderedBody');
$body = $ref->getValue($mailable);
$this->assertStringContainsString('/reset-password?', $body);
$this->assertStringNotContainsString('/admin/reset-password?', $body);
}
/**
* redirect_prefix='admin' 시 /admin/reset-password URL 생성
*/
public function test_to_mail_generates_admin_reset_url_with_redirect_prefix(): void
{
$this->createResetPasswordTemplate();
$notification = new ResetPasswordNotification($this->testToken, 'admin');
$user = User::factory()->create(['email' => 'admin@example.com']);
$mailable = $notification->toMail($user);
$ref = new \ReflectionProperty($mailable, 'renderedBody');
$body = $ref->getValue($mailable);
$this->assertStringContainsString('/admin/reset-password?', $body);
}
/**
* redirect_prefix=null 시 기본 URL (하위 호환성)
*/
public function test_to_mail_null_redirect_prefix_uses_default_url(): void
{
$this->createResetPasswordTemplate();
$notification = new ResetPasswordNotification($this->testToken, null);
$user = User::factory()->create(['email' => 'test@example.com']);
$mailable = $notification->toMail($user);
$ref = new \ReflectionProperty($mailable, 'renderedBody');
$body = $ref->getValue($mailable);
$this->assertStringContainsString('/reset-password?', $body);
$this->assertStringNotContainsString('/admin/reset-password?', $body);
}
}
@@ -0,0 +1,121 @@
<?php
namespace Tests\Unit\Notifications\Auth;
use App\Enums\ExtensionOwnerType;
use App\Mail\DbTemplateMail;
use App\Models\MailTemplate;
use App\Models\User;
use App\Notifications\Auth\WelcomeNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;
/**
* WelcomeNotification 테스트
*
* 회원가입 완료 알림의 채널 결정 및 이메일 생성을 검증합니다.
*/
class WelcomeNotificationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
Config::set('app.name', 'G7 Test');
Config::set('app.url', 'https://g7.test');
}
/**
* via()가 기본적으로 mail 채널을 반환하는지 확인
*/
public function test_via_returns_mail_channel_by_default(): void
{
$notification = new WelcomeNotification();
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$channels = $notification->via($user);
$this->assertContains('mail', $channels);
}
/**
* toMail()이 DbTemplateMail을 반환하는지 확인
*/
public function test_to_mail_returns_db_template_mail(): void
{
MailTemplate::factory()->withType('welcome')->create();
$notification = new WelcomeNotification();
$user = User::factory()->create(['email' => 'test@example.com', 'name' => 'Test User']);
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
}
/**
* Mailable의 templateType이 welcome인지 확인
*/
public function test_mailable_template_type_is_welcome(): void
{
MailTemplate::factory()->withType('welcome')->create();
$notification = new WelcomeNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals('welcome', $mailable->getTemplateType());
}
/**
* Mailable의 module이 core인지 확인
*/
public function test_mailable_module_is_core(): void
{
MailTemplate::factory()->withType('welcome')->create();
$notification = new WelcomeNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertEquals(ExtensionOwnerType::Core, $mailable->getExtensionType());
$this->assertEquals('core', $mailable->getExtensionIdentifier());
}
/**
* 비활성 템플릿에서 스킵 인스턴스 반환
*/
public function test_to_mail_returns_skipped_when_template_inactive(): void
{
MailTemplate::factory()->withType('welcome')->inactive()->create();
$notification = new WelcomeNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
$this->assertTrue($mailable->isSkipped());
}
/**
* 템플릿 미존재 시 스킵 인스턴스 반환
*/
public function test_to_mail_returns_skipped_when_no_template(): void
{
$notification = new WelcomeNotification();
$user = User::factory()->create();
$mailable = $notification->toMail($user);
$this->assertInstanceOf(DbTemplateMail::class, $mailable);
$this->assertTrue($mailable->isSkipped());
}
}
@@ -0,0 +1,218 @@
<?php
namespace Tests\Unit\Notifications;
use App\Extension\HookManager;
use App\Models\User;
use App\Notifications\BaseNotification;
use Illuminate\Notifications\Notification;
use Mockery;
use Tests\TestCase;
/**
* BaseNotification 테스트
*
* 알림 기본 추상 클래스의 via() 채널 결정 및 훅 연동을 검증합니다.
*/
class BaseNotificationTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
/**
* BaseNotification이 Notification을 상속하는지 확인
*/
public function test_extends_laravel_notification(): void
{
$notification = $this->createConcreteNotification();
$this->assertInstanceOf(Notification::class, $notification);
}
/**
* via()가 기본적으로 ['mail'] 채널을 반환하는지 확인
*/
public function test_via_returns_mail_channel_by_default(): void
{
$notification = $this->createConcreteNotification();
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$channels = $notification->via($user);
$this->assertEquals(['mail'], $channels);
}
/**
* Filter 훅으로 채널을 추가할 수 있는지 확인
*/
public function test_hook_can_add_channels(): void
{
// 훅 리스너 등록: database 채널 추가
HookManager::addFilter(
'test.prefix.notification.channels',
function (array $channels) {
$channels[] = 'database';
return $channels;
}
);
$notification = $this->createConcreteNotification('test.prefix', 'test_type');
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$channels = $notification->via($user);
$this->assertContains('mail', $channels);
$this->assertContains('database', $channels);
// 훅 정리
HookManager::clearFilter('test.prefix.notification.channels');
}
/**
* 훅에 notification type이 전달되는지 확인
*/
public function test_hook_receives_notification_type(): void
{
$receivedType = null;
HookManager::addFilter(
'test.prefix.notification.channels',
function (array $channels, string $type) use (&$receivedType) {
$receivedType = $type;
return $channels;
}
);
$notification = $this->createConcreteNotification('test.prefix', 'welcome');
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$notification->via($user);
$this->assertEquals('welcome', $receivedType);
// 훅 정리
HookManager::clearFilter('test.prefix.notification.channels');
}
/**
* 훅에 notifiable 객체가 전달되는지 확인
*/
public function test_hook_receives_notifiable(): void
{
$receivedNotifiable = null;
HookManager::addFilter(
'test.prefix.notification.channels',
function (array $channels, string $type, object $notifiable) use (&$receivedNotifiable) {
$receivedNotifiable = $notifiable;
return $channels;
}
);
$notification = $this->createConcreteNotification('test.prefix', 'welcome');
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$notification->via($user);
$this->assertSame($user, $receivedNotifiable);
// 훅 정리
HookManager::clearFilter('test.prefix.notification.channels');
}
/**
* 훅 이름이 {hookPrefix}.notification.channels 형식인지 확인
*/
public function test_hook_name_format(): void
{
$hookCalled = false;
// core.auth 접두사로 등록
HookManager::addFilter(
'core.auth.notification.channels',
function (array $channels) use (&$hookCalled) {
$hookCalled = true;
return $channels;
}
);
$notification = $this->createConcreteNotification('core.auth', 'welcome');
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$notification->via($user);
$this->assertTrue($hookCalled, '훅 이름이 core.auth.notification.channels 형식이어야 합니다');
// 훅 정리
HookManager::clearFilter('core.auth.notification.channels');
}
/**
* 게시판 모듈 훅 접두사 형식 확인
*/
public function test_board_module_hook_prefix(): void
{
$hookCalled = false;
HookManager::addFilter(
'sirsoft-board.notification.channels',
function (array $channels) use (&$hookCalled) {
$hookCalled = true;
return $channels;
}
);
$notification = $this->createConcreteNotification('sirsoft-board', 'new_comment');
$user = new User(['email' => 'test@example.com', 'name' => 'Test User']);
$notification->via($user);
$this->assertTrue($hookCalled, '훅 이름이 sirsoft-board.notification.channels 형식이어야 합니다');
// 훅 정리
HookManager::clearFilter('sirsoft-board.notification.channels');
}
/**
* 테스트용 구체 알림 클래스를 생성합니다.
*
* @param string $hookPrefix 훅 접두사
* @param string $notificationType 알림 유형
* @return BaseNotification
*/
private function createConcreteNotification(
string $hookPrefix = 'test.prefix',
string $notificationType = 'test_type'
): BaseNotification {
return new class($hookPrefix, $notificationType) extends BaseNotification
{
public function __construct(
private string $hookPrefix,
private string $notificationType
) {}
protected function getHookPrefix(): string
{
return $this->hookPrefix;
}
protected function getNotificationType(): string
{
return $this->notificationType;
}
public function toMail(object $notifiable): void
{
// 테스트용 빈 구현
}
};
}
}