@@ -134,7 +134,6 @@ public function testHasManyAndBelongsTo()
134134 $ user ->clients ()->create (array ('name ' => 'Buffet Bar Inc. ' ));
135135
136136 $ user = User::with ('clients ' )->find ($ user ->_id );
137-
138137 $ client = Client::with ('users ' )->first ();
139138
140139 $ clients = $ client ->getRelation ('users ' );
@@ -148,15 +147,13 @@ public function testHasManyAndBelongsTo()
148147 $ this ->assertCount (1 , $ client ->users );
149148
150149 // Now create a new user to an existing client
151- $ client ->users ()->create (array ('name ' => 'Jane Doe ' ));
152-
153- $ otherClient = User::where ('name ' , '= ' , 'Jane Doe ' )->first ()->clients ()->get ();
150+ $ user = $ client ->users ()->create (array ('name ' => 'Jane Doe ' ));
154151
155- $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ otherClient );
156- $ this ->assertInstanceOf ('Client ' , $ otherClient [ 0 ] );
157- $ this ->assertCount (1 , $ otherClient );
152+ $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ user -> clients );
153+ $ this ->assertInstanceOf ('Client ' , $ user -> clients -> first () );
154+ $ this ->assertCount (1 , $ user -> clients );
158155
159- // Now attach an existing client to an existing user
156+ // Get user and unattached client
160157 $ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
161158 $ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
162159
@@ -167,6 +164,8 @@ public function testHasManyAndBelongsTo()
167164 // Assert they are not attached
168165 $ this ->assertFalse (in_array ($ client ->_id , $ user ->client_ids ));
169166 $ this ->assertFalse (in_array ($ user ->_id , $ client ->user_ids ));
167+ $ this ->assertCount (1 , $ user ->clients );
168+ $ this ->assertCount (1 , $ client ->users );
170169
171170 // Attach the client to the user
172171 $ user ->clients ()->attach ($ client );
@@ -178,6 +177,21 @@ public function testHasManyAndBelongsTo()
178177 // Assert they are attached
179178 $ this ->assertTrue (in_array ($ client ->_id , $ user ->client_ids ));
180179 $ this ->assertTrue (in_array ($ user ->_id , $ client ->user_ids ));
180+ $ this ->assertCount (2 , $ user ->clients );
181+ $ this ->assertCount (2 , $ client ->users );
182+
183+ // Detach clients from user
184+ $ user ->clients ()->sync (array ());
185+
186+ // Get the new user model
187+ $ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
188+ $ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
189+
190+ // Assert they are not attached
191+ $ this ->assertFalse (in_array ($ client ->_id , $ user ->client_ids ));
192+ $ this ->assertFalse (in_array ($ user ->_id , $ client ->user_ids ));
193+ $ this ->assertCount (0 , $ user ->clients );
194+ $ this ->assertCount (1 , $ client ->users );
181195 }
182196
183197 public function testHasManyAndBelongsToAttachesExistingModels ()
@@ -205,6 +219,7 @@ public function testHasManyAndBelongsToAttachesExistingModels()
205219 // Assert there are two client objects in the relationship
206220 $ this ->assertCount (2 , $ user ->clients );
207221
222+ // Add more clients
208223 $ user ->clients ()->sync ($ moreClients );
209224
210225 $ user = User::with ('clients ' )->find ($ user ->_id );
0 commit comments