@@ -12,6 +12,7 @@ public function tearDown()
1212 Item::truncate ();
1313 Role::truncate ();
1414 Client::truncate ();
15+ Group::truncate ();
1516 }
1617
1718 public function testHasMany ()
@@ -126,16 +127,22 @@ public function testEasyRelation()
126127 $ this ->assertEquals ('admin ' , $ role ->type );
127128 }
128129
129- public function testHasManyAndBelongsTo ()
130+ public function testBelongsToMany ()
130131 {
131132 $ user = User::create (array ('name ' => 'John Doe ' ));
132133
134+ // Add 2 clients
133135 $ user ->clients ()->save (new Client (array ('name ' => 'Pork Pies Ltd. ' )));
134136 $ user ->clients ()->create (array ('name ' => 'Buffet Bar Inc. ' ));
135137
138+ // Refetch
136139 $ user = User::with ('clients ' )->find ($ user ->_id );
137140 $ client = Client::with ('users ' )->first ();
138141
142+ // Check for relation attributes
143+ $ this ->assertTrue (array_key_exists ('user_ids ' , $ client ->getAttributes ()));
144+ $ this ->assertTrue (array_key_exists ('client_ids ' , $ user ->getAttributes ()));
145+
139146 $ clients = $ client ->getRelation ('users ' );
140147 $ users = $ user ->getRelation ('clients ' );
141148
@@ -194,7 +201,7 @@ public function testHasManyAndBelongsTo()
194201 $ this ->assertCount (1 , $ client ->users );
195202 }
196203
197- public function testHasManyAndBelongsToAttachesExistingModels ()
204+ public function testBelongsToManyAttachesExistingModels ()
198205 {
199206 $ user = User::create (array ('name ' => 'John Doe ' , 'client_ids ' => array ('1234523 ' )));
200207
@@ -222,12 +229,34 @@ public function testHasManyAndBelongsToAttachesExistingModels()
222229 // Add more clients
223230 $ user ->clients ()->sync ($ moreClients );
224231
232+ // Refetch
225233 $ user = User::with ('clients ' )->find ($ user ->_id );
226234
227235 // Assert there are now still 2 client objects in the relationship
228236 $ this ->assertCount (2 , $ user ->clients );
229- // Assert that the new relationships name start with synced
230- $ this ->assertStringStartsWith ('synced ' , $ user ->clients [0 ]->name );
231- $ this ->assertStringStartsWith ('synced ' , $ user ->clients [1 ]->name );
237+
238+ // Assert that the new relationships name start with synced
239+ $ this ->assertStringStartsWith ('synced ' , $ user ->clients [0 ]->name );
240+ $ this ->assertStringStartsWith ('synced ' , $ user ->clients [1 ]->name );
241+ }
242+
243+ public function testBelongsToManyCustom ()
244+ {
245+ $ user = User::create (array ('name ' => 'John Doe ' ));
246+ $ group = $ user ->groups ()->create (array ('name ' => 'Admins ' ));
247+
248+ // Refetch
249+ $ user = User::find ($ user ->_id );
250+ $ group = Group::find ($ group ->_id );
251+
252+ // Check for custom relation attributes
253+ $ this ->assertTrue (array_key_exists ('users ' , $ group ->getAttributes ()));
254+ $ this ->assertTrue (array_key_exists ('groups ' , $ user ->getAttributes ()));
255+
256+ // Assert they are attached
257+ $ this ->assertTrue (in_array ($ group ->_id , $ user ->groups ));
258+ $ this ->assertTrue (in_array ($ user ->_id , $ group ->users ));
259+ $ this ->assertEquals ($ group ->_id , $ user ->groups ()->first ()->_id );
260+ $ this ->assertEquals ($ user ->_id , $ group ->users ()->first ()->_id );
232261 }
233262}
0 commit comments