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.sync', [ '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_of_other_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.sync', [ '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->assertDatabaseEmpty(UserDish::class); $this->assertEmpty($dish->refresh()->users->count()); } }