Skip to content

Commit e03f57d

Browse files
committed
Fix tests;
1 parent 48fdeaa commit e03f57d

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

tests/HybridRelationsTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\DB;
99
use MongoDB\Laravel\Tests\Models\Book;
1010
use MongoDB\Laravel\Tests\Models\Experience;
11+
use MongoDB\Laravel\Tests\Models\Label;
1112
use MongoDB\Laravel\Tests\Models\Role;
1213
use MongoDB\Laravel\Tests\Models\Skill;
1314
use MongoDB\Laravel\Tests\Models\SqlBook;
@@ -40,6 +41,7 @@ public function tearDown(): void
4041
SqlRole::truncate();
4142
Skill::truncate();
4243
Experience::truncate();
44+
Label::truncate();
4345
}
4446

4547
public function testSqlRelations()
@@ -282,36 +284,36 @@ public function testHybridMorphToManySqlModelToMongoModel()
282284
$user2 = SqlUser::query()->find($user2->id);
283285

284286
// Create Mongodb skills
285-
$skill = Skill::query()->create(['name' => 'Laravel']);
286-
$skill2 = Skill::query()->create(['name' => 'MongoDB']);
287+
$label = Label::query()->create(['name' => 'Laravel']);
288+
$label2 = Label::query()->create(['name' => 'MongoDB']);
287289

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

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

298300
// Attach MorphToMany
299-
$user->skills()->sync([]);
301+
$user->labels()->sync([]);
300302
$check = SqlUser::query()->find($user->id);
301-
$this->assertEquals(0, $check->skills->count());
302-
$user->skills()->attach($skill);
303-
$user->skills()->attach($skill); // ignore duplicates
303+
$this->assertEquals(0, $check->labels->count());
304+
$user->labels()->attach($label);
305+
$user->labels()->attach($label); // ignore duplicates
304306
$check = SqlUser::query()->find($user->id);
305-
$this->assertEquals(1, $check->skills->count());
307+
$this->assertEquals(1, $check->labels->count());
306308

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

312314
// Inverse MorphToMany (pivot is empty)
313-
$skill->sqlUsers()->sync([$user->id, $user2->id]);
314-
$check = Skill::query()->find($skill->_id);
315+
$label->sqlUsers()->sync([$user->id, $user2->id]);
316+
$check = Label::query()->find($label->_id);
315317
$this->assertEquals(2, $check->sqlUsers->count());
316318
}
317319

tests/Models/Label.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MongoDB\Laravel\Tests\Models;
66

7+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
78
use MongoDB\Laravel\Eloquent\Model as Eloquent;
89

910
/**
@@ -23,14 +24,16 @@ class Label extends Eloquent
2324
'chapters',
2425
];
2526

26-
/**
27-
* Get all the posts that are assigned this tag.
28-
*/
2927
public function users()
3028
{
3129
return $this->morphedByMany(User::class, 'labelled');
3230
}
3331

32+
public function sqlUsers(): MorphToMany
33+
{
34+
return $this->morphedByMany(SqlUser::class, 'labeled');
35+
}
36+
3437
public function clients()
3538
{
3639
return $this->morphedByMany(Client::class, 'labelled');

tests/Models/Skill.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@ public function sqlUsers(): BelongsToMany
1818
{
1919
return $this->belongsToMany(SqlUser::class);
2020
}
21-
22-
public function sqlUsers(): MorphToMany
23-
{
24-
return $this->morphedByMany(SqlUser::class, 'skilled');
25-
}
2621
}

tests/Models/SqlUser.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function sqlBooks(): HasMany
4444
return $this->hasMany(SqlBook::class);
4545
}
4646

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

5252
public function experiences(): MorphToMany
@@ -68,20 +68,25 @@ public static function executeSchema(): void
6868
$table->string('name');
6969
$table->timestamps();
7070
});
71+
72+
// Pivot table for BelongsToMany relationship with Skill
7173
if (! $schema->hasTable('skill_sql_user')) {
7274
$schema->create('skill_sql_user', function (Blueprint $table) {
7375
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
7476
$table->string((new Skill())->getForeignKey());
7577
$table->primary([(new self())->getForeignKey(), (new Skill())->getForeignKey()]);
7678
});
7779
}
78-
if (! $schema->hasTable('skilleds')) {
79-
$schema->create('skilleds', function (Blueprint $table) {
80+
81+
// Pivot table for MorphToMany relationship with Label
82+
if (! $schema->hasTable('labeleds')) {
83+
$schema->create('labeleds', function (Blueprint $table) {
8084
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
81-
$table->morphs('skilled');
85+
$table->morphs('labeled');
8286
});
8387
}
8488

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

0 commit comments

Comments
 (0)