From d609b0d39b5972146e6a96152d03f7f1d02f05dc Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 4 Jul 2025 04:11:37 -0500 Subject: [PATCH] Create test for app/Observers/EntryFlagObserver --- app/Models/Entry.php | 7 +- app/Observers/EntryFlagObserver.php | 33 +---- .../Observers/EntryFlagObserverMocksTest.php | 29 +++++ .../app/Observers/EntryFlagObserverTest.php | 119 ++++++++++++++++++ 4 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 tests/Feature/app/Observers/EntryFlagObserverMocksTest.php create mode 100644 tests/Feature/app/Observers/EntryFlagObserverTest.php diff --git a/app/Models/Entry.php b/app/Models/Entry.php index 0fbff74..081793e 100644 --- a/app/Models/Entry.php +++ b/app/Models/Entry.php @@ -129,8 +129,11 @@ class Entry extends Model public function removeFlag($flag): void { - // remove the related auditionFlag where flag_name = $flag - $this->flags()->where('flag_name', $flag)->delete(); + $thisFlag = EntryFlag::where('flag_name', $flag) + ->where('entry_id', $this->id)->first(); + if ($thisFlag) { + $thisFlag->delete(); + } $this->load('flags'); } diff --git a/app/Observers/EntryFlagObserver.php b/app/Observers/EntryFlagObserver.php index 6338aa5..b14a8d9 100644 --- a/app/Observers/EntryFlagObserver.php +++ b/app/Observers/EntryFlagObserver.php @@ -7,6 +7,8 @@ use App\Models\Doubler; use App\Models\EntryFlag; use Illuminate\Support\Facades\Cache; +use function in_array; + class EntryFlagObserver { /** @@ -14,7 +16,7 @@ class EntryFlagObserver */ public function creating(EntryFlag $entryFlag): void { - if (in_array($entryFlag->flag_name, ['declined', 'no_show', 'failed_prelim'])) { + if (in_array($entryFlag->flag_name->value, ['declined', 'no_show', 'failed_prelim'])) { if ($entryFlag->entry->audition->hasFlag('seats_published') || $entryFlag->entry->audition->hasFlag('advancement_published')) { throw new AuditionAdminException('Cannot change flag for published auditions.'); } @@ -29,23 +31,12 @@ class EntryFlagObserver } - /** - * Handle the EntryFlag "updated" event. - */ - public function updated(EntryFlag $entryFlag): void - { - Doubler::syncDoublers(); - Cache::forget('rank_advancement_'.$entryFlag->entry->audition_id); - Cache::forget('rank_seating_'.$entryFlag->entry->audition_id); - - } - /** * Handle the EntryFlag "deleted" event. */ public function deleting(EntryFlag $entryFlag): void { - if (in_array($entryFlag->flag_name, ['declined', 'no_show', 'failed_prelim'])) { + if (in_array($entryFlag->flag_name->value, ['declined', 'no_show', 'failed_prelim'])) { if ($entryFlag->entry->audition->hasFlag('seats_published') || $entryFlag->entry->audition->hasFlag('advancement_published')) { throw new AuditionAdminException('Cannot change flag for published auditions.'); } @@ -59,20 +50,4 @@ class EntryFlagObserver Cache::forget('rank_seating_'.$entryFlag->entry->audition_id); } - - /** - * Handle the EntryFlag "restored" event. - */ - public function restored(EntryFlag $entryFlag): void - { - // - } - - /** - * Handle the EntryFlag "force deleted" event. - */ - public function forceDeleted(EntryFlag $entryFlag): void - { - // - } } diff --git a/tests/Feature/app/Observers/EntryFlagObserverMocksTest.php b/tests/Feature/app/Observers/EntryFlagObserverMocksTest.php new file mode 100644 index 0000000..c6d7bf1 --- /dev/null +++ b/tests/Feature/app/Observers/EntryFlagObserverMocksTest.php @@ -0,0 +1,29 @@ +entry = Entry::factory()->create(); + app()->forgetInstance(Doubler::class); + +}); + +afterEach(function () { + Mockery::close(); + \Mockery::getConfiguration()->allowMockingNonExistentMethods(true); + Cache::flush(); +}); + +it('syncs doublers when a flag is added', function () { + Mockery::mock('overload:App\Models\Doubler') + ->shouldReceive('syncDoublers') + ->once() + ->andReturn(null); + + $this->entry->addFlag('declined'); +})->skip(); +// TODO Figure out how to test diff --git a/tests/Feature/app/Observers/EntryFlagObserverTest.php b/tests/Feature/app/Observers/EntryFlagObserverTest.php new file mode 100644 index 0000000..0274307 --- /dev/null +++ b/tests/Feature/app/Observers/EntryFlagObserverTest.php @@ -0,0 +1,119 @@ +entry = Entry::factory()->create(); + +}); + +afterEach(function () { + + Cache::flush(); +}); + +it('it throws an exception if we try to add a declined flag while seats are published', function () { + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->addFlag('declined'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to add a no_show flag while seats are published', function () { + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->addFlag('no_show'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to add a failed_prelim flag while seats are published', function () { + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->addFlag('failed_prelim'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to add a declined flag while advancement is published', function () { + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->addFlag('declined'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to add a no_show flag while advancement is published', function () { + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->addFlag('no_show'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to add a failed_prelim flag while advancement is published', function () { + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->addFlag('failed_prelim'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('clears caches when adding a flag', function () { + Cache::put('rank_advancement_'.$this->entry->audition_id, 'test', 60); + Cache::put('rank_seating_'.$this->entry->audition_id, 'test', 60); + expect(Cache::has('rank_advancement_'.$this->entry->audition_id))->toBeTrue() + ->and(Cache::has('rank_seating_'.$this->entry->audition_id))->toBeTrue(); + + $this->entry->addFlag('declined'); + expect(Cache::has('rank_advancement_'.$this->entry->audition_id))->toBeFalse() + ->and(Cache::has('rank_seating_'.$this->entry->audition_id))->toBeFalse(); +}); + +it('it throws an exception if we try to delete a declined flag while seats are published', function () { + $this->entry->addFlag('declined'); + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->removeFlag('declined'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to delete a no_show flag while seats are published', function () { + $this->entry->addFlag('no_show'); + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->removeFlag('no_show'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to delete a failed_prelim flag while seats are published', function () { + $this->entry->addFlag('failed_prelim'); + $this->entry->audition->addFlag('seats_published'); + $this->entry->refresh(); + $this->entry->removeFlag('failed_prelim'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to delete a declined flag while advancement is published', function () { + $this->entry->addFlag('declined'); + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->removeFlag('declined'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to delete a no_show flag while advancement is published', function () { + $this->entry->addFlag('no_show'); + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->removeFlag('no_show'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('it throws an exception if we try to delete a failed_prelim flag while advancement is published', function () { + $this->entry->addFlag('failed_prelim'); + $this->entry->audition->addFlag('advancement_published'); + $this->entry->refresh(); + $this->entry->removeFlag('failed_prelim'); +})->throws(AuditionAdminException::class, 'Cannot change flag for published auditions'); + +it('clears caches when deleting a flag', function () { + $this->entry->addFlag('no_show'); + Cache::put('rank_advancement_'.$this->entry->audition_id, 'test', 60); + Cache::put('rank_seating_'.$this->entry->audition_id, 'test', 60); + expect(Cache::has('rank_advancement_'.$this->entry->audition_id))->toBeTrue() + ->and(Cache::has('rank_seating_'.$this->entry->audition_id))->toBeTrue(); + + EntryFlag::first()->delete(); + expect(Cache::has('rank_advancement_'.$this->entry->audition_id))->toBeFalse() + ->and(Cache::has('rank_seating_'.$this->entry->audition_id))->toBeFalse(); +});