auditionadmin/tests/Feature/Actions/EnterScoreTest.php

245 lines
8.0 KiB
PHP

<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Tabulation\EnterScore;
use App\Exceptions\ScoreEntryException;
use App\Models\Entry;
use App\Models\Room;
use App\Models\ScoreSheet;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->scoreEntry = App::make(EnterScore::class);
});
test('throws an exception if the user does not exist', function () {
$user = User::factory()->make();
$entry = Entry::factory()->create();
enterScore($user, $entry, []);
})->throws(ScoreEntryException::class, 'User does not exist');
test('throws an exception if the entry does not exist', function () {
$user = User::factory()->create();
$entry = Entry::factory()->make();
enterScore($user, $entry, []);
})->throws(ScoreEntryException::class, 'Entry does not exist');
it('throws an exception if the seats for the entries audition are published', function () {
// Arrange
$user = User::factory()->create();
$entry = Entry::factory()->create();
$entry->audition->addFlag('seats_published');
// Act & Assert
enterScore($user, $entry, []);
})->throws(ScoreEntryException::class, 'Cannot score an entry in an audition with published seats');
it('throws an exception if the advancement for the entries audition is published', function () {
// Arrange
$user = User::factory()->create();
$entry = Entry::factory()->create();
$entry->audition->addFlag('advancement_published');
// Act & Assert
enterScore($user, $entry, []);
})->throws(ScoreEntryException::class, 'Cannot score an entry in an audition with published advancement');
it('throws an exception if too many scores are submitted', function () {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
Room::find(1000)->addJudge($judge);
// Act & Assert
enterScore($judge, $entry, [1, 2, 5, 3, 2, 1]);
})->throws(ScoreEntryException::class, 'Invalid number of scores');
it('throws an exception if too few scores are submitted', function () {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
Room::find(1000)->addJudge($judge);
// Act & Assert
enterScore($judge, $entry, [1, 2, 5, 3]);
})->throws(ScoreEntryException::class, 'Invalid number of scores');
it('throws an exception if the user is not assigned to judge the entry', function () {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
// Act & Assert
enterScore($judge, $entry, [1, 2, 5, 3, 7]);
})->throws(ScoreEntryException::class, 'This judge is not assigned to judge this entry');
it('throws an exception if an invalid subscore is provided', function () {
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1006 => 88,
];
enterScore($judge, $entry, $scores);
})->throws(ScoreEntryException::class, 'Invalid Score Submission');
it('throws an exception if a submitted subscore exceeds the maximum allowed', function () {
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1005 => 101,
];
enterScore($judge, $entry, $scores);
})->throws(ScoreEntryException::class, 'Supplied subscore exceeds maximum allowed');
it('removes an existing no_show flag from the entry if one exists', function () {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
$entry->addFlag('no_show');
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1005 => 98,
];
// Act
enterScore($judge, $entry, $scores);
// Assert
expect($entry->hasFlag('no_show'))->toBeFalse();
});
it('saves the score with a properly formatted subscore object', function () {
// Arrange
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
$entry->addFlag('no_show');
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1005 => 98,
];
$desiredReturn = [
1001 => [
'score' => 98,
'subscore_id' => 1001,
'subscore_name' => 'Scale',
],
1002 => [
'score' => 90,
'subscore_id' => 1002,
'subscore_name' => 'Etude 1',
],
1003 => [
'score' => 87,
'subscore_id' => 1003,
'subscore_name' => 'Etude 2',
],
1004 => [
'score' => 78,
'subscore_id' => 1004,
'subscore_name' => 'Sight Reading',
],
1005 => [
'score' => 98,
'subscore_id' => 1005,
'subscore_name' => 'Tone',
],
];
// Act
$newScore = enterScore($judge, $entry, $scores);
// Assert
$checkScoreSheet = ScoreSheet::find($newScore->id);
expect($checkScoreSheet->exists())->toBeTrue()
->and($checkScoreSheet->subscores)->toBe($desiredReturn);
});
it('throws an exception of the entry already has a score by the judge', function() {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
$entry->addFlag('no_show');
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1005 => 98,
];
// Act
enterScore($judge, $entry, $scores);
// Assert
enterScore($judge, $entry, $scores);
})->throws(ScoreEntryException::class, 'That judge has already entered scores for that entry');
it('allows an existing sore sheet to be updated', function() {
// Arrange
loadSampleAudition();
$judge = User::factory()->create();
$entry = Entry::factory()->create(['audition_id' => 1000]);
$entry->addFlag('no_show');
Room::find(1000)->addJudge($judge);
$scores = [
1001 => 98,
1002 => 90,
1003 => 87,
1004 => 78,
1005 => 98,
];
enterScore($judge, $entry, $scores);
// Act
$scoreToEdit = ScoreSheet::where('user_id', $judge->id)->where('entry_id', $entry->id)->first();
expect($scoreToEdit->exists())->toBeTrue();
$newScores = [
1001 => 40,
1002 => 41,
1003 => 42,
1004 => 43,
1005 => 44,
];
$pencil = App::make(EnterScore::class);
$pencil($judge, $entry, $newScores, $scoreToEdit);
$desiredReturn = [
1001 => [
'score' => 40,
'subscore_id' => 1001,
'subscore_name' => 'Scale',
],
1002 => [
'score' => 41,
'subscore_id' => 1002,
'subscore_name' => 'Etude 1',
],
1003 => [
'score' => 42,
'subscore_id' => 1003,
'subscore_name' => 'Etude 2',
],
1004 => [
'score' => 43,
'subscore_id' => 1004,
'subscore_name' => 'Sight Reading',
],
1005 => [
'score' => 44,
'subscore_id' => 1005,
'subscore_name' => 'Tone',
],
];
$checkScoreSheet = ScoreSheet::where('user_id', $judge->id)->where('entry_id', $entry->id)->first();
expect($checkScoreSheet->exists())->toBeTrue()
->and($checkScoreSheet->subscores)->toBe($desiredReturn);
});