Skip to content

Commit ac7174a

Browse files
committed
Adding some more tests for mysql relations
1 parent 9559b29 commit ac7174a

File tree

11 files changed

+130
-48
lines changed

11 files changed

+130
-48
lines changed

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
<directory>tests/ModelTest.php</directory>
3232
<directory>tests/RelationsTest.php</directory>
3333
</testsuite>
34+
<testsuite name="relations">
35+
<directory>tests/RelationsTest.php</directory>
36+
</testsuite>
3437
</testsuites>
3538
</phpunit>

tests/RelationsTest.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testHasMany()
3232

3333
$items = $user->items;
3434
$this->assertEquals(3, count($items));
35-
}
35+
}
3636

3737
public function testBelongsTo()
3838
{
@@ -264,38 +264,66 @@ public function testMysqlModel()
264264
{
265265
// A bit dirty
266266
MysqlUser::executeSchema();
267+
MysqlBook::executeSchema();
268+
MysqlRole::executeSchema();
267269

268270
$user = new MysqlUser;
269271
$this->assertInstanceOf('MysqlUser', $user);
270272
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
271273

274+
// Mysql User
272275
$user->name = "John Doe";
273276
$user->save();
274277
$this->assertTrue(is_int($user->id));
275278

276-
// Has many
279+
// SQL has many
277280
$book = new Book(array('title' => 'Game of Thrones'));
278281
$user->books()->save($book);
279282
$user = MysqlUser::find($user->id); // refetch
280283
$this->assertEquals(1, count($user->books));
284+
285+
// MongoDB belongs to
281286
$book = $user->books()->first(); // refetch
282287
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
283288

284-
// Has one
289+
// SQL has one
285290
$role = new Role(array('type' => 'admin'));
286291
$user->role()->save($role);
287292
$user = MysqlUser::find($user->id); // refetch
288293
$this->assertEquals('admin', $user->role->type);
294+
295+
// MongoDB beelongs to
289296
$role = $user->role()->first(); // refetch
290297
$this->assertEquals('John Doe', $role->mysqlUser->name);
291298

292-
// belongsToMany DOES NOT WORK YET
293-
/*$client = new Client(array('name' => 'Pork Pies Ltd.'));
294-
$user->clients()->save($client);
295-
$user = MysqlUser::find($user->id); // refetch
296-
$this->assertEquals(1, count($user->clients));*/
299+
// MongoDB User
300+
$user = new User;
301+
$user->name = "John Doe";
302+
$user->save();
303+
304+
// MongoDB has many
305+
$book = new MysqlBook(array('title' => 'Game of Thrones'));
306+
$user->mysqlBooks()->save($book);
307+
$user = User::find($user->_id); // refetch
308+
$this->assertEquals(1, count($user->mysqlBooks));
309+
310+
// SQL belongs to
311+
$book = $user->mysqlBooks()->first(); // refetch
312+
$this->assertEquals('John Doe', $book->author->name);
313+
314+
// MongoDB has one
315+
$role = new MysqlRole(array('type' => 'admin'));
316+
$user->mysqlRole()->save($role);
317+
$user = User::find($user->_id); // refetch
318+
$this->assertEquals('admin', $user->mysqlRole->type);
319+
320+
// SQL belongs to
321+
$role = $user->mysqlRole()->first(); // refetch
322+
$this->assertEquals('John Doe', $role->user->name);
297323

298324
// Dirty again :)
299325
MysqlUser::truncate();
326+
MysqlBook::truncate();
327+
MysqlRole::truncate();
300328
}
301329
}

tests/models/Book.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
class Book extends Eloquent {
66

77
protected $collection = 'books';
8-
98
protected static $unguarded = true;
10-
119
protected $primaryKey = 'title';
1210

1311
public function author()

tests/models/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2+
23
use Jenssegers\Mongodb\Model as Eloquent;
34

45
class Client extends Eloquent {
56

67
protected $collection = 'clients';
7-
88
protected static $unguarded = true;
99

1010
public function users()

tests/models/Group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2+
23
use Jenssegers\Mongodb\Model as Eloquent;
34

45
class Group extends Eloquent {
56

67
protected $collection = 'groups';
7-
88
protected static $unguarded = true;
99

1010
public function users()

tests/models/Item.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class Item extends Eloquent {
66

77
protected $collection = 'roles';
8-
98
protected static $unguarded = true;
109

1110
public function user()
@@ -18,4 +17,4 @@ public function scopeSharp($query)
1817
return $query->where('type', 'sharp');
1918
}
2019

21-
}
20+
}

tests/models/MysqlBook.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use \Illuminate\Support\Facades\Schema;
4+
use Jenssegers\Eloquent\Model as Eloquent;
5+
6+
class MysqlBook extends Eloquent {
7+
8+
protected $connection = 'mysql';
9+
protected $table = 'books';
10+
protected static $unguarded = true;
11+
protected $primaryKey = 'title';
12+
13+
public function author()
14+
{
15+
return $this->belongsTo('User', 'author_id');
16+
}
17+
18+
/**
19+
* Check if we need to run the schema
20+
* @return [type] [description]
21+
*/
22+
public static function executeSchema()
23+
{
24+
$schema = Schema::connection('mysql');
25+
26+
if (!$schema->hasTable('books'))
27+
{
28+
Schema::connection('mysql')->create('books', function($table)
29+
{
30+
$table->string('title');
31+
$table->string('author_id');
32+
$table->timestamps();
33+
});
34+
}
35+
}
36+
37+
}

tests/models/MysqlRole.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use \Illuminate\Support\Facades\Schema;
4+
use Jenssegers\Eloquent\Model as Eloquent;
5+
6+
class MysqlRole extends Eloquent {
7+
8+
protected $connection = 'mysql';
9+
protected $table = 'roles';
10+
protected static $unguarded = true;
11+
12+
public function user()
13+
{
14+
return $this->belongsTo('User');
15+
}
16+
17+
public function mysqlUser()
18+
{
19+
return $this->belongsTo('MysqlUser', 'role_id');
20+
}
21+
22+
/**
23+
* Check if we need to run the schema
24+
* @return [type] [description]
25+
*/
26+
public static function executeSchema()
27+
{
28+
$schema = Schema::connection('mysql');
29+
30+
if (!$schema->hasTable('roles'))
31+
{
32+
Schema::connection('mysql')->create('roles', function($table)
33+
{
34+
$table->string('type');
35+
$table->string('user_id');
36+
$table->timestamps();
37+
});
38+
}
39+
}
40+
41+
}

tests/models/MysqlUser.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,11 @@ public function books()
1414
return $this->hasMany('Book', 'author_id');
1515
}
1616

17-
public function items()
18-
{
19-
return $this->hasMany('Item');
20-
}
21-
2217
public function role()
2318
{
2419
return $this->hasOne('Role', 'role_id');
2520
}
2621

27-
public function clients()
28-
{
29-
return $this->belongsToMany('Client');
30-
}
31-
32-
public function groups()
33-
{
34-
return $this->belongsToMany('Group', null, 'users', 'groups');
35-
}
36-
3722
/**
3823
* Check if we need to run the schema
3924
* @return [type] [description]
@@ -51,22 +36,6 @@ public static function executeSchema()
5136
$table->timestamps();
5237
});
5338
}
54-
55-
/*if (!$schema->hasColumn('users', 'id'))
56-
{
57-
Schema::connection('mysql')->table('users', function($table)
58-
{
59-
$table->increments('id');
60-
});
61-
}
62-
63-
if (!$schema->hasColumn('users', 'name'))
64-
{
65-
Schema::connection('mysql')->table('users', function($table)
66-
{
67-
$table->string('name');
68-
});
69-
}*/
7039
}
7140

7241
}

tests/models/Role.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class Role extends Eloquent {
66

77
protected $collection = 'roles';
8-
98
protected static $unguarded = true;
109

1110
public function user()

0 commit comments

Comments
 (0)