Create test for app/Observers/EntryFlagObserver

This commit is contained in:
Matt Young 2025-07-04 04:11:37 -05:00
parent 0c3a673bad
commit d609b0d39b
4 changed files with 157 additions and 31 deletions

View File

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

View File

@ -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
{
//
}
}

View File

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

View File

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