Skip to content

Commit 2904008

Browse files
committed
Add tests for detach
1 parent 0a79647 commit 2904008

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function attach($id, array $attributes = array(), $touch = true)
144144
*/
145145
protected function createAttachRecords($ids, array $attributes)
146146
{
147-
$records = array();;
147+
$records = array();
148148

149149
// To create the attachment records, we will simply spin through the IDs given
150150
// and create a new record to insert for each ID. Each ID may actually be a
@@ -185,7 +185,7 @@ public function detach($ids = array(), $touch = true)
185185
// Once we have all of the conditions set on the statement, we are ready
186186
// to run the delete on the pivot table. Then, if the touch parameter
187187
// is true, we will go ahead and touch all related models to sync.
188-
foreach($ids as $id)
188+
foreach ($ids as $id)
189189
{
190190
$query->pull($this->otherKey, $id);
191191
}

tests/RelationsTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)