diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 39edbb030..4372f6b7d 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -12,6 +12,7 @@ public function tearDown() Item::truncate(); Role::truncate(); Client::truncate(); + Group::truncate(); } public function testHasMany() @@ -126,16 +127,22 @@ public function testEasyRelation() $this->assertEquals('admin', $role->type); } - public function testHasManyAndBelongsTo() + public function testBelongsToMany() { $user = User::create(array('name' => 'John Doe')); + // Add 2 clients $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.'))); $user->clients()->create(array('name' => 'Buffet Bar Inc.')); + // Refetch $user = User::with('clients')->find($user->_id); $client = Client::with('users')->first(); + // Check for relation attributes + $this->assertTrue(array_key_exists('user_ids', $client->getAttributes())); + $this->assertTrue(array_key_exists('client_ids', $user->getAttributes())); + $clients = $client->getRelation('users'); $users = $user->getRelation('clients'); @@ -194,7 +201,7 @@ public function testHasManyAndBelongsTo() $this->assertCount(1, $client->users); } - public function testHasManyAndBelongsToAttachesExistingModels() + public function testBelongsToManyAttachesExistingModels() { $user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523'))); @@ -222,12 +229,34 @@ public function testHasManyAndBelongsToAttachesExistingModels() // Add more clients $user->clients()->sync($moreClients); + // Refetch $user = User::with('clients')->find($user->_id); // Assert there are now still 2 client objects in the relationship $this->assertCount(2, $user->clients); - // Assert that the new relationships name start with synced - $this->assertStringStartsWith('synced', $user->clients[0]->name); - $this->assertStringStartsWith('synced', $user->clients[1]->name); + + // Assert that the new relationships name start with synced + $this->assertStringStartsWith('synced', $user->clients[0]->name); + $this->assertStringStartsWith('synced', $user->clients[1]->name); + } + + public function testBelongsToManyCustom() + { + $user = User::create(array('name' => 'John Doe')); + $group = $user->groups()->create(array('name' => 'Admins')); + + // Refetch + $user = User::find($user->_id); + $group = Group::find($group->_id); + + // Check for custom relation attributes + $this->assertTrue(array_key_exists('users', $group->getAttributes())); + $this->assertTrue(array_key_exists('groups', $user->getAttributes())); + + // Assert they are attached + $this->assertTrue(in_array($group->_id, $user->groups)); + $this->assertTrue(in_array($user->_id, $group->users)); + $this->assertEquals($group->_id, $user->groups()->first()->_id); + $this->assertEquals($user->_id, $group->users()->first()->_id); } } diff --git a/tests/models/Client.php b/tests/models/Client.php index de55ceab6..2660e00b7 100644 --- a/tests/models/Client.php +++ b/tests/models/Client.php @@ -6,9 +6,9 @@ class Client extends Eloquent { protected $collection = 'clients'; protected static $unguarded = true; - + public function users() { return $this->belongsToMany('User'); } -} \ No newline at end of file +} diff --git a/tests/models/Group.php b/tests/models/Group.php new file mode 100644 index 000000000..5332948d3 --- /dev/null +++ b/tests/models/Group.php @@ -0,0 +1,14 @@ +belongsToMany('User', 'test_collection', 'groups', 'users'); + } +} diff --git a/tests/models/User.php b/tests/models/User.php index 652d48f51..5a8d352fe 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -8,9 +8,9 @@ class User extends Eloquent implements UserInterface, RemindableInterface { protected $collection = 'users'; - + protected $dates = array('birthday'); - + protected static $unguarded = true; public function books() @@ -27,12 +27,17 @@ public function role() { return $this->hasOne('Role'); } - + public function clients() { return $this->belongsToMany('Client'); } + public function groups() + { + return $this->belongsToMany('Group', 'test_collection', 'users', 'groups'); + } + /** * Get the unique identifier for the user. * @@ -62,4 +67,4 @@ public function getReminderEmail() { return $this->email; } -} \ No newline at end of file +}