From d734fe4d031f9976e0d67f9f42fc1765edcd46b4 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 13:58:23 +1030 Subject: [PATCH 1/6] Add a history to the mock handler and a new method for getting last request. Use this functionality to test the url for CategoryMetafield::getAll() #44 --- .../Categories/CategoryMetafieldsApiTest.php | 8 ++++++++ tests/BigCommerce/BigCommerceApiTest.php | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php index 9b246e27..d10c9876 100644 --- a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php +++ b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php @@ -32,4 +32,12 @@ public function testCanGetCategoryMetafields(): void $this->assertEquals(2, $response->getPagination()->total); $this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace); } + + public function testCanGetSetsApiUrlCorrectly(): void + { + $this->setReturnData('catalog__categories__111__metafields__get_all.json'); + $this->getApi()->catalog()->category(111)->metafields()->getAll(); + + $this->assertEquals('catalog/categories/111/metafields', $this->getLastRequest()->getUri()->getPath()); + } } diff --git a/tests/BigCommerce/BigCommerceApiTest.php b/tests/BigCommerce/BigCommerceApiTest.php index 434c7ab3..9048e686 100644 --- a/tests/BigCommerce/BigCommerceApiTest.php +++ b/tests/BigCommerce/BigCommerceApiTest.php @@ -5,6 +5,8 @@ use BigCommerce\ApiV3\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; +use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; @@ -19,6 +21,7 @@ abstract class BigCommerceApiTest extends TestCase private const API_ACCESS_TOKEN = "TOKEN"; private Client $api; + private array $container = []; public function getApi(): Client { @@ -40,8 +43,21 @@ protected function setReturnData(string $filename, int $statusCode = 200, array new Response($statusCode, $headers, file_get_contents(self::RESPONSES_PATH . $filename)), ]); - $client = new \GuzzleHttp\Client(['handler' => HandlerStack::create($mock)]); + $handlerStack = HandlerStack::create($mock); + $handlerStack->push(Middleware::history($this->container)); + + $client = new \GuzzleHttp\Client(['handler' => $handlerStack]); $this->getApi()->setRestClient($client); } + + public function getRequestHistory(): array + { + return $this->container; + } + + public function getLastRequest(): Request + { + return end($this->container)['request']; + } } From 15e1100f846f2a36a873c4b884f33a8f1b76fe6a Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 13:59:47 +1030 Subject: [PATCH 2/6] Fix incorrect endpoint constant for CategoryMetafieldsApi:METAFIELDS_ENDPOINT --- .../Api/Catalog/Categories/CategoryMetafieldsApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php index 2907a593..6c6ef5ed 100644 --- a/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php +++ b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php @@ -10,7 +10,7 @@ class CategoryMetafieldsApi extends ResourceApi { private const RESOURCE_NAME = 'metafields'; private const METAFIELD_ENDPOINT = 'catalog/categories/{category_id}/metafields/%d'; - private const METAFIELDS_ENDPOINT = 'catalog/categories/{category_id}/metafields'; + private const METAFIELDS_ENDPOINT = 'catalog/categories/%d/metafields'; protected function singleResourceEndpoint(): string { From a453ac1a725dc8167a888a2a5001b0c03571e870 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 14:01:51 +1030 Subject: [PATCH 3/6] Fix typo in CategoryMetafieldsApi:METAFIELD_ENDPOINT #44 --- .../Api/Catalog/Categories/CategoryMetafieldsApi.php | 2 +- .../Catalog/Categories/CategoryMetafieldsApiTest.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php index 6c6ef5ed..011dc248 100644 --- a/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php +++ b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php @@ -9,7 +9,7 @@ class CategoryMetafieldsApi extends ResourceApi { private const RESOURCE_NAME = 'metafields'; - private const METAFIELD_ENDPOINT = 'catalog/categories/{category_id}/metafields/%d'; + private const METAFIELD_ENDPOINT = 'catalog/categories/%d/metafields/%d'; private const METAFIELDS_ENDPOINT = 'catalog/categories/%d/metafields'; protected function singleResourceEndpoint(): string diff --git a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php index d10c9876..d34d2636 100644 --- a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php +++ b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php @@ -33,11 +33,19 @@ public function testCanGetCategoryMetafields(): void $this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace); } - public function testCanGetSetsApiUrlCorrectly(): void + public function testCanSetApiUrlCorrectlyForGetAll(): void { $this->setReturnData('catalog__categories__111__metafields__get_all.json'); $this->getApi()->catalog()->category(111)->metafields()->getAll(); $this->assertEquals('catalog/categories/111/metafields', $this->getLastRequest()->getUri()->getPath()); } + + public function testCanSetApiUrlCorrectlyForGet(): void + { + $this->setReturnData('catalog__categories__158__metafields__8__get.json'); + $this->getApi()->catalog()->category(158)->metafield(8)->get(); + + $this->assertEquals('catalog/categories/158/metafields/8', $this->getLastRequest()->getUri()->getPath()); + } } From f2311aa2156501affa36ec5c1d67416213c003bc Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 14:09:44 +1030 Subject: [PATCH 4/6] Fix endpoints for brand metafields that were switched and incorrect --- src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php | 4 ++-- .../BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php b/src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php index ce0906de..9e6e2b58 100644 --- a/src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php +++ b/src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php @@ -9,8 +9,8 @@ class BrandMetafieldsApi extends ResourceApi { private const RESOURCE_NAME = 'metafields'; - private const METAFIELD_ENDPOINT = 'catalog/brands/{brand_id}/metafields'; - private const METAFIELDS_ENDPOINT = 'catalog/brands/{brand_id}/metafields/%d'; + private const METAFIELDS_ENDPOINT = 'catalog/brands/%d/metafields'; + private const METAFIELD_ENDPOINT = 'catalog/brands/%d/metafields/%d'; protected function singleResourceEndpoint(): string { diff --git a/tests/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php b/tests/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php index daebade6..e2bf3f9c 100644 --- a/tests/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php +++ b/tests/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php @@ -22,6 +22,7 @@ public function testCanGetBrandMetafield(): void $response = $this->getApi()->catalog()->brand(158)->metafield(8)->get(); $this->assertEquals('Shelf 3, Bin 5', $response->getMetafield()->value); + $this->assertEquals('catalog/brands/158/metafields/8', $this->getLastRequest()->getUri()->getPath()); } public function testCanGetAllBrandMetafields(): void @@ -31,5 +32,6 @@ public function testCanGetAllBrandMetafields(): void $response = $this->getApi()->catalog()->brand(11)->metafields()->getAll(); $this->assertEquals(2, $response->getPagination()->total); $this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace); + $this->assertEquals('catalog/brands/11/metafields', $this->getLastRequest()->getUri()->getPath()); } } From f9592798e8f78473f370c5a4355c83532be325d1 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 14:18:15 +1030 Subject: [PATCH 5/6] Implement tests for OrderMetafieldsApi and fix issue with endpoints --- .../Api/Orders/OrderMetafieldsApi.php | 4 +- .../Api/Orders/OrderMetafieldsApiTest.php | 11 ++++- .../orders__1__metafields__get_all.json | 40 +++++++++++++++++++ .../orders__2__metafields__3__get.json | 15 +++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/BigCommerce/responses/orders__1__metafields__get_all.json create mode 100644 tests/BigCommerce/responses/orders__2__metafields__3__get.json diff --git a/src/BigCommerce/Api/Orders/OrderMetafieldsApi.php b/src/BigCommerce/Api/Orders/OrderMetafieldsApi.php index b57fbcdd..54d8dfd1 100644 --- a/src/BigCommerce/Api/Orders/OrderMetafieldsApi.php +++ b/src/BigCommerce/Api/Orders/OrderMetafieldsApi.php @@ -9,8 +9,8 @@ class OrderMetafieldsApi extends ResourceApi { private const RESOURCE_NAME = 'metafields'; - private const METAFIELD_ENDPOINT = 'orders/{order_id}/metafields'; - private const METAFIELDS_ENDPOINT = 'orders/{order_id}/metafields/%d'; + private const METAFIELDS_ENDPOINT = 'orders/%d/metafields'; + private const METAFIELD_ENDPOINT = 'orders/%d/metafields/%d'; protected function singleResourceEndpoint(): string { diff --git a/tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php b/tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php index f1b274bf..0289aaa7 100644 --- a/tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php +++ b/tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php @@ -8,11 +8,18 @@ class OrderMetafieldsApiTest extends BigCommerceApiTest { public function testCanGetOrderMetafield(): void { - $this->markTestIncomplete(); + $this->setReturnData('orders__2__metafields__3__get.json'); + + $this->getApi()->order(2)->metafield(3)->get(); + $this->assertEquals('orders/2/metafields/3', $this->getLastRequest()->getUri()->getPath()); } public function testCanGetAllOrderMetafields(): void { - $this->markTestIncomplete(); + $this->setReturnData('orders__1__metafields__get_all.json'); + + $response = $this->getApi()->order(1)->metafields()->getAll(); + $this->assertEquals(2, $response->getPagination()->total); + $this->assertEquals('orders/1/metafields', $this->getLastRequest()->getUri()->getPath()); } } diff --git a/tests/BigCommerce/responses/orders__1__metafields__get_all.json b/tests/BigCommerce/responses/orders__1__metafields__get_all.json new file mode 100644 index 00000000..f385c90b --- /dev/null +++ b/tests/BigCommerce/responses/orders__1__metafields__get_all.json @@ -0,0 +1,40 @@ +{ + "data": [ + { + "permission_set": "app_only", + "namespace": "Warehouse Locations", + "key": "Location", + "value": "4HG", + "description": "Location in the warehouse", + "resource_type": "brand", + "resource_id": 1, + "id": 6, + "created_at": "1973-01-20T21:34:57.903Z", + "updated_at": "1990-12-30T00:29:23.515Z" + }, + { + "permission_set": "read", + "namespace": "Warehouse Locations", + "key": "Location", + "value": "4HG", + "description": "Location in the warehouse", + "resource_type": "order", + "resource_id": 1, + "id": 6, + "created_at": "1973-01-20T21:34:57.903Z", + "updated_at": "1990-12-30T00:29:23.515Z" + } + ], + "meta": { + "pagination": { + "total": 2, + "count": 2, + "per_page": 50, + "current_page": 1, + "total_pages": 1, + "links": { + "current": "?page=1&limit=50" + } + } + } +} \ No newline at end of file diff --git a/tests/BigCommerce/responses/orders__2__metafields__3__get.json b/tests/BigCommerce/responses/orders__2__metafields__3__get.json new file mode 100644 index 00000000..1fc32932 --- /dev/null +++ b/tests/BigCommerce/responses/orders__2__metafields__3__get.json @@ -0,0 +1,15 @@ +{ + "data": { + "id": 3, + "key": "location_id", + "value": "Shelf 3, Bin 5", + "namespace": "Inventory Namespace", + "permission_set": "read", + "resource_type": "order", + "resource_id": 2, + "description": "Where products are located", + "date_created": "2018-09-13T16:42:37+00:00", + "date_modified": "2018-09-13T16:42:37+00:00" + }, + "meta": {} +} \ No newline at end of file From 400ae59364b21b7a8bcf983a9d8aada8aca9a078 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 31 Mar 2021 14:22:41 +1030 Subject: [PATCH 6/6] Add new lines to end of files and update release notes --- RELEASE_NOTES.md | 2 +- tests/BigCommerce/responses/orders__1__metafields__get_all.json | 2 +- tests/BigCommerce/responses/orders__2__metafields__3__get.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 93cb44f3..77cf10a2 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,3 @@ ### Bug Fix -Fix issue with all batch operations under PHP 7.4 +Fix issue with Metafield APIs not working. diff --git a/tests/BigCommerce/responses/orders__1__metafields__get_all.json b/tests/BigCommerce/responses/orders__1__metafields__get_all.json index f385c90b..ae88af35 100644 --- a/tests/BigCommerce/responses/orders__1__metafields__get_all.json +++ b/tests/BigCommerce/responses/orders__1__metafields__get_all.json @@ -37,4 +37,4 @@ } } } -} \ No newline at end of file +} diff --git a/tests/BigCommerce/responses/orders__2__metafields__3__get.json b/tests/BigCommerce/responses/orders__2__metafields__3__get.json index 1fc32932..deb97800 100644 --- a/tests/BigCommerce/responses/orders__2__metafields__3__get.json +++ b/tests/BigCommerce/responses/orders__2__metafields__3__get.json @@ -12,4 +12,4 @@ "date_modified": "2018-09-13T16:42:37+00:00" }, "meta": {} -} \ No newline at end of file +}