Commit fe90f122 authored by Jens Segers's avatar Jens Segers

Add tests for detach

parent d709d4a8
...@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
protected function createAttachRecords($ids, array $attributes) protected function createAttachRecords($ids, array $attributes)
{ {
$records = array();; $records = array();
// To create the attachment records, we will simply spin through the IDs given // To create the attachment records, we will simply spin through the IDs given
// and create a new record to insert for each ID. Each ID may actually be a // and create a new record to insert for each ID. Each ID may actually be a
...@@ -185,7 +185,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -185,7 +185,7 @@ class BelongsToMany extends EloquentBelongsToMany {
// Once we have all of the conditions set on the statement, we are ready // Once we have all of the conditions set on the statement, we are ready
// to run the delete on the pivot table. Then, if the touch parameter // to run the delete on the pivot table. Then, if the touch parameter
// is true, we will go ahead and touch all related models to sync. // is true, we will go ahead and touch all related models to sync.
foreach($ids as $id) foreach ($ids as $id)
{ {
$query->pull($this->otherKey, $id); $query->pull($this->otherKey, $id);
} }
......
...@@ -134,7 +134,6 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -134,7 +134,6 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$user->clients()->create(array('name' => 'Buffet Bar Inc.')); $user->clients()->create(array('name' => 'Buffet Bar Inc.'));
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
$client = Client::with('users')->first(); $client = Client::with('users')->first();
$clients = $client->getRelation('users'); $clients = $client->getRelation('users');
...@@ -148,15 +147,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -148,15 +147,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertCount(1, $client->users); $this->assertCount(1, $client->users);
// Now create a new user to an existing client // Now create a new user to an existing client
$client->users()->create(array('name' => 'Jane Doe')); $user = $client->users()->create(array('name' => 'Jane Doe'));
$otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $otherClient); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->clients);
$this->assertInstanceOf('Client', $otherClient[0]); $this->assertInstanceOf('Client', $user->clients->first());
$this->assertCount(1, $otherClient); $this->assertCount(1, $user->clients);
// Now attach an existing client to an existing user // Get user and unattached client
$user = User::where('name', '=', 'Jane Doe')->first(); $user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
...@@ -167,6 +164,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -167,6 +164,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are not attached // Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids)); $this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids)); $this->assertFalse(in_array($user->_id, $client->user_ids));
$this->assertCount(1, $user->clients);
$this->assertCount(1, $client->users);
// Attach the client to the user // Attach the client to the user
$user->clients()->attach($client); $user->clients()->attach($client);
...@@ -178,6 +177,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -178,6 +177,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are attached // Assert they are attached
$this->assertTrue(in_array($client->_id, $user->client_ids)); $this->assertTrue(in_array($client->_id, $user->client_ids));
$this->assertTrue(in_array($user->_id, $client->user_ids)); $this->assertTrue(in_array($user->_id, $client->user_ids));
$this->assertCount(2, $user->clients);
$this->assertCount(2, $client->users);
// Detach clients from user
$user->clients()->sync(array());
// Get the new user model
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids));
$this->assertCount(0, $user->clients);
$this->assertCount(1, $client->users);
} }
public function testHasManyAndBelongsToAttachesExistingModels() public function testHasManyAndBelongsToAttachesExistingModels()
...@@ -205,6 +219,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -205,6 +219,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert there are two client objects in the relationship // Assert there are two client objects in the relationship
$this->assertCount(2, $user->clients); $this->assertCount(2, $user->clients);
// Add more clients
$user->clients()->sync($moreClients); $user->clients()->sync($moreClients);
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment