273 lines
9.9 KiB
PHP
273 lines
9.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\User\Dish;
|
||
|
|
|
||
|
|
use App\Models\Dish;
|
||
|
|
use App\Models\MinimumRecurrence;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\UserDish;
|
||
|
|
use App\Models\UserDishRecurrence;
|
||
|
|
use App\Models\WeeklyRecurrence;
|
||
|
|
use App\WeekdaysEnum;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
||
|
|
use Tests\TestCase;
|
||
|
|
use Tests\Traits\HasPlanner;
|
||
|
|
|
||
|
|
class StoreRecurrenceForUserDishTest extends TestCase
|
||
|
|
{
|
||
|
|
use HasPlanner;
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_it_adds_fixed_recurrence_to_user_dish(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$recurrenceType = WeeklyRecurrence::class;
|
||
|
|
$recurrenceValue = WeekdaysEnum::Thursday->value;
|
||
|
|
|
||
|
|
$user = User::factory()->planner($planner)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
$user->dishes()->attach($dish);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.users.dishes.recurrences.store', [
|
||
|
|
'dish' => $dish,
|
||
|
|
'user' => $user,
|
||
|
|
]), [
|
||
|
|
[
|
||
|
|
'type' => $recurrenceType,
|
||
|
|
'value' => $recurrenceValue,
|
||
|
|
],
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('user_dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('dish.name', $dish->name)
|
||
|
|
->has('user', fn ($json) => $json
|
||
|
|
->where('id', $user->id)
|
||
|
|
->where('name', $user->name)
|
||
|
|
)
|
||
|
|
->has('recurrences', 1)
|
||
|
|
->has('recurrences.0', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('type', $recurrenceType)
|
||
|
|
->where('value', $recurrenceValue)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(WeeklyRecurrence::class, 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_adds_minimum_recurrence_to_user_dish(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$recurrenceType = MinimumRecurrence::class;
|
||
|
|
$recurrenceValue = 5;
|
||
|
|
|
||
|
|
$user = User::factory()->planner($planner)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
$user->dishes()->attach($dish);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.users.dishes.recurrences.store', [
|
||
|
|
'dish' => $dish,
|
||
|
|
'user' => $user
|
||
|
|
]), [
|
||
|
|
[
|
||
|
|
'type' => $recurrenceType,
|
||
|
|
'value' => $recurrenceValue,
|
||
|
|
]
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('user_dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('dish.id', $dish->id)
|
||
|
|
->where('dish.planner_id', $dish->planner_id)
|
||
|
|
->where('dish.name', $dish->name)
|
||
|
|
->where('user.id', $user->id)
|
||
|
|
->where('user.name', $user->name)
|
||
|
|
->has('recurrences', 1)
|
||
|
|
->has('recurrences.0', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('type', $recurrenceType)
|
||
|
|
->where('value', $recurrenceValue)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(MinimumRecurrence::class, 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_adds_multiple_recurrences_to_user_dish(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$user = User::factory()->planner($planner)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
$user->dishes()->attach($dish);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.users.dishes.recurrences.store', [
|
||
|
|
'dish' => $dish,
|
||
|
|
'user' => $user
|
||
|
|
]), [
|
||
|
|
[
|
||
|
|
'type' => MinimumRecurrence::class,
|
||
|
|
'value' => 5,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'type' => WeeklyRecurrence::class,
|
||
|
|
'value' => WeekdaysEnum::Thursday->value,
|
||
|
|
],
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('user_dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('dish.id', $dish->id)
|
||
|
|
->where('dish.planner_id', $dish->planner_id)
|
||
|
|
->where('dish.name', $dish->name)
|
||
|
|
->where('user.id', $user->id)
|
||
|
|
->where('user.name', $user->name)
|
||
|
|
->has('recurrences', 2)
|
||
|
|
->has('recurrences.0', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('type', MinimumRecurrence::class)
|
||
|
|
->where('value', 5)
|
||
|
|
)
|
||
|
|
->has('recurrences.1', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('type', WeeklyRecurrence::class)
|
||
|
|
->where('value', WeekdaysEnum::Thursday->value)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(MinimumRecurrence::class, 1);
|
||
|
|
$this->assertDatabaseHas(MinimumRecurrence::class, [
|
||
|
|
'days' => 5,
|
||
|
|
]);
|
||
|
|
$this->assertDatabaseCount(WeeklyRecurrence::class, 1);
|
||
|
|
$this->assertDatabaseHas(WeeklyRecurrence::class, [
|
||
|
|
'weekday' => WeekdaysEnum::Thursday->value,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_removes_all_recurrences(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
$user = User::factory()->planner($planner)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
$user->dishes()->attach($dish);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.users.dishes.recurrences.store', [
|
||
|
|
'dish' => $dish,
|
||
|
|
'user' => $user
|
||
|
|
]), [])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('user_dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('dish.id', $dish->id)
|
||
|
|
->where('dish.planner_id', $dish->planner_id)
|
||
|
|
->where('dish.name', $dish->name)
|
||
|
|
->where('user.id', $user->id)
|
||
|
|
->where('user.name', $user->name)
|
||
|
|
->where('recurrences', [])
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseEmpty(MinimumRecurrence::class);
|
||
|
|
$this->assertDatabaseEmpty(WeeklyRecurrence::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_removes_other_recurrences_to_user_dish(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
MinimumRecurrence::query()->truncate();
|
||
|
|
WeeklyRecurrence::query()->truncate();
|
||
|
|
UserDishRecurrence::query()->truncate();
|
||
|
|
|
||
|
|
$user = User::factory()->planner($planner)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
|
||
|
|
$userDish = UserDish::factory()->create([
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'dish_id' => $dish->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$recurrence = MinimumRecurrence::factory()->create([
|
||
|
|
'days' => 5,
|
||
|
|
]);
|
||
|
|
|
||
|
|
UserDishRecurrence::factory()->create([
|
||
|
|
'user_dish_id' => $userDish->id,
|
||
|
|
'recurrence_id' => $recurrence->id,
|
||
|
|
'recurrence_type' => MinimumRecurrence::class,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(MinimumRecurrence::class, 1);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.users.dishes.recurrences.store', [
|
||
|
|
'dish' => $dish,
|
||
|
|
'user' => $user
|
||
|
|
]), [
|
||
|
|
[
|
||
|
|
'type' => WeeklyRecurrence::class,
|
||
|
|
'value' => WeekdaysEnum::Thursday->value,
|
||
|
|
],
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('user_dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('dish.id', $dish->id)
|
||
|
|
->where('dish.name', $dish->name)
|
||
|
|
->where('user.id', $user->id)
|
||
|
|
->where('user.name', $user->name)
|
||
|
|
->has('recurrences', 1)
|
||
|
|
->has('recurrences.0', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('type', WeeklyRecurrence::class)
|
||
|
|
->where('value', WeekdaysEnum::Thursday->value)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(MinimumRecurrence::class, 0);
|
||
|
|
$this->assertDatabaseCount(WeeklyRecurrence::class, 1);
|
||
|
|
$this->assertDatabaseHas(WeeklyRecurrence::class, [
|
||
|
|
'weekday' => WeekdaysEnum::Thursday->value,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|