-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nstack-io/develop
Develop -> Master
- Loading branch information
Showing
12 changed files
with
440 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"data": { | ||
"id": 315, | ||
"key": 41 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"data": { | ||
"id": 315, | ||
"key": 1515151 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"data": { | ||
"id": 315, | ||
"key": 1515151 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.