Create test for app/Observers/EntryObserver

This commit is contained in:
Matt Young 2025-07-04 13:08:52 -05:00
parent 86fb7a7e62
commit 429b26b3f7
3 changed files with 50 additions and 22 deletions

View File

@ -35,11 +35,8 @@ class EntryObserver
{
$syncer = app(DoublerSync::class);
// Update doubler table when an entry is updated
$syncer($entry->audition->event_id);
if ($entry->wasChanged('audition_id')) {
$syncer();
}
}
/**
* Handle the Entry "deleted" event.
@ -51,20 +48,4 @@ class EntryObserver
$audition = Audition::where('id', $entry->audition_id)->first();
$syncer($audition->event_id);
}
/**
* Handle the Entry "restored" event.
*/
public function restored(Entry $entry): void
{
}
/**
* Handle the Entry "force deleted" event.
*/
public function forceDeleted(Entry $entry): void
{
}
}

View File

@ -44,8 +44,8 @@ class AuditionFactory extends Factory
'score_order' => $this->faker->numberBetween(2, 50),
'entry_deadline' => Carbon::tomorrow(),
'entry_fee' => 1000,
'minimum_grade' => $this->faker->numberBetween(7, 9),
'maximum_grade' => $this->faker->numberBetween(8, 12),
'minimum_grade' => 1,
'maximum_grade' => 20,
'for_seating' => 1,
'for_advancement' => 1,
'room_id' => null,

View File

@ -0,0 +1,47 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Entries\CreateEntry;
use App\Actions\Tabulation\DoublerSync;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\Event;
use App\Models\Student;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->mock = Mockery::mock(DoublerSync::class);
app()->instance(DoublerSync::class, $this->mock);
});
afterEach(function () {
Mockery::close();
});
it('syncs doublers if an entry is created that makes or adds to a doubler', function () {
$event = Event::factory()->create();
$student = Student::factory()->create();
$audition1 = Audition::factory()->forEvent($event)->create();
$audition2 = Audition::factory()->forEvent($event)->create();
$audition3 = Audition::factory()->forEvent($event)->create();
$entryMaker = app(CreateEntry::class);
$this->mock->shouldReceive('__invoke')->twice();
$entryMaker($student, $audition1);
$entryMaker($student, $audition2);
$entryMaker($student, $audition3);
});
it('syncs doublers when an entry is updated', function () {
$this->mock->shouldReceive('__invoke')->once();
$entry = Entry::factory()->create();
$entry->update(['audition_id' => Audition::factory()->create()->id]);
});
it('syncs doublers when an entry is deleted', function () {
$this->mock->shouldReceive('__invoke')->once();
$entry = Entry::factory()->create();
$entry->delete();
});