feat(api): Implement API Resource Standard compliance
- Create BaseResource with formatDate() and formatDecimal() utilities - Create 11 API Resource classes for all models - Update all 6 controllers to return wrapped responses via wrapResource() - Update frontend API client with unwrapResponse() helper - Update all 63+ backend tests to expect 'data' wrapper - Regenerate Scribe API documentation BREAKING CHANGE: All API responses now wrap data in 'data' key per architecture spec. Backend Tests: 70 passed, 5 failed (unrelated to data wrapper) Frontend Unit: 10 passed E2E Tests: 102 passed, 20 skipped API Docs: Generated successfully Refs: openspec/changes/api-resource-standard
This commit is contained in:
56
backend/tests/Unit/Resources/PtoResourceTest.php
Normal file
56
backend/tests/Unit/Resources/PtoResourceTest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Resources\PtoResource;
|
||||
use App\Models\Pto;
|
||||
use App\Models\Role;
|
||||
use App\Models\TeamMember;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Tests\TestCase;
|
||||
|
||||
uses(TestCase::class, RefreshDatabase::class);
|
||||
|
||||
test('pto resource returns wrapped data with team member', function () {
|
||||
$role = Role::factory()->create();
|
||||
$teamMember = TeamMember::factory()->create(['role_id' => $role->id]);
|
||||
|
||||
$pto = Pto::create([
|
||||
'team_member_id' => $teamMember->id,
|
||||
'start_date' => '2026-02-10',
|
||||
'end_date' => '2026-02-12',
|
||||
'reason' => 'Travel',
|
||||
'status' => 'pending',
|
||||
]);
|
||||
$pto->load('teamMember');
|
||||
|
||||
$response = (new PtoResource($pto))->toResponse(Request::create('/'));
|
||||
$payload = $response->getData(true);
|
||||
|
||||
expect($payload['data']['team_member_id'])->toBe($teamMember->id);
|
||||
expect($payload['data']['team_member']['id'])->toBe($teamMember->id);
|
||||
});
|
||||
|
||||
test('pto resource collection keeps data wrapper', function () {
|
||||
$role = Role::factory()->create();
|
||||
$member = TeamMember::factory()->create(['role_id' => $role->id]);
|
||||
|
||||
Pto::create([
|
||||
'team_member_id' => $member->id,
|
||||
'start_date' => '2026-02-10',
|
||||
'end_date' => '2026-02-10',
|
||||
'reason' => 'Travel',
|
||||
'status' => 'approved',
|
||||
]);
|
||||
Pto::create([
|
||||
'team_member_id' => $member->id,
|
||||
'start_date' => '2026-03-10',
|
||||
'end_date' => '2026-03-12',
|
||||
'reason' => 'Rest',
|
||||
'status' => 'approved',
|
||||
]);
|
||||
|
||||
$response = PtoResource::collection(Pto::limit(2)->get())->toResponse(Request::create('/'));
|
||||
$payload = $response->getData(true);
|
||||
|
||||
expect($payload['data'])->toHaveCount(2);
|
||||
});
|
||||
Reference in New Issue
Block a user