Rewrite tabulation #14
|
|
@ -2,11 +2,16 @@
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Exceptions\TabulationException;
|
||||||
use App\Models\Entry;
|
use App\Models\Entry;
|
||||||
use App\Models\Event;
|
use App\Models\Event;
|
||||||
use App\Models\Student;
|
use App\Models\Student;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a collection of doublers for the event in the form of
|
||||||
|
* [studentId => [student=>student, entries=>[entries]]
|
||||||
|
*/
|
||||||
class DoublerService
|
class DoublerService
|
||||||
{
|
{
|
||||||
public function doublersForEvent(Event $event)
|
public function doublersForEvent(Event $event)
|
||||||
|
|
@ -17,8 +22,12 @@ class DoublerService
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws TabulationException
|
||||||
|
*/
|
||||||
protected function findDoublersForEvent(Event $event): array
|
protected function findDoublersForEvent(Event $event): array
|
||||||
{
|
{
|
||||||
|
$this->validateEvent($event);
|
||||||
$entries = $event->entries;
|
$entries = $event->entries;
|
||||||
$entries->load('student.school');
|
$entries->load('student.school');
|
||||||
$entries->load('audition');
|
$entries->load('audition');
|
||||||
|
|
@ -35,4 +44,14 @@ class DoublerService
|
||||||
|
|
||||||
return $doubler_array;
|
return $doubler_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws TabulationException
|
||||||
|
*/
|
||||||
|
protected function validateEvent(Event $event)
|
||||||
|
{
|
||||||
|
if (! $event->exists) {
|
||||||
|
throw new TabulationException('Invalid event provided');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Exceptions\TabulationException;
|
||||||
|
use App\Models\Audition;
|
||||||
|
use App\Models\Entry;
|
||||||
|
use App\Models\Event;
|
||||||
|
use App\Models\Student;
|
||||||
|
use App\Services\DoublerService;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
|
|
||||||
|
use function PHPUnit\Framework\assertArrayNotHasKey;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->doublerService = App::make(DoublerService::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws an error if an invalid event is provided', function () {
|
||||||
|
$event = Event::factory()->make();
|
||||||
|
$this->doublerService->doublersForEvent($event);
|
||||||
|
})->throws(TabulationException::class, 'Invalid event provided');
|
||||||
|
it('returns doublers for an event', function () {
|
||||||
|
$concertEvent = Event::factory()->create(['name' => 'Concert Band', 'id' => 1000]);
|
||||||
|
$jazzEvent = Event::factory()->create(['name' => 'Jazz Band', 'id' => 1001]);
|
||||||
|
|
||||||
|
$auditionAS = Audition::factory()->create([
|
||||||
|
'event_id' => 1000, 'name' => 'Alto Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1000,
|
||||||
|
]);
|
||||||
|
$auditionTS = Audition::factory()->create([
|
||||||
|
'event_id' => 1000, 'name' => 'Tenor Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1001,
|
||||||
|
]);
|
||||||
|
$auditionBS = Audition::factory()->create([
|
||||||
|
'event_id' => 1000, 'name' => 'Baritone Sax', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1002,
|
||||||
|
]);
|
||||||
|
$auditionCL = Audition::factory()->create([
|
||||||
|
'event_id' => 1000, 'name' => 'Clarinet', 'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1003,
|
||||||
|
]);
|
||||||
|
$auditionBCL = Audition::factory()->create([
|
||||||
|
'event_id' => 1000, 'name' => 'Bass Clarinet',
|
||||||
|
'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1004,
|
||||||
|
]);
|
||||||
|
$auditionJAS = Audition::factory()->create([
|
||||||
|
'event_id' => 1001, 'name' => 'Jazz Alto', 'minimum_grade' => 7,
|
||||||
|
'maximum_grade' => 12, 'id' => 1005,
|
||||||
|
]);
|
||||||
|
$auditionJTS = Audition::factory()->create([
|
||||||
|
'event_id' => 1001, 'name' => 'Jazz Tenor', 'minimum_grade' => 7,
|
||||||
|
'maximum_grade' => 12, 'id' => 1006,
|
||||||
|
]);
|
||||||
|
$auditionJBS = Audition::factory()->create([
|
||||||
|
'event_id' => 1001, 'name' => 'Jazz Baritone',
|
||||||
|
'minimum_grade' => 7, 'maximum_grade' => 12, 'id' => 1007,
|
||||||
|
]);
|
||||||
|
$allSaxDude = Student::factory()->create(['grade' => 11, 'id' => 1000]);
|
||||||
|
$clarinetGal = Student::factory()->create(['grade' => 9, 'id' => 1001]);
|
||||||
|
$justAlto = Student::factory()->create(['grade' => 9, 'id' => 1002]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1000]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1001]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1002]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1005]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1006]);
|
||||||
|
Entry::create(['student_id' => 1000, 'audition_id' => 1007]);
|
||||||
|
Entry::create(['student_id' => 1001, 'audition_id' => 1003]);
|
||||||
|
Entry::create(['student_id' => 1001, 'audition_id' => 1004]);
|
||||||
|
Entry::create(['student_id' => 1002, 'audition_id' => 1000]);
|
||||||
|
Entry::create(['student_id' => 1002, 'audition_id' => 1005]);
|
||||||
|
|
||||||
|
$return = $this->doublerService->doublersForEvent($concertEvent);
|
||||||
|
expect(count($return))->toBe(2);
|
||||||
|
expect($return[1000]['student']->id)->toBe($allSaxDude->id);
|
||||||
|
expect($return[1000]['entries']->count())->toBe(3);
|
||||||
|
expect($return[1001]['entries']->count())->toBe(2);
|
||||||
|
assertArrayNotHasKey(1002, $return);
|
||||||
|
$return = $this->doublerService->doublersForEvent($jazzEvent);
|
||||||
|
expect(count($return))->toBe(1);
|
||||||
|
expect($return[1000]['student']->id)->toBe($allSaxDude->id);
|
||||||
|
expect($return[1000]['entries']->count())->toBe(3);
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue