auditionadmin/tests-old/Feature/Models/UserTest.php

133 lines
4.6 KiB
PHP

<?php
use App\Models\Entry;
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('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);
});