v7.0.0-beta.1 release
This commit is contained in:
commit
6595fd0eb5
3967 files changed
+1875383
No files matched your search
@@ -0,0 +1,514 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Contracts\Repositories\LayoutExtensionRepositoryInterface;
|
||||
use App\Enums\LayoutExtensionType;
|
||||
use App\Enums\LayoutSourceType;
|
||||
use App\Models\LayoutExtension;
|
||||
use App\Models\Template;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* LayoutExtensionRepository 단위 테스트
|
||||
*/
|
||||
class LayoutExtensionRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private LayoutExtensionRepositoryInterface $repository;
|
||||
|
||||
private Template $template;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = $this->app->make(LayoutExtensionRepositoryInterface::class);
|
||||
$this->template = Template::factory()->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension Point 조회 테스트
|
||||
*/
|
||||
public function test_get_by_extension_point(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-bottom', // 다른 확장점
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getByExtensionPoint($this->template->id, 'sidebar-top');
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('sidebar-top', $result->first()->target_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension Point 우선순위 정렬 테스트
|
||||
*/
|
||||
public function test_get_by_extension_point_ordered_by_priority(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_identifier' => 'low-priority',
|
||||
'priority' => 50,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_identifier' => 'high-priority',
|
||||
'priority' => 10,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getByExtensionPoint($this->template->id, 'sidebar-top');
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $result);
|
||||
$this->assertEquals('high-priority', $result->first()->source_identifier);
|
||||
$this->assertEquals('low-priority', $result->last()->source_identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 비활성 Extension Point 제외 테스트
|
||||
*/
|
||||
public function test_get_by_extension_point_excludes_inactive(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'is_active' => false,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getByExtensionPoint($this->template->id, 'sidebar-top');
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overlay 조회 테스트
|
||||
*/
|
||||
public function test_get_overlays_by_layout(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::Overlay,
|
||||
'target_name' => 'admin/dashboard',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint, // 다른 타입
|
||||
'target_name' => 'admin/dashboard',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-other',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getOverlaysByLayout($this->template->id, 'admin/dashboard');
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals(LayoutExtensionType::Overlay, $result->first()->extension_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 확장 생성 테스트
|
||||
*/
|
||||
public function test_create_extension(): void
|
||||
{
|
||||
// Arrange
|
||||
$data = [
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'header-actions',
|
||||
'source_type' => LayoutSourceType::Plugin,
|
||||
'source_identifier' => 'sirsoft-analytics',
|
||||
'content' => ['component' => ['type' => 'basic', 'name' => 'Button']],
|
||||
'priority' => 20,
|
||||
'is_active' => true,
|
||||
];
|
||||
|
||||
// Act
|
||||
$result = $this->repository->create($data);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(LayoutExtension::class, $result);
|
||||
$this->assertEquals('header-actions', $result->target_name);
|
||||
$this->assertEquals(LayoutSourceType::Plugin, $result->source_type);
|
||||
$this->assertDatabaseHas('template_layout_extensions', [
|
||||
'target_name' => 'header-actions',
|
||||
'source_identifier' => 'sirsoft-analytics',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 출처별 soft delete 테스트
|
||||
*/
|
||||
public function test_soft_delete_by_source(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->count(3)->create([
|
||||
'template_id' => $this->template->id,
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'source_type' => LayoutSourceType::Plugin,
|
||||
'source_identifier' => 'sirsoft-other',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$deleted = $this->repository->softDeleteBySource(
|
||||
LayoutSourceType::Module,
|
||||
'sirsoft-ecommerce'
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(3, $deleted);
|
||||
$this->assertEquals(1, LayoutExtension::count());
|
||||
$this->assertEquals(3, LayoutExtension::onlyTrashed()->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 출처별 복원 테스트
|
||||
*/
|
||||
public function test_restore_by_source(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->count(2)->create([
|
||||
'template_id' => $this->template->id,
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
]);
|
||||
|
||||
$this->repository->softDeleteBySource(LayoutSourceType::Module, 'sirsoft-ecommerce');
|
||||
$this->assertEquals(0, LayoutExtension::count());
|
||||
|
||||
// Act
|
||||
$restored = $this->repository->restoreBySource(LayoutSourceType::Module, 'sirsoft-ecommerce');
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(2, $restored);
|
||||
$this->assertEquals(2, LayoutExtension::count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 출처별 영구 삭제 테스트
|
||||
*/
|
||||
public function test_force_delete_by_source(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->count(2)->create([
|
||||
'template_id' => $this->template->id,
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
]);
|
||||
|
||||
// Soft delete first
|
||||
$this->repository->softDeleteBySource(LayoutSourceType::Module, 'sirsoft-ecommerce');
|
||||
|
||||
// Act
|
||||
$deleted = $this->repository->forceDeleteBySource(LayoutSourceType::Module, 'sirsoft-ecommerce');
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(2, $deleted);
|
||||
$this->assertEquals(0, LayoutExtension::withTrashed()->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension Point 템플릿 오버라이드 조회 테스트
|
||||
*/
|
||||
public function test_find_template_override_for_extension_point(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Template,
|
||||
'source_identifier' => 'sirsoft-admin_basic',
|
||||
'override_target' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->findTemplateOverrideForExtensionPoint(
|
||||
$this->template->id,
|
||||
'sidebar-top',
|
||||
'sirsoft-ecommerce'
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals('sirsoft-ecommerce', $result->override_target);
|
||||
$this->assertEquals(LayoutSourceType::Template, $result->source_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension Point 템플릿 오버라이드 없음 테스트
|
||||
*/
|
||||
public function test_find_template_override_for_extension_point_returns_null_when_not_found(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Module, // 모듈 출처 (템플릿 아님)
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->findTemplateOverrideForExtensionPoint(
|
||||
$this->template->id,
|
||||
'sidebar-top',
|
||||
'sirsoft-ecommerce'
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overlay 템플릿 오버라이드 조회 테스트
|
||||
*/
|
||||
public function test_find_template_override_for_overlay(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::Overlay,
|
||||
'target_name' => 'admin/settings',
|
||||
'source_type' => LayoutSourceType::Template,
|
||||
'source_identifier' => 'sirsoft-admin_basic',
|
||||
'override_target' => 'sirsoft-ecommerce',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->findTemplateOverrideForOverlay(
|
||||
$this->template->id,
|
||||
'admin/settings',
|
||||
'sirsoft-ecommerce'
|
||||
);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals(LayoutExtensionType::Overlay, $result->extension_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 오버라이드 고려한 Extension Point 조회 테스트
|
||||
*/
|
||||
public function test_get_resolved_extension_points(): void
|
||||
{
|
||||
// Arrange - 모듈 확장
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'priority' => 50,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Arrange - 다른 모듈 확장
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-other',
|
||||
'priority' => 40,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Arrange - 템플릿 오버라이드 (sirsoft-ecommerce 확장을 오버라이드)
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::ExtensionPoint,
|
||||
'target_name' => 'sidebar-top',
|
||||
'source_type' => LayoutSourceType::Template,
|
||||
'source_identifier' => 'sirsoft-admin_basic',
|
||||
'override_target' => 'sirsoft-ecommerce',
|
||||
'priority' => 10,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getResolvedExtensionPoints($this->template->id, 'sidebar-top');
|
||||
|
||||
// Assert - sirsoft-ecommerce 모듈 확장은 제외되고, 템플릿 오버라이드와 sirsoft-other만 남음
|
||||
$this->assertCount(2, $result);
|
||||
|
||||
$identifiers = $result->pluck('source_identifier')->toArray();
|
||||
$this->assertContains('sirsoft-admin_basic', $identifiers);
|
||||
$this->assertContains('sirsoft-other', $identifiers);
|
||||
$this->assertNotContains('sirsoft-ecommerce', $identifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 오버라이드 고려한 Overlay 조회 테스트
|
||||
*/
|
||||
public function test_get_resolved_overlays(): void
|
||||
{
|
||||
// Arrange - 모듈 오버레이
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::Overlay,
|
||||
'target_name' => 'admin/dashboard',
|
||||
'source_type' => LayoutSourceType::Module,
|
||||
'source_identifier' => 'sirsoft-ecommerce',
|
||||
'priority' => 50,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Arrange - 템플릿 오버라이드
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'extension_type' => LayoutExtensionType::Overlay,
|
||||
'target_name' => 'admin/dashboard',
|
||||
'source_type' => LayoutSourceType::Template,
|
||||
'source_identifier' => 'sirsoft-admin_basic',
|
||||
'override_target' => 'sirsoft-ecommerce',
|
||||
'priority' => 10,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getResolvedOverlays($this->template->id, 'admin/dashboard');
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals(LayoutSourceType::Template, $result->first()->source_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 템플릿 ID로 모든 확장 조회 테스트
|
||||
*/
|
||||
public function test_get_by_template_id(): void
|
||||
{
|
||||
// Arrange
|
||||
$otherTemplate = Template::factory()->create();
|
||||
|
||||
LayoutExtension::factory()->count(3)->create([
|
||||
'template_id' => $this->template->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->count(2)->create([
|
||||
'template_id' => $otherTemplate->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getByTemplateId($this->template->id);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(3, $result);
|
||||
$result->each(function ($extension) {
|
||||
$this->assertEquals($this->template->id, $extension->template_id);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 템플릿 ID로 모든 확장 삭제 테스트
|
||||
*/
|
||||
public function test_delete_by_template_id(): void
|
||||
{
|
||||
// Arrange
|
||||
$otherTemplate = Template::factory()->create();
|
||||
|
||||
LayoutExtension::factory()->count(3)->create([
|
||||
'template_id' => $this->template->id,
|
||||
]);
|
||||
|
||||
LayoutExtension::factory()->count(2)->create([
|
||||
'template_id' => $otherTemplate->id,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$deleted = $this->repository->deleteByTemplateId($this->template->id);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(3, $deleted);
|
||||
$this->assertEquals(2, LayoutExtension::count());
|
||||
$this->assertEquals(0, LayoutExtension::withTrashed()->where('template_id', $this->template->id)->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* 삭제된 확장 포함하여 삭제 테스트
|
||||
*/
|
||||
public function test_delete_by_template_id_includes_trashed(): void
|
||||
{
|
||||
// Arrange
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
]);
|
||||
|
||||
// Soft delete
|
||||
$this->repository->softDeleteBySource(LayoutSourceType::Module, 'sirsoft-ecommerce');
|
||||
|
||||
LayoutExtension::factory()->create([
|
||||
'template_id' => $this->template->id,
|
||||
'source_identifier' => 'another-module',
|
||||
]);
|
||||
|
||||
// 현재: 1개 활성, 이전 soft delete된 것들
|
||||
$trashedCount = LayoutExtension::onlyTrashed()->where('template_id', $this->template->id)->count();
|
||||
$activeCount = LayoutExtension::where('template_id', $this->template->id)->count();
|
||||
|
||||
// Act
|
||||
$deleted = $this->repository->deleteByTemplateId($this->template->id);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals($trashedCount + $activeCount, $deleted);
|
||||
$this->assertEquals(0, LayoutExtension::withTrashed()->where('template_id', $this->template->id)->count());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Enums\LayoutSourceType;
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplateLayout;
|
||||
use App\Repositories\LayoutRepository;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LayoutRepositoryCacheTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private LayoutRepository $repository;
|
||||
|
||||
private Template $template;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = app(LayoutRepository::class);
|
||||
$this->template = Template::factory()->create();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function get_by_source_identifier_returns_module_layouts(): void
|
||||
{
|
||||
// Given: 모듈 소스 타입 레이아웃 생성
|
||||
$moduleLayout = TemplateLayout::factory()
|
||||
->fromModule('sirsoft-ecommerce')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-ecommerce.admin_products_index',
|
||||
]);
|
||||
|
||||
// 플러그인 소스 타입 레이아웃도 생성 (조회에서 제외되어야 함)
|
||||
TemplateLayout::factory()
|
||||
->fromPlugin('sirsoft-tosspayments')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-tosspayments.plugin_settings',
|
||||
]);
|
||||
|
||||
// When: 모듈 소스 타입으로 조회
|
||||
$result = $this->repository->getBySourceIdentifier('sirsoft-ecommerce', LayoutSourceType::Module);
|
||||
|
||||
// Then: 모듈 레이아웃만 반환
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals($moduleLayout->id, $result->first()->id);
|
||||
$this->assertEquals(LayoutSourceType::Module, $result->first()->source_type);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function get_by_source_identifier_returns_plugin_layouts(): void
|
||||
{
|
||||
// Given: 플러그인 소스 타입 레이아웃 생성
|
||||
$pluginLayout = TemplateLayout::factory()
|
||||
->fromPlugin('sirsoft-tosspayments')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-tosspayments.plugin_settings',
|
||||
]);
|
||||
|
||||
// 모듈 소스 타입 레이아웃도 생성 (조회에서 제외되어야 함)
|
||||
TemplateLayout::factory()
|
||||
->fromModule('sirsoft-ecommerce')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-ecommerce.admin_products_index',
|
||||
]);
|
||||
|
||||
// When: 플러그인 소스 타입으로 조회
|
||||
$result = $this->repository->getBySourceIdentifier('sirsoft-tosspayments', LayoutSourceType::Plugin);
|
||||
|
||||
// Then: 플러그인 레이아웃만 반환
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals($pluginLayout->id, $result->first()->id);
|
||||
$this->assertEquals(LayoutSourceType::Plugin, $result->first()->source_type);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function get_by_module_identifier_does_not_find_plugin_layouts(): void
|
||||
{
|
||||
// Given: 플러그인 소스 타입 레이아웃만 존재
|
||||
TemplateLayout::factory()
|
||||
->fromPlugin('sirsoft-tosspayments')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-tosspayments.plugin_settings',
|
||||
]);
|
||||
|
||||
// When: 기존 getByModuleIdentifier로 조회
|
||||
$result = $this->repository->getByModuleIdentifier('sirsoft-tosspayments');
|
||||
|
||||
// Then: 플러그인 레이아웃은 찾을 수 없음 (source_type이 Module으로 하드코딩)
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function get_by_source_identifier_finds_plugin_layouts_correctly(): void
|
||||
{
|
||||
// Given: 플러그인 소스 타입 레이아웃만 존재
|
||||
TemplateLayout::factory()
|
||||
->fromPlugin('sirsoft-tosspayments')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-tosspayments.plugin_settings',
|
||||
]);
|
||||
|
||||
// When: 새 getBySourceIdentifier로 플러그인 타입 지정하여 조회
|
||||
$result = $this->repository->getBySourceIdentifier('sirsoft-tosspayments', LayoutSourceType::Plugin);
|
||||
|
||||
// Then: 플러그인 레이아웃을 올바르게 찾음
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('sirsoft-tosspayments.plugin_settings', $result->first()->name);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function plugin_layout_cache_invalidation_clears_layout_service_cache(): void
|
||||
{
|
||||
// Given: 플러그인 레이아웃이 LayoutService 캐시에 저장되어 있음
|
||||
$pluginLayout = TemplateLayout::factory()
|
||||
->fromPlugin('sirsoft-tosspayments')
|
||||
->create([
|
||||
'template_id' => $this->template->id,
|
||||
'name' => 'sirsoft-tosspayments.plugin_settings',
|
||||
]);
|
||||
|
||||
$cacheKey = "template.{$this->template->id}.layout.sirsoft-tosspayments.plugin_settings";
|
||||
Cache::put($cacheKey, ['version' => '1.0.0', 'components' => []], 3600);
|
||||
|
||||
// 캐시가 존재하는지 확인
|
||||
$this->assertNotNull(Cache::get($cacheKey));
|
||||
|
||||
// When: getBySourceIdentifier로 플러그인 레이아웃을 찾은 후 캐시 삭제
|
||||
$layouts = $this->repository->getBySourceIdentifier('sirsoft-tosspayments', LayoutSourceType::Plugin);
|
||||
|
||||
foreach ($layouts as $layout) {
|
||||
Cache::forget("template.{$layout->template_id}.layout.{$layout->name}");
|
||||
}
|
||||
|
||||
// Then: 캐시가 삭제됨
|
||||
$this->assertNull(Cache::get($cacheKey));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function versioned_public_layout_cache_key_is_cleared(): void
|
||||
{
|
||||
// Given: PublicLayoutController 형식의 버전 포함 캐시 키가 존재
|
||||
$cacheVersion = time();
|
||||
Cache::put('extension_cache_version', $cacheVersion);
|
||||
|
||||
$cacheKey = "layout.sirsoft-admin_basic.sirsoft-tosspayments.plugin_settings.v{$cacheVersion}";
|
||||
Cache::put($cacheKey, ['version' => '1.0.0', 'components' => []], 3600);
|
||||
|
||||
// 캐시가 존재하는지 확인
|
||||
$this->assertNotNull(Cache::get($cacheKey));
|
||||
|
||||
// When: 현재 캐시 버전을 읽어 올바른 캐시 키를 삭제
|
||||
$currentVersion = (int) Cache::get('extension_cache_version', 0);
|
||||
Cache::forget("layout.sirsoft-admin_basic.sirsoft-tosspayments.plugin_settings.v{$currentVersion}");
|
||||
|
||||
// Then: 버전 포함 캐시가 삭제됨
|
||||
$this->assertNull(Cache::get($cacheKey));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,766 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplateLayout;
|
||||
use App\Models\TemplateLayoutVersion;
|
||||
use App\Repositories\LayoutVersionRepository;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LayoutVersionRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private LayoutVersionRepository $repository;
|
||||
|
||||
private Template $template;
|
||||
|
||||
private TemplateLayout $layout;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = new LayoutVersionRepository(new TemplateLayoutVersion);
|
||||
$this->template = Template::factory()->create();
|
||||
$this->layout = TemplateLayout::factory()->create(['template_id' => $this->template->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* saveVersion 메서드 테스트 - 첫 번째 버전
|
||||
*/
|
||||
public function test_save_version_creates_first_version(): void
|
||||
{
|
||||
// Arrange
|
||||
$content = [
|
||||
'version' => '1.0.0',
|
||||
'components' => [
|
||||
['type' => 'header', 'props' => ['title' => 'Test']],
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$version = $this->repository->saveVersion($this->layout->id, $content);
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf(TemplateLayoutVersion::class, $version);
|
||||
$this->assertEquals(1, $version->version);
|
||||
$this->assertEquals($this->layout->id, $version->layout_id);
|
||||
$this->assertEquals($content, $version->content);
|
||||
}
|
||||
|
||||
/**
|
||||
* saveVersion 메서드 테스트 - 버전 자동 증가
|
||||
*/
|
||||
public function test_save_version_auto_increments_version_number(): void
|
||||
{
|
||||
// Arrange
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
$content = ['test' => 'data'];
|
||||
|
||||
// Act
|
||||
$newVersion = $this->repository->saveVersion($this->layout->id, $content);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(3, $newVersion->version);
|
||||
$this->assertEquals($content, $newVersion->content);
|
||||
}
|
||||
|
||||
/**
|
||||
* getVersions 메서드 테스트 - 최신순 정렬
|
||||
*/
|
||||
public function test_get_versions_returns_latest_first(): void
|
||||
{
|
||||
// Arrange
|
||||
$version1 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
$version2 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
$version3 = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 3,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$versions = $this->repository->getVersions($this->layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(3, $versions);
|
||||
$this->assertEquals(3, $versions->first()->version);
|
||||
$this->assertEquals(1, $versions->last()->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* getVersions 메서드 테스트 - 빈 결과
|
||||
*/
|
||||
public function test_get_versions_returns_empty_collection_when_no_versions(): void
|
||||
{
|
||||
// Act
|
||||
$versions = $this->repository->getVersions($this->layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(0, $versions);
|
||||
$this->assertTrue($versions->isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* getVersion 메서드 테스트 - 성공
|
||||
*/
|
||||
public function test_get_version_returns_specific_version(): void
|
||||
{
|
||||
// Arrange
|
||||
$version = TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getVersion($version->id);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($version->id, $result->id);
|
||||
$this->assertEquals($version->version, $result->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* getVersion 메서드 테스트 - 존재하지 않는 버전
|
||||
*/
|
||||
public function test_get_version_returns_null_when_not_found(): void
|
||||
{
|
||||
// Act
|
||||
$result = $this->repository->getVersion(99999);
|
||||
|
||||
// Assert
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* getNextVersion 메서드 테스트 - 버전이 없을 때
|
||||
*/
|
||||
public function test_get_next_version_returns_one_when_no_versions(): void
|
||||
{
|
||||
// Act
|
||||
$nextVersion = $this->repository->getNextVersion($this->layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(1, $nextVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* getNextVersion 메서드 테스트 - 기존 버전이 있을 때
|
||||
*/
|
||||
public function test_get_next_version_returns_max_plus_one(): void
|
||||
{
|
||||
// Arrange
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 5,
|
||||
]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => 3,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$nextVersion = $this->repository->getNextVersion($this->layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(6, $nextVersion); // max(5) + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* 다른 레이아웃의 버전은 영향을 주지 않는지 테스트
|
||||
*/
|
||||
public function test_versions_are_isolated_by_layout(): void
|
||||
{
|
||||
// Arrange
|
||||
$otherLayout = TemplateLayout::factory()->create(['template_id' => $this->template->id]);
|
||||
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $otherLayout->id,
|
||||
'version' => 10,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$nextVersion = $this->repository->getNextVersion($this->layout->id);
|
||||
$versions = $this->repository->getVersions($this->layout->id);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(1, $nextVersion); // 다른 레이아웃의 버전은 무시
|
||||
$this->assertCount(0, $versions);
|
||||
}
|
||||
|
||||
/**
|
||||
* saveVersion 연속 호출 테스트
|
||||
*/
|
||||
public function test_save_version_sequential_calls(): void
|
||||
{
|
||||
// Arrange
|
||||
$content1 = ['data' => 'version 1'];
|
||||
$content2 = ['data' => 'version 2'];
|
||||
$content3 = ['data' => 'version 3'];
|
||||
|
||||
// Act
|
||||
$v1 = $this->repository->saveVersion($this->layout->id, $content1);
|
||||
$v2 = $this->repository->saveVersion($this->layout->id, $content2);
|
||||
$v3 = $this->repository->saveVersion($this->layout->id, $content3);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(1, $v1->version);
|
||||
$this->assertEquals(2, $v2->version);
|
||||
$this->assertEquals(3, $v3->version);
|
||||
|
||||
$versions = $this->repository->getVersions($this->layout->id);
|
||||
$this->assertCount(3, $versions);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 추가된 키
|
||||
*/
|
||||
public function test_calculate_changes_detects_added_keys(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'description' => 'new field',
|
||||
'author' => 'John Doe',
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $changes['added']);
|
||||
$this->assertContains('description', $changes['added']);
|
||||
$this->assertContains('author', $changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
$this->assertEmpty($changes['modified']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 삭제된 키
|
||||
*/
|
||||
public function test_calculate_changes_detects_removed_keys(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'deprecated' => 'old field',
|
||||
'legacy' => 'another old field',
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $changes['removed']);
|
||||
$this->assertContains('deprecated', $changes['removed']);
|
||||
$this->assertContains('legacy', $changes['removed']);
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['modified']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 수정된 값
|
||||
*/
|
||||
public function test_calculate_changes_detects_modified_values(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'status' => 'draft',
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'version' => '2.0.0',
|
||||
'status' => 'published',
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(2, $changes['modified']);
|
||||
$this->assertContains('version', $changes['modified']);
|
||||
$this->assertContains('status', $changes['modified']);
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 중첩 객체 추가
|
||||
*/
|
||||
public function test_calculate_changes_detects_nested_added_keys(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
'locale' => 'ko',
|
||||
],
|
||||
'metadata' => [
|
||||
'author' => 'John',
|
||||
'tags' => ['test', 'demo'],
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('config.locale', $changes['added']);
|
||||
$this->assertContains('metadata', $changes['added']);
|
||||
$this->assertContains('metadata.author', $changes['added']);
|
||||
$this->assertContains('metadata.tags', $changes['added']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 중첩 객체 삭제
|
||||
*/
|
||||
public function test_calculate_changes_detects_nested_removed_keys(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
'locale' => 'ko',
|
||||
'deprecated' => 'old',
|
||||
],
|
||||
'legacy' => [
|
||||
'old_field' => 'value',
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('config.locale', $changes['removed']);
|
||||
$this->assertContains('config.deprecated', $changes['removed']);
|
||||
$this->assertContains('legacy', $changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 중첩 객체 수정
|
||||
*/
|
||||
public function test_calculate_changes_detects_nested_modified_values(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
'locale' => 'ko',
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'config' => [
|
||||
'theme' => 'light',
|
||||
'locale' => 'en',
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('config.theme', $changes['modified']);
|
||||
$this->assertContains('config.locale', $changes['modified']);
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 복합 변경 (추가+삭제+수정)
|
||||
*/
|
||||
public function test_calculate_changes_detects_complex_changes(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'deprecated' => 'old',
|
||||
'components' => [
|
||||
'header' => ['title' => 'Old Title'],
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'version' => '2.0.0',
|
||||
'description' => 'new field',
|
||||
'components' => [
|
||||
'header' => ['title' => 'New Title'],
|
||||
'footer' => ['text' => 'Footer'],
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
// Added
|
||||
$this->assertContains('description', $changes['added']);
|
||||
$this->assertContains('components.footer', $changes['added']);
|
||||
$this->assertContains('components.footer.text', $changes['added']);
|
||||
|
||||
// Removed
|
||||
$this->assertContains('deprecated', $changes['removed']);
|
||||
|
||||
// Modified
|
||||
$this->assertContains('version', $changes['modified']);
|
||||
$this->assertContains('components.header.title', $changes['modified']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 변경사항 없음
|
||||
*/
|
||||
public function test_calculate_changes_detects_no_changes(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'version' => '1.0.0',
|
||||
'config' => [
|
||||
'theme' => 'dark',
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
$this->assertEmpty($changes['modified']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 빈 배열
|
||||
*/
|
||||
public function test_calculate_changes_handles_empty_arrays(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [];
|
||||
$new = ['name' => 'test'];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $changes['added']);
|
||||
$this->assertContains('name', $changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
$this->assertEmpty($changes['modified']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 깊은 중첩 구조
|
||||
*/
|
||||
public function test_calculate_changes_handles_deeply_nested_structure(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'level1' => [
|
||||
'level2' => [
|
||||
'level3' => [
|
||||
'level4' => 'old value',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'level1' => [
|
||||
'level2' => [
|
||||
'level3' => [
|
||||
'level4' => 'new value',
|
||||
'level4_new' => 'added',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('level1.level2.level3.level4', $changes['modified']);
|
||||
$this->assertContains('level1.level2.level3.level4_new', $changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 대용량 JSON 성능 (1MB 이상)
|
||||
*/
|
||||
public function test_calculate_changes_performance_with_large_json(): void
|
||||
{
|
||||
// Arrange - 대용량 JSON 생성 (약 1MB)
|
||||
$old = [];
|
||||
$new = [];
|
||||
|
||||
// 1000개의 컴포넌트 생성
|
||||
for ($i = 0; $i < 1000; $i++) {
|
||||
$old["component_{$i}"] = [
|
||||
'type' => 'section',
|
||||
'id' => "comp_{$i}",
|
||||
'props' => [
|
||||
'title' => "Component {$i}",
|
||||
'description' => str_repeat("Long description for component {$i}. ", 10),
|
||||
'metadata' => [
|
||||
'author' => 'Test Author',
|
||||
'created' => '2025-01-01',
|
||||
'tags' => ['tag1', 'tag2', 'tag3'],
|
||||
],
|
||||
],
|
||||
'children' => [
|
||||
[
|
||||
'type' => 'text',
|
||||
'content' => str_repeat("Sample content {$i}. ", 20),
|
||||
],
|
||||
[
|
||||
'type' => 'image',
|
||||
'src' => "/images/image_{$i}.jpg",
|
||||
'alt' => "Image {$i}",
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// new는 일부 수정된 버전
|
||||
$new["component_{$i}"] = $old["component_{$i}"];
|
||||
if ($i % 10 === 0) {
|
||||
// 10번째마다 수정
|
||||
$new["component_{$i}"]['props']['title'] = "Modified Component {$i}";
|
||||
}
|
||||
if ($i % 15 === 0) {
|
||||
// 15번째마다 새 필드 추가
|
||||
$new["component_{$i}"]['props']['updated'] = '2025-01-10';
|
||||
}
|
||||
}
|
||||
|
||||
// 일부 컴포넌트 삭제 (old에만 존재)
|
||||
for ($i = 1000; $i < 1010; $i++) {
|
||||
$old["component_{$i}"] = ['type' => 'deleted', 'id' => "comp_{$i}"];
|
||||
}
|
||||
|
||||
// 일부 컴포넌트 추가 (new에만 존재)
|
||||
for ($i = 1010; $i < 1020; $i++) {
|
||||
$new["component_{$i}"] = ['type' => 'added', 'id' => "comp_{$i}"];
|
||||
}
|
||||
|
||||
// JSON 크기 확인 (약 1MB 이상인지)
|
||||
$jsonSize = strlen(json_encode($old)) + strlen(json_encode($new));
|
||||
$this->assertGreaterThan(1024 * 1024, $jsonSize, 'JSON size should be greater than 1MB');
|
||||
|
||||
// Act - 실행 시간 측정
|
||||
$startTime = microtime(true);
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
$executionTime = microtime(true) - $startTime;
|
||||
|
||||
// Assert - 성능 검증 (2초 이내 완료)
|
||||
$this->assertLessThan(2.0, $executionTime, 'Large JSON diff should complete within 2 seconds');
|
||||
|
||||
// 변경사항 정확도 검증
|
||||
$this->assertNotEmpty($changes['added']);
|
||||
$this->assertNotEmpty($changes['removed']);
|
||||
$this->assertNotEmpty($changes['modified']);
|
||||
|
||||
// 예상되는 변경사항 수 검증
|
||||
// 100개 수정 (component_0, 10, 20, ..., 990)
|
||||
// 67개 추가 (component_0.props.updated, 15, 30, ..., 975 + 10개 새 컴포넌트)
|
||||
// 10개 삭제 (component_1000 ~ 1009)
|
||||
$this->assertGreaterThanOrEqual(100, count($changes['modified']));
|
||||
$this->assertGreaterThanOrEqual(10, count($changes['removed']));
|
||||
$this->assertGreaterThanOrEqual(10, count($changes['added']));
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 배열 vs 스칼라 값 타입 변경
|
||||
*/
|
||||
public function test_calculate_changes_detects_type_change_from_scalar_to_array(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'tags' => 'single-tag',
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'tags' => ['tag1', 'tag2', 'tag3'],
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('tags', $changes['modified']);
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculateChanges 메서드 테스트 - 배열 vs 스칼라 값 타입 변경 (반대)
|
||||
*/
|
||||
public function test_calculate_changes_detects_type_change_from_array_to_scalar(): void
|
||||
{
|
||||
// Arrange
|
||||
$old = [
|
||||
'name' => 'test',
|
||||
'tags' => ['tag1', 'tag2', 'tag3'],
|
||||
];
|
||||
|
||||
$new = [
|
||||
'name' => 'test',
|
||||
'tags' => 'single-tag',
|
||||
];
|
||||
|
||||
// Act
|
||||
$changes = $this->repository->calculateChanges($old, $new);
|
||||
|
||||
// Assert
|
||||
$this->assertContains('tags', $changes['modified']);
|
||||
$this->assertEmpty($changes['added']);
|
||||
$this->assertEmpty($changes['removed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* getNextVersion 테스트 - 10 이상 버전에서 올바른 정렬 확인
|
||||
*
|
||||
* varchar 타입에서는 max('9') > max('10')으로 잘못 계산되던 버그 수정 확인
|
||||
*/
|
||||
public function test_get_next_version_handles_double_digit_versions_correctly(): void
|
||||
{
|
||||
// Arrange - 1부터 12까지 버전 생성
|
||||
for ($i = 1; $i <= 12; $i++) {
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => $i,
|
||||
]);
|
||||
}
|
||||
|
||||
// Act
|
||||
$nextVersion = $this->repository->getNextVersion($this->layout->id);
|
||||
|
||||
// Assert - max(12) + 1 = 13이어야 함 (varchar였다면 max('9') + 1 = 10 반환)
|
||||
$this->assertEquals(13, $nextVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* getVersions 테스트 - 10 이상 버전에서 올바른 정렬 확인
|
||||
*
|
||||
* varchar 타입에서는 '9' > '10' > '11' 순으로 잘못 정렬되던 버그 수정 확인
|
||||
*/
|
||||
public function test_get_versions_sorts_double_digit_versions_correctly(): void
|
||||
{
|
||||
// Arrange - 8, 9, 10, 11, 12 버전 생성 (순서 무작위)
|
||||
$versions = [10, 8, 12, 9, 11];
|
||||
foreach ($versions as $v) {
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => $v,
|
||||
]);
|
||||
}
|
||||
|
||||
// Act
|
||||
$result = $this->repository->getVersions($this->layout->id);
|
||||
|
||||
// Assert - 12, 11, 10, 9, 8 순서여야 함 (varchar였다면 9, 8, 12, 11, 10 순서)
|
||||
$this->assertEquals(12, $result[0]->version);
|
||||
$this->assertEquals(11, $result[1]->version);
|
||||
$this->assertEquals(10, $result[2]->version);
|
||||
$this->assertEquals(9, $result[3]->version);
|
||||
$this->assertEquals(8, $result[4]->version);
|
||||
}
|
||||
|
||||
/**
|
||||
* saveVersion 테스트 - 10번째 버전 이후에도 정상 저장
|
||||
*/
|
||||
public function test_save_version_works_after_version_10(): void
|
||||
{
|
||||
// Arrange - 1부터 10까지 버전 생성
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
TemplateLayoutVersion::factory()->create([
|
||||
'layout_id' => $this->layout->id,
|
||||
'version' => $i,
|
||||
]);
|
||||
}
|
||||
|
||||
// Act - 11번째 버전 저장
|
||||
$content = ['test' => 'version 11'];
|
||||
$newVersion = $this->repository->saveVersion($this->layout->id, $content);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(11, $newVersion->version);
|
||||
$this->assertEquals($content, $newVersion->content);
|
||||
|
||||
// 12번째 버전도 저장 가능해야 함
|
||||
$content12 = ['test' => 'version 12'];
|
||||
$version12 = $this->repository->saveVersion($this->layout->id, $content12);
|
||||
$this->assertEquals(12, $version12->version);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Enums\MailSendStatus;
|
||||
use App\Models\MailSendLog;
|
||||
use App\Repositories\MailSendLogRepository;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* MailSendLogRepository 테스트
|
||||
*
|
||||
* 메일 발송 이력 리포지토리의 필터링, 페이지네이션, 통계 조회를 검증합니다.
|
||||
*/
|
||||
class MailSendLogRepositoryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private MailSendLogRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repository = app(MailSendLogRepository::class);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// create
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 발송 이력을 생성합니다
|
||||
*/
|
||||
public function test_create_returns_mail_send_log(): void
|
||||
{
|
||||
$data = [
|
||||
'recipient_email' => 'test@example.com',
|
||||
'recipient_name' => 'Test User',
|
||||
'subject' => 'Test Subject',
|
||||
'template_type' => 'welcome',
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'source' => 'notification',
|
||||
'status' => 'sent',
|
||||
'sent_at' => now(),
|
||||
];
|
||||
|
||||
$log = $this->repository->create($data);
|
||||
|
||||
$this->assertInstanceOf(MailSendLog::class, $log);
|
||||
$this->assertTrue($log->exists);
|
||||
$this->assertEquals('test@example.com', $log->recipient_email);
|
||||
$this->assertEquals('notification', $log->source);
|
||||
$this->assertDatabaseHas('mail_send_logs', [
|
||||
'recipient_email' => 'test@example.com',
|
||||
'source' => 'notification',
|
||||
]);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// getPaginated - 기본 동작
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 필터 없이 전체 목록을 페이지네이션하여 반환
|
||||
*/
|
||||
public function test_get_paginated_returns_all_logs_without_filters(): void
|
||||
{
|
||||
MailSendLog::factory()->count(5)->create();
|
||||
|
||||
$result = $this->repository->getPaginated();
|
||||
|
||||
$this->assertCount(5, $result->items());
|
||||
$this->assertEquals(5, $result->total());
|
||||
}
|
||||
|
||||
/**
|
||||
* 빈 결과를 올바르게 반환
|
||||
*/
|
||||
public function test_get_paginated_returns_empty_when_no_logs(): void
|
||||
{
|
||||
$result = $this->repository->getPaginated();
|
||||
|
||||
$this->assertCount(0, $result->items());
|
||||
$this->assertEquals(0, $result->total());
|
||||
}
|
||||
|
||||
/**
|
||||
* sent_at 기준 최신순 정렬
|
||||
*/
|
||||
public function test_get_paginated_orders_by_sent_at_desc(): void
|
||||
{
|
||||
$older = MailSendLog::factory()->create(['sent_at' => now()->subDays(2)]);
|
||||
$newer = MailSendLog::factory()->create(['sent_at' => now()]);
|
||||
|
||||
$result = $this->repository->getPaginated();
|
||||
$items = $result->items();
|
||||
|
||||
$this->assertEquals($newer->id, $items[0]->id);
|
||||
$this->assertEquals($older->id, $items[1]->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* perPage 파라미터로 페이지 크기 제어
|
||||
*/
|
||||
public function test_get_paginated_respects_per_page(): void
|
||||
{
|
||||
MailSendLog::factory()->count(10)->create();
|
||||
|
||||
$result = $this->repository->getPaginated([], 3);
|
||||
|
||||
$this->assertCount(3, $result->items());
|
||||
$this->assertEquals(10, $result->total());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// getPaginated - 필터
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* extension_type 필터
|
||||
*/
|
||||
public function test_get_paginated_filters_by_extension_type(): void
|
||||
{
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Core, 'core')->count(3)->create();
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Module, 'sirsoft-ecommerce')->count(2)->create();
|
||||
|
||||
$result = $this->repository->getPaginated(['extension_type' => 'core']);
|
||||
|
||||
$this->assertCount(3, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* template_type 필터
|
||||
*/
|
||||
public function test_get_paginated_filters_by_template_type(): void
|
||||
{
|
||||
MailSendLog::factory()->withTemplateType('welcome')->count(2)->create();
|
||||
MailSendLog::factory()->withTemplateType('password_reset')->count(3)->create();
|
||||
|
||||
$result = $this->repository->getPaginated(['template_type' => 'welcome']);
|
||||
|
||||
$this->assertCount(2, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* status 필터
|
||||
*/
|
||||
public function test_get_paginated_filters_by_status(): void
|
||||
{
|
||||
MailSendLog::factory()->count(4)->create(['status' => MailSendStatus::Sent->value]);
|
||||
MailSendLog::factory()->failed()->count(2)->create();
|
||||
|
||||
$result = $this->repository->getPaginated(['status' => MailSendStatus::Failed->value]);
|
||||
|
||||
$this->assertCount(2, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* search 필터 - 이메일 검색
|
||||
*/
|
||||
public function test_get_paginated_search_matches_email(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['recipient_email' => 'admin@example.com']);
|
||||
MailSendLog::factory()->create(['recipient_email' => 'user@example.com']);
|
||||
|
||||
$result = $this->repository->getPaginated(['search' => 'example.com']);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
$this->assertEquals('admin@example.com', $result->items()[0]->recipient_email);
|
||||
}
|
||||
|
||||
/**
|
||||
* search 필터 - 수신자 이름 검색
|
||||
*/
|
||||
public function test_get_paginated_search_matches_recipient_name(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['recipient_name' => 'Kim Admin']);
|
||||
MailSendLog::factory()->create(['recipient_name' => 'Lee User']);
|
||||
|
||||
$result = $this->repository->getPaginated(['search' => 'Kim']);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* search 필터 - 제목 검색
|
||||
*/
|
||||
public function test_get_paginated_search_matches_subject(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['subject' => '비밀번호 재설정 안내']);
|
||||
MailSendLog::factory()->create(['subject' => '환영합니다']);
|
||||
|
||||
$result = $this->repository->getPaginated(['search' => '비밀번호']);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* date_from 필터
|
||||
*/
|
||||
public function test_get_paginated_filters_by_date_from(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['sent_at' => Carbon::parse('2026-01-01')]);
|
||||
MailSendLog::factory()->create(['sent_at' => Carbon::parse('2026-03-01')]);
|
||||
|
||||
$result = $this->repository->getPaginated(['date_from' => '2026-02-01']);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* date_to 필터
|
||||
*/
|
||||
public function test_get_paginated_filters_by_date_to(): void
|
||||
{
|
||||
MailSendLog::factory()->create(['sent_at' => Carbon::parse('2026-01-15')]);
|
||||
MailSendLog::factory()->create(['sent_at' => Carbon::parse('2026-03-15')]);
|
||||
|
||||
$result = $this->repository->getPaginated(['date_to' => '2026-02-01']);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
}
|
||||
|
||||
/**
|
||||
* 복합 필터 조합
|
||||
*/
|
||||
public function test_get_paginated_combines_multiple_filters(): void
|
||||
{
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Core, 'core')->withTemplateType('welcome')->create([
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
]);
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Core, 'core')->withTemplateType('welcome')->failed()->create();
|
||||
MailSendLog::factory()->forExtension(ExtensionOwnerType::Module, 'sirsoft-ecommerce')->withTemplateType('welcome')->create();
|
||||
|
||||
$result = $this->repository->getPaginated([
|
||||
'extension_type' => 'core',
|
||||
'template_type' => 'welcome',
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $result->items());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// getStatistics
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* 빈 테이블에서 통계 반환
|
||||
*/
|
||||
public function test_get_statistics_returns_zeros_when_empty(): void
|
||||
{
|
||||
$stats = $this->repository->getStatistics();
|
||||
|
||||
$this->assertEquals(0, $stats['total']);
|
||||
$this->assertEquals(0, $stats['sent']);
|
||||
$this->assertEquals(0, $stats['failed']);
|
||||
$this->assertEquals(0, $stats['today']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 올바른 통계 수치 반환
|
||||
*/
|
||||
public function test_get_statistics_returns_correct_counts(): void
|
||||
{
|
||||
// sent 3건
|
||||
MailSendLog::factory()->count(3)->create([
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
// failed 2건
|
||||
MailSendLog::factory()->failed()->count(2)->create([
|
||||
'sent_at' => now(),
|
||||
]);
|
||||
// 어제 sent 1건
|
||||
MailSendLog::factory()->create([
|
||||
'status' => MailSendStatus::Sent->value,
|
||||
'sent_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$stats = $this->repository->getStatistics();
|
||||
|
||||
$this->assertEquals(6, $stats['total']);
|
||||
$this->assertEquals(4, $stats['sent']);
|
||||
$this->assertEquals(2, $stats['failed']);
|
||||
$this->assertEquals(5, $stats['today']);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user