Commit bc9b05ed authored by unknown's avatar unknown

Relations test updated

parent e93b2625
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
class RelationsTest extends PHPUnit_Framework_TestCase { class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() { public function setUp() {
User::truncate();
Book::truncate();
Item::truncate();
Role::truncate();
Client::truncate();
} }
public function tearDown() public function tearDown()
...@@ -11,6 +16,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -11,6 +16,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Book::truncate(); Book::truncate();
Item::truncate(); Item::truncate();
Role::truncate(); Role::truncate();
Client::truncate();
} }
public function testHasMany() public function testHasMany()
...@@ -102,27 +108,89 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -102,27 +108,89 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type); $this->assertEquals('admin', $role->type);
} }
public function testEasyRelation() public function testHasManyAndBelongsTo()
{ {
// Has Many
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
$item = Item::create(array('type' => 'knife'));
$user->items()->save($item);
$user = User::find($user->_id); $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
$items = $user->items; $user->clients()->create(array('name' => 'Buffet Bar Inc.'));
$this->assertEquals(1, count($items));
$this->assertInstanceOf('Item', $items[0]);
// Has one $user = User::with('clients')->find($user->_id);
$user = User::create(array('name' => 'John Doe'));
$role = Role::create(array('type' => 'admin'));
$user->role()->save($role);
$user = User::find($user->_id); $client = Client::with('users')->first();
$role = $user->role;
$this->assertInstanceOf('Role', $role); $clients = $client->getRelation('users');
$this->assertEquals('admin', $role->type); $users = $user->getRelation('clients');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
$this->assertInstanceOf('Client', $users[0]);
$this->assertInstanceOf('User', $clients[0]);
$this->assertCount(2, $user->clients);
$this->assertCount(1, $client->users);
// Now create a new user to an existing client
$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('Client', $otherClient[0]);
$this->assertCount(1, $otherClient);
// Now attach an existing client to an existing user
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Check the models are what they should be
$this->assertInstanceOf('Client', $client);
$this->assertInstanceOf('User', $user);
// Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids));
// Attach the client to the user
$user->clients()->attach($client);
// Get the new user model
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Assert they are attached
$this->assertTrue(in_array($client->_id, $user->client_ids));
$this->assertTrue(in_array($user->_id, $client->user_ids));
} }
public function testHasManyAndBelongsToAttachesExistingModels()
{
$user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523')));
$clients = array(
Client::create(array('name' => 'Pork Pies Ltd.'))->_id,
Client::create(array('name' => 'Buffet Bar Inc.'))->_id
);
$moreClients = array(
Client::create(array('name' => 'Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id
);
$user->clients()->sync($clients);
$user = User::with('clients')->find($user->_id);
// Assert non attached ID's are detached succesfully
$this->assertFalse(in_array('1234523', $user->client_ids));
// Assert there are two client objects in the relationship
$this->assertCount(2, $user->clients);
$user->clients()->sync($moreClients);
$user = User::with('clients')->find($user->_id);
// Assert there are now 4 client objects in the relationship
$this->assertCount(4, $user->clients);
}
} }
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