From 3d22d1c7ff62420b0d8279a811eca38c8d758fa6 Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Mon, 21 Mar 2022 10:24:18 +0100 Subject: [PATCH] ability to add & remove a chart category (#51) --- src/Charts/Charts.php | 15 +++++++++ tests/Charts/ManageCategoriesTest.php | 45 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/Charts/ManageCategoriesTest.php diff --git a/src/Charts/Charts.php b/src/Charts/Charts.php index 48086c0..358f31b 100644 --- a/src/Charts/Charts.php +++ b/src/Charts/Charts.php @@ -60,6 +60,21 @@ public function update(string $key, string $name = null, array $categories = nul $this->client->post('/charts/' . $key, ['json' => $request]); } + public function addCategory(string $key, CategoryRequestBuilder $category): void + { + $this->client->post('/charts/' . $key . '/categories', ['json' => $category]); + } + + /** + * @param string $key + * @param $categoryKey string|int + * @return void + */ + public function removeCategory(string $key, $categoryKey): void + { + $this->client->delete('/charts/' . $key . '/categories/' . $categoryKey); + } + public function retrieve(string $key): Chart { $res = $this->client->get('/charts/' . $key); diff --git a/tests/Charts/ManageCategoriesTest.php b/tests/Charts/ManageCategoriesTest.php new file mode 100644 index 0000000..044d379 --- /dev/null +++ b/tests/Charts/ManageCategoriesTest.php @@ -0,0 +1,45 @@ + 1, 'label' => 'Category 1', 'color' => '#aaaaaa', 'accessible' => true] + ]; + $chart = $this->seatsioClient->charts->create('aChart', null, $categories); + + $category2 = (new CategoryRequestBuilder())->setKey(2)->setLabel('Category 2')->setColor('blue'); + $this->seatsioClient->charts->addCategory($chart->key, $category2); + + $retrievedChart = $this->seatsioClient->charts->retrievePublishedVersion($chart->key); + self::assertEquals('aChart', $retrievedChart->name); + self::assertEquals([ + (object)['key' => 1, 'label' => 'Category 1', 'color' => '#aaaaaa', 'accessible' => true], + (object)['key' => 2, 'label' => 'Category 2', 'color' => 'blue', 'accessible' => false] + ], $retrievedChart->categories->list); + } + + public function testRemoveCategory() + { + $categories = [ + (object)['key' => 1, 'label' => 'Category 1', 'color' => '#aaaaaa', 'accessible' => true], + (object)['key' => 'cat2', 'label' => 'Category 2', 'color' => '#bbbbbb', 'accessible' => false] + ]; + $chart = $this->seatsioClient->charts->create('aChart', null, $categories); + + $this->seatsioClient->charts->removeCategory($chart->key, 1); + + $retrievedChart = $this->seatsioClient->charts->retrievePublishedVersion($chart->key); + self::assertEquals('aChart', $retrievedChart->name); + self::assertEquals([ + (object)['key' => 'cat2', 'label' => 'Category 2', 'color' => '#bbbbbb', 'accessible' => false] + ], $retrievedChart->categories->list); + } + +}