Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Feb 6, 2024
1 parent abc0be8 commit 1b48054
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
14 changes: 3 additions & 11 deletions app/src/Database/Models/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
28 changes: 13 additions & 15 deletions app/tests/Integration/Database/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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'));
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1593,7 +1591,7 @@ public function locations(): BelongsToMany
{
return $this->belongsToMany(
EloquentTestLocation::class,
'assignments',
'assignables',
'task_id',
'location_id'
);
Expand All @@ -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;
Expand Down

0 comments on commit 1b48054

Please sign in to comment.