auditionadmin/tests/Feature/Pages/Setup/DrawStoreTest.php

97 lines
3.9 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\Entry;
use App\Services\DrawService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
uses(RefreshDatabase::class);
it('allows admin to seat entries and returns to the index with a status message', function () {
$audition = Audition::factory()->create();
Entry::factory()->count(10)->create(['audition_id' => $audition->id]);
actAsAdmin();
/** @noinspection PhpUnhandledExceptionInspection */
$this->post(route('admin.draw.store'), ['audition' => [$audition->id => 'on']])
->assertSessionHasNoErrors()
->assertSessionHas('status', 'Draw completed successfully')
->assertRedirect(route('admin.draw.index'));
});
it('returns an error message if no auditions were selected', function () {
// Arrange
actAsAdmin();
// Act & Assert
$response = $this->post(route('admin.draw.store'));
$response
->assertSessionHas('error', 'No auditions were selected')
->assertRedirect(route('admin.draw.index'));
});
it('only allows admin to run a draw', function () {
// Act & Assert
$this->post(route('admin.draw.store'))->assertRedirect(route('home'));
actAsNormal();
$this->post(route('admin.draw.store'))
->assertSessionHas('error', 'You are not authorized to perform this action')
->assertRedirect(route('dashboard'));
});
it('returns with error if a draw is requested for a non-extant audition', function () {
// Arrange
actAsAdmin();
// Act & Assert
$response = $this->post(route('admin.draw.store', ['audition[999]' => 'on']));
$response
->assertSessionHas('error', 'One or more invalid auditions were selected')
->assertRedirect(route('admin.draw.index'));
});
it('sets a drawn flag on the audition once a draw is run', function () {
// Arrange
$audition = Audition::factory()->create();
actAsAdmin();
// Act & Assert
$this->post(route('admin.draw.store', ['audition['.$audition->id.']' => 'on']));
expect($audition->hasFlag('drawn'))->toBeTrue();
});
it('refuses to draw an audition that has an existing draw', function () {
// Arrange
$audition = Audition::factory()->create();
$audition->addFlag('drawn');
actAsAdmin();
// Act & Assert
$this->post(route('admin.draw.store', ['audition['.$audition->id.']' => 'on']))
->assertSessionHas('error', 'Invalid attempt to draw an audition that has already been drawn')
->assertRedirect(route('admin.draw.index'));
});
it('randomizes the order of the entries in the audition', function () {
// Arrange
$audition = Audition::factory()->hasEntries(10)->create();
$entries = Entry::factory()->count(30)->create(['audition_id' => $audition->id]);
actAsAdmin();
// Act & Assert
$this->post(route('admin.draw.store'), ['audition['.$audition->id.']' => 'on']);
// assert that entries sorted by draw_number are in a random order
expect($audition->entries->sortBy('draw_number')->pluck('id'))
->not()->toBe($entries->pluck('id'));
});
it('sets the draw_number column on each entry in the audition based on the randomized entry order', function () {
// Arrange
$audition = Audition::factory()->hasEntries(10)->create();
Entry::all()->each(fn ($entry) => expect($entry->draw_number)->toBeNull());
#$drawService = new DrawService();
$drawService = App::make(DrawService::class);
$drawService->runOneDraw($audition);
// Act & Assert
Entry::all()->each(fn ($entry) => expect($entry->draw_number)->not()->toBeNull());
});
it('only sets draw numbers in the specified audition', function () {
// Arrange
$audition = Audition::factory()->hasEntries(10)->create();
$bonusEntry = Entry::factory()->create();
#$drawService = new DrawService();
$drawService = App::make(DrawService::class);
$drawService->runOneDraw($audition);
// Act & Assert
expect($bonusEntry->draw_number)->toBeNull();
});