api/tests/Feature/MealPostListTest.php

38 lines
1.0 KiB
PHP

<?php
use App\Models\MealPosts;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
uses(RefreshDatabase::class);
it('returns only the authenticated user meal posts', function () {
$user = User::factory()->create();
$otherUser = User::factory()->create();
$oldMealPost = MealPosts::factory()->for($user, 'user')->create([
'eaten_at' => '2026-05-18 12:00:00',
]);
$latestMealPost = MealPosts::factory()->for($user, 'user')->create([
'eaten_at' => '2026-05-19 12:00:00',
]);
$otherMealPost = MealPosts::factory()->for($otherUser, 'user')->create([
'eaten_at' => '2026-05-20 12:00:00',
]);
Sanctum::actingAs($user);
$response = $this->getJson('/api/me/meal-posts');
$response
->assertOk()
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $latestMealPost->id)
->assertJsonPath('data.1.id', $oldMealPost->id)
->assertJsonMissing([
'id' => $otherMealPost->id,
]);
});