Implement enum for EntryFlags

This commit is contained in:
Matt Young 2024-07-08 12:33:51 -05:00
parent a9fdc1a7f0
commit a0913861fc
6 changed files with 47 additions and 20 deletions

12
app/Enums/EntryFlags.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace App\Enums;
enum EntryFlags: string
{
case WILL_ADVANCE = 'will_advance';
case DECLINED = 'declined';
case NO_SHOW = 'no_show';
case FAILED_PRELIM = 'failed_prelim';
}

View File

@ -83,7 +83,6 @@ class Audition extends Model
$flags[] = $checkFlag->flag_name->value;
}
// return true if any flag in $this->flags has a flag_name of declined without making another db query if flags are loaded
return in_array($flag, $flags);
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Enums\EntryFlags;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -66,8 +67,12 @@ class Entry extends Model
public function hasFlag($flag): bool
{
// return true if any flag in $this->flags has a flag_name of declined without making another db query if flags are loaded
return $this->flags->contains('flag_name', $flag);
$flags = [];
foreach ($this->flags as $checkFlag) {
$flags[] = $checkFlag->flag_name->value;
}
return in_array($flag, $flags);
}
@ -76,7 +81,13 @@ class Entry extends Model
if ($this->hasFlag($flag)) {
return;
}
$this->flags()->create(['flag_name' => $flag]);
$enum = match ($flag) {
'will_advance' => EntryFlags::WILL_ADVANCE,
'declined' => EntryFlags::DECLINED,
'no_show' => EntryFlags::NO_SHOW,
'failed_prelim' => EntryFlags::FAILED_PRELIM,
};
$this->flags()->create(['flag_name' => $enum]);
$this->load('flags');
}
@ -118,8 +129,8 @@ class Entry extends Model
{
$query->whereDoesntHave('flags', function (Builder $query) {
$query->where('flag_name', 'declined')
->orWhere('flag_name', 'no-show')
->orWhere('flag_name', 'failed-prelim');
->orWhere('flag_name', 'no_show')
->orWhere('flag_name', 'failed_prelim');
});
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Enums\EntryFlags;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -9,8 +10,10 @@ class EntryFlag extends Model
{
protected $guarded = [];
// Possible flags include
// - declined: used if a doubler declines a seat in this audition. Checked by DoublerService
protected $casts = [
'flag_name' => EntryFlags::class,
];
public function entry(): BelongsTo
{
return $this->belongsTo(Entry::class);

View File

@ -1,5 +1,6 @@
<?php
use App\Enums\EntryFlags;
use App\Models\Audition;
use App\Models\Ensemble;
use App\Models\Entry;
@ -77,11 +78,11 @@ test('has advancement votes', function () {
it('can have flags and can check for a specific flag', function () {
// Arrange
$entry = Entry::factory()->create();
EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => 'declined']);
EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => 'no-show']);
EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => EntryFlags::DECLINED]);
EntryFlag::create(['entry_id' => $entry->id, 'flag_name' => EntryFlags::NO_SHOW]);
// Act & Assert
expect($entry->flags->count())->toBe(2)
->and($entry->flags->first()->flag_name)->toBe('declined')
->and($entry->flags->first()->flag_name->value)->toBe('declined')
->and($entry->hasFlag('declined'))->toBeTrue()
->and($entry->hasFlag('random'))->toBeFalse();
});
@ -90,15 +91,15 @@ it('can set and remove a flag', function () {
// Arrange
$entry = Entry::factory()->create();
// Act
$entry->addFlag('Test Flag');
$entry->addFlag('Second Flag');
$entry->addFlag('declined');
$entry->addFlag('will_advance');
// Assert
expect($entry->hasFlag('Test Flag'))->toBeTrue()
->and($entry->hasFlag('Second Flag'))->toBeTrue()
expect($entry->hasFlag('declined'))->toBeTrue()
->and($entry->hasFlag('will_advance'))->toBeTrue()
->and($entry->hasFlag('random'))->toBeFalse();
$entry->removeFlag('Test Flag');
expect($entry->hasFlag('Test Flag'))->toBeFalse()
->and($entry->hasFlag('Second Flag'))->toBeTrue();
$entry->removeFlag('declined');
expect($entry->hasFlag('declined'))->toBeFalse()
->and($entry->hasFlag('will_advance'))->toBeTrue();
});
it('always has a score sheet count available', function () {
@ -176,8 +177,8 @@ it('has a forAdvancement scope that only returns those entries entered for advan
it('has an available scope that returns only auditions without a flag that would prevent seating', function () {
Entry::factory()->count(10)->create();
Entry::factory()->create()->addFlag('declined');
Entry::factory()->create()->addFlag('no-show');
Entry::factory()->create()->addFlag('failed-prelim');
Entry::factory()->create()->addFlag('no_show');
Entry::factory()->create()->addFlag('failed_prelim');
// Act & Assert
expect(Entry::all()->count())->toBe(13)
->and(Entry::available()->count())->toBe(10)

View File

@ -96,6 +96,7 @@ it('allows an assigned judge to enter scores', function () {
$score[$subscore->id] = mt_rand(0, 100);
}
// Act & Assert
/** @noinspection PhpUnhandledExceptionInspection */
$this->post(route('judging.saveScoreSheet', $this->entries->first()),
['advancement-vote' => 'yes', 'score' => $score])
->assertSessionHasNoErrors()