From 475f0d0889a311ecc840ccaae323e7be59317193 Mon Sep 17 00:00:00 2001 From: Tiago Araujo Date: Tue, 3 Sep 2019 16:58:25 +0200 Subject: [PATCH] Added collection client --- README.md | 4 +- src/Clients/CollectionsClient.php | 89 +++++++++++++++++++++++++ src/Clients/IpAddressesClient.php | 1 - tests/CollectionsTest.php | 64 ++++++++++++++++++ tests/mocks/collection-create-item.json | 6 ++ tests/mocks/collection-show-1.json | 19 ++++++ tests/mocks/collection-show-2.json | 26 ++++++++ tests/mocks/collection-show-item.json | 6 ++ tests/mocks/collection-update-item.json | 6 ++ tests/objects/CollectionItem.php | 61 +++++++++++++++++ tests/objects/CollectionShow1.php | 74 ++++++++++++++++++++ tests/objects/CollectionShow2.php | 87 ++++++++++++++++++++++++ 12 files changed, 440 insertions(+), 3 deletions(-) create mode 100644 src/Clients/CollectionsClient.php create mode 100644 tests/CollectionsTest.php create mode 100644 tests/mocks/collection-create-item.json create mode 100644 tests/mocks/collection-show-1.json create mode 100644 tests/mocks/collection-show-2.json create mode 100644 tests/mocks/collection-show-item.json create mode 100644 tests/mocks/collection-update-item.json create mode 100644 tests/objects/CollectionItem.php create mode 100644 tests/objects/CollectionShow1.php create mode 100644 tests/objects/CollectionShow2.php diff --git a/README.md b/README.md index 323e2d6..b00fba2 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ $config = new \NStack\Config('APP_ID', 'REST_KEY'); $nstack = new \NStack\NStack($config); ``` -## Features +## 💡 Features [x] Geographic continent [x] Geographic countries @@ -42,7 +42,7 @@ $nstack = new \NStack\NStack($config); [x] Content Localize languages [x] Content Localize proposals [x] Content Files - [ ] Content Collections + [x] Content Collections [ ] Notify updates [ ] UGC pushlogs [ ] Valitors diff --git a/src/Clients/CollectionsClient.php b/src/Clients/CollectionsClient.php new file mode 100644 index 0000000..3d8b6a7 --- /dev/null +++ b/src/Clients/CollectionsClient.php @@ -0,0 +1,89 @@ + + */ +class CollectionsClient extends NStackClient +{ + /** @var string */ + protected $path = 'content/collections'; + + /** + * view + * + * @param int $collectionId + * @return array + */ + public function view(int $collectionId): array + { + $response = $this->client->get($this->buildPath($this->path . '/' . $collectionId)); + $contents = $response->getBody()->getContents(); + return json_decode($contents, true)['data']; + } + + /** + * createItem + * + * @param int $collectionId + * @param array $params + * @return array + */ + public function createItem(int $collectionId, array $params): array + { + $response = $this->client->post($this->buildPath($this->path . '/' . $collectionId . '/items'), [ + 'form_params' => $params + ]); + $contents = $response->getBody()->getContents(); + return json_decode($contents, true)['data']; + } + + /** + * deleteItem + * + * @param int $collectionId + * @param int $itemId + */ + public function deleteItem(int $collectionId, int $itemId) + { + $this->client->delete($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId)); + } + + /** + * updateItem + * + * @param int $collectionId + * @param int $itemId + * @param array $params + * @return array + */ + public function updateItem(int $collectionId, int $itemId, array $params): array + { + $response = $this->client->post($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId . '/update'), [ + 'form_params' => $params + ]); + $contents = $response->getBody()->getContents(); + return json_decode($contents, true)['data']; + } + + /** + * viewItem + * + * @param int $collectionId + * @param int $itemId + * @return array + */ + public function viewItem(int $collectionId, int $itemId): array + { + $response = $this->client->get($this->buildPath($this->path . '/' . $collectionId . '/items/' . $itemId)); + $contents = $response->getBody()->getContents(); + return json_decode($contents, true)['data']; + } +} \ No newline at end of file diff --git a/src/Clients/IpAddressesClient.php b/src/Clients/IpAddressesClient.php index 0fc7455..b5fd781 100644 --- a/src/Clients/IpAddressesClient.php +++ b/src/Clients/IpAddressesClient.php @@ -34,7 +34,6 @@ public function index(): IpAddress /** * show * - * * @param String $ip * @return IpAddress * @throws FailedToParseException diff --git a/tests/CollectionsTest.php b/tests/CollectionsTest.php new file mode 100644 index 0000000..7ee7db0 --- /dev/null +++ b/tests/CollectionsTest.php @@ -0,0 +1,64 @@ +getClientWithMockedGet('collection-show-1.json'); + $client = new CollectionsClient($this->getConfig(), $client); + $entry = $client->view(10); + foreach ($entry as $item) { + $this->assertInstanceOf(CollectionShow1::class, new CollectionShow1($item)); + } + + $client = $this->getClientWithMockedGet('collection-show-2.json'); + $client = new CollectionsClient($this->getConfig(), $client); + $entry = $client->view(10); + foreach ($entry as $item) { + $this->assertInstanceOf(CollectionShow2::class, new CollectionShow2($item)); + } + } + + public function testCreateItem() + { + $client = $this->getClientWithMockedPost('collection-create-item.json'); + $client = new CollectionsClient($this->getConfig(), $client); + $entry = $client->createItem(10, ["id" => 39, "key" => "41"]); + $this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry)); + } + + public function testDeleteItem() + { + $mock = $this->getMockBuilder('CollectionsClient') + ->setMethods(array('delete')) + ->getMock(); + + $mock->expects($this->once()) + ->method('delete'); + + $mock->delete(10, 15); + } + + public function testUpdateItem() + { + $client = $this->getClientWithMockedPost('collection-update-item.json'); + $client = new CollectionsClient($this->getConfig(), $client); + $entry = $client->updateItem(10, 315, ["id" => 39, "key" => "50"]); + $this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry)); + } + + public function testViewItem() + { + $client = $this->getClientWithMockedGet('collection-show-item.json'); + $client = new CollectionsClient($this->getConfig(), $client); + $entry = $client->viewItem(39, 315); + $this->assertInstanceOf(CollectionItem::class, new CollectionItem($entry)); + } +} diff --git a/tests/mocks/collection-create-item.json b/tests/mocks/collection-create-item.json new file mode 100644 index 0000000..c74f9a7 --- /dev/null +++ b/tests/mocks/collection-create-item.json @@ -0,0 +1,6 @@ +{ + "data": { + "id": 315, + "key": 41 + } +} \ No newline at end of file diff --git a/tests/mocks/collection-show-1.json b/tests/mocks/collection-show-1.json new file mode 100644 index 0000000..d684994 --- /dev/null +++ b/tests/mocks/collection-show-1.json @@ -0,0 +1,19 @@ +{ + "data": [ + { + "id": 10, + "text": "1", + "image": "http://d1gwekl0pol55k.cloudfront.net/nstack/images/original/collections/Pastedimageat2016_09_2204_38PM_M1HbuXPagn.png" + } + ], + "meta": { + "pagination": { + "total": 1, + "count": 1, + "per_page": 20, + "current_page": 1, + "total_pages": 1, + "links": {} + } + } +} \ No newline at end of file diff --git a/tests/mocks/collection-show-2.json b/tests/mocks/collection-show-2.json new file mode 100644 index 0000000..c8ad784 --- /dev/null +++ b/tests/mocks/collection-show-2.json @@ -0,0 +1,26 @@ +{ + "data": [ + { + "id": 309, + "Title": "Bread", + "Description": "A delicious loaf of rye bread", + "Price": 5 + }, + { + "id": 310, + "Title": "Water", + "Description": "A 1l bottle of spring water", + "Price": 2 + } + ], + "meta": { + "pagination": { + "total": 2, + "count": 2, + "per_page": 20, + "current_page": 1, + "total_pages": 1, + "links": {} + } + } +} \ No newline at end of file diff --git a/tests/mocks/collection-show-item.json b/tests/mocks/collection-show-item.json new file mode 100644 index 0000000..98b7eaa --- /dev/null +++ b/tests/mocks/collection-show-item.json @@ -0,0 +1,6 @@ +{ + "data": { + "id": 315, + "key": 1515151 + } +} \ No newline at end of file diff --git a/tests/mocks/collection-update-item.json b/tests/mocks/collection-update-item.json new file mode 100644 index 0000000..98b7eaa --- /dev/null +++ b/tests/mocks/collection-update-item.json @@ -0,0 +1,6 @@ +{ + "data": { + "id": 315, + "key": 1515151 + } +} \ No newline at end of file diff --git a/tests/objects/CollectionItem.php b/tests/objects/CollectionItem.php new file mode 100644 index 0000000..c00851b --- /dev/null +++ b/tests/objects/CollectionItem.php @@ -0,0 +1,61 @@ + + */ +class CollectionItem extends Model +{ + /** @var int */ + protected $id; + + /** @var string */ + protected $key; + + /** + * parse + * + * @param array $data + */ + public function parse(array $data) + { + $this->id = (int)$data['id']; + $this->key = (string)$data['key']; + } + + /** + * toArray + * + * @return array + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'key' => $this->key, + ]; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return string + */ + public function getKey(): string + { + return $this->key; + } + +} \ No newline at end of file diff --git a/tests/objects/CollectionShow1.php b/tests/objects/CollectionShow1.php new file mode 100644 index 0000000..9cafb01 --- /dev/null +++ b/tests/objects/CollectionShow1.php @@ -0,0 +1,74 @@ + + */ +class CollectionShow1 extends Model +{ + /** @var int */ + protected $id; + + /** @var string */ + protected $text; + + /** @var string */ + protected $image; + + /** + * parse + * + * @param array $data + */ + public function parse(array $data) + { + $this->id = (int)$data['id']; + $this->text = (string)$data['text']; + $this->image = (string)$data['image']; + } + + /** + * toArray + * + * @return array + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'text' => $this->text, + 'image' => $this->image, + ]; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return string + */ + public function getText(): string + { + return $this->text; + } + + /** + * @return string + */ + public function getImage(): string + { + return $this->image; + } + +} \ No newline at end of file diff --git a/tests/objects/CollectionShow2.php b/tests/objects/CollectionShow2.php new file mode 100644 index 0000000..82c9e1f --- /dev/null +++ b/tests/objects/CollectionShow2.php @@ -0,0 +1,87 @@ + + */ +class CollectionShow2 extends Model +{ + /** @var int */ + protected $id; + + /** @var string */ + protected $title; + + /** @var string */ + protected $description; + + /** @var float */ + protected $price; + + /** + * parse + * + * @param array $data + */ + public function parse(array $data) + { + $this->id = (int)$data['id']; + $this->title = (string)$data['Title']; + $this->description = (string)$data['Description']; + $this->price = (string)$data['Price']; + } + + /** + * toArray + * + * @return array + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'Title' => $this->title, + 'Description' => $this->description, + 'Price' => $this->price, + ]; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @return float + */ + public function getPrice(): float + { + return $this->price; + } + +} \ No newline at end of file