Skip to content

Commit

Permalink
Adding tests for custom belongsToMany keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Nov 29, 2013
1 parent 92debf5 commit 34ea812
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
39 changes: 34 additions & 5 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function tearDown()
Item::truncate();
Role::truncate();
Client::truncate();
Group::truncate();
}

public function testHasMany()
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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')));

Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions tests/models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Client extends Eloquent {
protected $collection = 'clients';

protected static $unguarded = true;

public function users()
{
return $this->belongsToMany('User');
}
}
}
14 changes: 14 additions & 0 deletions tests/models/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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');
}
}
13 changes: 9 additions & 4 deletions tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
*
Expand Down Expand Up @@ -62,4 +67,4 @@ public function getReminderEmail()
{
return $this->email;
}
}
}

0 comments on commit 34ea812

Please sign in to comment.