- 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
31 lines
946 B
PHP
31 lines
946 B
PHP
<?php
|
|
|
|
use App\Http\Resources\UserResource;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\Request;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class, RefreshDatabase::class);
|
|
|
|
test('user resource wraps response with data', function () {
|
|
$user = User::factory()->create();
|
|
|
|
$response = (new UserResource($user))->toResponse(Request::create('/'));
|
|
$payload = $response->getData(true);
|
|
|
|
expect(array_key_exists('data', $payload))->toBeTrue();
|
|
expect($payload['data']['id'])->toBe($user->id);
|
|
expect($payload['data'])->toHaveKey('email');
|
|
});
|
|
|
|
test('user resource collection honors data wrapper', function () {
|
|
$users = User::factory()->count(2)->create();
|
|
|
|
$response = UserResource::collection($users)->toResponse(Request::create('/'));
|
|
$payload = $response->getData(true);
|
|
|
|
expect($payload['data'])->toHaveCount(2);
|
|
expect($payload['data'][0])->toHaveKey('id');
|
|
});
|