Skip to content

Commit 12c7c2c

Browse files
authored
Merge branch 'bigcommerce:master' into namespace-fix-pr
2 parents f3c2880 + 05cc281 commit 12c7c2c

9 files changed

Lines changed: 70 additions & 47 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ script/*
88
/build
99
/test/reports
1010
.env
11+
.idea
1112
/.php_cs
1213
/.php_cs.cache

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
],
2020
"require": {
2121
"php": ">=7.0",
22-
"firebase/php-jwt": "~3.0 || ~5.0"
22+
"firebase/php-jwt": "~3.0 || ~5.0",
23+
"ext-curl": "*"
2324
},
2425
"require-dev": {
2526
"codeless/jugglecode": "1.0",

src/Bigcommerce/Api/Connection.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class Connection
2828
/**
2929
* @var array Hash of HTTP request headers.
3030
*/
31-
private $headers = array();
31+
private $headers = [];
3232

3333
/**
3434
* @var array Hash of headers from HTTP response
3535
*/
36-
private $responseHeaders = array();
36+
private $responseHeaders = [];
3737

3838
/**
3939
* The status line of the response.
@@ -95,8 +95,9 @@ public function __construct()
9595
define('STDIN', fopen('php://stdin', 'r'));
9696
}
9797
$this->curl = curl_init();
98-
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array($this, 'parseHeader'));
99-
curl_setopt($this->curl, CURLOPT_WRITEFUNCTION, array($this, 'parseBody'));
98+
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, [$this, 'parseHeader']);
99+
curl_setopt($this->curl, CURLOPT_WRITEFUNCTION, [$this, 'parseBody']);
100+
curl_setopt($this->curl, CURLOPT_USERAGENT, 'PHP CURL - Bigcommerce API Client');
100101

101102
// Set to a blank string to make cURL include all encodings it can handle (gzip, deflate, identity) in the 'Accept-Encoding' request header and respect the 'Content-Encoding' response header
102103
curl_setopt($this->curl, CURLOPT_ENCODING, '');
@@ -257,7 +258,7 @@ private function getContentType()
257258
private function initializeRequest()
258259
{
259260
$this->responseBody = '';
260-
$this->responseHeaders = array();
261+
$this->responseHeaders = [];
261262
$this->lastError = false;
262263
$this->addHeader('Accept', $this->getContentType());
263264

@@ -277,7 +278,7 @@ private function initializeRequest()
277278
private function handleResponse()
278279
{
279280
if (curl_errno($this->curl)) {
280-
throw new NetworkError(curl_error($this->curl), curl_errno($this->curl));
281+
throw new NetworkError(curl_error($this->curl), curl_errno($this->curl), $this->responseHeaders);
281282
}
282283

283284
$body = ($this->rawResponse) ? $this->getBody() : json_decode($this->getBody());
@@ -286,14 +287,14 @@ private function handleResponse()
286287

287288
if ($status >= 400 && $status <= 499) {
288289
if ($this->failOnError) {
289-
throw new ClientError($body, $status);
290+
throw new ClientError($body, $status, $this->responseHeaders);
290291
} else {
291292
$this->lastError = $body;
292293
return false;
293294
}
294295
} elseif ($status >= 500 && $status <= 599) {
295296
if ($this->failOnError) {
296-
throw new ServerError($body, $status);
297+
throw new ServerError($body, $status, $this->responseHeaders);
297298
} else {
298299
$this->lastError = $body;
299300
return false;
@@ -342,7 +343,7 @@ private function followRedirectPath()
342343
$this->get($url);
343344
} else {
344345
$errorString = "Too many redirects when trying to follow location.";
345-
throw new NetworkError($errorString, CURLE_TOO_MANY_REDIRECTS);
346+
throw new NetworkError($errorString, CURLE_TOO_MANY_REDIRECTS, $this->responseHeaders);
346347
}
347348
} else {
348349
$this->redirectsFollowed = 0;

src/Bigcommerce/Api/Error.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,32 @@
77
*/
88
class Error extends \Exception
99
{
10-
public function __construct($message, $code)
10+
/**
11+
* @var array
12+
*/
13+
private $headers;
14+
15+
/**
16+
* @param $message
17+
* @param $code
18+
* @param string[] $headers
19+
*/
20+
public function __construct($message, $code, array $headers = [])
1121
{
1222
if (is_array($message)) {
1323
$message = $message[0]->message;
1424
}
1525

1626
parent::__construct($message, $code);
27+
$this->headers = $headers;
1728
}
29+
30+
/**
31+
* @return string[]
32+
*/
33+
public function getResponseHeaders(): array
34+
{
35+
return $this->headers;
36+
}
37+
1838
}

test/Unit/Api/ClientErrorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ClientErrorTest extends TestCase
88
{
99
public function testStringifyingReturnsTheMessageAndCodeAppropriately()
1010
{
11-
$error = new ClientError('message here', 100);
11+
$error = new ClientError('message here', 100, []);
1212
$this->assertSame('Client Error (100): message here', (string)$error);
1313
}
1414
}

test/Unit/Api/ClientTest.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,18 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable()
283283

284284
public function collections()
285285
{
286-
return array(
286+
return [
287287
// path function classname
288-
array('products', 'getProducts', 'Product'),
289-
array('brands', 'getBrands', 'Brand'),
290-
array('orders', 'getOrders', 'Order'),
291-
array('customers', 'getCustomers', 'Customer'),
292-
array('coupons', 'getCoupons', 'Coupon'),
293-
array('order_statuses', 'getOrderStatuses', 'OrderStatus'),
294-
array('categories', 'getCategories', 'Category'),
295-
array('options', 'getOptions', 'Option'),
296-
array('optionsets', 'getOptionSets', 'OptionSet'),
297-
array('products/skus', 'getSkus', 'Sku'),
298-
array('requestlogs', 'getRequestLogs', 'RequestLog'),
299-
array('pages', 'getPages', 'Page'),
300-
);
288+
['products', 'getProducts', 'Product'],
289+
['brands', 'getBrands', 'Brand'],
290+
['orders', 'getOrders', 'Order'],
291+
['customers', 'getCustomers', 'Customer'],
292+
['coupons', 'getCoupons', 'Coupon'],
293+
['categories', 'getCategories', 'Category'],
294+
['options', 'getOptions', 'Option'],
295+
['optionsets', 'getOptionSets', 'OptionSet'],
296+
['products/skus', 'getSkus', 'Sku'],
297+
];
301298
}
302299

303300
/**
@@ -322,11 +319,6 @@ public function testGettingASpecificResourceReturnsACollectionOfThatResource($pa
322319
*/
323320
public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($path, $fnName, $class)
324321
{
325-
if (in_array($path, array('order_statuses', 'requestlogs', 'pages'))) {
326-
//$this->markTestSkipped(sprintf('The API does not currently support getting the count of %s', $path));
327-
return;
328-
}
329-
330322
$this->connection->expects($this->once())
331323
->method('get')
332324
->with($this->basePath . '/' . $path . '/count', false)
@@ -339,19 +331,19 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat
339331

340332
public function resources()
341333
{
342-
return array(
334+
return [
343335
// path function classname
344-
array('products', '%sProduct', 'Product'),
345-
array('brands', '%sBrand', 'Brand'),
346-
array('orders', '%sOrder', 'Order'),
347-
array('customers', '%sCustomer', 'Customer'),
348-
array('categories', '%sCategory', 'Category'),
349-
array('options', '%sOption', 'Option'),
350-
array('optionsets', '%sOptionSet', 'OptionSet'),
351-
array('coupons', '%sCoupon', 'Coupon'),
352-
array('currencies', '%sCurrency', 'Currency'),
353-
array('pages', '%sPage', 'Page'),
354-
);
336+
['products', '%sProduct', 'Product'],
337+
['brands', '%sBrand', 'Brand'],
338+
['orders', '%sOrder', 'Order'],
339+
['customers', '%sCustomer', 'Customer'],
340+
['categories', '%sCategory', 'Category'],
341+
['options', '%sOption', 'Option'],
342+
['optionsets', '%sOptionSet', 'OptionSet'],
343+
['coupons', '%sCoupon', 'Coupon'],
344+
['currencies', '%sCurrency', 'Currency'],
345+
['pages', '%sPage', 'Page'],
346+
];
355347
}
356348

357349
/**

test/Unit/Api/ErrorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class ErrorTest extends TestCase
99
public function testConstructorHandlesArrayOfMessageObjects()
1010
{
1111
$messageObj = (object)array('message' => 'message here');
12-
$error = new Error(array($messageObj), 0);
12+
$error = new Error(array($messageObj), 0, []);
1313
$this->assertSame('message here', $error->getMessage());
1414
}
1515

1616
public function testConstructorPassesMessageAndCodeThrough()
1717
{
18-
$error = new Error('message here', 100);
18+
$error = new Error('message here', 100, []);
1919
$this->assertSame('message here', $error->getMessage());
2020
$this->assertSame(100, $error->getCode());
2121
}

test/Unit/Api/NetworkErrorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NetworkErrorTest extends TestCase
88
{
99
public function testBehavesExactlyLikeException()
1010
{
11-
$error = new NetworkError('message', 100);
11+
$error = new NetworkError('message', 100, []);
1212
$this->assertSame('message', $error->getMessage());
1313
$this->assertSame(100, $error->getCode());
1414
}

test/Unit/Api/ServerErrorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ class ServerErrorTest extends TestCase
88
{
99
public function testBehavesExactlyLikeException()
1010
{
11-
$error = new ServerError('message', 100);
11+
$error = new ServerError('message', 100, []);
1212
$this->assertSame('message', $error->getMessage());
1313
$this->assertSame(100, $error->getCode());
1414
}
15+
16+
public function testServerErrorCarriesResponseHeaders()
17+
{
18+
$error = new ServerError('message', 100, ['x-bc-header' => 'abc-123']);
19+
$this->assertSame('message', $error->getMessage());
20+
$this->assertSame(100, $error->getCode());
21+
$this->assertSame(['x-bc-header' => 'abc-123'], $error->getResponseHeaders());
22+
}
1523
}

0 commit comments

Comments
 (0)