Skip to content

Commit 91bfb9c

Browse files
committed
Testing Legacy support
1 parent 532975b commit 91bfb9c

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function hydratePivotRelation(array $models)
3838
$keyToUse = $this->getTable() == $model->getTable() ? $this->getForeignKey() : $this->getRelatedKey();
3939
$pcontent = $model->getAttributes()[$keyToUse];
4040
$model->setRelation($this->accessor, $this->newExistingPivot(
41-
$pcontent[0]
41+
is_string($pcontent[0]) ? ['_id' => $pcontent] : $pcontent[0]
4242
));
4343
}
4444
}

tests/RelationsTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ public function testBelongsToManyAttachArray()
330330
$client1 = Client::create(['name' => 'Test 1'])->_id;
331331
$client2 = Client::create(['name' => 'Test 2'])->_id;
332332

333-
$user = User::where('name', '=', 'John Doe')->first();
334333
$user->clients()->attach([$client1, $client2]);
335334
$this->assertCount(2, $user->clients);
336335
}
@@ -342,7 +341,6 @@ public function testBelongsToManyAttachEloquentCollection()
342341
$client2 = Client::create(['name' => 'Test 2']);
343342
$collection = new \Illuminate\Database\Eloquent\Collection([$client1, $client2]);
344343

345-
$user = User::where('name', '=', 'John Doe')->first();
346344
$user->clients()->attach($collection);
347345
$this->assertCount(2, $user->clients);
348346
}
@@ -364,6 +362,38 @@ public function testBelongsToManySyncAlreadyPresent()
364362
$this->assertCount(1, $user['client_ids']);
365363
}
366364

365+
public function testBelongsToManyLegacy()
366+
{
367+
// Construct Legacy Relationship model
368+
$client1 = Client::create(['name' => 'Test 1']);
369+
$client2 = Client::create(['name' => 'Test 2']);
370+
$user1 = User::create(['name' => 'John Doe', 'client_ids' => [$client1->_id, $client2]]);
371+
$user2 = User::create(['name' => 'John Doe', 'client_ids' => [['_id' => $client1->_id], ['_id' => $client2]]]);
372+
$client1->fill(['user_ids' => [$user1->_id,$user2->_id]])->save();
373+
$client2->fill(['user_ids' => [$user1->_id,$user2->_id]])->save();
374+
375+
// Check for retrieval
376+
$this->assertCount(2, $user1->clients()->get(), "Get via Helper");
377+
$this->assertCount(2, $user1->clients, "Get via Attr");
378+
$this->assertCount(2, $user2->clients()->get(), "Get via Helper");
379+
$this->assertCount(2, $user2->clients, "Get via Attr");
380+
381+
// Check retrieval is correct
382+
$testMatrix = [
383+
[$client1, $user1->clients[0]],
384+
[$client2, $user1->clients[1]],
385+
[$client1, $user2->clients[0]],
386+
[$client2, $user2->clients[1]]
387+
];
388+
foreach ($testMatrix as $k => $test) {
389+
$this->assertEquals($test[0]->_id, $test[1]->_id, "Matrix #{$k}");
390+
}
391+
392+
// Check inverse
393+
$this->assertCount(2, $client1->users()->get(), "Get Inverse via Helper");
394+
$this->assertCount(2, $client1->users, "Get Inverse via Attr");
395+
}
396+
367397
public function testBelongsToManyCustom()
368398
{
369399
$user = User::create(['name' => 'John Doe']);
@@ -378,7 +408,6 @@ public function testBelongsToManyCustom()
378408
$this->assertArrayHasKey('groups', $user->getAttributes());
379409

380410
// Assert they are attached
381-
//TODO: Fix Recursion
382411
$userGroups = $user->groups;
383412
$this->assertContains($group->_id, $userGroups->pluck('_id')->toArray());
384413
$this->assertContains($user->_id, $group->users->pluck('_id')->toArray());

0 commit comments

Comments
 (0)