auditionadmin/tests/Feature/app/Models/StudentTest.php

103 lines
4.2 KiB
PHP

<?php
use App\Models\Audition;
use App\Models\Doubler;
use App\Models\DoublerRequest;
use App\Models\Entry;
use App\Models\Event;
use App\Models\HistoricalSeat;
use App\Models\NominationEnsembleEntry;
use App\Models\School;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->student = Student::factory()->create();
});
it('can return its nominations', function () {
NominationEnsembleEntry::factory()
->forStudent($this->student)->create();
expect($this->student->nominations()->count())->toEqual(1)
->and($this->student->nominations()->first())->toBeInstanceOf(NominationEnsembleEntry::class);
});
it('can return its school', function () {
expect($this->student->school->id)->toEqual(1)
->and($this->student->school)->toBeInstanceOf(School::class);
});
it('can return its historical seats', function () {
HistoricalSeat::factory()
->forStudent($this->student)->create();
expect($this->student->historicalSeats->count())->toEqual(1)
->and($this->student->historicalSeats()->first())->toBeInstanceOf(HistoricalSeat::class);
});
it('can return the students directors using either director or user', function () {
User::factory()->count(2)->create(['school_id' => $this->student->school_id]);
expect($this->student->directors()->count())->toEqual(2)
->and($this->student->directors()->first())->toBeInstanceOf(User::class);
});
it('can return its entries)', function () {
Entry::factory()->count(3)->forStudent($this->student)->create();
expect($this->student->entries()->count())->toEqual(3)
->and($this->student->entries()->first())->toBeInstanceOf(Entry::class);
});
it('can return its full name', function () {
expect($this->student->full_name())->toEqual($this->student->first_name.' '.$this->student->last_name)
->and($this->student->full_name(true))->toEqual($this->student->last_name.', '.$this->student->first_name);
});
it('can return its doubler requests', function () {
DoublerRequest::factory()->forStudent($this->student)->create();
DoublerRequest::factory()->forStudent($this->student)->create();
expect($this->student->doublerRequests()->count())->toEqual(2)
->and($this->student->doublerRequests()->first())->toBeInstanceOf(DoublerRequest::class);
});
it('can return its doublers', function () {
$event = Event::factory()->create();
DB::table('doublers')->insert([
'student_id' => $this->student->id,
'event_id' => $event->id,
'entries' => json_encode([1, 2, 3]),
'accepted_entry' => null,
]);
expect($this->student->doublers()->count())->toEqual(1)
->and($this->student->doublers()->first())->toBeInstanceOf(Doubler::class);
});
it('can check if it is a doubler in a given event', function () {
$event = Event::factory()->create();
expect($this->student->isDoublerInEvent($event))->toBeFalse();
DB::table('doublers')->insert([
'student_id' => $this->student->id,
'event_id' => $event->id,
'entries' => json_encode([1, 2, 3]),
'accepted_entry' => null,
]);
expect($this->student->isDoublerInEvent($event))->toBeTrue();
});
it('can return its entries for a given event', function () {
$event1 = Event::factory()->create();
$event2 = Event::factory()->create();
$audition1 = Audition::factory()->forEvent($event1)->create();
$audition2 = Audition::factory()->forEvent($event2)->create();
$audition3 = Audition::factory()->forEvent($event2)->create();
Entry::factory()->forStudent($this->student)->forAudition($audition1)->create();
Entry::factory()->forStudent($this->student)->forAudition($audition2)->create();
Entry::factory()->forStudent($this->student)->forAudition($audition3)->create();
expect($this->student->entries()->count())->toEqual(3)
->and($this->student->entriesForEvent($event1->id)->count())->toEqual(1)
->and($this->student->entriesForEvent($event1->id)->first())->toBeInstanceOf(Entry::class)
->and($this->student->entriesForEvent($event2)->count())->toEqual(2)
->and($this->student->entriesForEvent($event2)->first())->toBeInstanceOf(Entry::class);
});