Create tests for app/actions/tabulation/PublishSeats

This commit is contained in:
Matt Young 2025-07-02 11:35:13 -05:00
parent ba15191fca
commit fbd99c003d
2 changed files with 113 additions and 0 deletions

View File

@ -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 public function __invoke(Audition $audition, array $seats): void
{ {
if (count($seats) === 0) { if (count($seats) === 0) {

View File

@ -0,0 +1,92 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Tabulation\PublishSeats;
use App\Exceptions\AuditionAdminException;
use App\Models\Audition;
use App\Models\Ensemble;
use App\Models\Entry;
use App\Models\Seat;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->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.');