Prepare tests for BelongsToMany relation type
This commit is contained in:
parent
a30fc006bb
commit
e5aa1d651c
|
|
@ -29,4 +29,8 @@ class Author extends Model
|
|||
'phone' => 'Database\Tester\Models\Phone',
|
||||
];
|
||||
|
||||
public $belongsToMany = [
|
||||
'roles' => ['Database\Tester\Models\Role', 'table' => 'database_tester_authors_roles']
|
||||
];
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php namespace Database\Tester\Models;
|
||||
|
||||
use Model;
|
||||
|
||||
/**
|
||||
* Role Model
|
||||
*/
|
||||
class Role extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string The database table used by the model.
|
||||
*/
|
||||
public $table = 'database_tester_roles';
|
||||
|
||||
/**
|
||||
* @var array Guarded fields
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* @var array Fillable fields
|
||||
*/
|
||||
protected $fillable = [];
|
||||
|
||||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $belongsToMany = [
|
||||
'authors' => ['Database\Tester\Models\User', 'table' => 'database_tester_authors_roles']
|
||||
];
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php namespace Database\Tester\Updates;
|
||||
|
||||
use Schema;
|
||||
use October\Rain\Database\Updates\Migration;
|
||||
|
||||
class CreateRolesTable extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('database_tester_roles', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->string('name')->nullable();
|
||||
$table->text('description')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('database_tester_authors_roles', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->integer('author_id')->unsigned();
|
||||
$table->integer('role_id')->unsigned();
|
||||
$table->primary(['author_id', 'role_id']);
|
||||
$table->string('clearance_level')->nullable();
|
||||
$table->boolean('is_executive')->default(false);
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('database_tester_roles');
|
||||
Schema::dropIfExists('database_tester_authors_roles');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,3 +5,4 @@
|
|||
- create_authors_table.php
|
||||
- create_phones_table.php
|
||||
- create_categories_table.php
|
||||
- create_roles_table.php
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Database\Tester\Models\Role;
|
||||
use Database\Tester\Models\Author;
|
||||
|
||||
class BelongsToManyModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Role.php';
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
}
|
||||
|
||||
public function testSetRelationValue()
|
||||
{
|
||||
Model::unguard();
|
||||
$author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$role1 = Role::create(['name' => "Designer", 'description' => "Quality"]);
|
||||
$role2 = Role::create(['name' => "Programmer", 'description' => "Speed"]);
|
||||
$role3 = Role::create(['name' => "Manager", 'description' => "Budget"]);
|
||||
Model::reguard();
|
||||
|
||||
// Add to collection
|
||||
$this->assertFalse($author->roles->contains($role1));
|
||||
$author->roles()->add($role1);
|
||||
|
||||
// TODO: Should this occur as part of add() above?
|
||||
$author->roles->push($role1);
|
||||
|
||||
$this->assertTrue($author->roles->contains($role1));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue