Skip to content

Commit 4979017

Browse files
authored
Merge pull request #45 from aligent/fix/44-incorrect-url-for-get-all-metafields
Fix incorrect url for all metafields apis
2 parents d4989ff + 400ae59 commit 4979017

10 files changed

+106
-10
lines changed

RELEASE_NOTES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
### Bug Fix
22

3-
Fix issue with all batch operations under PHP 7.4
3+
Fix issue with Metafield APIs not working.

src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class BrandMetafieldsApi extends ResourceApi
1010
{
1111
private const RESOURCE_NAME = 'metafields';
12-
private const METAFIELD_ENDPOINT = 'catalog/brands/{brand_id}/metafields';
13-
private const METAFIELDS_ENDPOINT = 'catalog/brands/{brand_id}/metafields/%d';
12+
private const METAFIELDS_ENDPOINT = 'catalog/brands/%d/metafields';
13+
private const METAFIELD_ENDPOINT = 'catalog/brands/%d/metafields/%d';
1414

1515
protected function singleResourceEndpoint(): string
1616
{

src/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApi.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class CategoryMetafieldsApi extends ResourceApi
1010
{
1111
private const RESOURCE_NAME = 'metafields';
12-
private const METAFIELD_ENDPOINT = 'catalog/categories/{category_id}/metafields/%d';
13-
private const METAFIELDS_ENDPOINT = 'catalog/categories/{category_id}/metafields';
12+
private const METAFIELD_ENDPOINT = 'catalog/categories/%d/metafields/%d';
13+
private const METAFIELDS_ENDPOINT = 'catalog/categories/%d/metafields';
1414

1515
protected function singleResourceEndpoint(): string
1616
{

src/BigCommerce/Api/Orders/OrderMetafieldsApi.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class OrderMetafieldsApi extends ResourceApi
1010
{
1111
private const RESOURCE_NAME = 'metafields';
12-
private const METAFIELD_ENDPOINT = 'orders/{order_id}/metafields';
13-
private const METAFIELDS_ENDPOINT = 'orders/{order_id}/metafields/%d';
12+
private const METAFIELDS_ENDPOINT = 'orders/%d/metafields';
13+
private const METAFIELD_ENDPOINT = 'orders/%d/metafields/%d';
1414

1515
protected function singleResourceEndpoint(): string
1616
{

tests/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApiTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testCanGetBrandMetafield(): void
2222

2323
$response = $this->getApi()->catalog()->brand(158)->metafield(8)->get();
2424
$this->assertEquals('Shelf 3, Bin 5', $response->getMetafield()->value);
25+
$this->assertEquals('catalog/brands/158/metafields/8', $this->getLastRequest()->getUri()->getPath());
2526
}
2627

2728
public function testCanGetAllBrandMetafields(): void
@@ -31,5 +32,6 @@ public function testCanGetAllBrandMetafields(): void
3132
$response = $this->getApi()->catalog()->brand(11)->metafields()->getAll();
3233
$this->assertEquals(2, $response->getPagination()->total);
3334
$this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace);
35+
$this->assertEquals('catalog/brands/11/metafields', $this->getLastRequest()->getUri()->getPath());
3436
}
3537
}

tests/BigCommerce/Api/Catalog/Categories/CategoryMetafieldsApiTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public function testCanGetCategoryMetafields(): void
3232
$this->assertEquals(2, $response->getPagination()->total);
3333
$this->assertEquals('Warehouse Locations', $response->getMetafields()[0]->namespace);
3434
}
35+
36+
public function testCanSetApiUrlCorrectlyForGetAll(): void
37+
{
38+
$this->setReturnData('catalog__categories__111__metafields__get_all.json');
39+
$this->getApi()->catalog()->category(111)->metafields()->getAll();
40+
41+
$this->assertEquals('catalog/categories/111/metafields', $this->getLastRequest()->getUri()->getPath());
42+
}
43+
44+
public function testCanSetApiUrlCorrectlyForGet(): void
45+
{
46+
$this->setReturnData('catalog__categories__158__metafields__8__get.json');
47+
$this->getApi()->catalog()->category(158)->metafield(8)->get();
48+
49+
$this->assertEquals('catalog/categories/158/metafields/8', $this->getLastRequest()->getUri()->getPath());
50+
}
3551
}

tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ class OrderMetafieldsApiTest extends BigCommerceApiTest
88
{
99
public function testCanGetOrderMetafield(): void
1010
{
11-
$this->markTestIncomplete();
11+
$this->setReturnData('orders__2__metafields__3__get.json');
12+
13+
$this->getApi()->order(2)->metafield(3)->get();
14+
$this->assertEquals('orders/2/metafields/3', $this->getLastRequest()->getUri()->getPath());
1215
}
1316

1417
public function testCanGetAllOrderMetafields(): void
1518
{
16-
$this->markTestIncomplete();
19+
$this->setReturnData('orders__1__metafields__get_all.json');
20+
21+
$response = $this->getApi()->order(1)->metafields()->getAll();
22+
$this->assertEquals(2, $response->getPagination()->total);
23+
$this->assertEquals('orders/1/metafields', $this->getLastRequest()->getUri()->getPath());
1724
}
1825
}

tests/BigCommerce/BigCommerceApiTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use BigCommerce\ApiV3\Client;
66
use GuzzleHttp\Handler\MockHandler;
77
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Middleware;
9+
use GuzzleHttp\Psr7\Request;
810
use GuzzleHttp\Psr7\Response;
911
use PHPUnit\Framework\TestCase;
1012

@@ -19,6 +21,7 @@ abstract class BigCommerceApiTest extends TestCase
1921
private const API_ACCESS_TOKEN = "TOKEN";
2022

2123
private Client $api;
24+
private array $container = [];
2225

2326
public function getApi(): Client
2427
{
@@ -40,8 +43,21 @@ protected function setReturnData(string $filename, int $statusCode = 200, array
4043
new Response($statusCode, $headers, file_get_contents(self::RESPONSES_PATH . $filename)),
4144
]);
4245

43-
$client = new \GuzzleHttp\Client(['handler' => HandlerStack::create($mock)]);
46+
$handlerStack = HandlerStack::create($mock);
47+
$handlerStack->push(Middleware::history($this->container));
48+
49+
$client = new \GuzzleHttp\Client(['handler' => $handlerStack]);
4450

4551
$this->getApi()->setRestClient($client);
4652
}
53+
54+
public function getRequestHistory(): array
55+
{
56+
return $this->container;
57+
}
58+
59+
public function getLastRequest(): Request
60+
{
61+
return end($this->container)['request'];
62+
}
4763
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"data": [
3+
{
4+
"permission_set": "app_only",
5+
"namespace": "Warehouse Locations",
6+
"key": "Location",
7+
"value": "4HG",
8+
"description": "Location in the warehouse",
9+
"resource_type": "brand",
10+
"resource_id": 1,
11+
"id": 6,
12+
"created_at": "1973-01-20T21:34:57.903Z",
13+
"updated_at": "1990-12-30T00:29:23.515Z"
14+
},
15+
{
16+
"permission_set": "read",
17+
"namespace": "Warehouse Locations",
18+
"key": "Location",
19+
"value": "4HG",
20+
"description": "Location in the warehouse",
21+
"resource_type": "order",
22+
"resource_id": 1,
23+
"id": 6,
24+
"created_at": "1973-01-20T21:34:57.903Z",
25+
"updated_at": "1990-12-30T00:29:23.515Z"
26+
}
27+
],
28+
"meta": {
29+
"pagination": {
30+
"total": 2,
31+
"count": 2,
32+
"per_page": 50,
33+
"current_page": 1,
34+
"total_pages": 1,
35+
"links": {
36+
"current": "?page=1&limit=50"
37+
}
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"data": {
3+
"id": 3,
4+
"key": "location_id",
5+
"value": "Shelf 3, Bin 5",
6+
"namespace": "Inventory Namespace",
7+
"permission_set": "read",
8+
"resource_type": "order",
9+
"resource_id": 2,
10+
"description": "Where products are located",
11+
"date_created": "2018-09-13T16:42:37+00:00",
12+
"date_modified": "2018-09-13T16:42:37+00:00"
13+
},
14+
"meta": {}
15+
}

0 commit comments

Comments
 (0)