@@ -194,4 +194,49 @@ public function testHybridWith()
194
194
$ this ->assertEquals ($ user ->id , $ user ->books ->count ());
195
195
});
196
196
}
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
+ }
197
242
}
0 commit comments