Skip to content

Commit 3201ece

Browse files
committed
Tests for sync method;
1 parent 3979320 commit 3201ece

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/RelationsTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,36 @@ public function testBelongsToMany(): void
255255
$this->assertCount(1, $client->users);
256256
}
257257

258+
public function testSyncBelongsToMany()
259+
{
260+
$user = User::create(['name' => 'John Doe']);
261+
262+
$first = Client::query()->create(['name' => 'Hans']);
263+
$second = Client::query()->create(['name' => 'Thomas']);
264+
265+
$user->load('clients');
266+
self::assertEmpty($user->clients);
267+
268+
$user->clients()->sync($first);
269+
270+
$user->load('clients');
271+
self::assertCount(1, $user->clients);
272+
self::assertTrue($user->clients->first()->is($first));
273+
274+
$user->clients()->sync($second);
275+
276+
$user->load('clients');
277+
self::assertCount(1, $user->clients);
278+
self::assertTrue($user->clients->first()->is($second));
279+
280+
$user->clients()->syncWithoutDetaching($first);
281+
282+
$user->load('clients');
283+
self::assertCount(2, $user->clients);
284+
self::assertTrue($user->clients->first()->is($first));
285+
self::assertTrue($user->clients->last()->is($second));
286+
}
287+
258288
public function testBelongsToManyAttachesExistingModels(): void
259289
{
260290
$user = User::create(['name' => 'John Doe', 'client_ids' => ['1234523']]);

0 commit comments

Comments
 (0)