Skip to content

Commit 7cb8fea

Browse files
committed
Added Attrs test
1 parent d12bf6e commit 7cb8fea

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tests/RelationsTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,35 @@ public function testBelongsToManySync()
280280
$this->assertCount(1, $user->clients);
281281
}
282282

283+
public function testBelongsToManySyncAttrs()
284+
{
285+
// create test instances
286+
/** @var User $user */
287+
$user = User::create(['name' => 'John Doe']);
288+
$client1 = Client::create(['name' => 'Pork Pies Ltd.'])->_id;
289+
$client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id;
290+
291+
// Sync multiple
292+
$user->clients()->sync([$client1 => ['fresh_client' => true], $client2 => ['fresh_client' => false]]);
293+
294+
//Check Sync Success
295+
$this->assertEquals($user->client_ids[0]['_id'], $client1);
296+
$this->assertEquals($user->client_ids[1]['_id'], $client2);
297+
298+
//Check reverse
299+
$this->assertEquals($user->_id, Client::find($client1)->user_ids[0]['_id']);
300+
$this->assertEquals($user->_id, Client::find($client2)->user_ids[0]['_id']);
301+
302+
$clients = $user->clients;
303+
$this->assertCount(2, $clients);
304+
305+
foreach ($user->clients()->withPivot('fresh_client')->get() as $item) {
306+
$this->assertIsArray($item->pivot->toArray());
307+
$this->assertEquals($item->_id == $client1, $item->pivot->fresh_client);
308+
}
309+
$this->assertTrue(true);
310+
}
311+
283312
public function testBelongsToManyAttachArray()
284313
{
285314
$user = User::create(['name' => 'John Doe']);
@@ -369,7 +398,7 @@ public function testMorph()
369398
$this->assertEquals($photo->imageable->name, $user->name);
370399

371400
$user = User::with('photos')->find($user->_id);
372-
$relations = $user->getRelations();
401+
$relations = $user->getRlations();
373402
$this->assertArrayHasKey('photos', $relations);
374403
$this->assertEquals(1, $relations['photos']->count());
375404

0 commit comments

Comments
 (0)