auditionadmin/tests/Feature/app/Actions/Draw/RunDrawTest.php

100 lines
3.4 KiB
PHP

<?php
use App\Actions\Draw\RunDraw;
use App\Models\Audition;
use App\Models\Entry;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('assigns sequential draw numbers with no_show entries at the end', function () {
// Create an audition with entries
$audition = Audition::factory()->create();
// Create entries: some flagged as no_show, some not
$entries = Entry::factory()
->count(5)
->for($audition)
->create();
// Flag two entries as no_show
$noShowEntries = $entries->take(2);
foreach ($noShowEntries as $entry) {
$entry->addFlag('no_show');
$entry->save();
}
// Optionally, assign some existing draw numbers to test clearing
// foreach ($entries as $entry) {
// $entry->draw_number = 99;
// $entry->save();
// }
// Run the draw
app(RunDraw::class)($audition);
// Reload entries from DB to get fresh data
$entries = $audition->entries()->orderBy('draw_number')->get();
// Assert all draw_numbers are sequential starting at 1
$drawNumbers = $entries->pluck('draw_number')->all();
expect($drawNumbers)->toEqual(range(1, $entries->count()));
// Assert entries without no_show flag come first
$entriesWithoutNoShow = $entries->filter(fn ($e) => ! $e->hasFlag('no_show'));
$entriesWithNoShow = $entries->filter(fn ($e) => $e->hasFlag('no_show'));
// The max draw_number of entries without no_show should be less than min draw_number of no_show entries
if ($entriesWithNoShow->isNotEmpty()) {
expect($entriesWithoutNoShow->max('draw_number'))->toBeLessThan($entriesWithNoShow->min('draw_number'));
}
// Assert the audition has the 'drawn' flag
expect($audition->hasFlag('drawn'))->toBeTrue();
});
it('runs draw on multiple auditions correctly', function () {
// Create multiple auditions
$auditions = Audition::factory()->count(3)->create();
// For each audition, create entries with some flagged as no_show
foreach ($auditions as $audition) {
$entries = Entry::factory()->count(4)->for($audition)->create();
// Flag one entry as no_show per audition
$entries->first()->addFlag('no_show');
$entries->first()->save();
// // Assign dummy draw numbers to test clearing
// foreach ($entries as $entry) {
// $entry->draw_number = 99;
// $entry->save();
// }
}
// Run the draw on all auditions
app(RunDraw::class)($auditions);
// Reload auditions with entries
$auditions = $auditions->load('entries');
foreach ($auditions as $audition) {
$entries = $audition->entries->sortBy('draw_number')->values();
// Assert draw numbers are sequential starting at 1
$drawNumbers = $entries->pluck('draw_number')->all();
expect($drawNumbers)->toEqual(range(1, $entries->count()));
// Separate no_show and other entries
$entriesWithoutNoShow = $entries->filter(fn ($e) => ! $e->hasFlag('no_show'));
$entriesWithNoShow = $entries->filter(fn ($e) => $e->hasFlag('no_show'));
if ($entriesWithNoShow->isNotEmpty()) {
expect($entriesWithoutNoShow->max('draw_number'))->toBeLessThan($entriesWithNoShow->min('draw_number'));
}
// Assert the audition has the 'drawn' flag
expect($audition->hasFlag('drawn'))->toBeTrue();
}
});