89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Dish;
|
||
|
|
|
||
|
|
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 AddUsersToDishTest extends TestCase
|
||
|
|
{
|
||
|
|
use HasPlanner;
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_it_syncs_users_to_a_user_dish(): void
|
||
|
|
{
|
||
|
|
$userCount = 4;
|
||
|
|
$planner = $this->planner;
|
||
|
|
$users = User::factory()->planner($planner)->count($userCount)->create();
|
||
|
|
$dish = Dish::factory()->planner($planner)->create();
|
||
|
|
|
||
|
|
$this->assertDatabaseEmpty(UserDish::class);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.dishes.users.add', [
|
||
|
|
'dish' => $dish,
|
||
|
|
]), [
|
||
|
|
'users' => $users->map->id->toArray(),
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('planner_id', $planner->id)
|
||
|
|
->where('name', $dish->name)
|
||
|
|
->has('users', $userCount)
|
||
|
|
->where('users', $users
|
||
|
|
->map(fn (User $user) => [
|
||
|
|
'id' => $user->id,
|
||
|
|
'name' => $user->name,
|
||
|
|
])->toArray()
|
||
|
|
)
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(UserDish::class, $userCount);
|
||
|
|
|
||
|
|
$dish->refresh();
|
||
|
|
$this->assertEquals($userCount, $dish->users->count());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_does_not_sync_users_to_a_user_dish_from_another_planner(): void
|
||
|
|
{
|
||
|
|
$userCount = 4;
|
||
|
|
$planner = $this->planner;
|
||
|
|
$otherPlanner = Planner::factory()->create();
|
||
|
|
$users = User::factory()->planner($otherPlanner)->count($userCount)->create();
|
||
|
|
$dish = Dish::factory()->planner($otherPlanner)->create();
|
||
|
|
|
||
|
|
$this->assertDatabaseEmpty(UserDish::class);
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.dishes.users.add', [
|
||
|
|
'dish' => $dish,
|
||
|
|
]), [
|
||
|
|
'users' => $users->map->id->toArray(),
|
||
|
|
])
|
||
|
|
->assertStatus(404)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', false)
|
||
|
|
->where('payload', null)
|
||
|
|
->where('errors', ['MODEL_NOT_FOUND'])
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(UserDish::class, 0);
|
||
|
|
$this->assertEquals(0, $dish->refresh()->users->count());
|
||
|
|
}
|
||
|
|
}
|