Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ $nstack = new \NStack\NStack($config);
[x] Geographic continent
[x] Geographic countries
[x] Geographic languages
[ ] Geographic Timezone
[x] Geographic Timezone
[x] Geographic Timezones
[x] Geographic Ip addresses
[x] Content Localize resources
[x] Content Localize languages
[ ] Content Localize proposals
[x] Content Localize proposals
[ ] Content Files
[ ] Content Collections
[ ] Notify updates
Expand Down
89 changes: 89 additions & 0 deletions src/Clients/ProposalsClient.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;
use NStack\Models\Proposal;
use NStack\Models\ProposalDeleted;

/**
* Class ProposalsClient
*
* @package NStack\Clients
* @author Tiago Araujo <tiar@nodesagency.com>
*/
class ProposalsClient extends NStackClient
{
/** @var string */
protected $path = 'localize/proposals';

/**
* index
*
* @param String|null $guid
* @return array
* @throws FailedToParseException
*/
public function index(String $guid = null): array
{
$response = $this->client->get($this->buildPath($this->path . '?guid=' . $guid));
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);

$array = [];
foreach ($data['data'] as $object) {
$array[] = new Proposal($object);
}

return $array;
}

/**
* store
*
* @param String|null $guid
* @param String $key
* @param String $value
* @param String $platform
* @param String $locale
* @param String $section
* @return Proposal
* @throws FailedToParseException
*/
public function store(
String $guid,
String $key,
String $value,
String $platform,
String $locale,
String $section
)
{
$response = $this->client->post($this->buildPath($this->path), [
'form_params' => [
'key' => $key,
'value' => $value,
'locale' => $locale,
'platform' => $platform,
'guid' => $guid,
'section' => $section,
]
]);
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);
return new Proposal($data['data']);
}

/**
* delete
*
* @param int $id
* @param String $guid
*/
public function delete(int $id, String $guid)
{
$this->client->delete($this->buildPath($this->path . '/' . $id . '?guid=' . $guid));
}

}
4 changes: 1 addition & 3 deletions src/Clients/TimezoneClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public function show($id): Timezone
*/
public function showByLatLng(float $lat, float $lng): Timezone
{
$response = $this->client->get(
$this->buildPath($this->path . '/by_lat_lng?=lat_lng' . $lat . ',' . $lng)
);
$response = $this->client->get($this->buildPath($this->path . '/by_lat_lng?lat_lng=' . $lat . ',' . $lng));
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);
return new Timezone($data['data']);
Expand Down
68 changes: 68 additions & 0 deletions src/Models/Proposal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace NStack\Models;

/**
* Class Proposal
*
* @package NStack\Models
* @author Tiago Araujo <tiar@nodesagency.com>
*/
class Proposal extends Model
{
/** @var int */
protected $id;

/** @var int */
protected $applicationId;

/** @var string */
protected $key;

/** @var string */
protected $section;

/** @var string */
protected $locale;

/** @var string */
protected $value;

/** @var string */
protected $canDelete;

/**
* parse
*
* @param array $data
*/
public function parse(array $data)
{
$this->id = (int)$data['id'];
$this->applicationId = (int)$data['application_id'];
$this->key = (string)$data['key'];
$this->section = (string)$data['section'];
$this->locale = (string)$data['locale'];
$this->value = (string)$data['value'];
$this->canDelete = (string)$data['can_delete'];
}

/**
* toArray
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'application_id' => $this->applicationId,
'key' => $this->key,
'section' => $this->section,
'locale' => $this->locale,
'value' => $this->value,
'can_delete' => $this->canDelete,
];
}

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

namespace NStack\Tests;

use NStack\Clients\ProposalsClient;
use NStack\Models\Proposal;

class ProposalTest extends TestCase
{
public function testProposalIndex()
{
$client = $this->getClientWithMockedGet('proposals-index.json');

$client = new ProposalsClient($this->getConfig(), $client);
$list = $client->index('test');

foreach ($list as $item) {
$this->assertInstanceOf(Proposal::class, $item);
}
}

public function testProposalStore()
{
$client = $this->getClientWithMockedPost('proposals-store.json');

$client = new ProposalsClient($this->getConfig(), $client);
$object = $client->store(
'test',
'forceHeader',
'new Text',
'en-GB',
'mobile',
'versionControl'
);

$this->assertInstanceOf(Proposal::class, $object);
}

public function testProposalDelete()
{
$mock = $this->getMockBuilder('ProposalsClient')
->setMethods(array('delete'))
->getMock();

$mock->expects($this->once())
->method('delete');

$mock->delete(1, 'test');
}
}
36 changes: 36 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ protected function getClientWithMockedGet(string $filename): Client
return $guzzle;
}

/**
* getClientWithMockedPost
*
* @param string $filename
* @return Client
* @author Tiago Araujo <tiar@nodesagency.com>
*/
protected function getClientWithMockedPost(string $filename): Client
{
$response = new Response(200, ['Content-Type' => 'application/json'],
$this->getMockAsString($filename));

$guzzle = \Mockery::mock(Client::class);
$guzzle->shouldReceive('post')->once()->andReturn($response);

return $guzzle;
}

/**
* getClientWithMockedDelete
*
* @param string $filename
* @return Client
* @author Tiago Araujo <tiar@nodesagency.com>
*/
protected function getClientWithMockedDelete(string $filename): Client
{
$response = new Response(200, ['Content-Type' => 'application/json'],
$this->getMockAsString($filename));

$guzzle = \Mockery::mock(Client::class);
$guzzle->shouldReceive('delete')->once()->andReturn($response);

return $guzzle;
}

/**
* getMockAsString
*
Expand Down
3 changes: 3 additions & 0 deletions tests/mocks/proposals-delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "Proposal deleted"
}
13 changes: 13 additions & 0 deletions tests/mocks/proposals-index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": [
{
"id": 1,
"application_id": 31,
"key": "no",
"section": "default",
"locale": "en-GB",
"value": "test",
"can_delete": false
}
]
}
11 changes: 11 additions & 0 deletions tests/mocks/proposals-store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"id": 13,
"application_id": 1,
"key": "forceHeader",
"section": "versionControl",
"locale": "en-GB",
"value": "new text2",
"can_delete": true
}
}