14
14
use MongoDB \Laravel \Tests \Models \Item ;
15
15
use MongoDB \Laravel \Tests \Models \Label ;
16
16
use MongoDB \Laravel \Tests \Models \Photo ;
17
+ use MongoDB \Laravel \Tests \Models \PlanetOid ;
17
18
use MongoDB \Laravel \Tests \Models \Role ;
18
19
use MongoDB \Laravel \Tests \Models \Skill ;
19
20
use MongoDB \Laravel \Tests \Models \Soft ;
21
+ use MongoDB \Laravel \Tests \Models \SpaceExplorerOid ;
20
22
use MongoDB \Laravel \Tests \Models \User ;
21
23
22
24
class RelationsTest extends TestCase
@@ -35,6 +37,8 @@ public function tearDown(): void
35
37
Photo::truncate ();
36
38
Label::truncate ();
37
39
Skill::truncate ();
40
+ SpaceExplorerOid::truncate ();
41
+ PlanetOid::truncate ();
38
42
}
39
43
40
44
public function testHasMany (): void
@@ -1273,4 +1277,105 @@ public function testWhereBelongsTo()
1273
1277
1274
1278
$ this ->assertCount (3 , $ items );
1275
1279
}
1280
+
1281
+ public function testBelongsToManyOid (): void
1282
+ {
1283
+ $ explorer = SpaceExplorerOid::create (['name ' => 'John Doe ' ]);
1284
+
1285
+ // Add 2 explorer
1286
+ $ explorer ->planetsVisited ()->save (new PlanetOid (['name ' => 'Mars ' ]));
1287
+ $ explorer ->planetsVisited ()->create (['name ' => 'Jupiter ' ]);
1288
+
1289
+ // Refetch
1290
+ $ explorer = SpaceExplorerOid::with ('planetsVisited ' )->find ($ explorer ->_id );
1291
+ $ planet = PlanetOid::with ('visitors ' )->first ();
1292
+
1293
+ // Check for relation attributes
1294
+ $ this ->assertArrayHasKey ('space_explorer_oid_ids ' , $ planet ->getAttributes ());
1295
+ $ this ->assertArrayHasKey ('planet_oid_ids ' , $ explorer ->getAttributes ());
1296
+
1297
+ $ planets = $ explorer ->getRelation ('planetsVisited ' );
1298
+ $ explorers = $ planet ->getRelation ('visitors ' );
1299
+
1300
+ $ this ->assertInstanceOf (Collection::class, $ planets );
1301
+ $ this ->assertInstanceOf (Collection::class, $ explorers );
1302
+ $ this ->assertInstanceOf (SpaceExplorerOid::class, $ explorers [0 ]);
1303
+ $ this ->assertInstanceOf (PlanetOid::class, $ planets [0 ]);
1304
+ $ this ->assertCount (2 , $ explorer ->planetsVisited );
1305
+ $ this ->assertCount (1 , $ planet ->visitors );
1306
+
1307
+ // Now create a new explorer to an existing planet
1308
+ $ explorer = $ planet ->visitors ()->create (['name ' => 'skaywalker ' ]);
1309
+
1310
+ $ this ->assertInstanceOf (Collection::class, $ explorer ->planetsVisited );
1311
+ $ this ->assertInstanceOf (PlanetOid::class, $ explorer ->planetsVisited ->first ());
1312
+ $ this ->assertCount (1 , $ explorer ->planetsVisited );
1313
+
1314
+ // Get explorer and unattached planet
1315
+ $ explorer = SpaceExplorerOid::where ('name ' , '= ' , 'skaywalker ' )->first ();
1316
+ $ planet = PlanetOid::where ('name ' , '= ' , 'Jupiter ' )->first ();
1317
+
1318
+ // Check the models are what they should be
1319
+ $ this ->assertInstanceOf (PlanetOid::class, $ planet );
1320
+ $ this ->assertInstanceOf (SpaceExplorerOid::class, $ explorer );
1321
+
1322
+ // Assert they are not attached
1323
+ $ this ->assertNotContains ($ planet ->_id , $ explorer ->planet_oid_ids );
1324
+ $ this ->assertNotContains ($ explorer ->_id , $ planet ->space_explorer_oid_ids );
1325
+ $ this ->assertCount (1 , $ explorer ->planetsVisited );
1326
+ $ this ->assertCount (1 , $ planet ->visitors );
1327
+
1328
+ // Attach the planet to the explorer
1329
+ $ explorer ->planetsVisited ()->attach ($ planet );
1330
+
1331
+ // Get the new explorer model
1332
+ $ explorer = SpaceExplorerOid::where ('name ' , '= ' , 'skaywalker ' )->first ();
1333
+ $ planet = PlanetOid::where ('name ' , '= ' , 'Mars ' )->first ();
1334
+
1335
+ // Assert they are attached
1336
+ $ this ->assertNotContains ($ planet ->_id , $ explorer ->planet_oid_ids );
1337
+ $ this ->assertNotContains ($ explorer ->_id , $ planet ->space_explorer_oid_ids );
1338
+ $ this ->assertCount (2 , $ explorer ->planetsVisited );
1339
+ $ this ->assertCount (2 , $ planet ->visitors );
1340
+
1341
+
1342
+ // Detach planets from explorer
1343
+ $ explorer ->planetsVisited ()->sync ([]);
1344
+
1345
+ // Get the new user model
1346
+ $ explorer = SpaceExplorerOid::where ('name ' , '= ' , 'skaywalker ' )->first ();
1347
+ $ planet = PlanetOid::where ('name ' , '= ' , 'Mars ' )->first ();
1348
+
1349
+ // Assert they are attached
1350
+ $ this ->assertNotContains ($ planet ->_id , $ explorer ->planet_oid_ids );
1351
+ $ this ->assertNotContains ($ explorer ->_id , $ planet ->space_explorer_oid_ids );
1352
+ $ this ->assertCount (0 , $ explorer ->planetsVisited );
1353
+ $ this ->assertCount (1 , $ planet ->visitors );
1354
+ }
1355
+
1356
+ public function testBelongsToManySyncOid (): void
1357
+ {
1358
+ // create test instances
1359
+ $ explorer = SpaceExplorerOid::create (['name ' => 'John Doe ' ]);
1360
+ $ planet1 = PlanetOid::create (['name ' => 'Mars ' ]);
1361
+ $ planet2 = PlanetOid::create (['name ' => 'Jupiter ' ]);
1362
+
1363
+ // Sync multiple
1364
+ $ explorer ->planetsVisited ()->sync ([$ planet1 ->_id , $ planet2 ->_id ]);
1365
+ $ this ->assertCount (2 , $ explorer ->planetsVisited );
1366
+
1367
+ // Sync single wrapped by an array
1368
+ $ explorer ->planetsVisited ()->sync ([$ planet1 ->_id ]);
1369
+ $ explorer ->load ('planetsVisited ' );
1370
+
1371
+ $ this ->assertCount (1 , $ explorer ->planetsVisited );
1372
+ self ::assertTrue ($ explorer ->planetsVisited ->first ()->is ($ planet1 ));
1373
+
1374
+ // Sync single model
1375
+ $ explorer ->planetsVisited ()->sync ($ planet2 );
1376
+ $ explorer ->load ('planetsVisited ' );
1377
+
1378
+ $ this ->assertCount (1 , $ explorer ->planetsVisited );
1379
+ self ::assertTrue ($ explorer ->planetsVisited ->first ()->is ($ planet2 ));
1380
+ }
1276
1381
}
0 commit comments