true, 'message' => '조회 성공', 'data' => [ 'data' => [ ['id' => 1, 'name' => '홍길동', 'is_active' => true, 'deleted' => null], ], 'pagination' => ['current_page' => 1, 'total' => 10], ], ]; $schema = $inferrer->infer($body); $this->assertSame('collection', $schema['shape']); $this->assertTrue($schema['pagination']); $this->assertSame(['success', 'message', 'data'], $schema['envelope']); $fields = collect($schema['fields'])->keyBy('name'); $this->assertSame('integer', $fields['id']['type']); $this->assertSame('string', $fields['name']['type']); $this->assertSame('boolean', $fields['is_active']['type']); $this->assertSame('null', $fields['deleted']['type']); $this->assertSame('홍길동', $fields['name']['sample']); } #[Test] public function 단건_응답에서_객체_필드를_추론한다(): void { $inferrer = new ResponseSchemaInferrer; $body = [ 'success' => true, 'data' => ['total' => 155, 'ratio' => 0.5, 'labels' => ['ko' => 91]], ]; $schema = $inferrer->infer($body); $this->assertSame('object', $schema['shape']); $this->assertFalse($schema['pagination']); $fields = collect($schema['fields'])->keyBy('name'); $this->assertSame('integer', $fields['total']['type']); $this->assertSame('number', $fields['ratio']['type']); $this->assertSame('object', $fields['labels']['type']); } #[Test] public function 표_셀에서_파이프_문자를_이스케이프한다(): void { $inferrer = new ResponseSchemaInferrer; $body = ['success' => true, 'data' => ['note' => 'a|b|c']]; $schema = $inferrer->infer($body); $fields = collect($schema['fields'])->keyBy('name'); $this->assertStringNotContainsString('|b', str_replace('\\|', '', $fields['note']['sample'])); $this->assertStringContainsString('\\|', $fields['note']['sample']); } #[Test] public function 스캐폴더가_엔드포인트_섹션을_표준_포맷으로_생성한다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.index', 'controller' => 'App\\Http\\Controllers\\Api\\Admin\\UserController', 'controller_method' => 'index', 'permission' => 'core.users.read', 'middleware' => ['auth:sanctum', 'App\\Http\\Middleware\\AdminMiddleware'], 'path_params' => [], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'page', 'type' => 'integer', 'required' => false, 'allowed' => 'min 1'], ], 'hook_filters' => ['core.user.list_validation_rules']]; $schema = [ 'envelope' => ['success', 'data'], 'shape' => 'collection', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => true, ]; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null]); $this->assertStringContainsString('### GET /api/admin/users', $section); $this->assertStringContainsString('@generated:start:api.admin.users.index', $section); $this->assertStringContainsString('`auth:sanctum` + `admin` + `permission:core.users.read`', $section); $this->assertStringContainsString('| page | query | integer | 아니오 | min 1 |', $section); $this->assertStringContainsString('core.user.list_validation_rules', $section); $this->assertStringContainsString('| id | integer | `1` |', $section); // 에러 응답 표: auth:sanctum→401, admin+permission→403, FormRequest(params/hook)→422 $this->assertStringContainsString('**에러 응답**', $section); $this->assertStringContainsString('| 401 | Unauthenticated |', $section); $this->assertStringContainsString('| 403 | Forbidden | 요구 권한(`core.users.read`)이 없는 경우 |', $section); $this->assertStringContainsString('| 422 | Unprocessable Entity |', $section); $this->assertStringContainsString('@generated:end', $section); // 에러 표는 @generated 블록 내부(재생성 대상)여야 한다 $genStart = strpos($section, '@generated:start'); $genEnd = strpos($section, '@generated:end'); $errorPos = strpos($section, '**에러 응답**'); $this->assertGreaterThan($genStart, $errorPos); $this->assertLessThan($genEnd, $errorPos); } #[Test] public function 에러_섹션이_라우트_메타에서_대표_상태코드를_추론한다(): void { $scaffolder = new ApiDocScaffolder; // path param 존재 + admin + permission + FormRequest → 401/403/422/404 전부 $route = [ 'method' => 'PUT', 'uri' => '/api/admin/users/{user}', 'name' => 'api.admin.users.update', 'controller' => 'C', 'controller_method' => 'update', 'permission' => 'core.users.update', 'middleware' => ['auth:sanctum', 'App\\Http\\Middleware\\AdminMiddleware'], 'path_params' => ['user'], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'name', 'type' => 'string', 'required' => true, 'allowed' => ''], ], 'hook_filters' => []]; $section = $scaffolder->endpointSection($route, $request, null, ['status' => null, 'skipped_reason' => 'write-method']); $this->assertStringContainsString('| 401 | Unauthenticated |', $section); $this->assertStringContainsString('| 403 | Forbidden | 요구 권한(`core.users.update`)이 없는 경우 |', $section); $this->assertStringContainsString('| 422 | Unprocessable Entity |', $section); $this->assertStringContainsString('| 404 | Not Found |', $section); } #[Test] public function optional_sanctum_공개조회는_401을_유발하지_않는다(): void { // optional.sanctum(선택 인증)은 미인증도 허용 → 401 없음. // path param 만 있으므로 404 만 노출. $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/modules/sirsoft-board/boards/{slug}', 'name' => 'api.modules.sirsoft-board.boards.show', 'controller' => 'C', 'controller_method' => 'show', 'permission' => null, 'middleware' => ['api', 'optional.sanctum'], 'path_params' => ['slug'], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['data'], 'shape' => 'object', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null]); $this->assertStringNotContainsString('| 401 |', $section); $this->assertStringNotContainsString('| 403 |', $section); $this->assertStringNotContainsString('| 422 |', $section); $this->assertStringContainsString('| 404 | Not Found |', $section); } #[Test] public function 완전_공개_조회는_대표_에러_없음으로_표기한다(): void { // 인증·권한·FormRequest·path param 전무 → 대표 에러 없음. $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/locales', 'name' => 'api.locales.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => ['api'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['data'], 'shape' => 'collection', 'fields' => [['name' => 'code', 'type' => 'string', 'sample' => 'ko']], 'pagination' => false]; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null]); $this->assertStringContainsString('대표 에러 없음', $section); } #[Test] public function optional_sanctum_라우트는_선택적_인증으로_표기한다(): void { // optional.sanctum(회원/비회원 모두 접근)을 auth:sanctum(인증 필수)로 // 오표기하면 공개 API 계약이 왜곡된다(게시판 공개 조회 등). 별도 표기 강제. $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/modules/sirsoft-board/boards/{slug}/posts', 'name' => 'api.modules.sirsoft-board.boards.posts.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => 'user,sirsoft-board.{slug}.posts.read', 'middleware' => ['api', 'optional.sanctum', 'throttle:600,1'], 'path_params' => ['slug'], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['data'], 'shape' => 'collection', 'fields' => [], 'pagination' => true]; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null]); // optional.sanctum 은 선택적 인증으로 표기되고 auth:sanctum 으로 오표기되지 않는다 $this->assertStringContainsString('optional.sanctum', $section); $this->assertStringNotContainsString('`auth:sanctum`', $section); } #[Test] public function 컬럼_주석이_있으면_응답_필드_설명으로_채운다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => [], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = [ 'envelope' => ['data'], 'shape' => 'object', 'fields' => [ ['name' => 'nickname', 'type' => 'string', 'sample' => 'hong'], ['name' => 'unknown_field', 'type' => 'string', 'sample' => 'x'], ], 'pagination' => false, ]; $commentMap = ['nickname' => '닉네임']; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null], $commentMap); // 주석 있는 필드는 설명이 채워지고, 없는 필드는 TODO 유지 $this->assertStringContainsString('| nickname | string | `hong` | 닉네임 |', $section); $this->assertStringContainsString('| unknown_field | string | `x` | |', $section); } #[Test] public function 사람이_쓴_설명에_인라인_코드_파이프가_있어도_잘리지_않고_승계된다(): void { // 회귀: 표 행을 셀로 나눌 때 인라인 코드(`|`) 안의 파이프까지 구분자로 보면 셀 수가 늘고, // "마지막 셀 = 설명" 규칙이 파이프 뒤 조각을 설명으로 승계한다. 그 결과 재생성 한 번에 // 문장 앞부분이 통째로 사라진다 — 오류도 경고도 없이 문서만 훼손된다. // 실제 사례: 스케줄 문서의 command 설명("셸 메타문자(`|`, `;` …)")이 "`, `;` …" 로 잘렸다. $scaffolder = new ApiDocScaffolder; $humanDescription = '실행할 명령. 셸 메타문자(`|`, `;`, `$`)가 포함되면 거부됩니다'; $route = [ 'method' => 'POST', 'uri' => '/api/admin/schedules', 'name' => 'api.admin.schedules.store', 'controller' => 'C', 'controller_method' => 'store', 'permission' => null, 'middleware' => [], 'path_params' => [], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'command', 'type' => 'string', 'required' => true, 'allowed' => 'max 2000'], ], 'hook_filters' => []]; // 1회차: 생성된 문서의 command 행 설명 셀을 사람이 직접 채운 상태로 만든다 $first = $scaffolder->endpointSection($route, $request, null, ['status' => null, 'skipped_reason' => 'write-method']); $humanRow = '| command | body | string | 예 | max 2000 | '.$humanDescription.' |'; $existing = "# 문서\n\n".preg_replace( '/^\| command \| body \|.*$/m', $humanRow, $first ); $this->assertStringContainsString($humanRow, $existing, '전제 실패: 사람 설명이 문서에 들어가지 않았다'); // 2회차: 재생성 → 사람 설명이 원문 그대로 승계되어야 한다 $regenerated = $scaffolder->endpointSection($route, $request, null, ['status' => null, 'skipped_reason' => 'write-method']); $merged = $scaffolder->mergeDocument($existing, '# 문서', [$regenerated], ['api.admin.schedules.store']); $this->assertStringContainsString($humanRow, $merged, '인라인 코드 안의 파이프를 셀 구분자로 보아 설명이 잘렸다'); } #[Test] public function 쓰기_메서드는_응답_필드를_실측_제외로_표기한다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'POST', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.store', 'controller' => 'C', 'controller_method' => 'store', 'permission' => 'core.users.create', 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $section = $scaffolder->endpointSection( $route, ['request_class' => null, 'params' => [], 'hook_filters' => []], null, ['status' => null, 'skipped_reason' => 'write-method'] ); $this->assertStringContainsString('실측 제외: write-method', $section); } #[Test] public function 재생성_시_사람이_작성한_설명을_보존한다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/x', 'name' => 'api.x.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => [], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['data'], 'shape' => 'object', 'fields' => [['name' => 'a', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $section = $scaffolder->endpointSection($route, $request, $schema, ['status' => 200, 'skipped_reason' => null]); $header = "# X\n"; // 최초 생성 $first = $scaffolder->mergeDocument(null, $header, [$section], ['api.x.index']); $this->assertStringContainsString('TODO: 이 엔드포인트의 용도', $first); // 사람이 설명을 채운 상태 $withProse = str_replace( '**설명** ', '**설명** 실제 사람이 작성한 설명입니다.', $first ); // 재생성: 새 섹션으로 병합해도 사람 서술 보존 $regenerated = $scaffolder->mergeDocument($withProse, $header, [$section], ['api.x.index']); $this->assertStringContainsString('실제 사람이 작성한 설명입니다.', $regenerated); $this->assertStringNotContainsString('TODO: 이 엔드포인트의 용도', $regenerated); } #[Test] public function ge_t_인증_엔드포인트_요청_예시가_raw_htt_p_요청이다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/me', 'name' => 'api.me.show', 'controller' => 'C', 'controller_method' => 'show', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['data'], 'shape' => 'object', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $probeMeta = [ 'status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/me', 'body' => ['success' => true, 'data' => ['id' => 1], 'message' => null, 'error' => null], ]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); $this->assertStringContainsString('**요청 예시**', $section); // curl 이 아니라 raw HTTP 요청 라인 + 헤더 $this->assertStringNotContainsString('curl', $section); $this->assertStringContainsString('GET /api/me HTTP/1.1', $section); // Host 는 실측 기준 URL(g7.example.com)이 아니라 공개 placeholder 로 마스킹된다. $this->assertStringContainsString('Host: api.example.com', $section); $this->assertStringNotContainsString('g7.example.com', $section); $this->assertStringContainsString('Accept: application/json', $section); $this->assertStringContainsString('Authorization: Bearer {YOUR_TOKEN}', $section); // 실측 토큰 평문이 유출되지 않아야 한다 (placeholder 마스킹) $this->assertStringNotContainsString('Bearer eyJ', $section); } #[Test] public function 바디_메서드_요청_예시가_json_바디를_가진_raw_htt_p_요청이다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'POST', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.store', 'controller' => 'C', 'controller_method' => 'store', 'permission' => 'core.users.create', 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'name', 'type' => 'string', 'required' => true, 'allowed' => 'max 255'], ['name' => 'age', 'type' => 'integer', 'required' => true, 'allowed' => ''], ['name' => 'nickname', 'type' => 'string', 'required' => false, 'allowed' => ''], ], 'hook_filters' => []]; // 쓰기 메서드는 실측 제외 → body null $probeMeta = [ 'status' => null, 'skipped_reason' => 'write-method', 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/admin/users', 'body' => null, ]; $section = $scaffolder->endpointSection($route, $request, null, $probeMeta); // curl 이 아니라 raw HTTP 요청 라인 + Content-Type 헤더 + JSON 바디 $this->assertStringNotContainsString('curl', $section); $this->assertStringContainsString('POST /api/admin/users HTTP/1.1', $section); $this->assertStringContainsString('Host: api.example.com', $section); $this->assertStringContainsString('Content-Type: application/json', $section); // 전체 파라미터를 바디로 조립하고, 이름 기반 현실적 값을 채운다(placeholder "string" 남발 방지). $this->assertStringContainsString('"name": "예시 이름"', $section); $this->assertStringContainsString('"age": 1', $section); $this->assertStringContainsString('"nickname"', $section); // 응답 예시는 실측 제외 마커 $this->assertStringContainsString('실측 제외: write-method — 응답 예시는 사람이 작성', $section); } #[Test] public function 실측_body_응답_예시는_envelope_통짜_json_이다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/me', 'name' => 'api.me.show', 'controller' => 'C', 'controller_method' => 'show', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'object', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $body = ['success' => true, 'data' => ['id' => 1, 'name' => '홍길동'], 'message' => null, 'error' => null]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/me', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); $this->assertStringContainsString('**응답 예시**', $section); $this->assertStringContainsString('HTTP/1.1 200', $section); $this->assertStringContainsString('"success": true', $section); $this->assertStringContainsString('"홍길동"', $section); $this->assertStringContainsString('"message": null', $section); } #[Test] public function 목록_응답_예시는_2항목으로_절단된다(): void { $scaffolder = new ApiDocScaffolder; $rows = []; for ($i = 1; $i <= 5; $i++) { $rows[] = ['id' => $i, 'name' => "user{$i}"]; } $body = [ 'success' => true, 'data' => ['data' => $rows, 'pagination' => ['total' => 5, 'current_page' => 1]], 'message' => null, 'error' => null, ]; $route = [ 'method' => 'GET', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'collection', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => true]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/admin/users', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); // 대표 2항목 + 절단 주석 항목. user3~5 는 나타나지 않아야 한다. $this->assertStringContainsString('"user1"', $section); $this->assertStringContainsString('"user2"', $section); $this->assertStringNotContainsString('"user3"', $section); $this->assertStringContainsString('총 5건 중 2건 표시', $section); } #[Test] public function 요청_응답_예시_블록은_generated_블록_내부에_있어_재생성_대상이다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/me', 'name' => 'api.me.show', 'controller' => 'C', 'controller_method' => 'show', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'object', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $body = ['success' => true, 'data' => ['id' => 1], 'message' => null, 'error' => null]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/me', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); $genStart = strpos($section, '@generated:start'); $genEnd = strpos($section, '@generated:end'); $reqExample = strpos($section, '**요청 예시**'); $respExample = strpos($section, '**응답 예시**'); // 요청/응답 예시 블록은 모두 @generated 경계 내부(재생성 대상) $this->assertGreaterThan($genStart, $reqExample); $this->assertLessThan($genEnd, $reqExample); $this->assertGreaterThan($genStart, $respExample); $this->assertLessThan($genEnd, $respExample); } #[Test] public function 요청_예시가_generated_블록_안이라_재생성해도_멱등이다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/me', 'name' => 'api.me.show', 'controller' => 'C', 'controller_method' => 'show', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'object', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => false]; $body = ['success' => true, 'data' => ['id' => 1], 'message' => null, 'error' => null]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/me', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); $header = "# Me\n"; // 사람 서술을 채운 뒤 재생성해도 예시(생성 블록)는 갱신되고 서술은 보존된다. $first = $scaffolder->mergeDocument(null, $header, [$section], ['api.me.show']); $withProse = str_replace( '**설명** ', '**설명** 내 프로필을 조회한다.', $first ); $regenerated = $scaffolder->mergeDocument($withProse, $header, [$section], ['api.me.show']); $this->assertStringContainsString('내 프로필을 조회한다.', $regenerated); $this->assertStringNotContainsString('curl', $regenerated); $this->assertStringContainsString('GET /api/me HTTP/1.1', $regenerated); } #[Test] public function insert_example_blocks_는_표와_서술을_건드리지_않고_예시만_삽입한다(): void { $scaffolder = new ApiDocScaffolder; // 예시 블록이 아직 없는 기존 문서 (표에는 사람이 채운 도메인 서술 셀 포함) $existing = <<<'MD' # Me API 레퍼런스 ### GET /api/me - **라우트명**: `api.me.show` - **인증/권한**: `auth:sanctum` **요청 파라미터** _요청 파라미터 없음._ **응답 필드** (`data` 내부) | 필드 | 타입 | 실측 예시값 | 용도/설명 | | --- | --- | --- | --- | | uuid | string | `a231` | 외부 노출용 UUID (사람이 채운 도메인 서술) | **에러 응답** | 상태코드 | 의미 | 발생 조건 | | --- | --- | --- | | 401 | Unauthenticated | 유효한 Bearer 토큰이 없거나 만료된 경우 | **설명** 내 프로필을 조회한다. (사람이 채운 엔드포인트 서술) MD; $blocks = [ 'api.me.show' => [ 'request' => "```http\nGET /api/me HTTP/1.1\nHost: g7.example.com\nAccept: application/json\n```", 'response' => "```http\nHTTP/1.1 200\n```\n\n```json\n{\n \"success\": true\n}\n```", ], ]; [$updated, $inserted] = $scaffolder->insertExampleBlocks($existing, $blocks); // 예시 2블록 삽입됨 $this->assertSame(2, $inserted); $this->assertStringContainsString('**요청 예시**', $updated); $this->assertStringContainsString('**응답 예시**', $updated); $this->assertStringContainsString('GET /api/me HTTP/1.1', $updated); // 표의 사람 서술 셀은 불변 $this->assertStringContainsString('외부 노출용 UUID (사람이 채운 도메인 서술)', $updated); // 엔드포인트 서술도 불변 $this->assertStringContainsString('내 프로필을 조회한다. (사람이 채운 엔드포인트 서술)', $updated); // 삽입 위치: 요청 예시는 응답 필드 앞, 응답 예시는 에러 응답 앞 $this->assertLessThan(strpos($updated, '**응답 필드**'), strpos($updated, '**요청 예시**')); $this->assertLessThan(strpos($updated, '**에러 응답**'), strpos($updated, '**응답 예시**')); // 예시는 @generated 블록 내부 $this->assertLessThan(strpos($updated, '@generated:end'), strpos($updated, '**요청 예시**')); } #[Test] public function insert_example_blocks_는_재삽입_시_멱등이다(): void { $scaffolder = new ApiDocScaffolder; $existing = <<<'MD' ### GET /api/me - **인증/권한**: `auth:sanctum` **요청 파라미터** _요청 파라미터 없음._ **응답 필드** (`data` 내부) _단건._ **에러 응답** | 401 | MD; $blocks = [ 'api.me.show' => [ 'request' => "```http\nGET /api/me HTTP/1.1\nHost: x\n```", 'response' => "```json\n{}\n```", ], ]; [$once] = $scaffolder->insertExampleBlocks($existing, $blocks); [$twice] = $scaffolder->insertExampleBlocks($once, $blocks); // 2회 삽입해도 결과 동일 (요청/응답 예시가 중복 삽입되지 않음) $this->assertSame($once, $twice); $this->assertSame(1, substr_count($twice, '**요청 예시**')); $this->assertSame(1, substr_count($twice, '**응답 예시**')); } #[Test] public function 서술에_수기_응답예시가_있으면_블록에_응답예시를_넣지_않는다(): void { $scaffolder = new ApiDocScaffolder; // @generated 밖 서술에 사람이 미리 작성한 응답 예시가 있는 문서 // (파일 업로드·미설치 정적 예시처럼 실측 불가라 수기로 채운 경우) $existing = <<<'MD' ### GET /api/me/card - **인증/권한**: `auth:sanctum` **요청 파라미터** _요청 파라미터 없음._ **응답 필드** (`data` 내부) _단건._ **에러 응답** | 401 | **설명** 카드 조회. **응답 예시** (정적 — 실측 불가라 사람이 작성) ```json { "success": true, "data": { "id": 1 } } ``` MD; $blocks = [ 'api.me.card' => [ 'request' => "```http\nGET /api/me/card HTTP/1.1\nHost: api.example.com\n```", 'response' => '', ], ]; [$updated] = $scaffolder->insertExampleBlocks($existing, $blocks); // 요청 예시는 블록 안에 삽입된다 $this->assertStringContainsString('**요청 예시**', $updated); $this->assertStringContainsString('GET /api/me/card HTTP/1.1', $updated); // 응답 예시 헤딩은 정확히 1개 (수기 예시만, 블록 안 마커 미삽입 → 중복 헤딩 없음) $this->assertSame(1, substr_count($updated, '**응답 예시**')); // 수기 응답 예시는 보존 $this->assertStringContainsString('정적 — 실측 불가라 사람이 작성', $updated); // 블록 안에 실측 제외 마커가 들어가지 않아야 한다 $this->assertStringNotContainsString('실측 제외: http-500', $updated); } #[Test] public function 과거_삽입된_블록내_응답예시는_서술에_수기예시가_생기면_제거된다(): void { $scaffolder = new ApiDocScaffolder; // 과거 run 이 블록 안에 응답 예시 마커를 넣었고, 이후 서술에 수기 응답 예시가 추가된 상태 $existing = <<<'MD' ### GET /api/me/card - **인증/권한**: `auth:sanctum` **요청 파라미터** _요청 파라미터 없음._ **응답 필드** (`data` 내부) _단건._ **응답 예시** **에러 응답** | 401 | **응답 예시** (정적) ```json { "success": true } ``` MD; $blocks = [ 'api.me.card' => [ 'request' => "```http\nGET /api/me/card HTTP/1.1\nHost: api.example.com\n```", 'response' => '', ], ]; [$updated] = $scaffolder->insertExampleBlocks($existing, $blocks); // 블록 안 응답 예시가 제거되어 응답 예시 헤딩은 1개(수기 예시)만 남는다 $this->assertSame(1, substr_count($updated, '**응답 예시**')); $this->assertStringNotContainsString('실측 제외: http-500', $updated); // 수기 예시는 보존 $this->assertStringContainsString('**응답 예시** (정적)', $updated); } #[Test] public function 파일_업로드_요청_예시는_multipart_form_data_다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'POST', 'uri' => '/api/me/avatar', 'name' => 'api.me.avatar.upload', 'controller' => 'C', 'controller_method' => 'uploadAvatar', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'avatar', 'type' => 'image', 'required' => true, 'allowed' => 'max 2048'], ], 'hook_filters' => []]; $probeMeta = ['status' => null, 'skipped_reason' => 'write-method', 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/me/avatar', 'body' => null]; $section = $scaffolder->endpointSection($route, $request, null, $probeMeta); // 파일(image) 파라미터는 application/json 이 아니라 multipart/form-data 로 표기된다. $this->assertStringContainsString('Content-Type: multipart/form-data', $section); $this->assertStringNotContainsString('"avatar": "string"', $section); $this->assertStringContainsString('filename=', $section); $this->assertStringContainsString('name="avatar"', $section); } #[Test] public function ge_t_요청_예시는_query_파라미터를_url_쿼리스트링으로_붙인다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/search', 'name' => 'api.search.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => ['api'], 'path_params' => [], ]; $request = ['request_class' => 'X', 'params' => [ ['name' => 'q', 'type' => 'string', 'required' => true, 'allowed' => ''], ['name' => 'type', 'type' => 'string', 'required' => false, 'allowed' => '`post`, `product`'], ], 'hook_filters' => []]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/search', 'body' => ['success' => true, 'data' => []]]; $section = $scaffolder->endpointSection($route, $request, $schema = null, $probeMeta); // query 파라미터가 URL 쿼리스트링으로 반영된다 (in: 열거는 첫 값 채택). $this->assertMatchesRegularExpression('/GET \/api\/search\?[^ ]*q=/', $section); $this->assertStringContainsString('type=post', $section); } #[Test] public function 응답_예시는_토큰_비밀번호_등_민감값을_마스킹한다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'POST', 'uri' => '/api/auth/login', 'name' => 'api.auth.login', 'controller' => 'C', 'controller_method' => 'login', 'permission' => null, 'middleware' => ['api'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'object', 'fields' => [['name' => 'token', 'type' => 'string', 'sample' => '69|abc']], 'pagination' => false]; $body = ['success' => true, 'data' => [ 'token' => '69|NzX4qbOT4Xns28p6Ik7d7CvGiYn8kuyi8cS0J0AL5ad91266', 'token_type' => 'Bearer', 'user' => ['uuid' => 'abc', 'password' => '$2y$10$abcdefghijklmnop'], ], 'message' => null, 'error' => null]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/auth/login', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); // 실제 토큰/비밀번호 해시는 노출되지 않고 마스킹된다. $this->assertStringNotContainsString('NzX4qbOT4Xns28p6', $section); $this->assertStringNotContainsString('$2y$10$abcdefghijklmnop', $section); $this->assertStringContainsString('{MASKED}', $section); // token_type 은 민감값이 아니므로 보존된다. $this->assertStringContainsString('"token_type": "Bearer"', $section); } #[Test] public function 응답_예시_body의_절대_ur_l_호스트는_placeholder로_마스킹된다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/admin/users', 'name' => 'api.admin.users.index', 'controller' => 'C', 'controller_method' => 'index', 'permission' => null, 'middleware' => ['auth:sanctum'], 'path_params' => [], ]; $request = ['request_class' => null, 'params' => [], 'hook_filters' => []]; $schema = ['envelope' => ['success', 'data'], 'shape' => 'collection', 'fields' => [['name' => 'id', 'type' => 'integer', 'sample' => '1']], 'pagination' => true]; // 실측 응답의 페이지네이터 메타·콜백 URL 은 요청 base URL 의 호스트를 그대로 직렬화한다. $body = ['success' => true, 'data' => [ 'data' => [['id' => 1]], 'first_page_url' => 'https://g7_2.dev/api/admin/users?page=1', 'path' => 'https://g7_2.dev/api/admin/users', 'callback_url' => 'https://g7_2.dev:8443/callback', ], 'message' => null, 'error' => null]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7_2.dev', 'resolved_uri' => '/api/admin/users', 'body' => $body]; $section = $scaffolder->endpointSection($route, $request, $schema, $probeMeta); // 실측 기준 호스트(g7_2.dev)는 응답 예시 어디에도 노출되지 않고 placeholder 로 치환된다. $this->assertStringNotContainsString('g7_2.dev', $section); $this->assertStringContainsString('https://api.example.com/api/admin/users?page=1', $section); // 포트가 붙은 절대 URL 도 호스트+포트 통째로 마스킹된다. $this->assertStringContainsString('https://api.example.com/callback', $section); } #[Test] public function 파라미터_표는_path와_form_request_의_동일_이름을_중복_출력하지_않는다(): void { $scaffolder = new ApiDocScaffolder; $route = [ 'method' => 'GET', 'uri' => '/api/templates/{identifier}/assets/{path}', 'name' => 'api.templates.assets', 'controller' => 'C', 'controller_method' => 'assets', 'permission' => null, 'middleware' => ['api'], 'path_params' => ['identifier', 'path'], ]; // FormRequest 에도 identifier/path 규칙이 있는 경우 (중복 유발 조건) $request = ['request_class' => 'X', 'params' => [ ['name' => 'identifier', 'type' => 'string', 'required' => true, 'allowed' => ''], ['name' => 'path', 'type' => 'string', 'required' => true, 'allowed' => ''], ], 'hook_filters' => []]; $probeMeta = ['status' => 200, 'skipped_reason' => null, 'base_url' => 'https://g7.example.com', 'resolved_uri' => '/api/templates/x/assets/y', 'body' => ['success' => true, 'data' => []]]; $section = $scaffolder->endpointSection($route, $request, null, $probeMeta); // identifier/path 는 path 행으로만 1회 출력 (query 중복 행 없음). $this->assertSame(1, substr_count($section, '| identifier | path |')); $this->assertStringNotContainsString('| identifier | query |', $section); $this->assertSame(1, substr_count($section, '| path | path |')); } #[Test] public function readme_목차는_도메인_파일과_엔드포인트_수를_표로_나열한다(): void { $scaffolder = new ApiDocScaffolder; $entries = [ ['domain' => 'products', 'file' => 'products.md', 'count' => 30], ['domain' => 'orders', 'file' => 'orders.md', 'count' => 22], ]; $readme = $scaffolder->readmeIndex('모듈 `sirsoft-ecommerce`', $entries); // 소유 라벨·집계·도메인 링크가 표에 나타난다. $this->assertStringContainsString('# API 레퍼런스 문서 목차', $readme); $this->assertStringContainsString('모듈 `sirsoft-ecommerce`', $readme); $this->assertStringContainsString('**문서 수**: 2 · **엔드포인트 수**: 52', $readme); $this->assertStringContainsString('| [products.md](products.md) | `products` | 30 |', $readme); $this->assertStringContainsString('| [orders.md](orders.md) | `orders` | 22 |', $readme); // 도메인 알파벳 정렬 — orders 가 products 보다 먼저. $this->assertLessThan(strpos($readme, 'products.md'), strpos($readme, 'orders.md')); } #[Test] public function readme_재생성은_generated_블록만_갱신하고_사람_서술을_보존한다(): void { $scaffolder = new ApiDocScaffolder; $entries = [['domain' => 'pages', 'file' => 'pages.md', 'count' => 5]]; $first = $scaffolder->readmeIndex('모듈 `sirsoft-page`', $entries); // @generated 블록 밖에 사람 개요 서술을 덧붙인다. $human = $first."\n## 개요\n\n이 모듈은 정적 페이지를 관리합니다. (사람 서술)\n"; // 엔드포인트 수가 바뀐 재생성. $entriesUpdated = [['domain' => 'pages', 'file' => 'pages.md', 'count' => 6]]; $regen = $scaffolder->readmeIndex('모듈 `sirsoft-page`', $entriesUpdated, $human); // 표는 갱신되고(6), 사람 서술은 보존된다. $this->assertStringContainsString('엔드포인트 수**: 6', $regen); $this->assertStringContainsString('이 모듈은 정적 페이지를 관리합니다. (사람 서술)', $regen); // 재재생성해도 사람 서술 중복 없이 멱등. $regen2 = $scaffolder->readmeIndex('모듈 `sirsoft-page`', $entriesUpdated, $regen); $this->assertSame( substr_count($regen, '(사람 서술)'), substr_count($regen2, '(사람 서술)') ); } /** * 목록이 아닌 큰 응답(카탈로그·스펙)도 예시 크기 상한 안으로 절단되어야 한다. * * 회귀 배경 (#518): 절단 로직이 페이지네이션 형태(`data.data[]`)만 다뤄, 편집기 스펙처럼 * 목록이 아닌 응답이 통째로 직렬화됐다. templates.md 한 파일이 115KB → 8.65MB 가 됐다. */ #[Test] public function 목록이_아닌_큰_응답도_예시_크기_상한으로_절단된다(): void { $scaffolder = new ApiDocScaffolder; // 편집기 스펙 형태 — 중첩 객체 안에 큰 배열이 있고 페이지네이션 형태가 아니다. $palette = []; for ($i = 0; $i < 400; $i++) { $palette["component_{$i}"] = [ 'name' => "Component{$i}", 'description' => str_repeat('설명 텍스트 ', 40), 'props' => array_fill(0, 30, 'prop-value-'.str_repeat('x', 60)), ]; } $body = [ 'success' => true, 'message' => '조회 성공', 'data' => ['palette' => $palette, 'version' => '1.0.0'], ]; $rawSize = strlen((string) json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); $this->assertGreaterThan(500_000, $rawSize, '전제: 원본이 충분히 커야 절단을 검증할 수 있다'); $block = $scaffolder->responseExampleBlock(null, ['status' => 200, 'body' => $body]); // 존재를 먼저 확정한 뒤 크기를 단언한다. $this->assertStringContainsString('```json', $block); $this->assertLessThan( 16_384, strlen($block), '목록이 아닌 큰 응답도 상한 이하로 절단되어야 한다', ); $this->assertStringContainsString('생략', $block, '무엇이 잘렸는지 문서에 남아야 한다'); } /** * 재생성이 표의 사람 서술을 TODO 로 되돌리지 않아야 한다. * * 회귀 배경 (#518): 전체 재생성이 `@generated` 블록의 파라미터/응답 필드 표를 통째로 다시 * 조립하면서, 정적 추출로 재현할 수 없는 도메인 설명이 TODO 스텁으로 되돌아갔다 * (54개 파일 1,181행). `--examples-only` 로만 우회 가능했던 것을 재생성 경로에서 막는다. */ #[Test] public function 재생성이_표의_사람_서술을_todo_로_되돌리지_않는다(): void { $scaffolder = new ApiDocScaffolder; $existing = <<<'MD' # API ### GET /api/admin/things **요청 파라미터** | 이름 | 위치 | 타입 | 필수 | 기본값 | 설명 | | --- | --- | --- | --- | --- | --- | | no_category | query | boolean | 아니오 | — | 카테고리 미지정 상품만 필터 | | keyword | query | string | 아니오 | — | | **응답 필드** (`data` 내부) | 필드 | 타입 | 예시 | 설명 | | --- | --- | --- | --- | | uuid | string | `abc` | 외부 노출용 UUID | **설명** 사람이 쓴 서술. MD; // 재생성 결과 — 두 설명 모두 TODO 스텁으로 조립된 상태 $newSection = <<<'MD' ### GET /api/admin/things **요청 파라미터** | 이름 | 위치 | 타입 | 필수 | 기본값 | 설명 | | --- | --- | --- | --- | --- | --- | | no_category | query | boolean | 아니오 | — | | | keyword | query | string | 아니오 | — | | **응답 필드** (`data` 내부) | 필드 | 타입 | 예시 | 설명 | | --- | --- | --- | --- | | uuid | string | `abc` | | **설명** MD; $merged = $scaffolder->mergeDocument( $existing, '# API', [$newSection], ['api.admin.things.index'], ); // 사람이 채운 설명은 되살아난다. $this->assertStringContainsString('카테고리 미지정 상품만 필터', $merged); $this->assertStringContainsString('외부 노출용 UUID', $merged); // 원래도 비어 있던 셀은 TODO 로 남는다. $this->assertStringContainsString('| keyword | query | string | 아니오 | — | |', $merged); // 블록 밖 사람 서술도 그대로 보존된다. $this->assertStringContainsString('사람이 쓴 서술.', $merged); } /** * 실측 데이터가 없어 필드 표가 비면 기존 표를 유지해야 한다. * * 회귀 배경 (#518): 대상 데이터가 없는 상태로 재생성하자 응답 필드 표가 "실측 응답에 필드 없음" * 한 줄로 대체됐다 (reviews.md 388줄 소실). "이번에 데이터가 없었다" 는 사실은 이미 문서화된 * 필드 목록을 지울 근거가 못 된다. */ #[Test] public function 빈_실측이_기존_응답_필드_표를_지우지_않는다(): void { $scaffolder = new ApiDocScaffolder; $existing = <<<'MD' # API ### GET /api/admin/things **응답 필드** (`data` 내부) | 필드 | 타입 | 예시 | 설명 | | --- | --- | --- | --- | | id | integer | `99` | 기본 키 (내부 식별자) | | rating | integer | `5` | 별점 (1~5) | **응답 예시** ```json {} ``` MD; $newSection = <<<'MD' ### GET /api/admin/things **응답 필드** (`data` 내부) **응답 예시** ```json {} ``` MD; $merged = $scaffolder->mergeDocument($existing, '# API', [$newSection], ['api.admin.things.index']); $this->assertStringContainsString('| rating | integer | `5` | 별점 (1~5) |', $merged); $this->assertStringNotContainsString('실측 응답에 필드 없음', $merged); } /** * 기존 설명이 있으면 도구가 만든 일반 문구로 덮어쓰지 않는다. * * 도구의 설명은 필드명에서 유추한 일반 문구라 사람이 적은 도메인 사실보다 정보량이 적다. * 회귀 배경 (#518): `플러그인 이름 (다국어 JSON)` 이 `대상의 이름/명칭 (다국어 필드는 …)` 로, * `desc 는 주문별 가장 늦은 발송일` 같은 서술이 일반 문구로 대체돼 224건이 후퇴했다. * 값이 바뀌는 컬럼(타입·예시·허용값)은 그대로 재생성되므로 최신성은 유지된다. */ #[Test] public function 기존_설명은_도구의_일반_문구로_덮어쓰지_않는다(): void { $scaffolder = new ApiDocScaffolder; $existing = <<<'MD' # API ### GET /api/admin/things | 필드 | 타입 | 예시 | 설명 | | --- | --- | --- | --- | | name | string | `옛값` | 플러그인 이름 (다국어 JSON) | MD; $newSection = <<<'MD' ### GET /api/admin/things | 필드 | 타입 | 예시 | 설명 | | --- | --- | --- | --- | | name | string | `새값` | 대상의 이름/명칭 (다국어 필드는 로케일별 값 객체) | MD; $merged = $scaffolder->mergeDocument($existing, '# API', [$newSection], ['api.admin.things.index']); // 설명은 사람 서술을 유지하고, $this->assertStringContainsString('플러그인 이름 (다국어 JSON)', $merged); $this->assertStringNotContainsString('대상의 이름/명칭', $merged); // 실측으로 갱신되는 예시값은 새 값으로 바뀐다. $this->assertStringContainsString('`새값`', $merged); $this->assertStringNotContainsString('`옛값`', $merged); } /** * 상한 이하의 작은 응답은 손대지 않는다 (절단이 정상 예시를 훼손하지 않음). */ #[Test] public function 상한_이하_응답은_그대로_직렬화된다(): void { $scaffolder = new ApiDocScaffolder; $body = [ 'success' => true, 'message' => '조회 성공', 'data' => ['id' => 7, 'identifier' => 'sirsoft-admin_basic', 'version' => '1.0.4'], ]; $block = $scaffolder->responseExampleBlock(null, ['status' => 200, 'body' => $body]); $this->assertStringContainsString('"identifier": "sirsoft-admin_basic"', $block); $this->assertStringNotContainsString('생략', $block); } }