app/tests/Feature/User/ListUsersTest.php

79 lines
2.6 KiB
PHP

<?php
namespace Tests\Feature\User;
use App\Models\Dish;
use App\Models\Planner;
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 ListUsersTest extends TestCase
{
use HasPlanner;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->setUpHasPlanner();
}
public function test_user_can_see_list_of_users(): void
{
$planner = $this->planner;
$dishes = Dish::factory()->planner($planner)->count(rand(80, 100))->create();
$users = User::factory()->planner($planner)->count(rand(2, 10))->create();
$users->each(fn (User $user) => UserDish::factory()
->user($user)
->dish($dishes->random())
->create());
$this
->actingAs($planner)
->get(route('api.users.index'))
->assertStatus(200)
->assertJson(fn (AssertableJson $json) => $json
->where('success', true)
->has('payload', fn ($json) => $json
->where('users', $users
->sortBy('id')
->map(fn (User $user) => [
'id' => $user->id,
'name' => $user->name,
'user_dishes' => $user->userDishes->map(fn (UserDish $userDish) => [
'id' => $userDish->id,
'dish' => [
'id' => $userDish->dish->id,
'name' => $userDish->dish->name,
],
'recurrences' => [],
])->toArray(),
])
->toArray()
)
)
->where('errors', null)
);
}
public function test_user_cannot_see_list_of_users_of_other_planner(): void
{
$planner = $this->planner;
$otherPlanner = Planner::factory()->create();
User::factory()->planner($otherPlanner)->count(rand(2, 10))->create();
$this
->actingAs($planner)
->get(route('api.users.index'))
->assertStatus(200)
->assertJson(fn (AssertableJson $json) => $json
->where('success', true)
->where('payload.users', [])
->where('errors', null)
);
}
}