112 lines
4 KiB
PHP
112 lines
4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Schedule;
|
||
|
|
|
||
|
|
use App\Models\Dish;
|
||
|
|
use App\Models\Planner;
|
||
|
|
use App\Models\Schedule;
|
||
|
|
use App\Models\ScheduledUserDish;
|
||
|
|
use App\Models\User;
|
||
|
|
use Carbon\CarbonPeriod;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
||
|
|
use Tests\TestCase;
|
||
|
|
use Tests\Traits\HasPlanner;
|
||
|
|
|
||
|
|
class ReadScheduleTest extends TestCase
|
||
|
|
{
|
||
|
|
use HasPlanner;
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_single_day_can_be_read(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$userOne = User::factory()->planner($planner)->create(['name' => 'Melissa']);
|
||
|
|
$userTwo = User::factory()->planner($planner)->create(['name' => 'Jochen']);
|
||
|
|
|
||
|
|
$users = collect([$userOne, $userTwo]);
|
||
|
|
$dishes = Dish::factory()->planner($planner)->count(rand(15, 20))->create();
|
||
|
|
$dishes->each(fn (Dish $dish) => $dish->users()->sync($users->pluck('id')->toArray()));
|
||
|
|
|
||
|
|
$dateRangeStart = '2024-01-01';
|
||
|
|
$dateRangeEnd = '2024-01-07';
|
||
|
|
|
||
|
|
$period = CarbonPeriod::create($dateRangeStart, $dateRangeEnd);
|
||
|
|
|
||
|
|
foreach ($period as $date) {
|
||
|
|
$schedule = Schedule::factory()->planner($planner)->date($date)->create();
|
||
|
|
|
||
|
|
$users->each(fn (User $user) => ScheduledUserDish::factory()
|
||
|
|
->schedule($schedule)
|
||
|
|
->userDish($user->userDishes->random())
|
||
|
|
->create()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$randomSchedule = $planner->schedules->random();
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->get(route('api.schedule.show', $randomSchedule->date->format('Y-m-d')))
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('schedule', fn (AssertableJson $json) => $json
|
||
|
|
->where('id', $randomSchedule->id)
|
||
|
|
->where('is_skipped', false)
|
||
|
|
->where('date', $randomSchedule->date->format('Y-m-d'))
|
||
|
|
->has('scheduled_user_dishes', 2)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_single_day_cannot_be_read_by_other_planner(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$otherPlanner = Planner::factory()->create();
|
||
|
|
$userOne = User::factory()->planner($otherPlanner)->create(['name' => 'Melissa']);
|
||
|
|
$userTwo = User::factory()->planner($otherPlanner)->create(['name' => 'Jochen']);
|
||
|
|
|
||
|
|
$users = collect([$userOne, $userTwo]);
|
||
|
|
$dishes = Dish::factory()->planner($otherPlanner)->count(rand(15, 20))->create();
|
||
|
|
$dishes->each(fn (Dish $dish) => $dish->users()->sync($users->pluck('id')->toArray()));
|
||
|
|
|
||
|
|
$dateRangeStart = '2024-01-01';
|
||
|
|
$dateRangeEnd = '2024-01-07';
|
||
|
|
|
||
|
|
$period = CarbonPeriod::create($dateRangeStart, $dateRangeEnd);
|
||
|
|
|
||
|
|
foreach ($period as $date) {
|
||
|
|
$schedule = Schedule::factory()->planner($otherPlanner)->date($date)->create();
|
||
|
|
|
||
|
|
$users->each(fn (User $user) => ScheduledUserDish::factory()
|
||
|
|
->schedule($schedule)
|
||
|
|
->userDish($user->userDishes->random())
|
||
|
|
->create()
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->assertEmpty($planner->schedules);
|
||
|
|
|
||
|
|
$randomDateFromPeriod = collect($period)->random()->format('Y-m-d');
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->get(route('api.schedule.show', $randomDateFromPeriod))
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload.schedule', fn (AssertableJson $json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('date', $randomDateFromPeriod)
|
||
|
|
->where('is_skipped', null)
|
||
|
|
->where('scheduled_user_dishes', [])
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|