Skip to content

Commit

Permalink
Merge pull request #12 from nstack-io/develop
Browse files Browse the repository at this point in the history
Develop -> Master
  • Loading branch information
Casperhr authored Sep 3, 2019
2 parents 556562e + 799ba84 commit 8743ff2
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
89 changes: 89 additions & 0 deletions src/Clients/CollectionsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace NStack\Clients;

use NStack\Exceptions\FailedToParseException;
use NStack\Models\IpAddress;

/**
* Class CollectionsClient
*
* @package NStack\Clients
* @author Tiago Araujo <[email protected]>
*/
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'];
}
}
1 change: 0 additions & 1 deletion src/Clients/IpAddressesClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function index(): IpAddress
/**
* show
*
*
* @param String $ip
* @return IpAddress
* @throws FailedToParseException
Expand Down
64 changes: 64 additions & 0 deletions tests/CollectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace NStack\Tests;

use NStack\Clients\CollectionsClient;
use NStack\Tests\objects\CollectionItem;
use NStack\Tests\objects\CollectionShow1;
use NStack\Tests\objects\CollectionShow2;

class CollectionsTest extends TestCase
{
public function testView()
{
$client = $this->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));
}
}
6 changes: 6 additions & 0 deletions tests/mocks/collection-create-item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"id": 315,
"key": 41
}
}
19 changes: 19 additions & 0 deletions tests/mocks/collection-show-1.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
}
26 changes: 26 additions & 0 deletions tests/mocks/collection-show-2.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
}
6 changes: 6 additions & 0 deletions tests/mocks/collection-show-item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"id": 315,
"key": 1515151
}
}
6 changes: 6 additions & 0 deletions tests/mocks/collection-update-item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"id": 315,
"key": 1515151
}
}
61 changes: 61 additions & 0 deletions tests/objects/CollectionItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace NStack\Tests\objects;

use NStack\Models\Model;

/**
* Class CollectionItem
*
* @package NStack\Tests\objects
* @author Tiago Araujo <[email protected]>
*/
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;
}

}
74 changes: 74 additions & 0 deletions tests/objects/CollectionShow1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace NStack\Tests\objects;

use NStack\Models\Model;

/**
* Class CollectionShow1
*
* @package NStack\Tests\objects
* @author Tiago Araujo <[email protected]>
*/
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;
}

}
Loading

0 comments on commit 8743ff2

Please sign in to comment.