From fbe74571f61e7fd07f721339a9901a0bf0a49bcc Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 4 Jul 2025 14:02:13 -0500 Subject: [PATCH] Create test for app/Observers/ScoreSheetObserver --- app/Models/ScoreSheet.php | 3 ++ app/Observers/ScoreSheetObserver.php | 23 +++------ database/factories/ScoreSheetFactory.php | 46 +++++++++++++++++ .../app/Observers/ScoreSheetObserverTest.php | 49 +++++++++++++++++++ 4 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 database/factories/ScoreSheetFactory.php create mode 100644 tests/Feature/app/Observers/ScoreSheetObserverTest.php diff --git a/app/Models/ScoreSheet.php b/app/Models/ScoreSheet.php index e025272..b74ef4b 100644 --- a/app/Models/ScoreSheet.php +++ b/app/Models/ScoreSheet.php @@ -2,12 +2,15 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasOneThrough; class ScoreSheet extends Model { + use HasFactory; + protected $fillable = [ 'user_id', 'entry_id', diff --git a/app/Observers/ScoreSheetObserver.php b/app/Observers/ScoreSheetObserver.php index 5ade31e..b964ad3 100644 --- a/app/Observers/ScoreSheetObserver.php +++ b/app/Observers/ScoreSheetObserver.php @@ -4,6 +4,7 @@ namespace App\Observers; use App\Actions\Tabulation\TotalEntryScores; use App\Models\ScoreSheet; +use Illuminate\Support\Facades\Cache; class ScoreSheetObserver { @@ -14,6 +15,8 @@ class ScoreSheetObserver { $calculator = app(TotalEntryScores::class); $calculator($scoreSheet->entry, true); + Cache::forget('rank_advancement_'.$scoreSheet->entry->audition_id); + Cache::forget('rank_seating_'.$scoreSheet->entry->audition_id); } /** @@ -23,6 +26,8 @@ class ScoreSheetObserver { $calculator = app(TotalEntryScores::class); $calculator($scoreSheet->entry, true); + Cache::forget('rank_advancement_'.$scoreSheet->entry->audition_id); + Cache::forget('rank_seating_'.$scoreSheet->entry->audition_id); } /** @@ -32,21 +37,7 @@ class ScoreSheetObserver { $calculator = app(TotalEntryScores::class); $calculator($scoreSheet->entry, true); - } - - /** - * Handle the ScoreSheet "restored" event. - */ - public function restored(ScoreSheet $scoreSheet): void - { - // - } - - /** - * Handle the ScoreSheet "force deleted" event. - */ - public function forceDeleted(ScoreSheet $scoreSheet): void - { - // + Cache::forget('rank_advancement_'.$scoreSheet->entry->audition_id); + Cache::forget('rank_seating_'.$scoreSheet->entry->audition_id); } } diff --git a/database/factories/ScoreSheetFactory.php b/database/factories/ScoreSheetFactory.php new file mode 100644 index 0000000..987062b --- /dev/null +++ b/database/factories/ScoreSheetFactory.php @@ -0,0 +1,46 @@ + json_encode([1, 2, 3]), + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + 'seating_total' => $this->faker->randomFloat(), + 'advancement_total' => $this->faker->randomFloat(), + + 'user_id' => User::factory(), + 'entry_id' => Entry::factory(), + ]; + } + + public function forUser(User $user) + { + return $this->state(function (array $attributes) use ($user) { + return [ + 'user_id' => $user->id, + ]; + }); + } + + public function forEntry(Entry $entry) + { + return $this->state(function (array $attributes) use ($entry) { + return [ + 'entry_id' => $entry->id, + ]; + }); + } +} diff --git a/tests/Feature/app/Observers/ScoreSheetObserverTest.php b/tests/Feature/app/Observers/ScoreSheetObserverTest.php new file mode 100644 index 0000000..6c2404d --- /dev/null +++ b/tests/Feature/app/Observers/ScoreSheetObserverTest.php @@ -0,0 +1,49 @@ +mock = Mockery::mock(TotalEntryScores::class); + app()->instance(TotalEntryScores::class, $this->mock); + // Use Laravel's built-in facade mock + Cache::spy(); +}); + +afterEach(function () { + Mockery::close(); +}); + +it('totals an entries scores when a score sheet is created', function () { + $this->mock->shouldReceive('__invoke') + ->once() + ->with(\Mockery::type(Entry::class), true) + ->andReturn(null); + Cache::shouldReceive('forget')->twice(); + + $sheet = ScoreSheet::factory()->create(); +}); + +it('totals an entries scores when a score sheet is update', function () { + $this->mock->shouldReceive('__invoke') + ->twice() + ->with(\Mockery::type(Entry::class), true) + ->andReturn(null); + Cache::shouldReceive('forget')->times(4); + $sheet = ScoreSheet::factory()->create(); + $sheet->update(['seating_total' => 9]); +}); + +it('totals an entries scores when a score sheet is deleted', function () { + $this->mock->shouldReceive('__invoke') + ->twice() + ->with(\Mockery::type(Entry::class), true) + ->andReturn(null); + Cache::shouldReceive('forget')->times(4); + $sheet = ScoreSheet::factory()->create(); + $sheet->delete(); +});