app/tests/Feature/User/ShowUserWithDishesTest.php

66 lines
2.1 KiB
PHP
Raw Normal View History

2025-10-13 14:57:11 +02:00
<?php
namespace Tests\Feature\User;
use App\Models\Dish;
use App\Models\User;
use App\Models\UserDish;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
use Tests\Traits\HasPlanner;
class ShowUserWithDishesTest extends TestCase
{
use HasPlanner;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->setUpHasPlanner();
}
2025-10-13 14:57:11 +02:00
public function test_list_user_dishes(): void
{
$planner = $this->planner;
$users = User::factory()->planner($planner)->count(rand(3, 10))->create();
$dishes = Dish::factory()->planner($planner)->count(rand(3, 10))->create();
$dishes->each(function (Dish $dish) use ($users) {
$dish->users()->sync($users->random(rand(1, 3))->pluck('id'));
});
$targetUser = $users->random();
$targetUser->refresh();
$this
->actingAs($planner)
->get(route('api.users.dishes.index', [
'user' => $targetUser->id,
]))
->assertStatus(200)
->assertJson(fn (AssertableJson $json) => $json
->where('success', true)
->has('payload', fn ($json) => $json
->has('user', fn ($json) => $json
->where('id', $targetUser->id)
->where('name', $targetUser->name)
->has('user_dishes', $targetUser->dishes->count())
->where('user_dishes', $targetUser->userDishes
->map(fn (UserDish $userDish) => [
'id' => $userDish->id,
'dish' => [
'id' => $userDish->dish->id,
'name' => $userDish->dish->name,
],
'recurrences' => [],
])
->toArray()
)
)
)
->where('errors', null)
);
}
}