Skip to content

Commit 15c1d16

Browse files
committed
Merge pull request #142 from bc-bfenton/tests
Implementing more tests and adding/fixing methods
2 parents 6a25425 + 4389bb8 commit 15c1d16

11 files changed

Lines changed: 298 additions & 142 deletions

src/Bigcommerce/Api/Client.php

Lines changed: 53 additions & 8 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
*
@@ -1007,9 +1017,34 @@ public static function getOptionSet($id)
10071017
}
10081018

10091019
/**
1010-
* Status codes used to represent the state of an order.
1020+
* Update the given option set.
10111021
*
1012-
* @return array
1022+
* @param int $id option set id
1023+
* @param mixed $object
1024+
* @return mixed
1025+
*/
1026+
public static function updateOptionSet($id, $object)
1027+
{
1028+
return self::updateResource('/optionsets/' . $id, $object);
1029+
}
1030+
1031+
/**
1032+
* Delete the given option set.
1033+
*
1034+
* @param int $id option id
1035+
* @return mixed
1036+
*/
1037+
public static function deleteOptionSet($id)
1038+
{
1039+
Client::deleteResource('/optionsets/' . $id);
1040+
}
1041+
1042+
/**
1043+
* Status code used to represent the state of an order.
1044+
*
1045+
* @param int $id order status id
1046+
*
1047+
* @return mixed
10131048
*/
10141049
public static function getOrderStatus($id)
10151050
{
@@ -1061,6 +1096,16 @@ public static function updateSku($id, $object)
10611096
return self::updateResource('/product/skus/' . $id, $object);
10621097
}
10631098

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

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: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ public function testGettingASpecificResourceReturnsACollectionOfThatResource($pa
286286
*/
287287
public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($path, $fnName, $class)
288288
{
289-
if (in_array($path, array('order_statuses', 'products/skus', 'requestlogs'))) {
290-
$this->markTestSkipped(sprintf('The PHP client does not support getting the count of %s', $path));
289+
if (in_array($path, array('order_statuses', 'requestlogs'))) {
290+
//$this->markTestSkipped(sprintf('The API does not currently support getting the count of %s', $path));
291+
return;
291292
}
292293

293294
$this->connection->expects($this->once())
@@ -303,15 +304,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat
303304
public function resources()
304305
{
305306
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'),
307+
// path function classname
308+
array('products', '%sProduct', 'Product'),
309+
array('brands', '%sBrand', 'Brand'),
310+
array('orders', '%sOrder', 'Order'),
311+
array('customers', '%sCustomer', 'Customer'),
312+
array('categories', '%sCategory', 'Category'),
313+
array('options', '%sOption', 'Option'),
314+
array('optionsets', '%sOptionSet', 'OptionSet'),
315+
array('coupons', '%sCoupon', 'Coupon'),
315316
);
316317
}
317318

@@ -320,10 +321,6 @@ public function resources()
320321
*/
321322
public function testGettingASpecificResourceReturnsThatResource($path, $fnName, $class)
322323
{
323-
if (in_array($path, array('coupons'))) {
324-
$this->markTestSkipped(sprintf('The php client does not support getting a specified %s', $path));
325-
}
326-
327324
$this->connection->expects($this->once())
328325
->method('get')
329326
->with($this->basePath . '/' . $path . '/1', false)
@@ -339,10 +336,6 @@ public function testGettingASpecificResourceReturnsThatResource($path, $fnName,
339336
*/
340337
public function testCreatingASpecificResourcePostsToThatResource($path, $fnName, $class)
341338
{
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-
346339
$this->connection->expects($this->once())
347340
->method('post')
348341
->with($this->basePath . '/' . $path, (object)array());
@@ -356,10 +349,6 @@ public function testCreatingASpecificResourcePostsToThatResource($path, $fnName,
356349
*/
357350
public function testDeletingASpecificResourceDeletesToThatResource($path, $fnName, $class)
358351
{
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-
363352
$this->connection->expects($this->once())
364353
->method('delete')
365354
->with($this->basePath . '/' . $path . '/1');
@@ -373,10 +362,6 @@ public function testDeletingASpecificResourceDeletesToThatResource($path, $fnNam
373362
*/
374363
public function testUpdatingASpecificResourcePutsToThatResource($path, $fnName, $class)
375364
{
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-
380365
$this->connection->expects($this->once())
381366
->method('put')
382367
->with($this->basePath . '/' . $path . '/1');
@@ -488,7 +473,7 @@ public function testCreatingAnOptionSetPostsToTheOptionSetsResource()
488473
->method('post')
489474
->with($this->basePath . '/optionsets', (object)array());
490475

491-
Client::createOptionsets(array());
476+
Client::createOptionSet(array());
492477
}
493478

494479
public function testCreatingAnOptionPostsToTheOptionResource()
@@ -497,16 +482,16 @@ public function testCreatingAnOptionPostsToTheOptionResource()
497482
->method('post')
498483
->with($this->basePath . '/options', (object)array());
499484

500-
Client::createOptions(array());
485+
Client::createOption(array());
501486
}
502487

503-
public function testCreatingAnOptionSetsOptionPostsToTheOptionSetsOptionResource()
488+
public function testCreatingAnOptionSetOptionPostsToTheOptionSetsOptionsResource()
504489
{
505490
$this->connection->expects($this->once())
506491
->method('post')
507492
->with($this->basePath . '/optionsets/1/options', (object)array());
508493

509-
Client::createOptionsetsOptions(array(), 1);
494+
Client::createOptionSetOption(array(), 1);
510495
}
511496

512497
public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResource()
@@ -594,6 +579,33 @@ public function testDeletingAllOrdersDeletesToTheOrderResource()
594579
Client::deleteAllOrders();
595580
}
596581

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

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

test/Unit/Api/ConnectionTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Bigcommerce\Test\Unit;
4+
5+
use Bigcommerce\Api\Connection;
6+
7+
class ConnectionTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @var \Bigcommerce\Api\Connection;
11+
*/
12+
protected $object;
13+
14+
public function setUp()
15+
{
16+
$this->object = new Connection();
17+
}
18+
19+
public function testFailOnError()
20+
{
21+
$this->object->failOnError(false);
22+
$this->assertAttributeSame(false, 'failOnError', $this->object);
23+
$this->object->failOnError(true);
24+
$this->assertAttributeSame(true, 'failOnError', $this->object);
25+
}
26+
27+
public function testAddHeader()
28+
{
29+
$this->object->addHeader('Content-Length', 4);
30+
$this->assertAttributeContains('Content-Length: 4', 'headers', $this->object);
31+
}
32+
33+
/**
34+
* @depends testAddHeader
35+
*/
36+
public function testRemoveHeader()
37+
{
38+
$this->object->addHeader('Content-Length', 4);
39+
$this->object->removeHeader('Content-Length');
40+
$this->assertAttributeNotContains('Content-Length: 4', 'headers', $this->object);
41+
}
42+
}

0 commit comments

Comments
 (0)