auditionadmin/tests-old/Feature/Actions/UpdateEntryTest.php

220 lines
9.6 KiB
PHP

<?php
/** @noinspection PhpUnhandledExceptionInspection */
use App\Actions\Entries\UpdateEntry;
use App\Exceptions\ManageEntryException;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\ScoreSheet;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->updater = App::make(UpdateEntry::class);
});
it('throws an error if an invalid entry is provided', function () {
$this->updater->updateEntry(2, []);
})->throws(ManageEntryException::class, 'Invalid entry provided');
it('throws an error if we try to remove for_seating while seating is published', function () {
// Arrange
$entry = Entry::factory()->create();
$entry->audition->addFlag('seats_published');
$data = ['for_seating' => 0];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot remove seating from an entry in an audition where seats are published');
it('throws an error if we try to add for_seating while seating is published', function () {
// Arrange
$entry = Entry::factory()->advanceOnly()->create();
$entry->audition->addFlag('seats_published');
$data = ['for_seating' => 1];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot add seating to an entry in an audition where seats are published');
it('allows us to remove for_seating if seating is not published', function () {
// Arrange
$entry = Entry::factory()->create();
$data = ['for_seating' => 0];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_seating' => 0]);
});
it('allows us to add for_seating if seating is not published', function () {
// Arrange
$entry = Entry::factory()->advanceOnly()->create();
$data = ['for_seating' => 1];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_seating' => 1]);
});
it('throws an error if we try to remove for_advancement while seating is published', function () {
// Arrange
$entry = Entry::factory()->create();
$entry->audition->addFlag('advancement_published');
$data = ['for_advancement' => 0];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot remove advancement from an entry in an audition where advancement is published');
it('throws an error if we try to add for_advancement while advancement is published', function () {
// Arrange
$entry = Entry::factory()->seatingOnly()->create();
$entry->audition->addFlag('advancement_published');
$data = ['for_advancement' => 1];
// Act & Assert
$this->updater->updateEntry($entry, $data);
})->throws('Cannot add advancement to an entry in an audition where advancement is published');
it('allows us to remove for_advancement if advancement is not published', function () {
// Arrange
$entry = Entry::factory()->create();
$data = ['for_advancement' => 0];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_advancement' => 0]);
});
it('allows us to add for_advancement if advancement is not published', function () {
// Arrange
$entry = Entry::factory()->seatingOnly()->create();
$data = ['for_advancement' => 1];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'for_advancement' => 1]);
});
it('throws an exception if an attempt to change to an invalid audition is made', function () {
$entry = Entry::factory()->create();
$data = ['audition' => 2];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Invalid audition provided');
it('cannot change auditions if our current audition advancement is published', function () {
$entry = Entry::factory()->create();
$entry->audition->addFlag('advancement_published');
$otherAudition = Audition::factory()->create();
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class,
'Cannot change the audition for an entry where advancement for that entry\'s current audition is published');
it('cannot change auditions if our current audition seating is published', function () {
$entry = Entry::factory()->create();
$entry->audition->addFlag('seats_published');
$otherAudition = Audition::factory()->create();
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class,
'Cannot change the audition for an entry where seating for that entry\'s current audition is published');
it('cannot change auditions if our proposed audition advancement is published', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create();
$otherAudition->addFlag('advancement_published');
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the entry to an audition with published advancement');
it('cannot change auditions if our proposed audition seating is published', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create();
$otherAudition->addFlag('seats_published');
$data = ['audition' => $otherAudition];
// Act
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the entry to an audition with published seating');
it('will not let us switch to an audition that is too old for the student', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$entry->student->update(['grade' => 7]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'The grade of the student does not meet the minimum for that audition');
it('will not let us switch to an audition that is too young for the student', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create(['minimum_grade' => 8, 'maximum_grade' => 9]);
$entry->student->update(['grade' => 11]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'The grade of the student exceeds the maximum for that audition');
it('will not let us change auditions for an entry with scores', function () {
$entry = Entry::factory()->create();
$judge = User::factory()->create();
ScoreSheet::create([
'entry_id' => $entry->id,
'user_id' => $judge->id,
'subscores' => 100,
]);
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'Cannot change the audition for an entry with scores');
it('will not let us change to an audition that the student is already entered in', function () {
$entry = Entry::factory()->create();
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
Entry::create([
'student_id' => $entry->student->id,
'audition_id' => $otherAudition->id,
]);
$data = ['audition' => $otherAudition];
$this->updater->updateEntry($entry, $data);
})->throws(ManageEntryException::class, 'That student is already entered in that audition');
it('allows us to switch auditions', function () {
// Arrange
$entry = Entry::factory()->create();
$originalAudition = $entry->audition;
$otherAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $otherAudition];
// Act & Assert
$this->updater->updateEntry($entry, $data);
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'audition_id' => $otherAudition->id]);
$this->assertDatabaseMissing('entries',
['student_id' => $entry->student->id, 'audition_id' => $originalAudition->id]);
});
it('sets the draw number null if the new audition is not drawn', function () {
// Arrange
$entry = Entry::factory()->create(['draw_number' => 3]);
$newAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$data = ['audition' => $newAudition];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'draw_number' => null]);
});
it('sets the draw number last if the new audition is not drawn', function () {
// Arrange
$entry = Entry::factory()->create(['draw_number' => 3]);
$newAudition = Audition::factory()->create([
'minimum_grade' => $entry->student->grade, 'maximum_grade' => $entry->student->grade,
]);
$newAudition->addFlag('drawn');
foreach (range(1, 6) as $dn) {
Entry::factory()->create(['audition_id' => $newAudition->id, 'draw_number' => $dn]);
}
$data = ['audition' => $newAudition];
// Act
$this->updater->updateEntry($entry, $data);
// Assert
$this->assertDatabaseHas('entries', ['id' => $entry->id, 'draw_number' => 7]);
});