Write tests for Tree traits
This commit is contained in:
parent
b5611c3ed1
commit
d9090b3210
|
|
@ -0,0 +1,27 @@
|
|||
<?php namespace Database\Tester\Models;
|
||||
|
||||
use Model;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
/**
|
||||
* @var string The database table used by the model.
|
||||
*/
|
||||
public $table = 'database_tester_categories';
|
||||
}
|
||||
|
||||
|
||||
class CategorySimple extends Category
|
||||
{
|
||||
use \October\Rain\Database\Traits\SimpleTree;
|
||||
}
|
||||
|
||||
class CategoryNested extends Category
|
||||
{
|
||||
use \October\Rain\Database\Traits\NestedTree;
|
||||
|
||||
/**
|
||||
* @var string The database table used by the model.
|
||||
*/
|
||||
public $table = 'database_tester_categories_nested';
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php namespace Database\Tester\Updates;
|
||||
|
||||
use Schema;
|
||||
use October\Rain\Database\Updates\Migration;
|
||||
|
||||
class CreateCategoriesTable extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('database_tester_categories', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->integer('parent_id')->nullable();
|
||||
$table->string('name')->nullable();
|
||||
$table->string('slug')->nullable()->index()->unique();
|
||||
$table->string('description')->nullable();
|
||||
$table->integer('company_id')->unsigned()->nullable();
|
||||
$table->string('language', 3)->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
|
||||
Schema::create('database_tester_categories_nested', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->integer('parent_id')->nullable();
|
||||
$table->integer('nest_left')->nullable();
|
||||
$table->integer('nest_right')->nullable();
|
||||
$table->integer('nest_depth')->nullable();
|
||||
$table->string('name')->nullable();
|
||||
$table->string('slug')->nullable()->index()->unique();
|
||||
$table->string('description')->nullable();
|
||||
$table->integer('company_id')->unsigned()->nullable();
|
||||
$table->string('language', 3)->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('database_tester_categories');
|
||||
Schema::dropIfExists('database_tester_categories_nested');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -3,3 +3,4 @@
|
|||
- Create tables
|
||||
- create_posts_table.php
|
||||
- create_authors_table.php
|
||||
- create_categories_table.php
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Database\Tester\Models\CategoryNested;
|
||||
|
||||
class NestedTreeModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Category.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
$this->seedSampleTree();
|
||||
}
|
||||
|
||||
public function testGetAllRoot()
|
||||
{
|
||||
$items = CategoryNested::make()->getAllRoot();
|
||||
$this->assertEquals(2, $items->count());
|
||||
}
|
||||
|
||||
public function seedSampleTree()
|
||||
{
|
||||
Model::unguard();
|
||||
|
||||
$orange = CategoryNested::create([
|
||||
'name' => 'Category Orange',
|
||||
'description' => 'A root level test category',
|
||||
]);
|
||||
|
||||
$autumn = $orange->children()->create([
|
||||
'name' => 'Autumn Leaves',
|
||||
'description' => 'Disccusion about the season of falling leaves.'
|
||||
]);
|
||||
|
||||
$autumn->children()->create([
|
||||
'name' => 'September',
|
||||
'description' => 'The start of the fall season.'
|
||||
]);
|
||||
|
||||
$october = $autumn->children()->create([
|
||||
'name' => 'October',
|
||||
'description' => 'The middle of the fall season.'
|
||||
]);
|
||||
|
||||
$autumn->children()->create([
|
||||
'name' => 'November',
|
||||
'description' => 'The end of the fall season.'
|
||||
]);
|
||||
|
||||
$orange->children()->create([
|
||||
'name' => 'Summer Breeze',
|
||||
'description' => 'Disccusion about the wind at the ocean.'
|
||||
]);
|
||||
|
||||
$green = CategoryNested::create([
|
||||
'name' => 'Category Green',
|
||||
'description' => 'A root level test category',
|
||||
]);
|
||||
|
||||
$green->children()->create([
|
||||
'name' => 'Winter Snow',
|
||||
'description' => 'Disccusion about the frosty snow flakes.'
|
||||
]);
|
||||
|
||||
$green->children()->create([
|
||||
'name' => 'Spring Trees',
|
||||
'description' => 'Disccusion about the blooming gardens.'
|
||||
]);
|
||||
|
||||
Model::reguard();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Database\Tester\Models\CategorySimple;
|
||||
|
||||
class SimpleTreeModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Category.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
$this->seedSampleTree();
|
||||
}
|
||||
|
||||
public function testGetChildren()
|
||||
{
|
||||
// Not eager loaded
|
||||
$item = CategorySimple::first();
|
||||
$this->assertEquals(6, $item->getChildren()->count());
|
||||
|
||||
// Eager loaded
|
||||
$item = CategorySimple::make()->getAllRoot()->first();
|
||||
$this->assertEquals(6, $item->getChildren()->count());
|
||||
}
|
||||
|
||||
public function testGetAllRoot()
|
||||
{
|
||||
$items = CategorySimple::make()->getAllRoot();
|
||||
$this->assertEquals(3, $items->count());
|
||||
}
|
||||
|
||||
public function testGetChildCount()
|
||||
{
|
||||
// Not eager loaded
|
||||
$item = CategorySimple::first();
|
||||
$this->assertEquals(9, $item->getChildCount());
|
||||
|
||||
// Eager loaded
|
||||
$item = CategorySimple::make()->getAllRoot()->first();
|
||||
$this->assertEquals(9, $item->getChildCount());
|
||||
}
|
||||
|
||||
public function testGetAllChildren()
|
||||
{
|
||||
// Not eager loaded
|
||||
$item = CategorySimple::first();
|
||||
$this->assertEquals(9, $item->getAllChildren()->count());
|
||||
|
||||
// Eager loaded
|
||||
$item = CategorySimple::make()->getAllRoot()->first();
|
||||
$this->assertEquals(9, $item->getAllChildren()->count());
|
||||
}
|
||||
|
||||
public function seedSampleTree()
|
||||
{
|
||||
Model::unguard();
|
||||
|
||||
$webdev = CategorySimple::create([
|
||||
'name' => 'Web development'
|
||||
]);
|
||||
|
||||
$webdev->children()->create(['name' => 'HTML5']);
|
||||
$webdev->children()->create(['name' => 'CSS3']);
|
||||
$webdev->children()->create(['name' => 'jQuery']);
|
||||
$webdev->children()->create(['name' => 'Bootstrap']);
|
||||
$webdev->children()->create(['name' => 'Laravel']);
|
||||
$october = $webdev->children()->create(['name' => 'OctoberCMS']);
|
||||
$october->children()->create(['name' => 'September']);
|
||||
$october->children()->create(['name' => 'October']);
|
||||
$october->children()->create(['name' => 'November']);
|
||||
|
||||
$mobdev = CategorySimple::create([
|
||||
'name' => 'Mobile development'
|
||||
]);
|
||||
|
||||
$mobdev->children()->create(['name' => 'iOS']);
|
||||
$mobdev->children()->create(['name' => 'iPhone']);
|
||||
$mobdev->children()->create(['name' => 'iPad']);
|
||||
$mobdev->children()->create(['name' => 'Android']);
|
||||
|
||||
$design = CategorySimple::create([
|
||||
'name' => 'Graphic design'
|
||||
]);
|
||||
|
||||
$design->children()->create(['name' => 'Photoshop']);
|
||||
$design->children()->create(['name' => 'Illustrator']);
|
||||
$design->children()->create(['name' => 'Fireworks']);
|
||||
|
||||
Model::reguard();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue