Skip to content

Commit 968daa5

Browse files
committed
Implementing more tests and adding/fixing methods
Adding some tests for existing, uncovered methods Making getCreateFields and getUpdateFields operate on a clone of resources instead of the actual resources object. This fixes the "unknown failure" Incomplete tests since we no longer unset the properties before the URL is built Renaming methods & vars for PSR2 camel casing Renaming methods for consistency (plural collections, singular resources) Adding a few missing methods to un-skip existing tests
1 parent 6a25425 commit 968daa5

8 files changed

Lines changed: 131 additions & 67 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,23 @@ public static function getOptions($filter = array())
535535
* @param $object
536536
* @return mixed
537537
*/
538-
public static function createOptions($object)
538+
public static function createOption($object)
539539
{
540540
return self::createResource('/options', $object);
541541
}
542542

543+
/**
544+
* Update the given option.
545+
*
546+
* @param int $id category id
547+
* @param mixed $object
548+
* @return mixed
549+
*/
550+
public static function updateOption($id, $object)
551+
{
552+
return self::updateResource('/options/' . $id, $object);
553+
}
554+
543555
/**
544556
* Return the number of options in the collection
545557
*
@@ -561,7 +573,6 @@ public static function getOption($id)
561573
return self::getResource('/options/' . $id, 'Option');
562574
}
563575

564-
565576
/**
566577
* Delete the given option.
567578
*
@@ -967,24 +978,23 @@ public static function getOptionSets($filter = array())
967978
* @param $object
968979
* @return mixed
969980
*/
970-
public static function createOptionsets($object)
981+
public static function createOptionSet($object)
971982
{
972983
return self::createResource('/optionsets', $object);
973984
}
974985

975986
/**
976-
* Create Optionset Options
987+
* Create Option Set Options
977988
*
978989
* @param $object
979990
* @param $id
980991
* @return mixed
981992
*/
982-
public static function createOptionsetsOptions($object, $id)
993+
public static function createOptionSetOption($object, $id)
983994
{
984995
return self::createResource('/optionsets/' . $id . '/options', $object);
985996
}
986997

987-
988998
/**
989999
* Returns the total number of option sets in the collection.
9901000
*
@@ -1016,6 +1026,29 @@ public static function getOrderStatus($id)
10161026
return self::getResource('/order_statuses/' . $id, 'OrderStatus');
10171027
}
10181028

1029+
/**
1030+
* Update the given option set.
1031+
*
1032+
* @param int $id option set id
1033+
* @param mixed $object
1034+
* @return mixed
1035+
*/
1036+
public static function updateOptionSet($id, $object)
1037+
{
1038+
return self::updateResource('/optionsets/' . $id, $object);
1039+
}
1040+
1041+
/**
1042+
* Delete the given option set.
1043+
*
1044+
* @param int $id option id
1045+
* @return mixed
1046+
*/
1047+
public static function deleteOptionSet($id)
1048+
{
1049+
Client::deleteResource('/optionsets/' . $id);
1050+
}
1051+
10191052
/**
10201053
* Status codes used to represent the state of an order.
10211054
*
@@ -1061,6 +1094,16 @@ public static function updateSku($id, $object)
10611094
return self::updateResource('/product/skus/' . $id, $object);
10621095
}
10631096

1097+
/**
1098+
* Returns the total number of SKUs in the collection.
1099+
*
1100+
* @return int
1101+
*/
1102+
public static function getSkusCount()
1103+
{
1104+
return self::getCount('/products/skus/count');
1105+
}
1106+
10641107
/**
10651108
* Get a single coupon by given id.
10661109
*

src/Bigcommerce/Api/Resource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __isset($field)
6868

6969
public function getCreateFields()
7070
{
71-
$resource = $this->fields;
71+
$resource = clone $this->fields;
7272

7373
foreach ($this->ignoreOnCreate as $field) {
7474
unset($resource->$field);
@@ -79,7 +79,7 @@ public function getCreateFields()
7979

8080
public function getUpdateFields()
8181
{
82-
$resource = $this->fields;
82+
$resource = clone $this->fields;
8383

8484
foreach ($this->ignoreOnUpdate as $field) {
8585
unset($resource->$field);

src/Bigcommerce/Api/Resources/ProductImage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function create()
2929

3030
public function update()
3131
{
32-
Client::updateResource('/products/' . $this->fields->product_id . '/images/' . $this->id, $this->getUpdateFields());
32+
Client::updateResource(
33+
'/products/' . $this->fields->product_id . '/images/' . $this->id,
34+
$this->getUpdateFields()
35+
);
3336
}
3437
}

test/Unit/Api/ClientTest.php

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat
303303
public function resources()
304304
{
305305
return array(
306-
// path function classname
307-
array('products', '%sProduct', 'Product'),
308-
array('brands', '%sBrand', 'Brand'),
309-
array('orders', '%sOrder', 'Order'),
310-
array('customers', '%sCustomer', 'Customer'),
311-
array('categories', '%sCategory', 'Category'),
312-
array('options', '%sOption', 'Option'),
313-
array('optionsets', '%sOptionSet', 'OptionSet'),
314-
array('coupons', '%sCoupon', 'Coupon'),
306+
// path function classname
307+
array('products', '%sProduct', 'Product'),
308+
array('brands', '%sBrand', 'Brand'),
309+
array('orders', '%sOrder', 'Order'),
310+
array('customers', '%sCustomer', 'Customer'),
311+
array('categories', '%sCategory', 'Category'),
312+
array('options', '%sOption', 'Option'),
313+
array('optionsets', '%sOptionSet', 'OptionSet'),
314+
array('coupons', '%sCoupon', 'Coupon'),
315315
);
316316
}
317317

@@ -320,10 +320,6 @@ public function resources()
320320
*/
321321
public function testGettingASpecificResourceReturnsThatResource($path, $fnName, $class)
322322
{
323-
if (in_array($path, array('coupons'))) {
324-
$this->markTestSkipped(sprintf('The php client does not support getting a specified %s', $path));
325-
}
326-
327323
$this->connection->expects($this->once())
328324
->method('get')
329325
->with($this->basePath . '/' . $path . '/1', false)
@@ -339,10 +335,6 @@ public function testGettingASpecificResourceReturnsThatResource($path, $fnName,
339335
*/
340336
public function testCreatingASpecificResourcePostsToThatResource($path, $fnName, $class)
341337
{
342-
if (in_array($path, array('options', 'optionsets'))) {
343-
$this->markTestSkipped(sprintf('The php client does not support creating a specified %s', $path));
344-
}
345-
346338
$this->connection->expects($this->once())
347339
->method('post')
348340
->with($this->basePath . '/' . $path, (object)array());
@@ -356,10 +348,6 @@ public function testCreatingASpecificResourcePostsToThatResource($path, $fnName,
356348
*/
357349
public function testDeletingASpecificResourceDeletesToThatResource($path, $fnName, $class)
358350
{
359-
if (in_array($path, array('optionsets', 'coupons'))) {
360-
$this->markTestSkipped(sprintf('The php client does not support deleting a specified %s', $path));
361-
}
362-
363351
$this->connection->expects($this->once())
364352
->method('delete')
365353
->with($this->basePath . '/' . $path . '/1');
@@ -373,10 +361,6 @@ public function testDeletingASpecificResourceDeletesToThatResource($path, $fnNam
373361
*/
374362
public function testUpdatingASpecificResourcePutsToThatResource($path, $fnName, $class)
375363
{
376-
if (in_array($path, array('orders', 'options', 'optionsets'))) {
377-
$this->markTestSkipped(sprintf('The php client does not support updating a specified %s', $path));
378-
}
379-
380364
$this->connection->expects($this->once())
381365
->method('put')
382366
->with($this->basePath . '/' . $path . '/1');
@@ -488,7 +472,7 @@ public function testCreatingAnOptionSetPostsToTheOptionSetsResource()
488472
->method('post')
489473
->with($this->basePath . '/optionsets', (object)array());
490474

491-
Client::createOptionsets(array());
475+
Client::createOptionSet(array());
492476
}
493477

494478
public function testCreatingAnOptionPostsToTheOptionResource()
@@ -497,16 +481,16 @@ public function testCreatingAnOptionPostsToTheOptionResource()
497481
->method('post')
498482
->with($this->basePath . '/options', (object)array());
499483

500-
Client::createOptions(array());
484+
Client::createOption(array());
501485
}
502486

503-
public function testCreatingAnOptionSetsOptionPostsToTheOptionSetsOptionResource()
487+
public function testCreatingAnOptionSetOptionPostsToTheOptionSetsOptionsResource()
504488
{
505489
$this->connection->expects($this->once())
506490
->method('post')
507491
->with($this->basePath . '/optionsets/1/options', (object)array());
508492

509-
Client::createOptionsetsOptions(array(), 1);
493+
Client::createOptionSetOption(array(), 1);
510494
}
511495

512496
public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResource()
@@ -594,6 +578,33 @@ public function testDeletingAllOrdersDeletesToTheOrderResource()
594578
Client::deleteAllOrders();
595579
}
596580

581+
public function testDeletingAllBrandsDeletesToTheBrandsResource()
582+
{
583+
$this->connection->expects($this->once())
584+
->method('delete')
585+
->with($this->basePath . '/brands');
586+
587+
Client::deleteAllBrands();
588+
}
589+
590+
public function testDeletingAllCategoriesDeletesToTheCategoriesResource()
591+
{
592+
$this->connection->expects($this->once())
593+
->method('delete')
594+
->with($this->basePath . '/categories');
595+
596+
Client::deleteAllCategories();
597+
}
598+
599+
public function testDeletingAllProductsDeletesToTheProductsResource()
600+
{
601+
$this->connection->expects($this->once())
602+
->method('delete')
603+
->with($this->basePath . '/products');
604+
605+
Client::deleteAllProducts();
606+
}
607+
597608
public function testGettingOrderProductsCountCountsToTheOrderProductsResource()
598609
{
599610
$this->connection->expects($this->once())
@@ -616,6 +627,20 @@ public function testGettingOrderShipmentReturnsTheOrderShipmentResource()
616627
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\Shipment', $resource);
617628
}
618629

630+
public function testGettingOrderProductsReturnsTheOrderProductsCollection()
631+
{
632+
$this->connection->expects($this->once())
633+
->method('get')
634+
->with($this->basePath . '/orders/1/products', false)
635+
->will($this->returnValue(array(array(), array())));
636+
637+
$collection = Client::getOrderProducts(1);
638+
$this->assertInternalType('array', $collection);
639+
foreach ($collection as $resource) {
640+
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\OrderProduct', $resource);
641+
}
642+
}
643+
619644
public function testGettingOrderShipmentsReturnsTheOrderShipmentsResource()
620645
{
621646
$this->connection->expects($this->once())

test/Unit/Api/Resources/OptionValueTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,32 @@ class OptionValueTest extends ResourceTestBase
88
{
99
public function testOptionPassesThroughToConnection()
1010
{
11-
$optionvalue = new OptionValue((object)array('option_id' => 1));
11+
$optionValue = new OptionValue((object)array('option_id' => 1));
1212
$this->connection->expects($this->once())
1313
->method('get')
1414
->with($this->basePath . '/options/1')
1515
->will($this->returnValue(array(array())));
1616

17-
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\Option', $optionvalue->option);
17+
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\Option', $optionValue->option);
1818
}
1919

2020
public function testCreatePassesThroughToConnection()
2121
{
22-
$this->markTestIncomplete('This currently fails for unknown reasons');
23-
$optionvalue = new OptionValue((object)array('option_id' => 1));
22+
$optionValue = new OptionValue((object)array('option_id' => 1));
2423
$this->connection->expects($this->once())
2524
->method('post')
26-
->with($this->basePath . '/options/1/values', $optionvalue->getCreateFields());
25+
->with($this->basePath . '/options/1/values', $optionValue->getCreateFields());
2726

28-
$optionvalue->create();
27+
$optionValue->create();
2928
}
3029

3130
public function testUpdatePassesThroughToConnection()
3231
{
33-
$this->markTestIncomplete('This currently fails for unknown reasons');
34-
$optionvalue = new OptionValue((object)array('id' => 1, 'option_id' => 1));
32+
$optionValue = new OptionValue((object)array('id' => 1, 'option_id' => 1));
3533
$this->connection->expects($this->once())
3634
->method('put')
37-
->with($this->basePath . '/options/1/values/1', $optionvalue->getUpdateFields());
35+
->with($this->basePath . '/options/1/values/1', $optionValue->getUpdateFields());
3836

39-
$optionvalue->update();
37+
$optionValue->update();
4038
}
4139
}

test/Unit/Api/Resources/ProductCustomFieldTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,31 @@ class ProductCustomFieldTest extends ResourceTestBase
88
{
99
public function testCreatePassesThroughToConnection()
1010
{
11-
$this->markTestIncomplete('This currently fails for unknown reasons');
12-
$customfield = new ProductCustomField((object)array('product_id' => 1));
11+
$customField = new ProductCustomField((object)array('product_id' => 1));
1312
$this->connection->expects($this->once())
1413
->method('post')
15-
->with($this->basePath . '/products/1/customfields', $customfield->getCreateFields());
14+
->with($this->basePath . '/products/1/customfields', $customField->getCreateFields());
1615

17-
$customfield->create();
16+
$customField->create();
1817
}
1918

2019
public function testUpdatePassesThroughToConnection()
2120
{
22-
$this->markTestIncomplete('This currently fails for unknown reasons');
23-
$customfield = new ProductCustomField((object)(array('id' => 1, 'product_id' => 1)));
21+
$customField = new ProductCustomField((object)(array('id' => 1, 'product_id' => 1)));
2422
$this->connection->expects($this->once())
2523
->method('put')
26-
->with($this->basePath . '/products/1/customfields/1', $customfield->getUpdateFields());
24+
->with($this->basePath . '/products/1/customfields/1', $customField->getUpdateFields());
2725

28-
$customfield->update();
26+
$customField->update();
2927
}
3028

3129
public function testDeletePassesThroughToConnection()
3230
{
33-
$customfield = new ProductCustomField((object)(array('id' => 1, 'product_id' => 1)));
31+
$customField = new ProductCustomField((object)(array('id' => 1, 'product_id' => 1)));
3432
$this->connection->expects($this->once())
3533
->method('delete')
3634
->with($this->basePath . '/products/1/customfields/1');
3735

38-
$customfield->delete();
36+
$customField->delete();
3937
}
4038
}

0 commit comments

Comments
 (0)