Commit ac7174ae authored by Jens Segers's avatar Jens Segers

Adding some more tests for mysql relations

parent 9559b298
...@@ -31,5 +31,8 @@ ...@@ -31,5 +31,8 @@
<directory>tests/ModelTest.php</directory> <directory>tests/ModelTest.php</directory>
<directory>tests/RelationsTest.php</directory> <directory>tests/RelationsTest.php</directory>
</testsuite> </testsuite>
<testsuite name="relations">
<directory>tests/RelationsTest.php</directory>
</testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>
...@@ -32,7 +32,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -32,7 +32,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$items = $user->items; $items = $user->items;
$this->assertEquals(3, count($items)); $this->assertEquals(3, count($items));
} }
public function testBelongsTo() public function testBelongsTo()
{ {
...@@ -264,38 +264,66 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -264,38 +264,66 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
{ {
// A bit dirty // A bit dirty
MysqlUser::executeSchema(); MysqlUser::executeSchema();
MysqlBook::executeSchema();
MysqlRole::executeSchema();
$user = new MysqlUser; $user = new MysqlUser;
$this->assertInstanceOf('MysqlUser', $user); $this->assertInstanceOf('MysqlUser', $user);
$this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection()); $this->assertInstanceOf('Illuminate\Database\MySqlConnection', $user->getConnection());
// Mysql User
$user->name = "John Doe"; $user->name = "John Doe";
$user->save(); $user->save();
$this->assertTrue(is_int($user->id)); $this->assertTrue(is_int($user->id));
// Has many // SQL has many
$book = new Book(array('title' => 'Game of Thrones')); $book = new Book(array('title' => 'Game of Thrones'));
$user->books()->save($book); $user->books()->save($book);
$user = MysqlUser::find($user->id); // refetch $user = MysqlUser::find($user->id); // refetch
$this->assertEquals(1, count($user->books)); $this->assertEquals(1, count($user->books));
// MongoDB belongs to
$book = $user->books()->first(); // refetch $book = $user->books()->first(); // refetch
$this->assertEquals('John Doe', $book->mysqlAuthor->name); $this->assertEquals('John Doe', $book->mysqlAuthor->name);
// Has one // SQL has one
$role = new Role(array('type' => 'admin')); $role = new Role(array('type' => 'admin'));
$user->role()->save($role); $user->role()->save($role);
$user = MysqlUser::find($user->id); // refetch $user = MysqlUser::find($user->id); // refetch
$this->assertEquals('admin', $user->role->type); $this->assertEquals('admin', $user->role->type);
// MongoDB beelongs to
$role = $user->role()->first(); // refetch $role = $user->role()->first(); // refetch
$this->assertEquals('John Doe', $role->mysqlUser->name); $this->assertEquals('John Doe', $role->mysqlUser->name);
// belongsToMany DOES NOT WORK YET // MongoDB User
/*$client = new Client(array('name' => 'Pork Pies Ltd.')); $user = new User;
$user->clients()->save($client); $user->name = "John Doe";
$user = MysqlUser::find($user->id); // refetch $user->save();
$this->assertEquals(1, count($user->clients));*/
// MongoDB has many
$book = new MysqlBook(array('title' => 'Game of Thrones'));
$user->mysqlBooks()->save($book);
$user = User::find($user->_id); // refetch
$this->assertEquals(1, count($user->mysqlBooks));
// SQL belongs to
$book = $user->mysqlBooks()->first(); // refetch
$this->assertEquals('John Doe', $book->author->name);
// MongoDB has one
$role = new MysqlRole(array('type' => 'admin'));
$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);
// Dirty again :) // Dirty again :)
MysqlUser::truncate(); MysqlUser::truncate();
MysqlBook::truncate();
MysqlRole::truncate();
} }
} }
...@@ -5,9 +5,7 @@ use Jenssegers\Mongodb\Model as Eloquent; ...@@ -5,9 +5,7 @@ use Jenssegers\Mongodb\Model as Eloquent;
class Book extends Eloquent { class Book extends Eloquent {
protected $collection = 'books'; protected $collection = 'books';
protected static $unguarded = true; protected static $unguarded = true;
protected $primaryKey = 'title'; protected $primaryKey = 'title';
public function author() public function author()
......
<?php <?php
use Jenssegers\Mongodb\Model as Eloquent; use Jenssegers\Mongodb\Model as Eloquent;
class Client extends Eloquent { class Client extends Eloquent {
protected $collection = 'clients'; protected $collection = 'clients';
protected static $unguarded = true; protected static $unguarded = true;
public function users() public function users()
......
<?php <?php
use Jenssegers\Mongodb\Model as Eloquent; use Jenssegers\Mongodb\Model as Eloquent;
class Group extends Eloquent { class Group extends Eloquent {
protected $collection = 'groups'; protected $collection = 'groups';
protected static $unguarded = true; protected static $unguarded = true;
public function users() public function users()
......
...@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent; ...@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
class Item extends Eloquent { class Item extends Eloquent {
protected $collection = 'roles'; protected $collection = 'roles';
protected static $unguarded = true; protected static $unguarded = true;
public function user() public function user()
...@@ -18,4 +17,4 @@ class Item extends Eloquent { ...@@ -18,4 +17,4 @@ class Item extends Eloquent {
return $query->where('type', 'sharp'); return $query->where('type', 'sharp');
} }
} }
\ No newline at end of file
<?php
use \Illuminate\Support\Facades\Schema;
use Jenssegers\Eloquent\Model as Eloquent;
class MysqlBook extends Eloquent {
protected $connection = 'mysql';
protected $table = 'books';
protected static $unguarded = true;
protected $primaryKey = 'title';
public function author()
{
return $this->belongsTo('User', 'author_id');
}
/**
* Check if we need to run the schema
* @return [type] [description]
*/
public static function executeSchema()
{
$schema = Schema::connection('mysql');
if (!$schema->hasTable('books'))
{
Schema::connection('mysql')->create('books', function($table)
{
$table->string('title');
$table->string('author_id');
$table->timestamps();
});
}
}
}
<?php
use \Illuminate\Support\Facades\Schema;
use Jenssegers\Eloquent\Model as Eloquent;
class MysqlRole extends Eloquent {
protected $connection = 'mysql';
protected $table = 'roles';
protected static $unguarded = true;
public function user()
{
return $this->belongsTo('User');
}
public function mysqlUser()
{
return $this->belongsTo('MysqlUser', 'role_id');
}
/**
* Check if we need to run the schema
* @return [type] [description]
*/
public static function executeSchema()
{
$schema = Schema::connection('mysql');
if (!$schema->hasTable('roles'))
{
Schema::connection('mysql')->create('roles', function($table)
{
$table->string('type');
$table->string('user_id');
$table->timestamps();
});
}
}
}
...@@ -14,26 +14,11 @@ class MysqlUser extends Eloquent { ...@@ -14,26 +14,11 @@ class MysqlUser extends Eloquent {
return $this->hasMany('Book', 'author_id'); return $this->hasMany('Book', 'author_id');
} }
public function items()
{
return $this->hasMany('Item');
}
public function role() public function role()
{ {
return $this->hasOne('Role', 'role_id'); return $this->hasOne('Role', 'role_id');
} }
public function clients()
{
return $this->belongsToMany('Client');
}
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
}
/** /**
* Check if we need to run the schema * Check if we need to run the schema
* @return [type] [description] * @return [type] [description]
...@@ -51,22 +36,6 @@ class MysqlUser extends Eloquent { ...@@ -51,22 +36,6 @@ class MysqlUser extends Eloquent {
$table->timestamps(); $table->timestamps();
}); });
} }
/*if (!$schema->hasColumn('users', 'id'))
{
Schema::connection('mysql')->table('users', function($table)
{
$table->increments('id');
});
}
if (!$schema->hasColumn('users', 'name'))
{
Schema::connection('mysql')->table('users', function($table)
{
$table->string('name');
});
}*/
} }
} }
...@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent; ...@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
class Role extends Eloquent { class Role extends Eloquent {
protected $collection = 'roles'; protected $collection = 'roles';
protected static $unguarded = true; protected static $unguarded = true;
public function user() public function user()
......
...@@ -8,9 +8,7 @@ use Illuminate\Auth\Reminders\RemindableInterface; ...@@ -8,9 +8,7 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface { class User extends Eloquent implements UserInterface, RemindableInterface {
protected $collection = 'users'; protected $collection = 'users';
protected $dates = array('birthday'); protected $dates = array('birthday');
protected static $unguarded = true; protected static $unguarded = true;
public function books() public function books()
...@@ -18,6 +16,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -18,6 +16,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->hasMany('Book', 'author_id'); return $this->hasMany('Book', 'author_id');
} }
public function mysqlBooks()
{
return $this->hasMany('MysqlBook', 'author_id');
}
public function items() public function items()
{ {
return $this->hasMany('Item'); return $this->hasMany('Item');
...@@ -28,6 +31,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -28,6 +31,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->hasOne('Role'); return $this->hasOne('Role');
} }
public function mysqlRole()
{
return $this->hasOne('MysqlRole');
}
public function clients() public function clients()
{ {
return $this->belongsToMany('Client'); return $this->belongsToMany('Client');
......
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