From 2e070847062e5ae5e143999f60c3ce39a5421201 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 4 Jul 2020 17:50:55 +0200 Subject: [PATCH] Add parent_id in groups --- src/Api.php | 5 ++--- src/Group.php | 5 +++-- tests/Unit/GetGroupsTest.php | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Api.php b/src/Api.php index 21923a5..6c6f06f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -98,9 +98,8 @@ class Api { } public function groups($parentGroupId = null): Collection { - $parentGroupId = $parentGroupId ?: 'root'; - return collect($this->http()->get(self::$url.'/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.$parentGroupId)->json()['data'])->map(function($group) { - return Group::fromResponse($group); + return collect($this->http()->get(self::$url.'/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.($parentGroupId ?: 'root'))->json()['data'])->map(function($group) use ($parentGroupId) { + return Group::fromResponse($group, $parentGroupId); }); } diff --git a/src/Group.php b/src/Group.php index 5e619b2..ce56444 100644 --- a/src/Group.php +++ b/src/Group.php @@ -9,16 +9,17 @@ class Group implements Arrayable { public $name; public $id; - public static function fromResponse($response) { + public static function fromResponse($response, $parent) { $group = new self(); $group->name = $response['descriptor']; $group->id = $response['id']; + $group->parent_id = $parent; return $group; } public function toArray() { - return [ 'id' => $this->id, 'name' => $this->name ]; + return [ 'id' => $this->id, 'name' => $this->name, 'parent_id' => $this->parent_id ]; } public function subgroups() { diff --git a/tests/Unit/GetGroupsTest.php b/tests/Unit/GetGroupsTest.php index aca3c20..579a864 100644 --- a/tests/Unit/GetGroupsTest.php +++ b/tests/Unit/GetGroupsTest.php @@ -29,7 +29,7 @@ class GetGroupsTest extends TestCase Nami::login(); $this->assertEquals([ - ['id' => 100, 'name' => 'Group'] + ['id' => 100, 'name' => 'Group', 'parent_id' => null] ], Nami::groups()->toArray()); Http::assertSent(function($request) { @@ -70,10 +70,10 @@ class GetGroupsTest extends TestCase $subgroups = Nami::group(100)->subgroups(); $this->assertEquals([ - ['id' => 101300, 'name' => 'Siebengebirge'], - ['id' => 100900, 'name' => 'Sieg'], - ['id' => 100900, 'name' => 'Sieg'], - ['id' => 101000, 'name' => 'Voreifel'] + ['id' => 101300, 'parent_id' => 100, 'name' => 'Siebengebirge'], + ['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'], + ['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'], + ['id' => 101000, 'parent_id' => 100, 'name' => 'Voreifel'] ], $subgroups->toArray()); $subgroups->each(function($group) {