setter = app(SetHeadDirector::class); }); it('sets a head director flag for a user with a school', function () { // Arrange $school = School::factory()->create(); $user = User::factory()->create(['school_id' => $school->id]); $this->setter->setHeadDirector($user); $this->assertDatabaseHas('user_flags', [ 'user_id' => $user->id, 'flag_name' => 'head_director', ]); }); it('throws an error if the user has no school', function () { // Arrange $user = User::factory()->create(); // Act & Assert $this->setter->setHeadDirector($user); })->throws(AuditionAdminException::class, 'User is not associated with a school'); it('removes the head director flag from any other users as the school', function () { // Arrange $school = School::factory()->create(); $oldHead = User::factory()->create(['school_id' => $school->id]); $newHead = User::factory()->create(['school_id' => $school->id]); $oldHead->addFlag('head_director'); $this->assertDatabaseHas('user_flags', [ 'user_id' => $oldHead->id, 'flag_name' => 'head_director', ]); // Act $this->setter->setHeadDirector($newHead); $this->assertDatabaseHas('user_flags', [ 'user_id' => $newHead->id, 'flag_name' => 'head_director', ]); $this->assertDatabaseMissing('user_flags', [ 'user_id' => $oldHead->id, 'flag_name' => 'head_director', ]); });