@@ -330,7 +330,6 @@ public function testBelongsToManyAttachArray()
330
330
$ client1 = Client::create (['name ' => 'Test 1 ' ])->_id ;
331
331
$ client2 = Client::create (['name ' => 'Test 2 ' ])->_id ;
332
332
333
- $ user = User::where ('name ' , '= ' , 'John Doe ' )->first ();
334
333
$ user ->clients ()->attach ([$ client1 , $ client2 ]);
335
334
$ this ->assertCount (2 , $ user ->clients );
336
335
}
@@ -342,7 +341,6 @@ public function testBelongsToManyAttachEloquentCollection()
342
341
$ client2 = Client::create (['name ' => 'Test 2 ' ]);
343
342
$ collection = new \Illuminate \Database \Eloquent \Collection ([$ client1 , $ client2 ]);
344
343
345
- $ user = User::where ('name ' , '= ' , 'John Doe ' )->first ();
346
344
$ user ->clients ()->attach ($ collection );
347
345
$ this ->assertCount (2 , $ user ->clients );
348
346
}
@@ -364,6 +362,38 @@ public function testBelongsToManySyncAlreadyPresent()
364
362
$ this ->assertCount (1 , $ user ['client_ids ' ]);
365
363
}
366
364
365
+ public function testBelongsToManyLegacy ()
366
+ {
367
+ // Construct Legacy Relationship model
368
+ $ client1 = Client::create (['name ' => 'Test 1 ' ]);
369
+ $ client2 = Client::create (['name ' => 'Test 2 ' ]);
370
+ $ user1 = User::create (['name ' => 'John Doe ' , 'client_ids ' => [$ client1 ->_id , $ client2 ]]);
371
+ $ user2 = User::create (['name ' => 'John Doe ' , 'client_ids ' => [['_id ' => $ client1 ->_id ], ['_id ' => $ client2 ]]]);
372
+ $ client1 ->fill (['user_ids ' => [$ user1 ->_id ,$ user2 ->_id ]])->save ();
373
+ $ client2 ->fill (['user_ids ' => [$ user1 ->_id ,$ user2 ->_id ]])->save ();
374
+
375
+ // Check for retrieval
376
+ $ this ->assertCount (2 , $ user1 ->clients ()->get (), "Get via Helper " );
377
+ $ this ->assertCount (2 , $ user1 ->clients , "Get via Attr " );
378
+ $ this ->assertCount (2 , $ user2 ->clients ()->get (), "Get via Helper " );
379
+ $ this ->assertCount (2 , $ user2 ->clients , "Get via Attr " );
380
+
381
+ // Check retrieval is correct
382
+ $ testMatrix = [
383
+ [$ client1 , $ user1 ->clients [0 ]],
384
+ [$ client2 , $ user1 ->clients [1 ]],
385
+ [$ client1 , $ user2 ->clients [0 ]],
386
+ [$ client2 , $ user2 ->clients [1 ]]
387
+ ];
388
+ foreach ($ testMatrix as $ k => $ test ) {
389
+ $ this ->assertEquals ($ test [0 ]->_id , $ test [1 ]->_id , "Matrix # {$ k }" );
390
+ }
391
+
392
+ // Check inverse
393
+ $ this ->assertCount (2 , $ client1 ->users ()->get (), "Get Inverse via Helper " );
394
+ $ this ->assertCount (2 , $ client1 ->users , "Get Inverse via Attr " );
395
+ }
396
+
367
397
public function testBelongsToManyCustom ()
368
398
{
369
399
$ user = User::create (['name ' => 'John Doe ' ]);
@@ -378,7 +408,6 @@ public function testBelongsToManyCustom()
378
408
$ this ->assertArrayHasKey ('groups ' , $ user ->getAttributes ());
379
409
380
410
// Assert they are attached
381
- //TODO: Fix Recursion
382
411
$ userGroups = $ user ->groups ;
383
412
$ this ->assertContains ($ group ->_id , $ userGroups ->pluck ('_id ' )->toArray ());
384
413
$ this ->assertContains ($ user ->_id , $ group ->users ->pluck ('_id ' )->toArray ());
0 commit comments