From 118a465bb72522378a55689e09a320052d41767d Mon Sep 17 00:00:00 2001 From: Matt Young Date: Thu, 10 Jul 2025 04:08:53 -0500 Subject: [PATCH] Tests for BonusScoreRecordController.php --- .../Judging/BonusScoreRecordController.php | 12 +-- .../BonusScoreRecordControllerTest.php | 85 +++++++++++++++++++ 2 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/app/Http/Controllers/Judging/BonusScoreRecordControllerTest.php diff --git a/app/Http/Controllers/Judging/BonusScoreRecordController.php b/app/Http/Controllers/Judging/BonusScoreRecordController.php index 911fe3d..82a6d69 100644 --- a/app/Http/Controllers/Judging/BonusScoreRecordController.php +++ b/app/Http/Controllers/Judging/BonusScoreRecordController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers\Judging; use App\Actions\Tabulation\EnterBonusScore; -use App\Exceptions\ScoreEntryException; +use App\Exceptions\AuditionAdminException; use App\Http\Controllers\Controller; use App\Models\Entry; use Illuminate\Support\Facades\App; @@ -14,15 +14,17 @@ class BonusScoreRecordController extends Controller public function __invoke(Entry $entry) { $enterBonusScore = App::make(EnterBonusScore::class); + $max = $entry->audition->bonusScore()->first()->max_score; $validData = request()->validate([ - 'score' => 'required|integer', + 'score' => 'required|integer|min:0|max:'.$max, ]); try { $enterBonusScore(Auth::user(), $entry, $validData['score']); - } catch (ScoreEntryException $ex) { - return redirect()->back()->with('error', 'Score Entry Error - '.$ex->getMessage()); + } catch (AuditionAdminException $ex) { + return redirect(route('dashboard'))->with('error', 'Score Entry Error - '.$ex->getMessage()); } - return redirect()->route('judging.bonusScore.EntryList', $entry->audition)->with('Score Recorded Successfully'); + return redirect()->route('judging.bonusScore.EntryList', $entry->audition)->with('success', + 'Score Recorded Successfully'); } } diff --git a/tests/Feature/app/Http/Controllers/Judging/BonusScoreRecordControllerTest.php b/tests/Feature/app/Http/Controllers/Judging/BonusScoreRecordControllerTest.php new file mode 100644 index 0000000..9473a02 --- /dev/null +++ b/tests/Feature/app/Http/Controllers/Judging/BonusScoreRecordControllerTest.php @@ -0,0 +1,85 @@ +create(); + $entry = Entry::factory()->forAudition($audition)->create(); + $judge = User::factory()->create(); + $bonusScoreDefinition = BonusScoreDefinition::factory()->create(['max_score' => 100]); + $bonusScoreDefinition->auditions()->attach($audition); + $bonusScoreDefinition->judges()->attach($judge); + + // BonusScoreObserver needs regular scoring set up + $sg = ScoringGuide::factory()->create(); + $audition->update(['scoring_guide_id' => $sg->id]); + + $response = $this->actingAs($judge)->post(route('judging.bonusScores.recordScore', $entry), [ + 'score' => 28, + ]); + $response->assertRedirect(route('judging.bonusScore.EntryList', $audition)) + ->assertSessionHas('success', 'Score Recorded Successfully'); +}); + +it('sends us to the dashboard if we arent a judge', function () { + $audition = Audition::factory()->create(); + $entry = Entry::factory()->forAudition($audition)->create(); + $judge = User::factory()->create(); + $bonusScoreDefinition = BonusScoreDefinition::factory()->create(['max_score' => 100]); + $bonusScoreDefinition->auditions()->attach($audition); + + // BonusScoreObserver needs regular scoring set up + $sg = ScoringGuide::factory()->create(); + $audition->update(['scoring_guide_id' => $sg->id]); + + $response = $this->actingAs($judge)->post(route('judging.bonusScores.recordScore', $entry), [ + 'score' => 28, + ]); + $response->assertRedirect(route('dashboard')) + ->assertSessionHas('error', 'You are not assigned to judge.'); +}); + +it('sends us to judging home if we arent assigned to the audition', function () { + $audition = Audition::factory()->create(); + $entry = Entry::factory()->forAudition($audition)->create(); + $judge = User::factory()->create(); + $bonusScoreDefinition = BonusScoreDefinition::factory()->create(['max_score' => 100]); + $bonusScoreDefinition->auditions()->attach($audition); + $room = Room::factory()->create(); + $room->judges()->attach($judge); + // BonusScoreObserver needs regular scoring set up + $sg = ScoringGuide::factory()->create(); + $audition->update(['scoring_guide_id' => $sg->id]); + + $response = $this->actingAs($judge)->post(route('judging.bonusScores.recordScore', $entry), [ + 'score' => 28, + ]); + $response->assertRedirect(route('dashboard')) + ->assertSessionHas('error', 'Score Entry Error - That judge is not assigned to judge that bonus score'); +}); + +it('will not accept a score that is too high', function () { + $audition = Audition::factory()->create(); + $entry = Entry::factory()->forAudition($audition)->create(); + $judge = User::factory()->create(); + $bonusScoreDefinition = BonusScoreDefinition::factory()->create(['max_score' => 100]); + $bonusScoreDefinition->auditions()->attach($audition); + $bonusScoreDefinition->judges()->attach($judge); + + // BonusScoreObserver needs regular scoring set up + $sg = ScoringGuide::factory()->create(); + $audition->update(['scoring_guide_id' => $sg->id]); + + $response = $this->actingAs($judge)->post(route('judging.bonusScores.recordScore', $entry), [ + 'score' => 101, + ]); + $response->assertRedirect()->assertSessionHasErrors('score'); +});