목록이 뒤쪽 페이지로 갈수록 느려지는 원인은 건너뛰는 행의 넓은 컬럼까지 읽는 것이었다. 지연 조인(inner 는 키 컬럼만, outer 는 이번 페이지 행만 전체 컬럼)과 정렬 컬럼 해석을 코어 공통 트레이트로 두고 코어 8 + 확장 6 저장소에 적용했다. 감사 지적 처리: - 그룹 쿼리의 총 건수 오산 — count 는 groupBy 를 벗기지 않아 첫 그룹의 행 수를 총 건수로 돌려준다. getCountForPagination 으로 교정. - audit 전수 스캔이 버퍼 한계로 한 번도 동작하지 않던 결함과, 확장 경로를 놓치던 정규식 5건을 함께 고쳤다. 그동안 번들 확장의 위반이 보이지 않았다. raw 20건을 빌더 표현으로 치환하고 하드코딩 테이블명 16건을 모델 유래로 바꿨다. 이 과정에서 마이페이지 「내가 댓글 단 글」 검색이 조인 별칭 충돌로 SQL 오류를 내던 결함이 해소됐다. 재발 차단용 룰 2종을 신설하고, 컬럼 프루닝 룰을 error 로 승격했다 (전역 위반 0건 — warning 으로 두면 "0 error" 판정을 통과하며 무음으로 재누적된다). 정렬 허용 목록은 FormRequest 게이트를 덮어야 한다. 좁으면 검증을 통과한 정렬이 422 도 로그도 없이 기본값으로 되돌아간다(스케줄 실행 이력의 종료시각·소요시간 정렬이 그랬다). 호출처를 스캔해 매핑 완전성까지 검사하는 계약 테스트로 전수 강제한다. 해외 배송지 결함도 함께 고쳤다 — 조회는 city/state/postal_code 로 내려주고 폼은 intl_* 로 제출하는 비대칭 때문에, 수정 시 입력칸이 비어 보이고 저장하면 기존 값이 유실됐다. 쿠폰 수정의 유효기간 미검증과 1인당 사용 제한 부분 수정 차단도 함께 수정.
144 lines
4.0 KiB
PHP
144 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Repositories\Concerns;
|
|
|
|
use App\Repositories\Concerns\ResolvesSortSpec;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* 정렬 스펙 해석 Trait 단위 테스트
|
|
*
|
|
* 요청 유래 값이 화이트리스트 밖으로 새지 않는지, 그리고 부적절한 값이 와도 기본값으로
|
|
* 안전하게 폴백하는지를 고정한다.
|
|
*/
|
|
class ResolvesSortSpecTest extends TestCase
|
|
{
|
|
/** 테스트용 리포지토리를 생성합니다. */
|
|
private function repository(): object
|
|
{
|
|
return new class
|
|
{
|
|
use ResolvesSortSpec;
|
|
|
|
/**
|
|
* 정렬 스펙 해석을 그대로 노출합니다. (Trait 메서드는 protected)
|
|
*
|
|
* @param array $args resolveSortSpec 인자
|
|
* @return array 정렬 스펙
|
|
*/
|
|
public function resolve(...$args): array
|
|
{
|
|
return $this->resolveSortSpec(...$args);
|
|
}
|
|
};
|
|
}
|
|
|
|
public function test_allowed_column_and_direction_pass_through(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => 'created_at', 'sort_order' => 'asc'],
|
|
['id', 'created_at', 'status'],
|
|
'id',
|
|
);
|
|
|
|
$this->assertSame([['column' => 'created_at', 'direction' => 'asc']], $spec);
|
|
}
|
|
|
|
public function test_unknown_column_falls_back_to_default(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => 'admin_memo', 'sort_order' => 'desc'],
|
|
['id', 'created_at'],
|
|
'id',
|
|
);
|
|
|
|
$this->assertSame('id', $spec[0]['column']);
|
|
}
|
|
|
|
public function test_invalid_direction_falls_back_to_default(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => 'created_at', 'sort_order' => 'sideways'],
|
|
['created_at'],
|
|
'created_at',
|
|
'asc',
|
|
);
|
|
|
|
$this->assertSame('asc', $spec[0]['direction']);
|
|
}
|
|
|
|
public function test_column_alias_is_applied_before_whitelist_check(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => 'author', 'sort_order' => 'desc'],
|
|
['id', 'author_name'],
|
|
'id',
|
|
'desc',
|
|
['author' => 'author_name'],
|
|
);
|
|
|
|
$this->assertSame('author_name', $spec[0]['column']);
|
|
}
|
|
|
|
public function test_custom_filter_keys_are_honored(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['order_by' => 'view_count', 'order_direction' => 'asc'],
|
|
['id', 'view_count'],
|
|
'id',
|
|
'desc',
|
|
[],
|
|
'order_by',
|
|
'order_direction',
|
|
);
|
|
|
|
$this->assertSame([['column' => 'view_count', 'direction' => 'asc']], $spec);
|
|
}
|
|
|
|
public function test_backed_enum_values_are_normalized(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => SampleSortColumn::CreatedAt, 'sort_order' => SampleSortDirection::Asc],
|
|
['id', 'created_at'],
|
|
'id',
|
|
);
|
|
|
|
$this->assertSame([['column' => 'created_at', 'direction' => 'asc']], $spec);
|
|
}
|
|
|
|
public function test_missing_and_blank_values_fall_back(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => ' '],
|
|
['id', 'created_at'],
|
|
'created_at',
|
|
'desc',
|
|
);
|
|
|
|
$this->assertSame([['column' => 'created_at', 'direction' => 'desc']], $spec);
|
|
}
|
|
|
|
public function test_array_value_is_rejected(): void
|
|
{
|
|
$spec = $this->repository()->resolve(
|
|
['sort_by' => ['created_at'], 'sort_order' => ['asc']],
|
|
['id', 'created_at'],
|
|
'id',
|
|
);
|
|
|
|
$this->assertSame([['column' => 'id', 'direction' => 'desc']], $spec);
|
|
}
|
|
}
|
|
|
|
/** 테스트용 정렬 컬럼 Enum */
|
|
enum SampleSortColumn: string
|
|
{
|
|
case CreatedAt = 'created_at';
|
|
}
|
|
|
|
/** 테스트용 정렬 방향 Enum */
|
|
enum SampleSortDirection: string
|
|
{
|
|
case Asc = 'asc';
|
|
}
|