From 32a2852a6d2abcd205e5841ca75f5bdb32a9973c Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Tue, 13 Apr 2021 19:59:54 +0930 Subject: [PATCH] Implement the get all and get all for channel currency assignments endpoints #53 --- .../ChannelCurrencyAssignmentsApi.php | 54 ++++++++++++------- src/BigCommerce/Api/Channels/ChannelsApi.php | 5 ++ .../ChannelCurrencyAssignmentResponse.php | 4 ++ .../ChannelCurrencyAssignmentsApiTest.php | 15 ++++-- .../channel_currency_assignments.json | 12 +++++ 5 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 tests/BigCommerce/responses/channel_currency_assignments.json diff --git a/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php b/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php index a3de98a9..9dbc24d4 100644 --- a/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php +++ b/src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php @@ -2,7 +2,13 @@ namespace BigCommerce\ApiV3\Api\Channels; -use BigCommerce\ApiV3\Api\Generic\ResourceWithBatchUpdateApi; +use BigCommerce\ApiV3\Api\Generic\BatchUpdateResource; +use BigCommerce\ApiV3\Api\Generic\CreateResource; +use BigCommerce\ApiV3\Api\Generic\DeleteResource; +use BigCommerce\ApiV3\Api\Generic\GetAllResources; +use BigCommerce\ApiV3\Api\Generic\UpdateResource; +use BigCommerce\ApiV3\Api\Generic\V3ApiBase; +use BigCommerce\ApiV3\ResponseModels\Channel\ChannelCurrencyAssignmentsResponse; use BigCommerce\ApiV3\ResponseModels\PaginatedResponse; use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse; @@ -11,44 +17,56 @@ * * */ -class ChannelCurrencyAssignmentsApi extends ResourceWithBatchUpdateApi +class ChannelCurrencyAssignmentsApi extends V3ApiBase { + use GetAllResources; + use DeleteResource; + use UpdateResource; + use CreateResource; + use BatchUpdateResource; - public function batchUpdate(array $resources): PaginatedResponse - { - // TODO: Implement batchUpdate() method. - } +// batchcreate? - protected function multipleResourcesEndpoint(): string + private const CURRENCY_ASSIGNMENTS_ENDPOINT = 'channels/currency-assignments'; + private const CURRENCY_ASSIGNMENT_ENDPOINT = 'channels/%d/currency-assignments'; + + + public function create(): ChannelCurrencyAssignmentsResponse { - // TODO: Implement multipleResourcesEndpoint() method. } - protected function singleResourceEndpoint(): string + public function update(): ChannelCurrencyAssignmentsResponse { - // TODO: Implement singleResourceEndpoint() method. } - protected function resourceName(): string + public function batchUpdate(array $resources): ChannelCurrencyAssignmentsResponse { - // TODO: Implement resourceName() method. + // TODO: Implement batchUpdate() method. } - public function get(): SingleResourceResponse + public function getAll(array $filters = [], int $page = 1, int $limit = 250): ChannelCurrencyAssignmentsResponse { - // TODO: Implement get() method. + return new ChannelCurrencyAssignmentsResponse($this->getAllResources($filters, $page, $limit)); } - public function getAll(array $filters = [], int $page = 1, int $limit = 250): PaginatedResponse + protected function multipleResourcesEndpoint(): string { - // TODO: Implement getAll() method. + return self::CURRENCY_ASSIGNMENTS_ENDPOINT; } - public function create(): SingleResourceResponse + /** + * Currency Assignment endpoints are different, they are all multiple resource endpoints, that may or may not + * be filtered by channel id. + * + * @return string + */ + public function multipleResourceUrl(): string { + return $this->getParentResourceId() ? $this->singleResourceUrl() : $this->multipleResourcesEndpoint(); } - public function update(): SingleResourceResponse + public function singleResourceUrl(): string { + return sprintf(self::CURRENCY_ASSIGNMENT_ENDPOINT, $this->getParentResourceId()); } } diff --git a/src/BigCommerce/Api/Channels/ChannelsApi.php b/src/BigCommerce/Api/Channels/ChannelsApi.php index 0a6a2bc3..0c8a79bf 100644 --- a/src/BigCommerce/Api/Channels/ChannelsApi.php +++ b/src/BigCommerce/Api/Channels/ChannelsApi.php @@ -85,4 +85,9 @@ public function site(): ChannelSitesApi { return new ChannelSitesApi($this->getClient(), null, $this->getResourceId()); } + + public function currencyAssignments(): ChannelCurrencyAssignmentsApi + { + return new ChannelCurrencyAssignmentsApi($this->getClient(), null, $this->getResourceId()); + } } diff --git a/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentResponse.php b/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentResponse.php index c1ee73eb..bde67224 100644 --- a/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentResponse.php +++ b/src/BigCommerce/ResponseModels/Channel/ChannelCurrencyAssignmentResponse.php @@ -6,6 +6,10 @@ use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse; use stdClass; +/** + * Note this doesn't seem to be used by the API currently, it returns an array of assignments always. This class + * is included for completeness + */ class ChannelCurrencyAssignmentResponse extends SingleResourceResponse { private ChannelCurrencyAssignment $currencyAssignment; diff --git a/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php b/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php index a0ba6acf..9846a01b 100644 --- a/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php +++ b/tests/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApiTest.php @@ -8,11 +8,20 @@ class ChannelCurrencyAssignmentsApiTest extends BigCommerceApiTest { public function testCanGetCurrencyAssignment() { - $this->markTestIncomplete(); + $this->setReturnData('channel_currency_assignments.json'); + $channelId = 1; + + $currencyAssignments = $this->getApi() + ->channel($channelId)->currencyAssignments()->getAll()->getCurrencyAssignments(); + $this->assertEquals("channels/$channelId/currency-assignments", $this->getLastRequestPath()); + $this->assertEquals('AUD', $currencyAssignments[0]->default_currency); } - public function testCanGetAllCurrencyAssignments() + public function testCanGetAllChannelsCurrencyAssignments() { - $this->markTestIncomplete(); + $this->setReturnData('channel_currency_assignments.json'); + $currencyAssignments = $this->getApi()->channels()->currencyAssignments()->getAll()->getCurrencyAssignments(); + $this->assertEquals('channels/currency-assignments', $this->getLastRequestPath()); + $this->assertEquals('AUD', $currencyAssignments[0]->default_currency); } } diff --git a/tests/BigCommerce/responses/channel_currency_assignments.json b/tests/BigCommerce/responses/channel_currency_assignments.json new file mode 100644 index 00000000..84026f25 --- /dev/null +++ b/tests/BigCommerce/responses/channel_currency_assignments.json @@ -0,0 +1,12 @@ +{ + "data": [ + { + "channel_id": 1, + "enabled_currencies": [ + "AUD" + ], + "default_currency": "AUD" + } + ], + "meta": {} +}