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 {
Item::truncate();
Role::truncate();
Client::truncate();
Group::truncate();
}
public function testHasMany()
......@@ -126,16 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$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 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$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 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// 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);
}
}
......@@ -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
}
<?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');
}
}
......@@ -8,9 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface;
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 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
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 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return $this->email;
}
}
\ No newline at end of file
}
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