From 4e1d2ca445d53756d761b267d13b07a79a0d48b9 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 28 Nov 2015 14:12:50 +1100 Subject: [PATCH] Write tests for listsNested on tree traits --- .../database/tester/models/Category.php | 5 + .../plugins/database/NestedTreeModelTest.php | 67 +++++++++++ .../plugins/database/SimpleTreeModelTest.php | 109 ++++++++++++++++++ 3 files changed, 181 insertions(+) diff --git a/tests/fixtures/plugins/database/tester/models/Category.php b/tests/fixtures/plugins/database/tester/models/Category.php index 583f1f27b..f839280a5 100644 --- a/tests/fixtures/plugins/database/tester/models/Category.php +++ b/tests/fixtures/plugins/database/tester/models/Category.php @@ -8,6 +8,11 @@ class Category extends Model * @var string The database table used by the model. */ public $table = 'database_tester_categories'; + + public function getCustomNameAttribute() + { + return $this->name.' (#'.$this->id.')'; + } } diff --git a/tests/unit/plugins/database/NestedTreeModelTest.php b/tests/unit/plugins/database/NestedTreeModelTest.php index 11270de60..e8218cf73 100644 --- a/tests/unit/plugins/database/NestedTreeModelTest.php +++ b/tests/unit/plugins/database/NestedTreeModelTest.php @@ -21,6 +21,73 @@ class NestedTreeModelTest extends PluginTestCase $this->assertEquals(2, $items->count()); } + 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); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Column mismatch in listsNested method + */ + public function testListsNestedUnknownColumn() + { + CategoryNested::listsNested('custom_name', 'id'); + } + + 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); + } + public function seedSampleTree() { Model::unguard(); diff --git a/tests/unit/plugins/database/SimpleTreeModelTest.php b/tests/unit/plugins/database/SimpleTreeModelTest.php index f49964032..2535c73d0 100644 --- a/tests/unit/plugins/database/SimpleTreeModelTest.php +++ b/tests/unit/plugins/database/SimpleTreeModelTest.php @@ -54,6 +54,115 @@ class SimpleTreeModelTest extends PluginTestCase $this->assertEquals(9, $item->getAllChildren()->count()); } + public function testListsNested() + { + $array = CategorySimple::listsNested('name', 'id'); + $this->assertEquals([ + 1 => 'Web development', + 2 => '   HTML5', + 3 => '   CSS3', + 4 => '   jQuery', + 5 => '   Bootstrap', + 6 => '   Laravel', + 7 => '   OctoberCMS', + 8 => '      September', + 9 => '      October', + 10 => '      November', + 11 => 'Mobile development', + 12 => '   iOS', + 13 => '   iPhone', + 14 => '   iPad', + 15 => '   Android', + 16 => 'Graphic design', + 17 => '   Photoshop', + 18 => '   Illustrator', + 19 => '   Fireworks' + ], $array); + + $array = CategorySimple::listsNested('name', 'id', '--'); + $this->assertEquals([ + 1 => 'Web development', + 2 => '--HTML5', + 3 => '--CSS3', + 4 => '--jQuery', + 5 => '--Bootstrap', + 6 => '--Laravel', + 7 => '--OctoberCMS', + 8 => '----September', + 9 => '----October', + 10 => '----November', + 11 => 'Mobile development', + 12 => '--iOS', + 13 => '--iPhone', + 14 => '--iPad', + 15 => '--Android', + 16 => 'Graphic design', + 17 => '--Photoshop', + 18 => '--Illustrator', + 19 => '--Fireworks' + ], $array); + + $array = CategorySimple::listsNested('id', 'name', '**'); + $this->assertEquals([ + 'Web development' => '1', + 'HTML5' => '**2', + 'CSS3' => '**3', + 'jQuery' => '**4', + 'Bootstrap' => '**5', + 'Laravel' => '**6', + 'OctoberCMS' => '**7', + 'September' => '****8', + 'October' => '****9', + 'November' => '****10', + 'Mobile development' => '11', + 'iOS' => '**12', + 'iPhone' => '**13', + 'iPad' => '**14', + 'Android' => '**15', + 'Graphic design' => '16', + 'Photoshop' => '**17', + 'Illustrator' => '**18', + 'Fireworks' => '**19' + ], $array); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Column mismatch in listsNested method + */ + public function testListsNestedUnknownColumn() + { + CategorySimple::listsNested('custom_name', 'id'); + } + + public function testListsNestedFromCollection() + { + $array = CategorySimple::get()->listsNested('custom_name', 'id', '...'); + + $this->assertEquals([ + 1 => 'Web development (#1)', + 2 => '...HTML5 (#2)', + 3 => '...CSS3 (#3)', + 4 => '...jQuery (#4)', + 5 => '...Bootstrap (#5)', + 6 => '...Laravel (#6)', + 7 => '...OctoberCMS (#7)', + 8 => '......September (#8)', + 9 => '......October (#9)', + 10 => '......November (#10)', + 11 => 'Mobile development (#11)', + 12 => '...iOS (#12)', + 13 => '...iPhone (#13)', + 14 => '...iPad (#14)', + 15 => '...Android (#15)', + 16 => 'Graphic design (#16)', + 17 => '...Photoshop (#17)', + 18 => '...Illustrator (#18)', + 19 => '...Fireworks (#19)' + ], $array); + } + + public function seedSampleTree() { Model::unguard();