1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
require_once('tests/app.php');
class RelationsTest extends PHPUnit_Framework_TestCase {
public function setUp() {
}
public function tearDown()
{
User::truncate();
Book::truncate();
Item::truncate();
Role::truncate();
}
public function testHasMany()
{
$author = User::create(array('name' => 'George R. R. Martin'));
Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id));
Book::create(array('title' => 'A Clash of Kings', 'author_id' => $author->_id));
$books = $author->books;
$this->assertEquals(2, count($books));
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id));
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
Item::create(array('type' => 'sword', 'user_id' => $user->_id));
Item::create(array('type' => 'bag', 'user_id' => null));
$items = $user->items;
$this->assertEquals(3, count($items));
}
public function testBelongsTo()
{
$user = User::create(array('name' => 'George R. R. Martin'));
Book::create(array('title' => 'A Game of Thrones', 'author_id' => $user->_id));
$book = Book::create(array('title' => 'A Clash of Kings', 'author_id' => $user->_id));
$author = $book->author;
$this->assertEquals('George R. R. Martin', $author->name);
$user = User::create(array('name' => 'John Doe'));
$item = Item::create(array('type' => 'sword', 'user_id' => $user->_id));
$owner = $item->user;
$this->assertEquals('John Doe', $owner->name);
}
public function testHasOne()
{
$user = User::create(array('name' => 'John Doe'));
Role::create(array('type' => 'admin', 'user_id' => $user->_id));
$role = $user->role;
$this->assertEquals('admin', $role->type);
}
public function testWithBelongsTo()
{
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id));
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
Item::create(array('type' => 'sword', 'user_id' => $user->_id));
Item::create(array('type' => 'bag', 'user_id' => null));
$items = Item::with('user')->get();
$user = $items[0]->getRelation('user');
$this->assertInstanceOf('User', $user);
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(1, count($items[0]->getRelations()));
$this->assertEquals(null, $items[3]->getRelation('user'));
}
public function testWithHashMany()
{
$user = User::create(array('name' => 'John Doe'));
Item::create(array('type' => 'knife', 'user_id' => $user->_id));
Item::create(array('type' => 'shield', 'user_id' => $user->_id));
Item::create(array('type' => 'sword', 'user_id' => $user->_id));
Item::create(array('type' => 'bag', 'user_id' => null));
$user = User::with('items')->find($user->_id);
$items = $user->getRelation('items');
$this->assertEquals(3, count($items));
$this->assertInstanceOf('Item', $items[0]);
}
public function testWithHasOne()
{
$user = User::create(array('name' => 'John Doe'));
Role::create(array('type' => 'admin', 'user_id' => $user->_id));
Role::create(array('type' => 'guest', 'user_id' => $user->_id));
$user = User::with('role')->find($user->_id);
$role = $user->getRelation('role');
$this->assertInstanceOf('Role', $role);
$this->assertEquals('admin', $role->type);
}
}