feat(seo): 사이트맵 기여자 지연 스트리밍 + hreflang 계산 선형화
AbstractSitemapContributor 브리지 base 로 인터페이스를 바꾸지 않고 board/ecommerce/page 기여자를 getUrlsLazy 지연 스트리밍으로 전환해, 게시글이 수백만 건인 사이트에서도 URL 배열을 통째로 적재하지 않는다. SitemapGenerator 는 capability 를 감지해 지연 경로를 쓰고, base 를 상속하지 않은 제3자 구현체는 기존 getUrls 경로로 그대로 동작한다. hreflang alternate 집합을 base URL 당 한 번만 계산해 로케일별 <url> 에 재사용하도록 바꿔 계산 비용을 로케일 수의 제곱에서 선형으로 줄였다 (정상 구성 출력은 불변). 다국어 대체 링크 사용 여부 토글과 로케일 수 상한을 추가하고 설정/검증/관리자 UI/다국어(ko·en·ja) 표면을 동반했다.
This commit is contained in:
1 parent
1c99547bc1
commit
1a6fca0d6b
28 files changed
+726
-179
No files matched your search
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Seo;
|
||||
|
||||
use App\Seo\AbstractSitemapContributor;
|
||||
use App\Seo\Contracts\SitemapContributorInterface;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* AbstractSitemapContributor 단위 테스트
|
||||
*
|
||||
* getUrls()(배열)와 getUrlsLazy()(지연 제너레이터) 사이의 브리지를 검증합니다.
|
||||
* 구현체가 둘 중 하나만 오버라이드해도 나머지 한쪽이 자동으로 제공되어야 합니다.
|
||||
*/
|
||||
class AbstractSitemapContributorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* getUrls() 만 구현한 구현체는 getUrlsLazy() 가 그 배열을 지연 순회한다
|
||||
*/
|
||||
public function test_lazy_bridges_to_array_when_only_get_urls_is_implemented(): void
|
||||
{
|
||||
$contributor = new class extends AbstractSitemapContributor
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'array-only';
|
||||
}
|
||||
|
||||
public function getUrls(): array
|
||||
{
|
||||
return [
|
||||
['loc' => 'https://example.test/a'],
|
||||
['loc' => 'https://example.test/b'],
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
$lazy = $contributor->getUrlsLazy();
|
||||
|
||||
$this->assertInstanceOf(\Traversable::class, $lazy);
|
||||
$this->assertSame(
|
||||
['https://example.test/a', 'https://example.test/b'],
|
||||
array_column(iterator_to_array($lazy, false), 'loc'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* getUrlsLazy() 만 구현한 구현체는 getUrls() 가 그 스트림을 배열로 실체화한다
|
||||
*/
|
||||
public function test_array_bridges_to_lazy_when_only_get_urls_lazy_is_implemented(): void
|
||||
{
|
||||
$contributor = new class extends AbstractSitemapContributor
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'lazy-only';
|
||||
}
|
||||
|
||||
public function getUrlsLazy(): iterable
|
||||
{
|
||||
yield ['loc' => 'https://example.test/1'];
|
||||
yield ['loc' => 'https://example.test/2'];
|
||||
yield ['loc' => 'https://example.test/3'];
|
||||
}
|
||||
};
|
||||
|
||||
$urls = $contributor->getUrls();
|
||||
|
||||
$this->assertIsArray($urls);
|
||||
$this->assertSame(
|
||||
['https://example.test/1', 'https://example.test/2', 'https://example.test/3'],
|
||||
array_column($urls, 'loc'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* getUrlsLazy() 는 실제 지연 제너레이터여야 한다 (전량 실체화 금지)
|
||||
*
|
||||
* 소비를 시작하기 전에는 본문이 실행되지 않아야 대용량에서 메모리가 유계로 유지된다.
|
||||
*/
|
||||
public function test_get_urls_lazy_is_a_generator_that_defers_execution(): void
|
||||
{
|
||||
$contributor = new class extends AbstractSitemapContributor
|
||||
{
|
||||
public bool $executed = false;
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'deferred';
|
||||
}
|
||||
|
||||
public function getUrlsLazy(): iterable
|
||||
{
|
||||
$this->executed = true;
|
||||
yield ['loc' => 'https://example.test/x'];
|
||||
}
|
||||
};
|
||||
|
||||
$lazy = $contributor->getUrlsLazy();
|
||||
|
||||
// 아직 순회하지 않았으므로 제너레이터 본문이 실행되지 않았다.
|
||||
$this->assertFalse($contributor->executed, '순회 전에는 제너레이터 본문이 실행되지 않아야 합니다.');
|
||||
|
||||
iterator_to_array($lazy, false);
|
||||
|
||||
$this->assertTrue($contributor->executed, '순회하면 제너레이터 본문이 실행되어야 합니다.');
|
||||
}
|
||||
|
||||
/**
|
||||
* base 는 SitemapContributorInterface 를 구현한다 (기존 등록 경로 호환)
|
||||
*/
|
||||
public function test_is_a_sitemap_contributor(): void
|
||||
{
|
||||
$contributor = new class extends AbstractSitemapContributor
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'iface';
|
||||
}
|
||||
|
||||
public function getUrlsLazy(): iterable
|
||||
{
|
||||
yield from [];
|
||||
}
|
||||
};
|
||||
|
||||
$this->assertInstanceOf(SitemapContributorInterface::class, $contributor);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Unit\Seo;
|
||||
|
||||
use App\Seo\AbstractSitemapContributor;
|
||||
use App\Seo\Contracts\SitemapContributorInterface;
|
||||
use App\Seo\SitemapGenerator;
|
||||
use App\Seo\SitemapWriter;
|
||||
@@ -649,6 +650,66 @@ class SitemapGeneratorTest extends TestCase
|
||||
$this->assertContains(url('/ok'), array_column($writer->entries, 'loc'));
|
||||
}
|
||||
|
||||
// ─── drain capability 감지 (지연 스트리밍) ─────────────
|
||||
|
||||
/**
|
||||
* getUrlsLazy 를 가진 기여자는 지연 경로로 소진된다 (getUrls 미호출).
|
||||
*
|
||||
* getUrls() 를 호출하면 예외를 던지는 기여자를 사용해, generateToWriter 가
|
||||
* 지연 경로(getUrlsLazy)만 탄다는 것을 증명한다.
|
||||
*/
|
||||
public function test_generate_to_writer_uses_lazy_path_for_capable_contributor(): void
|
||||
{
|
||||
Log::shouldReceive('warning')->zeroOrMoreTimes();
|
||||
|
||||
$contributor = new class extends AbstractSitemapContributor
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'lazy-capable';
|
||||
}
|
||||
|
||||
public function getUrls(): array
|
||||
{
|
||||
throw new \LogicException('getUrls() 는 지연 경로에서 호출되면 안 됩니다.');
|
||||
}
|
||||
|
||||
public function getUrlsLazy(): iterable
|
||||
{
|
||||
yield ['url' => '/lazy/1'];
|
||||
yield ['url' => '/lazy/2'];
|
||||
}
|
||||
};
|
||||
|
||||
$this->generator->registerContributor($contributor);
|
||||
|
||||
$writer = new SpySitemapWriter;
|
||||
$this->generator->generateToWriter($writer);
|
||||
|
||||
$locs = array_column($writer->entries, 'loc');
|
||||
$this->assertContains(url('/lazy/1'), $locs);
|
||||
$this->assertContains(url('/lazy/2'), $locs);
|
||||
}
|
||||
|
||||
/**
|
||||
* getUrlsLazy 가 없는 raw 기여자는 기존 getUrls() 경로로 소진된다 (하위호환).
|
||||
*/
|
||||
public function test_generate_to_writer_uses_get_urls_for_raw_contributor(): void
|
||||
{
|
||||
Log::shouldReceive('warning')->zeroOrMoreTimes();
|
||||
|
||||
$contributor = Mockery::mock(SitemapContributorInterface::class);
|
||||
$contributor->shouldReceive('getIdentifier')->andReturn('raw-module');
|
||||
$contributor->shouldReceive('getUrls')->once()->andReturn([['url' => '/raw/1']]);
|
||||
|
||||
$this->generator->registerContributor($contributor);
|
||||
|
||||
$writer = new SpySitemapWriter;
|
||||
$this->generator->generateToWriter($writer);
|
||||
|
||||
$this->assertContains(url('/raw/1'), array_column($writer->entries, 'loc'));
|
||||
}
|
||||
|
||||
// ─── 헬퍼 메서드 ──────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -210,14 +210,14 @@ class SitemapXmlRendererTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: hreflang alternate 가 현재 N² 구조임을 고정한다 (S3 ⑮ 회귀 기준선)
|
||||
* urlBlock: hreflang 이 켜져 있으면 로케일별 <url> 마다 전체 alternate 집합을 넣는다
|
||||
*
|
||||
* 현재 구현은 <url> 블록마다 모든 로케일의 alternate + x-default 를 넣는다.
|
||||
* 즉 로케일 L 개면 L × (L + 1) 개의 xhtml:link 가 생성된다 — 이것이 S3 ⑮ 가
|
||||
* 해소하려는 N² 팽창이다. S3 에서 이 수치가 바뀌면 의도된 변경이므로
|
||||
* 이 테스트를 함께 갱신할 것.
|
||||
* sitemaps.org 규격상 각 로케일 <url> 은 자기 자신을 포함한 모든 로케일의 alternate 와
|
||||
* x-default 를 가져야 한다. 따라서 로케일 L 개면 L × (L + 1) 개의 xhtml:link 가 나온다
|
||||
* (3 로케일 → 12). S3 ⑮ 는 이 alternate 집합을 base URL 당 한 번만 계산해 로케일별
|
||||
* <url> 에 재사용하도록 바꿨다 — 계산 비용은 L² 에서 L 로 줄지만 정상 구성의 출력은 불변이다.
|
||||
*/
|
||||
public function test_url_block_currently_emits_quadratic_hreflang_links(): void
|
||||
public function test_url_block_emits_full_hreflang_set_per_locale(): void
|
||||
{
|
||||
$xml = $this->multiLocale(['ko', 'en', 'ja'])->urlBlock(['loc' => 'https://example.test/a']);
|
||||
|
||||
@@ -226,6 +226,90 @@ class SitemapXmlRendererTest extends TestCase
|
||||
$this->assertSame(3, substr_count($xml, 'hreflang="x-default"'));
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: 모든 로케일 <url> 의 alternate 집합이 동일하다 (재사용 검증)
|
||||
*
|
||||
* base URL 당 한 번 계산한 alternate 를 각 <url> 에 재사용하므로,
|
||||
* 로케일별 <url> 에 들어가는 alternate 링크 목록은 완전히 같아야 한다.
|
||||
*/
|
||||
public function test_url_block_reuses_identical_alternate_set_across_locales(): void
|
||||
{
|
||||
$xml = $this->multiLocale(['ko', 'en', 'ja'])->urlBlock(['loc' => 'https://example.test/a']);
|
||||
|
||||
// <url> 블록별로 alternate 링크 집합을 추출해 모두 동일한지 확인한다.
|
||||
preg_match_all('/<url>(.*?)<\/url>/s', $xml, $blocks);
|
||||
$this->assertCount(3, $blocks[1]);
|
||||
|
||||
$alternateSets = array_map(static function (string $block): array {
|
||||
preg_match_all('/<xhtml:link[^>]*\/>/', $block, $links);
|
||||
|
||||
return $links[0];
|
||||
}, $blocks[1]);
|
||||
|
||||
$this->assertSame($alternateSets[0], $alternateSets[1]);
|
||||
$this->assertSame($alternateSets[0], $alternateSets[2]);
|
||||
$this->assertCount(4, $alternateSets[0], '각 <url> 은 3 로케일 alternate + x-default = 4 개를 가져야 합니다.');
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: hreflang 을 끄면 로케일별 <url> 은 유지하되 alternate 를 생략한다
|
||||
*/
|
||||
public function test_url_block_omits_hreflang_when_disabled(): void
|
||||
{
|
||||
$renderer = new SitemapXmlRenderer(['ko', 'en', 'ja'], 'ko', hreflangEnabled: false);
|
||||
|
||||
$xml = $renderer->urlBlock(['loc' => 'https://example.test/a']);
|
||||
|
||||
// 로케일별 <url> 은 그대로 3개
|
||||
$this->assertSame(3, substr_count($xml, '<url>'));
|
||||
$this->assertSame(3, $renderer->urlBlockCount());
|
||||
// 로케일 URL 은 유지된다
|
||||
$this->assertStringContainsString('<loc>https://example.test/a?locale=en</loc>', $xml);
|
||||
// alternate 링크는 없다
|
||||
$this->assertStringNotContainsString('xhtml:link', $xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* urlsetHeader: hreflang 을 끄면 xhtml 네임스페이스를 선언하지 않는다 (미사용 방지)
|
||||
*/
|
||||
public function test_urlset_header_omits_xhtml_namespace_when_hreflang_disabled(): void
|
||||
{
|
||||
$header = (new SitemapXmlRenderer(['ko', 'en'], 'ko', hreflangEnabled: false))->urlsetHeader();
|
||||
|
||||
$this->assertStringNotContainsString('xmlns:xhtml', $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: 로케일 수가 상한을 초과하면 alternate 를 생략한다 (N² 팽창 방지)
|
||||
*/
|
||||
public function test_url_block_omits_hreflang_when_locale_count_exceeds_cap(): void
|
||||
{
|
||||
$locales = array_map(static fn (int $i): string => 'l'.$i, range(1, 6));
|
||||
$renderer = new SitemapXmlRenderer($locales, 'l1', maxHreflang: 5);
|
||||
|
||||
$xml = $renderer->urlBlock(['loc' => 'https://example.test/a']);
|
||||
|
||||
// 로케일별 <url> 은 그대로 6개 (상한은 alternate 방출만 막는다)
|
||||
$this->assertSame(6, substr_count($xml, '<url>'));
|
||||
$this->assertStringNotContainsString('xhtml:link', $xml);
|
||||
// 헤더도 xhtml 네임스페이스를 선언하지 않는다
|
||||
$this->assertStringNotContainsString('xmlns:xhtml', $renderer->urlsetHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: 상한 이하 로케일 수는 alternate 를 정상 방출한다 (경계값)
|
||||
*/
|
||||
public function test_url_block_emits_hreflang_at_cap_boundary(): void
|
||||
{
|
||||
$locales = array_map(static fn (int $i): string => 'l'.$i, range(1, 5));
|
||||
$renderer = new SitemapXmlRenderer($locales, 'l1', maxHreflang: 5);
|
||||
|
||||
$xml = $renderer->urlBlock(['loc' => 'https://example.test/a']);
|
||||
|
||||
// 5 로케일 → 5 × (5 + 1) = 30
|
||||
$this->assertSame(30, substr_count($xml, 'xhtml:link'));
|
||||
}
|
||||
|
||||
/**
|
||||
* urlBlock: x-default 는 항상 기본 로케일 URL 을 가리킨다
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user