RelationsTest.php 7.64 KB
Newer Older
Jens Segers's avatar
Jens Segers committed
1 2 3 4
<?php

class RelationsTest extends PHPUnit_Framework_TestCase {

5
    public function setUp() {
unknown's avatar
unknown committed
6 7 8 9
    }

    public function tearDown()
    {
10 11 12 13 14
        User::truncate();
        Book::truncate();
        Item::truncate();
        Role::truncate();
        Client::truncate();
unknown's avatar
unknown committed
15 16 17 18
    }

    public function testHasMany()
    {
19 20 21
        $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));
unknown's avatar
unknown committed
22

23 24
        $books = $author->books;
        $this->assertEquals(2, count($books));
unknown's avatar
unknown committed
25

26 27 28 29 30
        $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));
unknown's avatar
unknown committed
31

32 33 34
        $items = $user->items;
        $this->assertEquals(3, count($items));
}
unknown's avatar
unknown committed
35 36 37

    public function testBelongsTo()
    {
38 39 40
        $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));
unknown's avatar
unknown committed
41

42 43
        $author = $book->author;
        $this->assertEquals('George R. R. Martin', $author->name);
unknown's avatar
unknown committed
44

45 46
        $user = User::create(array('name' => 'John Doe'));
        $item = Item::create(array('type' => 'sword', 'user_id' => $user->_id));
unknown's avatar
unknown committed
47

48 49
        $owner = $item->user;
        $this->assertEquals('John Doe', $owner->name);
unknown's avatar
unknown committed
50 51 52 53
    }

    public function testHasOne()
    {
54 55
        $user = User::create(array('name' => 'John Doe'));
        Role::create(array('type' => 'admin', 'user_id' => $user->_id));
unknown's avatar
unknown committed
56

57 58
        $role = $user->role;
        $this->assertEquals('admin', $role->type);
unknown's avatar
unknown committed
59 60 61 62
    }

    public function testWithBelongsTo()
    {
63 64 65 66 67 68 69 70 71 72 73 74 75
        $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'));
unknown's avatar
unknown committed
76 77 78 79
    }

    public function testWithHashMany()
    {
80 81 82 83 84
        $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));
unknown's avatar
unknown committed
85

86
        $user = User::with('items')->find($user->_id);
unknown's avatar
unknown committed
87

88 89 90
        $items = $user->getRelation('items');
        $this->assertEquals(3, count($items));
        $this->assertInstanceOf('Item', $items[0]);
unknown's avatar
unknown committed
91 92 93 94
    }

    public function testWithHasOne()
    {
95 96 97
        $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));
unknown's avatar
unknown committed
98

99
        $user = User::with('role')->find($user->_id);
unknown's avatar
unknown committed
100

101 102 103
        $role = $user->getRelation('role');
        $this->assertInstanceOf('Role', $role);
        $this->assertEquals('admin', $role->type);
unknown's avatar
unknown committed
104 105 106 107
    }

    public function testEasyRelation()
    {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        // Has Many
        $user = User::create(array('name' => 'John Doe'));
        $item = Item::create(array('type' => 'knife'));
        $user->items()->save($item);

        $user = User::find($user->_id);
        $items = $user->items;
        $this->assertEquals(1, count($items));
        $this->assertInstanceOf('Item', $items[0]);

        // Has one
        $user = User::create(array('name' => 'John Doe'));
        $role = Role::create(array('type' => 'admin'));
        $user->role()->save($role);

        $user = User::find($user->_id);
        $role = $user->role;
        $this->assertInstanceOf('Role', $role);
        $this->assertEquals('admin', $role->type);
    }

    public function testHasManyAndBelongsTo()
    {
        $user = User::create(array('name' => 'John Doe'));

        $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
        $user->clients()->create(array('name' => 'Buffet Bar Inc.'));

        $user = User::with('clients')->find($user->_id);

        $client = Client::with('users')->first();

        $clients = $client->getRelation('users');
        $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
        );

        // Sync multiple records
        $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);
unknown's avatar
unknown committed
214
    }
215
}