Skip to content

Commit a6ae7ac

Browse files
committed
add test sync hybrid
1 parent 7aa00bc commit a6ae7ac

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/HybridRelationsTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,49 @@ public function testHybridWith()
194194
$this->assertEquals($user->id, $user->books->count());
195195
});
196196
}
197+
public function testHybridSync()
198+
{
199+
$user = new MysqlUser;
200+
$this->assertInstanceOf(MysqlUser::class, $user);
201+
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
202+
203+
// Mysql User
204+
$user->name = 'John Doe';
205+
$user->save();
206+
$this->assertIsInt($user->id);
207+
// SQL has many
208+
$book = new Book(['title' => 'Harry Potter']);
209+
$otherBook = new Book(['title' => 'Game of Thrones']);
210+
211+
$user->books()->sync([$book->id,$otherBook->id]);
212+
$user = MysqlUser::find($user->id); // refetch
213+
$this->assertCount(2, $user->books);
214+
215+
$user->books()->sync([$book->id]);
216+
$user = MysqlUser::find($user->id); // refetch
217+
$this->assertCount(1, $user->books);
218+
219+
$user->books()->sync([$book->id,$otherBook->id]);
220+
$user = MysqlUser::find($user->id); // refetch
221+
$this->assertCount(2, $user->books);
222+
// MongoDB User
223+
$user = new User;
224+
$user->name = 'John Doe';
225+
$user->save();
226+
// MongoDB has many
227+
$book = new MysqlBook(['title' => 'Harry Potter']);
228+
$otherBook = new MysqlBook(['title' => 'Game of Thrones']);
229+
230+
$user->mysqlBooks()->sync([$book->id,$otherBook->id]);
231+
$user = User::find($user->_id);
232+
$this->assertCount(2, $user->mysqlBooks);
233+
234+
$user->mysqlBooks()->sync([$book->id]);
235+
$user = User::find($user->_id);
236+
$this->assertCount(1, $user->mysqlBooks);
237+
238+
$user->mysqlBooks()->sync([$book->id,$otherBook->id]);
239+
$user = User::find($user->_id);
240+
$this->assertCount(2, $user->mysqlBooks);
241+
}
197242
}

0 commit comments

Comments
 (0)