<?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');
});
