From f38cf7c0039c9c5fef916d7956b7e1559edd31a5 Mon Sep 17 00:00:00 2001 From: Tiago Araujo Date: Mon, 2 Sep 2019 11:01:32 +0200 Subject: [PATCH 1/2] Fixed timezone url and readme --- README.md | 2 +- src/Clients/TimezoneClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef5b721..2d185b5 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ $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 diff --git a/src/Clients/TimezoneClient.php b/src/Clients/TimezoneClient.php index 256b166..563565a 100644 --- a/src/Clients/TimezoneClient.php +++ b/src/Clients/TimezoneClient.php @@ -62,7 +62,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) + $this->buildPath($this->path . '/by_lat_lng?lat_lng=' . $lat . ',' . $lng) ); $contents = $response->getBody()->getContents(); $data = json_decode($contents, true); From f089090d406509e6b7c3e40f3ac24d182b461d6e Mon Sep 17 00:00:00 2001 From: Tiago Araujo Date: Mon, 2 Sep 2019 14:28:32 +0200 Subject: [PATCH 2/2] Added proposal client --- README.md | 2 +- src/Clients/ProposalsClient.php | 89 +++++++++++++++++++++++++++++++ src/Models/Proposal.php | 68 +++++++++++++++++++++++ tests/ProposalTest.php | 50 +++++++++++++++++ tests/TestCase.php | 36 +++++++++++++ tests/mocks/proposals-delete.json | 3 ++ tests/mocks/proposals-index.json | 13 +++++ tests/mocks/proposals-store.json | 11 ++++ 8 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 src/Clients/ProposalsClient.php create mode 100644 src/Models/Proposal.php create mode 100644 tests/ProposalTest.php create mode 100644 tests/mocks/proposals-delete.json create mode 100644 tests/mocks/proposals-index.json create mode 100644 tests/mocks/proposals-store.json diff --git a/README.md b/README.md index ef5b721..e9f3fd1 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ $nstack = new \NStack\NStack($config); [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 diff --git a/src/Clients/ProposalsClient.php b/src/Clients/ProposalsClient.php new file mode 100644 index 0000000..f2cd96b --- /dev/null +++ b/src/Clients/ProposalsClient.php @@ -0,0 +1,89 @@ + + */ +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)); + } + +} \ No newline at end of file diff --git a/src/Models/Proposal.php b/src/Models/Proposal.php new file mode 100644 index 0000000..8a60dc9 --- /dev/null +++ b/src/Models/Proposal.php @@ -0,0 +1,68 @@ + + */ +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, + ]; + } + +} \ No newline at end of file diff --git a/tests/ProposalTest.php b/tests/ProposalTest.php new file mode 100644 index 0000000..db8ac76 --- /dev/null +++ b/tests/ProposalTest.php @@ -0,0 +1,50 @@ +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'); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index ea410ed..3a7310d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -40,6 +40,42 @@ protected function getClientWithMockedGet(string $filename): Client return $guzzle; } + /** + * getClientWithMockedPost + * + * @param string $filename + * @return Client + * @author Tiago Araujo + */ + 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 + */ + 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 * diff --git a/tests/mocks/proposals-delete.json b/tests/mocks/proposals-delete.json new file mode 100644 index 0000000..fd2f3d7 --- /dev/null +++ b/tests/mocks/proposals-delete.json @@ -0,0 +1,3 @@ +{ + "message": "Proposal deleted" +} \ No newline at end of file diff --git a/tests/mocks/proposals-index.json b/tests/mocks/proposals-index.json new file mode 100644 index 0000000..6576056 --- /dev/null +++ b/tests/mocks/proposals-index.json @@ -0,0 +1,13 @@ +{ + "data": [ + { + "id": 1, + "application_id": 31, + "key": "no", + "section": "default", + "locale": "en-GB", + "value": "test", + "can_delete": false + } + ] +} \ No newline at end of file diff --git a/tests/mocks/proposals-store.json b/tests/mocks/proposals-store.json new file mode 100644 index 0000000..aa28dfe --- /dev/null +++ b/tests/mocks/proposals-store.json @@ -0,0 +1,11 @@ +{ + "data": { + "id": 13, + "application_id": 1, + "key": "forceHeader", + "section": "versionControl", + "locale": "en-GB", + "value": "new text2", + "can_delete": true + } +} \ No newline at end of file