Commit ac7174ae authored by Jens Segers's avatar Jens Segers

Adding some more tests for mysql relations

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