Model User Test

This commit is contained in:
Matt Young 2024-07-01 14:07:22 -05:00
parent b46d488cc5
commit 3ba70bea89
2 changed files with 155 additions and 2 deletions

View File

@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
class User extends Authenticatable implements MustVerifyEmail class User extends Authenticatable implements MustVerifyEmail
{ {
@ -131,7 +132,7 @@ class User extends Authenticatable implements MustVerifyEmail
* *
* @return SchoolEmailDomain[] * @return SchoolEmailDomain[]
*/ */
public function possibleSchools() public function possibleSchools(): Collection
{ {
if ($this->school_id) { if ($this->school_id) {
$return[] = $this->school; $return[] = $this->school;
@ -148,7 +149,11 @@ class User extends Authenticatable implements MustVerifyEmail
return true; return true;
} }
return $this->is_tab; if ($this->is_tab) {
return true;
}
return false;
} }
public function scoreSheets(): HasMany public function scoreSheets(): HasMany

View File

@ -0,0 +1,148 @@
<?php
use App\Models\Entry;
use App\Models\JudgeAdvancementVote;
use App\Models\Room;
use App\Models\School;
use App\Models\SchoolEmailDomain;
use App\Models\Student;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->user = User::factory()->create([
'first_name' => 'Bandit',
'last_name' => 'Heeler',
'email' => 'bheeler@calypso.null',
]);
});
it('formats a full name and can put last first if needed', function () {
expect($this->user->full_name())->toBe('Bandit Heeler')
->and($this->user->full_name(true))->toBe('Heeler, Bandit');
});
it('formats a short name with the first initial of first name and full last name', function () {
expect($this->user->short_name())->toBe('B. Heeler');
});
it('checks if it has a school', function () {
expect($this->user->has_school())->toBeFalse();
$school = School::factory()->create(['name' => 'Calypso School']);
$this->user->school_id = $school->id;
$this->user->save();
expect($this->user->has_school())->toBeTrue();
});
it('has a school', function () {
// Arrange
$school = School::factory()->create(['name' => 'Calypso School']);
$this->user->school_id = $school->id;
$this->user->save();
// Act & Assert
expect($this->user->school->name)->toBe('Calypso School')
->and($this->user->school)->toBeInstanceOf(School::class);
});
it('has an email domain', function () {
expect($this->user->emailDomain())->toBe('calypso.null');
});
it('has students', function () {
// Arrange
$school = School::factory()->create(['name' => 'Calypso School']);
$this->user->school_id = $school->id;
$this->user->save();
Student::factory()->count(5)->create(['school_id' => $school->id]);
// Act & Assert
expect($this->user->students->count())->toBe(5)
->and($this->user->students->first())->toBeInstanceOf(Student::class);
});
it('has entries', function () {
// Arrange
$school = School::factory()->create(['name' => 'Calypso School']);
$this->user->school_id = $school->id;
$this->user->save();
$students = Student::factory()->count(5)->create(['school_id' => $school->id]);
foreach ($students as $student) {
Entry::factory()->count(2)->create(['student_id' => $student->id]);
}
// Act & Assert
expect($this->user->entries->count())->toBe(10)
->and($this->user->entries->first())->toBeInstanceOf(Entry::class);
});
it('has rooms also called judgingAssignments', function () {
// Arrange
$rooms = Room::factory()->count(3)->create();
foreach ($rooms as $room) {
$room->addJudge($this->user);
}
// Act & Assert
expect($this->user->rooms->count())->toBe(3)
->and($this->user->rooms->first())->toBeInstanceOf(Room::class)
->and($this->user->judgingAssignments->count())->toBe(3)
->and($this->user->judgingAssignments->first())->toBeInstanceOf(Room::class);
});
it('has advancement votes', function () {
// Arrange
$entries = Entry::factory()->count(5)->create();
foreach ($entries as $entry) {
JudgeAdvancementVote::create([
'user_id' => $this->user->id,
'entry_id' => $entry->id,
'vote' => fake()->randomElement(['yes', 'no', 'dq']),
]);
}
// Act & Assert
expect($this->user->advancementVotes->count())->toBe(5)
->and($this->user->advancementVotes->first())->toBeInstanceOf(JudgeAdvancementVote::class);
});
it('knows if it is a judge', function () {
expect($this->user->isJudge())->toBeFalse();
Room::factory()->create()->addJudge($this->user);
$this->user->load('judgingAssignments');
expect($this->user->isJudge())->toBeTrue();
});
it('knows what schools share its email domain', function () {
// Arrange
$school1 = School::factory()->create(['name' => 'Calypso Elementary School']);
$school2 = School::factory()->create(['name' => 'Calypso Middle School']);
$otherSchools = School::factory()->count(4)->create();
SchoolEmailDomain::create([
'school_id' => $school1->id,
'domain' => 'calypso.null',
]);
SchoolEmailDomain::create([
'school_id' => $school2->id,
'domain' => 'calypso.null',
]);
foreach ($otherSchools as $school) {
SchoolEmailDomain::create([
'school_id' => $school->id,
'domain' => fake()->domainName,
]);
}
// Act & Assert
expect($this->user->possibleSchools())->toBeInstanceOf(Collection::class)
->and($this->user->possibleSchools()->count())->toBe(2)
->and($this->user->possibleSchools()->first())->toBeInstanceOf(SchoolEmailDomain::class)
->and($this->user->possibleSchools()->first()->school)->toBeInstanceOf(School::class)
->and($this->user->possibleSchools()->first()->school->name)->toBe('Calypso Elementary School');
});
it('knows if it is a score tabulator', function () {
expect($this->user->canTab())->toBeFalse()
->and(User::factory()->tab()->create())->canTab()->toBe(true)
->and(User::factory()->admin()->create())->canTab()->toBe(true);
});