From 4b0b44d302a9849903ebb0e7e3a158d62dcd799d Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 17 Feb 2022 02:36:29 +0100 Subject: [PATCH] Add initialize groups Todo: Integrate this in the initializer --- app/Group.php | 8 +- app/Initialize/InitializeGroups.php | 27 ++++++ ..._011021_create_groups_parent_id_column.php | 33 +++++++ packages/laravel-nami | 2 +- tests/Unit/InitializeGroupsTest.php | 89 +++++++++++++++++++ 5 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 app/Initialize/InitializeGroups.php create mode 100644 database/migrations/2022_02_17_011021_create_groups_parent_id_column.php create mode 100644 tests/Unit/InitializeGroupsTest.php diff --git a/app/Group.php b/app/Group.php index f3e4bf47..f5ba2adb 100644 --- a/app/Group.php +++ b/app/Group.php @@ -4,16 +4,22 @@ namespace App; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class Group extends Model { use HasFactory; - public $fillable = ['nami_id', 'name']; + public $fillable = ['nami_id', 'name', 'parent_id']; public $timestamps = false; public static function nami(int $id): self { return static::firstWhere('nami_id', $id); } + + public function parent(): BelongsTo + { + return $this->belongsTo(static::class, 'parent_id'); + } } diff --git a/app/Initialize/InitializeGroups.php b/app/Initialize/InitializeGroups.php new file mode 100644 index 00000000..67e46aec --- /dev/null +++ b/app/Initialize/InitializeGroups.php @@ -0,0 +1,27 @@ +api = $api; + } + + public function handle(): void + { + $this->api->groups()->each(function($group) { + $parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]); + + $this->api->subgroupsOf($group->id)->each(function ($subgroup) use ($parent) { + Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]); + }); + }); + } +} diff --git a/database/migrations/2022_02_17_011021_create_groups_parent_id_column.php b/database/migrations/2022_02_17_011021_create_groups_parent_id_column.php new file mode 100644 index 00000000..edca1c7f --- /dev/null +++ b/database/migrations/2022_02_17_011021_create_groups_parent_id_column.php @@ -0,0 +1,33 @@ +foreignId('parent_id')->nullable()->constrained('groups'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('groups', function (Blueprint $table) { + $table->dropForeign(['group_id']); + $table->dropColumn('group_id'); + }); + } +} diff --git a/packages/laravel-nami b/packages/laravel-nami index 876e026f..829dd013 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 876e026fd0db17f694323e253ace58a6ffe90d87 +Subproject commit 829dd01358858c887dddaeadd6e4ddebd8f9fb0b diff --git a/tests/Unit/InitializeGroupsTest.php b/tests/Unit/InitializeGroupsTest.php new file mode 100644 index 00000000..8fc4a48f --- /dev/null +++ b/tests/Unit/InitializeGroupsTest.php @@ -0,0 +1,89 @@ +api = $this->createStub(Api::class); + } + + public function test_it_doesnt_initialize_groups_when_nothing_sent(): void + { + $this->api->method('groups')->willReturn(collect([])); + + $task = new InitializeGroups($this->api); + $task->handle(); + + $this->assertDatabaseCount('groups', 0); + } + + public function test_it_synchs_a_group_with_a_single_node_and_no_children(): void + { + $this->api->method('groups')->will($this->returnValueMap([ + [ + null, + collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]) + ] + ])); + $this->api->method('subgroupsOf')->willReturn(collect([])); + + (new InitializeGroups($this->api))->handle(); + + $this->assertDatabaseHas('groups', [ + 'nami_id' => 150, + 'name' => 'lorem', + 'parent_id' => null + ]); + } + + public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void + { + GroupModel::factory()->create(['nami_id' => 150]); + $this->api->method('groups')->will($this->returnValueMap([ + [ + null, + collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]) + ] + ])); + $this->api->method('subgroupsOf')->willReturn(collect([])); + + (new InitializeGroups($this->api))->handle(); + + $this->assertDatabaseCount('groups', 1); + } + + public function testItSynchsSubgroups(): void + { + GroupModel::factory()->create(['nami_id' => 150]); + $this->api->method('groups')->willReturn( + collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]) + ); + $this->api->method('subgroupsOf')->willReturn( + collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]) + ); + + (new InitializeGroups($this->api))->handle(); + + $this->assertDatabaseCount('groups', 2); + $subgroup = GroupModel::where('nami_id', 200)->firstOrFail(); + $this->assertEquals('subgroup', $subgroup->name); + $this->assertEquals(150, $subgroup->parent->nami_id); + } + +}