Skip to content

Commit

Permalink
Fix tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Dec 11, 2023
1 parent 48fdeaa commit 8440c57
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
32 changes: 17 additions & 15 deletions tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\Models\Book;
use MongoDB\Laravel\Tests\Models\Experience;
use MongoDB\Laravel\Tests\Models\Label;
use MongoDB\Laravel\Tests\Models\Role;
use MongoDB\Laravel\Tests\Models\Skill;
use MongoDB\Laravel\Tests\Models\SqlBook;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function tearDown(): void
SqlRole::truncate();
Skill::truncate();
Experience::truncate();
Label::truncate();
}

public function testSqlRelations()
Expand Down Expand Up @@ -282,36 +284,36 @@ public function testHybridMorphToManySqlModelToMongoModel()
$user2 = SqlUser::query()->find($user2->id);

// Create Mongodb skills
$skill = Skill::query()->create(['name' => 'Laravel']);
$skill2 = Skill::query()->create(['name' => 'MongoDB']);
$label = Label::query()->create(['name' => 'Laravel']);
$label2 = Label::query()->create(['name' => 'MongoDB']);

// MorphToMany (pivot is empty)
$user->skills()->sync([$skill->_id, $skill2->_id]);
$user->labels()->sync([$label->_id, $label2->_id]);
$check = SqlUser::query()->find($user->id);
$this->assertEquals(2, $check->skills->count());
$this->assertEquals(2, $check->labels->count());

// MorphToMany (pivot is not empty)
$user->skills()->sync($skill);
$user->labels()->sync($label);
$check = SqlUser::query()->find($user->id);
$this->assertEquals(1, $check->skills->count());
$this->assertEquals(1, $check->labels->count());

// Attach MorphToMany
$user->skills()->sync([]);
$user->labels()->sync([]);
$check = SqlUser::query()->find($user->id);
$this->assertEquals(0, $check->skills->count());
$user->skills()->attach($skill);
$user->skills()->attach($skill); // ignore duplicates
$this->assertEquals(0, $check->labels->count());
$user->labels()->attach($label);
$user->labels()->attach($label); // ignore duplicates
$check = SqlUser::query()->find($user->id);
$this->assertEquals(1, $check->skills->count());
$this->assertEquals(1, $check->labels->count());

// Inverse MorphToMany (pivot is empty)
$skill->sqlUsers()->sync([$user->id, $user2->id]);
$check = Skill::query()->find($skill->_id);
$label->sqlUsers()->sync([$user->id, $user2->id]);
$check = Label::query()->find($label->_id);
$this->assertEquals(2, $check->sqlUsers->count());

// Inverse MorphToMany (pivot is empty)
$skill->sqlUsers()->sync([$user->id, $user2->id]);
$check = Skill::query()->find($skill->_id);
$label->sqlUsers()->sync([$user->id, $user2->id]);
$check = Label::query()->find($label->_id);
$this->assertEquals(2, $check->sqlUsers->count());
}

Expand Down
9 changes: 6 additions & 3 deletions tests/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Relations\MorphToMany;
use MongoDB\Laravel\Eloquent\Model as Eloquent;

/**
Expand All @@ -23,14 +24,16 @@ class Label extends Eloquent
'chapters',
];

/**
* Get all the posts that are assigned this tag.
*/
public function users()
{
return $this->morphedByMany(User::class, 'labelled');
}

public function sqlUsers(): MorphToMany
{
return $this->morphedByMany(SqlUser::class, 'labeled');
}

public function clients()
{
return $this->morphedByMany(Client::class, 'labelled');
Expand Down
6 changes: 0 additions & 6 deletions tests/Models/Skill.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use MongoDB\Laravel\Eloquent\Model as Eloquent;

class Skill extends Eloquent
Expand All @@ -18,9 +17,4 @@ public function sqlUsers(): BelongsToMany
{
return $this->belongsToMany(SqlUser::class);
}

public function sqlUsers(): MorphToMany
{
return $this->morphedByMany(SqlUser::class, 'skilled');
}
}
15 changes: 10 additions & 5 deletions tests/Models/SqlUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function sqlBooks(): HasMany
return $this->hasMany(SqlBook::class);
}

public function skills(): MorphToMany
public function labels(): MorphToMany
{
return $this->morphToMany(Skill::class, 'skilled');
return $this->morphToMany(Label::class, 'labeled');
}

public function experiences(): MorphToMany
Expand All @@ -68,20 +68,25 @@ public static function executeSchema(): void
$table->string('name');
$table->timestamps();
});

// Pivot table for BelongsToMany relationship with Skill
if (! $schema->hasTable('skill_sql_user')) {
$schema->create('skill_sql_user', function (Blueprint $table) {
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
$table->string((new Skill())->getForeignKey());
$table->primary([(new self())->getForeignKey(), (new Skill())->getForeignKey()]);
});
}
if (! $schema->hasTable('skilleds')) {
$schema->create('skilleds', function (Blueprint $table) {

// Pivot table for MorphToMany relationship with Label
if (! $schema->hasTable('labeleds')) {
$schema->create('labeleds', function (Blueprint $table) {
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
$table->morphs('skilled');
$table->morphs('labeled');
});
}

// Pivot table for MorphedByMany relationship with Experience
if (! $schema->hasTable('experienceds')) {
$schema->create('experienceds', function (Blueprint $table) {
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
Expand Down

0 comments on commit 8440c57

Please sign in to comment.