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/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/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php index 2907a593..011dc248 100644 --- a/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php +++ b/src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php @@ -9,8 +9,8 @@ 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 METAFIELD_ENDPOINT = 'catalog/categories/%d/metafields/%d'; + private const METAFIELDS_ENDPOINT = 'catalog/categories/%d/metafields'; protected function singleResourceEndpoint(): string { 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/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()); } } diff --git a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php index 9b246e27..d34d2636 100644 --- a/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php +++ b/tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php @@ -32,4 +32,20 @@ public function testCanGetCategoryMetafields(): void $this->assertEquals(2, $response->getPagination()->total); $this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace); } + + 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()); + } } 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/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']; + } } 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..ae88af35 --- /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" + } + } + } +} 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..deb97800 --- /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": {} +}