Create test for app/Observers/ScoreSheetObserver

This commit is contained in:
Matt Young 2025-07-04 14:02:13 -05:00
parent 4a4947f8bf
commit fbe74571f6
4 changed files with 105 additions and 16 deletions

View File

@ -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',

View File

@ -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);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Database\Factories;
use App\Models\Entry;
use App\Models\ScoreSheet;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
class ScoreSheetFactory extends Factory
{
protected $model = ScoreSheet::class;
public function definition(): array
{
return [
'subscores' => 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,
];
});
}
}

View File

@ -0,0 +1,49 @@
<?php
use App\Actions\Tabulation\TotalEntryScores;
use App\Models\Entry;
use App\Models\ScoreSheet;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->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();
});