From fbd99c003d0963fcad9b30e01af399fee0ba13fb Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 2 Jul 2025 11:35:13 -0500 Subject: [PATCH] Create tests for app/actions/tabulation/PublishSeats --- app/Actions/Tabulation/PublishSeats.php | 21 +++++ .../Actions/Tabulation/PublishSeatsTest.php | 92 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tests/Feature/app/Actions/Tabulation/PublishSeatsTest.php diff --git a/app/Actions/Tabulation/PublishSeats.php b/app/Actions/Tabulation/PublishSeats.php index 5b14e4f..87f91ef 100644 --- a/app/Actions/Tabulation/PublishSeats.php +++ b/app/Actions/Tabulation/PublishSeats.php @@ -14,6 +14,27 @@ class PublishSeats // } + /** + * Publishes the given audition with the provided seats. + * + * This method first validates that the seats array is not empty. If the array is empty, + * an AuditionAdminException is thrown. + * + * Next, it deletes existing records in the `seats` table associated with the provided audition + * using the `audition_id`. + * + * Then, it iterates through the provided seats array to create new records in the `seats` table + * with the specified `ensemble_id`, `audition_id`, `seat`, and `entry_id`. + * + * Finally, it marks the audition as having its seats published by adding a relevant flag + * to the audition, and clears cached data associated with the results seat list and + * public results page entries in the cache store. + * + * @param Audition $audition The audition instance to be published. + * @param array $seats An array of seat data to be associated with the audition. + * + * @throws AuditionAdminException If the provided seats array is empty. + */ public function __invoke(Audition $audition, array $seats): void { if (count($seats) === 0) { diff --git a/tests/Feature/app/Actions/Tabulation/PublishSeatsTest.php b/tests/Feature/app/Actions/Tabulation/PublishSeatsTest.php new file mode 100644 index 0000000..49bf3c5 --- /dev/null +++ b/tests/Feature/app/Actions/Tabulation/PublishSeatsTest.php @@ -0,0 +1,92 @@ +audition = Audition::factory()->create(['minimum_grade' => 1, 'maximum_grade' => 14]); + $this->windEnsemble = Ensemble::create([ + 'event_id' => $this->audition->event_id, + 'name' => 'Wind Ensemble', + 'code' => 'we', + 'rank' => 1, + ]); + $this->alternates = Ensemble::create([ + 'event_id' => $this->audition->event_id, + 'name' => 'Alternates', + 'code' => 'alt', + 'rank' => 2, + ]); + $this->entries = Entry::factory()->count(10)->create(['audition_id' => $this->audition->id]); + $this->seatingArray = [ + 1 => [ + 'ensemble_id' => $this->windEnsemble->id, + 'audition_id' => $this->audition->id, + 'entry_id' => $this->entries[0]->id, + 'seat' => 1, + ], + 2 => [ + 'ensemble_id' => $this->windEnsemble->id, + 'audition_id' => $this->audition->id, + 'entry_id' => $this->entries[1]->id, + 'seat' => 2, + ], + 3 => [ + 'ensemble_id' => $this->windEnsemble->id, + 'audition_id' => $this->audition->id, + 'entry_id' => $this->entries[2]->id, + 'seat' => 3, + ], + 4 => [ + 'ensemble_id' => $this->alternates->id, + 'audition_id' => $this->audition->id, + 'entry_id' => $this->entries[3]->id, + 'seat' => 1, + ], + 5 => [ + 'ensemble_id' => $this->alternates->id, + 'audition_id' => $this->audition->id, + 'entry_id' => $this->entries[4]->id, + 'seat' => 2, + ], + ]; + $this->publisher = app(PublishSeats::class); + +}); + +it('seats an audition', function () { + ($this->publisher)($this->audition, $this->seatingArray); + expect($this->audition->hasFlag('seats_published'))->toBeTrue() + ->and(Seat::where('ensemble_id', $this->windEnsemble->id)->where('seat', 1) + ->first()->entry_id)->toEqual($this->entries[0]->id) + ->and(Seat::where('ensemble_id', $this->windEnsemble->id)->where('seat', 2) + ->first()->entry_id)->toEqual($this->entries[1]->id) + ->and(Seat::where('ensemble_id', $this->windEnsemble->id)->where('seat', 3) + ->first()->entry_id)->toEqual($this->entries[2]->id) + ->and(Seat::where('ensemble_id', $this->alternates->id)->where('seat', 1) + ->first()->entry_id)->toEqual($this->entries[3]->id) + ->and(Seat::where('ensemble_id', $this->alternates->id)->where('seat', 2) + ->first()->entry_id)->toEqual($this->entries[4]->id); +}); + +it('clears results and set list cache', function () { + $this->get(route('results')); + expect(Cache::has('publicResultsPage'))->toBeTrue() + ->and(Cache::has('resultsSeatList'))->toBeTrue(); + ($this->publisher)($this->audition, $this->seatingArray); + expect(Cache::has('publicResultsPage'))->toBeFalse() + ->and(Cache::has('resultsSeatList'))->toBeFalse(); +}); + +it('throws and exception if no seats are provided', function () { + ($this->publisher)($this->audition, []); +})->throws(AuditionAdminException::class, 'Cannot publish an audition with no seats.');