Write tests - Write tests for what was done to this point that will be kept #11
|
|
@ -21,7 +21,19 @@ class Student extends Model
|
|||
|
||||
public function users(): HasManyThrough
|
||||
{
|
||||
return $this->hasManyThrough(User::class, School::class);
|
||||
return $this->hasManyThrough(
|
||||
User::class, // The target model we want to access
|
||||
School::class, // The intermediate model through which we access the target model
|
||||
'id', // The foreign key on the intermediate model
|
||||
'school_id', // The foreign key on the target model
|
||||
'school_id', // The local key
|
||||
'id' // The local key on the intermediate model
|
||||
);
|
||||
}
|
||||
|
||||
public function directors(): HasManyThrough
|
||||
{
|
||||
return $this->users();
|
||||
}
|
||||
|
||||
public function entries(): HasMany
|
||||
|
|
@ -32,8 +44,9 @@ class Student extends Model
|
|||
public function full_name(bool $last_name_first = false): string
|
||||
{
|
||||
if ($last_name_first) {
|
||||
return ($this->last_name.', '.$this->first_name);
|
||||
return $this->last_name.', '.$this->first_name;
|
||||
}
|
||||
|
||||
return $this->first_name.' '.$this->last_name;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Entry;
|
||||
use App\Models\School;
|
||||
use App\Models\Student;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->school = School::factory()->create();
|
||||
$this->student = Student::factory()->create([
|
||||
'school_id' => $this->school->id,
|
||||
'first_name' => 'Chris',
|
||||
'last_name' => 'Tomlin',
|
||||
]);
|
||||
});
|
||||
|
||||
it('has a school', function () {
|
||||
expect($this->student->school->is($this->school))->toBeTrue()
|
||||
->and($this->student->school)->toBeInstanceOf(School::class);
|
||||
});
|
||||
|
||||
it('has users also called directors', function () {
|
||||
// Arrange
|
||||
$users = User::factory()->count(2)->create([
|
||||
'school_id' => $this->school->id,
|
||||
]);
|
||||
// Act & Assert
|
||||
expect($this->student->users->count())->toBe(2)
|
||||
->and($this->student->users->first()->is($users->first()))->toBeTrue()
|
||||
->and($this->student->directors->count())->toBe(2)
|
||||
->and($this->student->directors->first()->is($users->first()))->toBeTrue();
|
||||
});
|
||||
|
||||
it('has entries', function () {
|
||||
// Arrange
|
||||
$entry = Entry::factory()->create([
|
||||
'student_id' => $this->student->id,
|
||||
]);
|
||||
Entry::factory()->count(4)->create([
|
||||
'student_id' => $this->student->id,
|
||||
]);
|
||||
// Act & Assert
|
||||
expect($this->student->entries->count())->toBe(5)
|
||||
->and($this->student->entries->first()->is($entry))->toBeTrue()
|
||||
->and($this->student->entries->first())->toBeInstanceOf(Entry::class);
|
||||
});
|
||||
|
||||
it('formats a full name and can do it last name first if needed', function () {
|
||||
expect($this->student->full_name())->toBe('Chris Tomlin')
|
||||
->and($this->student->full_name(true))->toBe('Tomlin, Chris');
|
||||
});
|
||||
Loading…
Reference in New Issue