setUpHasPlanner(); } 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(); $dish->users()->attach($users); $removedUser = $users->random(); $remainingUsers = $users->reject(fn (User $user) => $user->id === $removedUser->id)->values(); $this->assertDatabaseCount(UserDish::class, $userCount); $this ->actingAs($planner) ->post(route('api.dishes.users.remove', [ 'dish' => $dish, ]), [ 'users' => [$removedUser->id], ]) ->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', $remainingUsers->count()) ->where('users', $remainingUsers ->map(fn (User $user) => [ 'id' => $user->id, 'name' => $user->name, ])->toArray() ) ) ) ->where('errors', null) ); $this->assertDatabaseCount(UserDish::class, $userCount - 1); $dish->refresh(); $this->assertEquals($userCount - 1, $dish->users->count()); } public function test_it_does_not_sync_users_to_a_user_dish_of_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(); $dish->users()->attach($users); $removedUser = $users->random(); $this->assertDatabaseCount(UserDish::class, $userCount); $this ->actingAs($planner) ->post(route('api.dishes.users.remove', [ 'dish' => $dish, ]), [ 'users' => [$removedUser->id], ]) ->assertStatus(404) ->assertJson(fn (AssertableJson $json) => $json ->where('success', false) ->where('payload', null) ->where('errors', ['MODEL_NOT_FOUND']) ); $this->assertDatabaseCount(UserDish::class, $userCount); } }