From d9090b3210d1988a3995b109b0ec509674d7d984 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 19 Sep 2015 05:32:49 +1000 Subject: [PATCH] Write tests for Tree traits --- .../database/tester/models/Category.php | 27 ++++++ .../updates/create_categories_table.php | 49 ++++++++++ .../database/tester/updates/version.yaml | 1 + .../plugins/database/NestedTreeModelTest.php | 75 +++++++++++++++ .../plugins/database/SimpleTreeModelTest.php | 94 +++++++++++++++++++ 5 files changed, 246 insertions(+) create mode 100644 tests/fixtures/plugins/database/tester/models/Category.php create mode 100644 tests/fixtures/plugins/database/tester/updates/create_categories_table.php create mode 100644 tests/unit/plugins/database/NestedTreeModelTest.php create mode 100644 tests/unit/plugins/database/SimpleTreeModelTest.php diff --git a/tests/fixtures/plugins/database/tester/models/Category.php b/tests/fixtures/plugins/database/tester/models/Category.php new file mode 100644 index 000000000..583f1f27b --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/Category.php @@ -0,0 +1,27 @@ +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'); + } + +} diff --git a/tests/fixtures/plugins/database/tester/updates/version.yaml b/tests/fixtures/plugins/database/tester/updates/version.yaml index 519509f0c..2fc4b2813 100644 --- a/tests/fixtures/plugins/database/tester/updates/version.yaml +++ b/tests/fixtures/plugins/database/tester/updates/version.yaml @@ -3,3 +3,4 @@ - Create tables - create_posts_table.php - create_authors_table.php + - create_categories_table.php diff --git a/tests/unit/plugins/database/NestedTreeModelTest.php b/tests/unit/plugins/database/NestedTreeModelTest.php new file mode 100644 index 000000000..11270de60 --- /dev/null +++ b/tests/unit/plugins/database/NestedTreeModelTest.php @@ -0,0 +1,75 @@ +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(); + } +} diff --git a/tests/unit/plugins/database/SimpleTreeModelTest.php b/tests/unit/plugins/database/SimpleTreeModelTest.php new file mode 100644 index 000000000..f49964032 --- /dev/null +++ b/tests/unit/plugins/database/SimpleTreeModelTest.php @@ -0,0 +1,94 @@ +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(); + } +}