From b46d488cc579fc38a645d73b4d4e2321f8a9a3ba Mon Sep 17 00:00:00 2001 From: Matt Young Date: Mon, 1 Jul 2024 13:25:32 -0500 Subject: [PATCH] Model Student Test --- app/Models/Student.php | 17 +++++++-- tests/Feature/Models/StudentTest.php | 54 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Models/StudentTest.php diff --git a/app/Models/Student.php b/app/Models/Student.php index f93465b..f4c9667 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -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; } } diff --git a/tests/Feature/Models/StudentTest.php b/tests/Feature/Models/StudentTest.php new file mode 100644 index 0000000..8db512b --- /dev/null +++ b/tests/Feature/Models/StudentTest.php @@ -0,0 +1,54 @@ +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'); +});