Files
Gnuboard7/tests/Unit/Console/Commands/SeoGenerateSitemapCommandTest.php
T
HeuJung 1629684156 feat(seo): 사이트맵 증분 저장소 + 리소스 리스너 재배선
사이트맵을 리소스 단위로 영속화(sitemap_urls)해 진정한 증분 갱신과
도메인 재쿼리 없는 파일 재작성을 구현했다. 리소스 공개/비공개/삭제 시
해당 항목만 색인이 갱신되고, 관리자=full·스케줄러=auto·리소스 변경=증분으로 동작한다.

- sitemap_urls 마이그레이션 + SitemapUrl 모델. loc(2048) 를 unique 에 직접 넣으면
 InnoDB 키 길이를 초과하므로 loc_hash(sha256) 컬럼을 identity 로 사용
- SitemapUrlRepository(멱등 upsert / remove / 스트리밍) + SitemapIndexer
 (+ 제3자용 sitemap.index.collect_for_resource 필터 훅)
- 4 리스너 재배선: 공개→색인 / 비공개·삭제→색인 해제 + 재생성 잡 디바운스 디스패치.
 3 기여자가 resource_type/resource_id 를 함께 emit 해 전체·증분 identity 를 일치
- 재생성 모드를 SitemapGenerationMode Backed Enum 으로 통일(Command→Job→Manager)
- seo:generate-sitemap 에 --rebuild / --mode 옵션 추가
2026-07-18 16:19:49 +09:00

117 lines
4.1 KiB
PHP

<?php
namespace Tests\Unit\Console\Commands;
use App\Enums\SitemapGenerationMode;
use App\Jobs\GenerateSitemapJob;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Config;
use Mockery;
use Tests\TestCase;
/**
* SeoGenerateSitemapCommand 유닛 테스트
*
* seo:generate-sitemap 커맨드의 큐 드라이버 분기, 강제 동기, 모드 옵션(--mode/--rebuild),
* 출력 메시지를 검증합니다.
*/
class SeoGenerateSitemapCommandTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
/**
* 큐 드라이버가 sync 가 아닐 때 비동기 디스패치되는지 확인합니다.
*/
public function test_command_dispatches_async_when_queue_driver_is_not_sync(): void
{
Config::set('queue.default', 'database');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap')
->expectsOutput('Sitemap 생성이 큐에 디스패치되었습니다. (모드: auto)')
->assertExitCode(Command::SUCCESS);
Bus::assertDispatched(GenerateSitemapJob::class, fn (GenerateSitemapJob $job) => $job->mode === SitemapGenerationMode::Auto);
Bus::assertNotDispatchedSync(GenerateSitemapJob::class);
}
/**
* 큐 드라이버가 sync 일 때 옵션 없이도 동기 실행되는지 확인합니다.
*/
public function test_command_dispatches_sync_when_queue_driver_is_sync(): void
{
Config::set('queue.default', 'sync');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap')
->expectsOutput('Sitemap이 생성되었습니다. (모드: auto)')
->assertExitCode(Command::SUCCESS);
Bus::assertDispatchedSync(GenerateSitemapJob::class);
}
/**
* --sync 옵션은 큐 드라이버와 무관하게 동기 실행을 강제합니다.
*/
public function test_command_forces_sync_when_option_provided(): void
{
Config::set('queue.default', 'database');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap', ['--sync' => true])
->expectsOutput('Sitemap이 생성되었습니다. (모드: auto)')
->assertExitCode(Command::SUCCESS);
Bus::assertDispatchedSync(GenerateSitemapJob::class);
}
/**
* --mode 옵션이 잡에 그대로 전달됩니다.
*/
public function test_command_passes_mode_option_to_job(): void
{
Config::set('queue.default', 'database');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap', ['--mode' => 'incremental'])
->expectsOutput('Sitemap 생성이 큐에 디스패치되었습니다. (모드: incremental)')
->assertExitCode(Command::SUCCESS);
Bus::assertDispatched(GenerateSitemapJob::class, fn (GenerateSitemapJob $job) => $job->mode === SitemapGenerationMode::Incremental);
}
/**
* --rebuild 옵션은 --mode 보다 우선하며 full 로 강제합니다.
*/
public function test_rebuild_option_forces_full_mode(): void
{
Config::set('queue.default', 'database');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap', ['--rebuild' => true, '--mode' => 'incremental'])
->expectsOutput('Sitemap 생성이 큐에 디스패치되었습니다. (모드: full)')
->assertExitCode(Command::SUCCESS);
Bus::assertDispatched(GenerateSitemapJob::class, fn (GenerateSitemapJob $job) => $job->mode === SitemapGenerationMode::Full);
}
/**
* 잘못된 --mode 값은 INVALID 로 거부하고 잡을 디스패치하지 않습니다.
*/
public function test_invalid_mode_is_rejected(): void
{
Config::set('queue.default', 'database');
Bus::fake([GenerateSitemapJob::class]);
$this->artisan('seo:generate-sitemap', ['--mode' => 'bogus'])
->assertExitCode(Command::INVALID);
Bus::assertNotDispatched(GenerateSitemapJob::class);
}
}