Commit 34ea8120 authored by Jens Segers's avatar Jens Segers

Adding tests for custom belongsToMany keys

parent 92debf56
...@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Item::truncate(); Item::truncate();
Role::truncate(); Role::truncate();
Client::truncate(); Client::truncate();
Group::truncate();
} }
public function testHasMany() public function testHasMany()
...@@ -126,16 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -126,16 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type); $this->assertEquals('admin', $role->type);
} }
public function testHasManyAndBelongsTo() public function testBelongsToMany()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
// Add 2 clients
$user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.'))); $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
$user->clients()->create(array('name' => 'Buffet Bar Inc.')); $user->clients()->create(array('name' => 'Buffet Bar Inc.'));
// Refetch
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
$client = Client::with('users')->first(); $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'); $clients = $client->getRelation('users');
$users = $user->getRelation('clients'); $users = $user->getRelation('clients');
...@@ -194,7 +201,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -194,7 +201,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertCount(1, $client->users); $this->assertCount(1, $client->users);
} }
public function testHasManyAndBelongsToAttachesExistingModels() public function testBelongsToManyAttachesExistingModels()
{ {
$user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523'))); $user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523')));
...@@ -222,12 +229,34 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -222,12 +229,34 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Add more clients // Add more clients
$user->clients()->sync($moreClients); $user->clients()->sync($moreClients);
// Refetch
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
// Assert there are now still 2 client objects in the relationship // Assert there are now still 2 client objects in the relationship
$this->assertCount(2, $user->clients); $this->assertCount(2, $user->clients);
// Assert that the new relationships name start with synced // Assert that the new relationships name start with synced
$this->assertStringStartsWith('synced', $user->clients[0]->name); $this->assertStringStartsWith('synced', $user->clients[0]->name);
$this->assertStringStartsWith('synced', $user->clients[1]->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);
}
} }
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Group extends Eloquent {
protected $collection = 'groups';
protected static $unguarded = true;
public function users()
{
return $this->belongsToMany('User', 'test_collection', 'groups', 'users');
}
}
...@@ -33,6 +33,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -33,6 +33,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->belongsToMany('Client'); return $this->belongsToMany('Client');
} }
public function groups()
{
return $this->belongsToMany('Group', 'test_collection', 'users', 'groups');
}
/** /**
* Get the unique identifier for the user. * Get the unique identifier for the user.
* *
......
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