HybridRelationsTest.php 6.26 KB
Newer Older
Jens Segers's avatar
Jens Segers committed
1 2
<?php

3
class HybridRelationsTest extends TestCase
Jens Segers's avatar
Jens Segers committed
4
{
Jens Segers's avatar
Jens Segers committed
5 6
    public function setUp()
    {
7 8
        parent::setUp();

Jens Segers's avatar
Jens Segers committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        MysqlUser::executeSchema();
        MysqlBook::executeSchema();
        MysqlRole::executeSchema();
    }

    public function tearDown()
    {
        MysqlUser::truncate();
        MysqlBook::truncate();
        MysqlRole::truncate();
    }

    public function testMysqlRelations()
    {
        $user = new MysqlUser;
        $this->assertInstanceOf('MysqlUser', $user);
        $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());

        // Mysql User
        $user->name = "John Doe";
        $user->save();
Gabriel Caruso's avatar
Gabriel Caruso committed
30
        $this->assertInternalType('int', $user->id);
Jens Segers's avatar
Jens Segers committed
31 32

        // SQL has many
33
        $book = new Book(['title' => 'Game of Thrones']);
Jens Segers's avatar
Jens Segers committed
34 35
        $user->books()->save($book);
        $user = MysqlUser::find($user->id); // refetch
Gabriel Caruso's avatar
Gabriel Caruso committed
36
        $this->assertCount(1, $user->books);
Jens Segers's avatar
Jens Segers committed
37 38 39 40 41 42

        // MongoDB belongs to
        $book = $user->books()->first(); // refetch
        $this->assertEquals('John Doe', $book->mysqlAuthor->name);

        // SQL has one
43
        $role = new Role(['type' => 'admin']);
Jens Segers's avatar
Jens Segers committed
44 45 46 47
        $user->role()->save($role);
        $user = MysqlUser::find($user->id); // refetch
        $this->assertEquals('admin', $user->role->type);

Jens Segers's avatar
Jens Segers committed
48
        // MongoDB belongs to
Jens Segers's avatar
Jens Segers committed
49 50 51 52 53 54 55 56 57
        $role = $user->role()->first(); // refetch
        $this->assertEquals('John Doe', $role->mysqlUser->name);

        // MongoDB User
        $user = new User;
        $user->name = "John Doe";
        $user->save();

        // MongoDB has many
58
        $book = new MysqlBook(['title' => 'Game of Thrones']);
Jens Segers's avatar
Jens Segers committed
59 60
        $user->mysqlBooks()->save($book);
        $user = User::find($user->_id); // refetch
Gabriel Caruso's avatar
Gabriel Caruso committed
61
        $this->assertCount(1, $user->mysqlBooks);
Jens Segers's avatar
Jens Segers committed
62 63 64 65 66 67

        // SQL belongs to
        $book = $user->mysqlBooks()->first(); // refetch
        $this->assertEquals('John Doe', $book->author->name);

        // MongoDB has one
68
        $role = new MysqlRole(['type' => 'admin']);
Jens Segers's avatar
Jens Segers committed
69 70 71 72 73 74 75 76
        $user->mysqlRole()->save($role);
        $user = User::find($user->_id); // refetch
        $this->assertEquals('admin', $user->mysqlRole->type);

        // SQL belongs to
        $role = $user->mysqlRole()->first(); // refetch
        $this->assertEquals('John Doe', $role->user->name);
    }
77

78
    public function testHybridWhereHas()
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    {
        $user = new MysqlUser;
        $otherUser = new MysqlUser;
        $this->assertInstanceOf('MysqlUser', $user);
        $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
        $this->assertInstanceOf('MysqlUser', $otherUser);
        $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $otherUser->getConnection());

        //MySql User
        $user->name = "John Doe";
        $user->id = 2;
        $user->save();
        // Other user
        $otherUser->name = 'Other User';
        $otherUser->id = 3;
        $otherUser->save();
        // Make sure they are created
Gabriel Caruso's avatar
Gabriel Caruso committed
96 97
        $this->assertInternalType('int', $user->id);
        $this->assertInternalType('int', $otherUser->id);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        // Clear to start
        $user->books()->truncate();
        $otherUser->books()->truncate();
        // Create books
        $otherUser->books()->saveMany([
            new Book(['title' => 'Harry Plants']),
            new Book(['title' => 'Harveys']),
        ]);
        // SQL has many
        $user->books()->saveMany([
            new Book(['title' => 'Game of Thrones']),
            new Book(['title' => 'Harry Potter']),
            new Book(['title' => 'Harry Planter']),
        ]);

        $users = MysqlUser::whereHas('books', function ($query) {
            return $query->where('title', 'LIKE', 'Har%');
        })->get();

        $this->assertEquals(2, $users->count());

        $users = MysqlUser::whereHas('books', function ($query) {
            return $query->where('title', 'LIKE', 'Harry%');
        }, '>=', 2)->get();

        $this->assertEquals(1, $users->count());

        $books = Book::whereHas('mysqlAuthor', function ($query) {
            return $query->where('name', 'LIKE', 'Other%');
        })->get();

        $this->assertEquals(2, $books->count());
    }
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    public function testHybridWith()
    {
        $user = new MysqlUser;
        $otherUser = new MysqlUser;
        $this->assertInstanceOf('MysqlUser', $user);
        $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
        $this->assertInstanceOf('MysqlUser', $otherUser);
        $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $otherUser->getConnection());

        //MySql User
        $user->name = "John Doe";
        $user->id = 2;
        $user->save();
        // Other user
        $otherUser->name = 'Other User';
        $otherUser->id = 3;
        $otherUser->save();
        // Make sure they are created
Gabriel Caruso's avatar
Gabriel Caruso committed
150 151
        $this->assertInternalType('int', $user->id);
        $this->assertInternalType('int', $otherUser->id);
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
        // Clear to start
        Book::truncate();
        MysqlBook::truncate();
        // Create books
        // Mysql relation
        $user->mysqlBooks()->saveMany([
            new MysqlBook(['title' => 'Game of Thrones']),
            new MysqlBook(['title' => 'Harry Potter']),
        ]);

        $otherUser->mysqlBooks()->saveMany([
            new MysqlBook(['title' => 'Harry Plants']),
            new MysqlBook(['title' => 'Harveys']),
            new MysqlBook(['title' => 'Harry Planter']),
        ]);
        // SQL has many Hybrid
        $user->books()->saveMany([
            new Book(['title' => 'Game of Thrones']),
            new Book(['title' => 'Harry Potter']),
        ]);

        $otherUser->books()->saveMany([
            new Book(['title' => 'Harry Plants']),
            new Book(['title' => 'Harveys']),
            new Book(['title' => 'Harry Planter']),
        ]);

        MysqlUser::with('books')->get()
            ->each(function ($user) {
                $this->assertEquals($user->id, $user->books->count());
            });

        MysqlUser::whereHas('mysqlBooks', function ($query) {
            return $query->where('title', 'LIKE', 'Harry%');
        })
            ->with('books')
            ->get()
            ->each(function ($user) {
                $this->assertEquals($user->id, $user->books->count());
            });
    }
Jens Segers's avatar
Jens Segers committed
193
}