@@ -9,7 +9,6 @@ class HybridRelationsTest extends TestCase
9
9
public function setUp (): void
10
10
{
11
11
parent ::setUp ();
12
-
13
12
MysqlUser::executeSchema ();
14
13
MysqlBook::executeSchema ();
15
14
MysqlRole::executeSchema ();
@@ -194,50 +193,49 @@ public function testHybridWith()
194
193
$ this ->assertEquals ($ user ->id , $ user ->books ->count ());
195
194
});
196
195
}
197
-
198
196
public function testHybridSync ()
199
197
{
200
198
$ user = new MysqlUser ;
199
+ $ otherUser = new MysqlUser ;
201
200
$ this ->assertInstanceOf (MysqlUser::class, $ user );
202
201
$ this ->assertInstanceOf (MySqlConnection::class, $ user ->getConnection ());
202
+ $ this ->assertInstanceOf (MysqlUser::class, $ otherUser );
203
+ $ this ->assertInstanceOf (MySqlConnection::class, $ otherUser ->getConnection ());
203
204
204
- // Mysql User
205
- $ user ->name = 'John Doe ' ;
206
- $ user ->save ();
207
- $ this ->assertIsInt ($ user ->id );
208
- // SQL has many
209
- $ book = new Book (['title ' => 'Harry Potter ' ]);
210
- $ otherBook = new Book (['title ' => 'Game of Thrones ' ]);
211
-
212
- $ user ->books ()->sync ([$ book ->id , $ otherBook ->id ]);
213
- $ user = MysqlUser::find ($ user ->id ); // refetch
214
- $ this ->assertCount (2 , $ user ->books );
215
-
216
- $ user ->books ()->sync ([$ book ->id ]);
217
- $ user = MysqlUser::find ($ user ->id ); // refetch
218
- $ this ->assertCount (1 , $ user ->books );
219
-
220
- $ user ->books ()->sync ([$ book ->id , $ otherBook ->id ]);
221
- $ user = MysqlUser::find ($ user ->id ); // refetch
222
- $ this ->assertCount (2 , $ user ->books );
223
- // MongoDB User
224
- $ user = new User ;
205
+ // Create Mysql Users
225
206
$ user ->name = 'John Doe ' ;
226
207
$ user ->save ();
227
- // MongoDB has many
228
- $ book = new MysqlBook (['title ' => 'Harry Potter ' ]);
229
- $ otherBook = new MysqlBook (['title ' => 'Game of Thrones ' ]);
230
-
231
- $ user ->mysqlBooks ()->sync ([$ book ->id , $ otherBook ->id ]);
232
- $ user = User::find ($ user ->_id );
233
- $ this ->assertCount (2 , $ user ->mysqlBooks );
234
-
235
- $ user ->mysqlBooks ()->sync ([$ book ->id ]);
236
- $ user = User::find ($ user ->_id );
237
- $ this ->assertCount (1 , $ user ->mysqlBooks );
238
-
239
- $ user ->mysqlBooks ()->sync ([$ book ->id , $ otherBook ->id ]);
240
- $ user = User::find ($ user ->_id );
241
- $ this ->assertCount (2 , $ user ->mysqlBooks );
208
+ $ user = MysqlUser::find ($ user ->id );
209
+ $ otherUser ->name = 'Maria Doe ' ;
210
+ $ otherUser ->save ();
211
+ // Create Mongodb Clients
212
+ $ client = Client::create (['name ' => 'Pork Pies Ltd. ' ]);
213
+ $ otherClient = Client::create (['name ' => 'Pork Pies Ltd. ' ]);
214
+
215
+ // sync 2 users
216
+ $ client ->usersMysql ()->sync ([$ user ->id , $ otherUser ->id ]);
217
+ $ client = Client::find ($ client ->_id );
218
+ $ this ->assertEquals (2 , $ client ->usersMysql ->count ());
219
+ // sync 1 user
220
+ $ client ->usersMysql ()->sync ([$ user ->id ]);
221
+ $ client = Client::find ($ client ->_id );
222
+ $ this ->assertEquals (1 , $ client ->usersMysql ->count ());
223
+ // sync 2 users again
224
+ $ client ->usersMysql ()->sync ([$ user ->id , $ otherUser ->id ]);
225
+ $ client = Client::find ($ client ->_id );
226
+ $ this ->assertEquals (2 , $ client ->usersMysql ->count ());
227
+
228
+ // sync 2 Clients
229
+ $ user ->clients ()->sync ([$ client ->_id ,$ otherClient ->_id ]);
230
+ $ user = MysqlUser::find ($ user ->id );
231
+ $ this ->assertEquals (2 , $ user ->clients ->count ());
232
+ // Sync 1 Client
233
+ $ user ->clients ()->sync ([$ client ->_id ]);
234
+ $ user = MysqlUser::find ($ user ->id );
235
+ $ this ->assertEquals (1 , $ user ->clients ->count ());
236
+ // Sync 2 Clients again
237
+ $ user ->clients ()->sync ([$ client ->_id ,$ otherClient ->_id ]);
238
+ $ user = MysqlUser::find ($ user ->id );
239
+ $ this ->assertEquals (2 , $ user ->clients ->count ());
242
240
}
243
241
}
0 commit comments