From 1b48054316a2789a5776963f0437efa7ba3be1f1 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Mon, 5 Feb 2024 22:00:16 -0500 Subject: [PATCH] Add tests --- .../Models/Concerns/HasRelationships.php | 14 ++-------- .../Integration/Database/DatabaseTest.php | 28 +++++++++---------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/app/src/Database/Models/Concerns/HasRelationships.php b/app/src/Database/Models/Concerns/HasRelationships.php index 8ba04833..d98902e5 100644 --- a/app/src/Database/Models/Concerns/HasRelationships.php +++ b/app/src/Database/Models/Concerns/HasRelationships.php @@ -132,14 +132,8 @@ public function belongsToManyThrough( // If no table names were provided, we can guess it by concatenating the parent // and through table names. The two model names are transformed to snake case // from their default CamelCase also. - if (is_null($firstJoiningTable)) { - $firstJoiningTable = $this->joiningTable($through); - } - - if (is_null($secondJoiningTable)) { - $secondJoiningTable = $through->joiningTable($instance); - } - + $firstJoiningTable = $firstJoiningTable ?? $this->joiningTable($through); + $secondJoiningTable = $secondJoiningTable ?? $through->joiningTable($instance); $firstForeignPivotKey = $firstForeignPivotKey ?? $this->getForeignKey(); $firstRelatedKey = $firstRelatedKey ?? $through->getForeignKey(); $secondForeignPivotKey = $secondForeignPivotKey ?? $through->getForeignKey(); @@ -206,9 +200,7 @@ public function belongsToManyUnique( // If no table name was provided, we can guess it by concatenating the two // models using underscores in alphabetical order. The two model names // are transformed to snake case from their default CamelCase also. - if (is_null($table)) { - $table = $this->joiningTable($related, $instance); - } + $table = $table ?? $this->joiningTable($related, $instance); return new BelongsToManyUnique( $instance->newQuery(), diff --git a/app/tests/Integration/Database/DatabaseTest.php b/app/tests/Integration/Database/DatabaseTest.php index a881d1ea..2471a692 100644 --- a/app/tests/Integration/Database/DatabaseTest.php +++ b/app/tests/Integration/Database/DatabaseTest.php @@ -112,7 +112,7 @@ protected function createSchema(): void $table->string('name'); }); - $this->schema->create('assignments', function ($table) { + $this->schema->create('assignables', function ($table) { $table->integer('task_id')->unsigned(); $table->integer('location_id')->unsigned(); $table->morphs('assignable'); @@ -140,7 +140,7 @@ public function tearDown(): void $this->schema->drop('permissions'); $this->schema->drop('tasks'); $this->schema->drop('locations'); - $this->schema->drop('assignments'); + $this->schema->drop('assignables'); $this->schema->drop('jobs'); Relation::morphMap([], false); @@ -159,7 +159,7 @@ public function testTableCreation(): void $this->assertTrue($this->schema->hasTable('permissions')); $this->assertTrue($this->schema->hasTable('tasks')); $this->assertTrue($this->schema->hasTable('locations')); - $this->assertTrue($this->schema->hasTable('assignments')); + $this->assertTrue($this->schema->hasTable('assignables')); $this->assertTrue($this->schema->hasTable('jobs')); } @@ -1447,11 +1447,10 @@ public function permissions(): BelongsToManyThrough public function assignmentTasks(): MorphToManyUnique { $relation = $this->morphToManyUnique( - EloquentTestTask::class, - 'assignable', - 'assignments', - null, - 'task_id' // Need to explicitly set this, since it doesn't match our related model name + related: EloquentTestTask::class, + name: 'assignable', + table: 'assignables', + relatedPivotKey: 'task_id' // Need to explicitly set this, since it doesn't match our related model name ); return $relation; @@ -1463,11 +1462,10 @@ public function assignmentTasks(): MorphToManyUnique public function tasks(): MorphToManyUnique { $relation = $this->morphToManyUnique( - EloquentTestTask::class, - 'assignable', - 'assignments', - null, - 'task_id' // Need to explicitly set this, since it doesn't match our related model name + related: EloquentTestTask::class, + name: 'assignable', + // table: 'assignments', // This is the default table name, so we don't need to specify it + relatedPivotKey: 'task_id' // Need to explicitly set this, since it doesn't match our related model name )->withTertiary(EloquentTestLocation::class, null, 'location_id'); return $relation; @@ -1593,7 +1591,7 @@ public function locations(): BelongsToMany { return $this->belongsToMany( EloquentTestLocation::class, - 'assignments', + 'assignables', 'task_id', 'location_id' ); @@ -1618,7 +1616,7 @@ class EloquentTestLocation extends EloquentTestModel */ class EloquentTestAssignment extends EloquentTestModel { - protected $table = 'assignments'; + protected $table = 'assignables'; protected $guarded = []; protected $primaryKey = null; public $incrementing = false;