Skip to content

Commit

Permalink
Merge pull request #45 from aligent/fix/44-incorrect-url-for-get-all-…
Browse files Browse the repository at this point in the history
…metafields

Fix incorrect url for all metafields apis
  • Loading branch information
jswift authored Mar 31, 2021
2 parents d4989ff + 400ae59 commit 4979017
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 10 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### Bug Fix

Fix issue with all batch operations under PHP 7.4
Fix issue with Metafield APIs not working.
4 changes: 2 additions & 2 deletions src/BigCommerce/Api/Catalog/Brands/BrandMetafieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/BigCommerce/Api/Orders/OrderMetafieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
11 changes: 9 additions & 2 deletions tests/BigCommerce/Api/Orders/OrderMetafieldsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
18 changes: 17 additions & 1 deletion tests/BigCommerce/BigCommerceApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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'];
}
}
40 changes: 40 additions & 0 deletions tests/BigCommerce/responses/orders__1__metafields__get_all.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
15 changes: 15 additions & 0 deletions tests/BigCommerce/responses/orders__2__metafields__3__get.json
Original file line number Diff line number Diff line change
@@ -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": {}
}

0 comments on commit 4979017

Please sign in to comment.