2015-09-18 19:32:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Database\Tester\Models\CategoryNested;
|
|
|
|
|
|
2020-02-07 08:59:39 +00:00
|
|
|
class NestedTreeModelTest extends PluginTestCase
|
2015-09-18 19:32:49 +00:00
|
|
|
{
|
2019-06-12 16:22:20 +00:00
|
|
|
public function setUp() : void
|
2015-09-18 19:32:49 +00:00
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Category.php';
|
|
|
|
|
|
|
|
|
|
$this->runPluginRefreshCommand('Database.Tester');
|
|
|
|
|
$this->seedSampleTree();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 00:29:07 +00:00
|
|
|
public function testGetNested()
|
|
|
|
|
{
|
|
|
|
|
$items = CategoryNested::getNested();
|
|
|
|
|
|
|
|
|
|
// Eager loaded
|
2017-04-24 11:38:19 +00:00
|
|
|
$items->each(function ($item) {
|
2016-02-27 00:29:07 +00:00
|
|
|
$this->assertTrue($item->relationLoaded('children'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(2, $items->count());
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 19:32:49 +00:00
|
|
|
public function testGetAllRoot()
|
|
|
|
|
{
|
2016-02-27 00:29:07 +00:00
|
|
|
$items = CategoryNested::getAllRoot();
|
|
|
|
|
|
|
|
|
|
// Not eager loaded
|
2017-04-24 11:38:19 +00:00
|
|
|
$items->each(function ($item) {
|
2016-02-27 00:29:07 +00:00
|
|
|
$this->assertFalse($item->relationLoaded('children'));
|
|
|
|
|
});
|
|
|
|
|
|
2015-09-18 19:32:49 +00:00
|
|
|
$this->assertEquals(2, $items->count());
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-28 03:12:50 +00:00
|
|
|
public function testListsNested()
|
|
|
|
|
{
|
|
|
|
|
$array = CategoryNested::listsNested('name', 'id');
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
1 => 'Category Orange',
|
|
|
|
|
2 => ' Autumn Leaves',
|
|
|
|
|
3 => ' September',
|
|
|
|
|
4 => ' October',
|
|
|
|
|
5 => ' November',
|
|
|
|
|
6 => ' Summer Breeze',
|
|
|
|
|
7 => 'Category Green',
|
|
|
|
|
8 => ' Winter Snow',
|
|
|
|
|
9 => ' Spring Trees'
|
|
|
|
|
], $array);
|
|
|
|
|
|
|
|
|
|
$array = CategoryNested::listsNested('name', 'id', '--');
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
1 => 'Category Orange',
|
|
|
|
|
2 => '--Autumn Leaves',
|
|
|
|
|
3 => '----September',
|
|
|
|
|
4 => '----October',
|
|
|
|
|
5 => '----November',
|
|
|
|
|
6 => '--Summer Breeze',
|
|
|
|
|
7 => 'Category Green',
|
|
|
|
|
8 => '--Winter Snow',
|
|
|
|
|
9 => '--Spring Trees'
|
|
|
|
|
], $array);
|
|
|
|
|
|
|
|
|
|
$array = CategoryNested::listsNested('description', 'name', '**');
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'Category Orange' => 'A root level test category',
|
|
|
|
|
'Autumn Leaves' => '**Disccusion about the season of falling leaves.',
|
|
|
|
|
'September' => '****The start of the fall season.',
|
|
|
|
|
'October' => '****The middle of the fall season.',
|
|
|
|
|
'November' => '****The end of the fall season.',
|
|
|
|
|
'Summer Breeze' => '**Disccusion about the wind at the ocean.',
|
|
|
|
|
'Category Green' => 'A root level test category',
|
|
|
|
|
'Winter Snow' => '**Disccusion about the frosty snow flakes.',
|
|
|
|
|
'Spring Trees' => '**Disccusion about the blooming gardens.'
|
|
|
|
|
], $array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testListsNestedFromCollection()
|
|
|
|
|
{
|
|
|
|
|
$array = CategoryNested::get()->listsNested('custom_name', 'id', '...');
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
1 => 'Category Orange (#1)',
|
|
|
|
|
2 => '...Autumn Leaves (#2)',
|
|
|
|
|
3 => '......September (#3)',
|
|
|
|
|
4 => '......October (#4)',
|
|
|
|
|
5 => '......November (#5)',
|
|
|
|
|
6 => '...Summer Breeze (#6)',
|
|
|
|
|
7 => 'Category Green (#7)',
|
|
|
|
|
8 => '...Winter Snow (#8)',
|
|
|
|
|
9 => '...Spring Trees (#9)'
|
|
|
|
|
], $array);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-18 19:32:49 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|