Skip to content

Commit 345d27d

Browse files
committed
Merge pull request #173 from bc-ruth/OMNI-1259
OMNI-1259 Expose product image resource to Client API
2 parents d261a4f + 2d8d2ee commit 345d27d

4 files changed

Lines changed: 103 additions & 11 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ public static function getRequestsRemaining()
13021302
$limit = self::connection()->getHeader('X-BC-ApiLimit-Remaining');
13031303
}
13041304

1305-
return intval($limit);
1305+
return (int)$limit;
13061306
}
13071307

13081308
/**
@@ -1471,4 +1471,53 @@ public static function getCurrenciesCount($filter = array())
14711471
$filter = Filter::create($filter);
14721472
return self::getCount('/currencies/count' . $filter->toQuery());
14731473
}
1474+
1475+
/**
1476+
* Create a new product image.
1477+
*
1478+
* @param string $productId
1479+
* @param mixed $object
1480+
* @return mixed
1481+
*/
1482+
public static function createProductImage($productId, $object)
1483+
{
1484+
return self::createResource('/products/' . $productId . '/images', $object);
1485+
}
1486+
1487+
/**
1488+
* Update a product image.
1489+
*
1490+
* @param string $productId
1491+
* @param string $imageId
1492+
* @param mixed $object
1493+
* @return mixed
1494+
*/
1495+
public static function updateProductImage($productId, $imageId, $object)
1496+
{
1497+
return self::updateResource('/products/' . $productId . '/images/' . $imageId, $object);
1498+
}
1499+
1500+
/**
1501+
* Returns a product image resource by the given product id.
1502+
*
1503+
* @param int $productId
1504+
* @param int $imageId
1505+
* @return Resources\ProductImage|string
1506+
*/
1507+
public static function getProductImage($productId, $imageId)
1508+
{
1509+
return self::getResource('/products/' . $productId . '/images/' . $imageId, 'ProductImage');
1510+
}
1511+
1512+
/**
1513+
* Delete the given product image.
1514+
*
1515+
* @param int $productId
1516+
* @param int $imageId
1517+
* @return mixed
1518+
*/
1519+
public static function deleteProductImage($productId, $imageId)
1520+
{
1521+
return self::deleteResource('/products/' . $productId . '/images/' . $imageId);
1522+
}
14741523
}

src/Bigcommerce/Api/Resources/ProductImage.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ class ProductImage extends Resource
2424

2525
public function create()
2626
{
27-
return Client::createResource('/products/' . $this->fields->product_id . '/images', $this->getCreateFields());
27+
return Client::createProductImage($this->product_id, $this->getCreateFields());
2828
}
2929

3030
public function update()
3131
{
32-
Client::updateResource(
33-
'/products/' . $this->fields->product_id . '/images/' . $this->id,
34-
$this->getUpdateFields()
35-
);
32+
return Client::updateProductImage($this->product_id, $this->id, $this->getUpdateFields());
3633
}
37-
34+
3835
public function delete()
3936
{
40-
Client::deleteResource('/products/' . $this->product_id . '/images/' . $this->id);
37+
return Client::deleteProductImage($this->product_id, $this->id);
4138
}
4239
}

test/Unit/Api/ClientTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,7 @@ public function testGettingProductImagesReturnsCollectionOfProductImages()
400400

401401
$collection = Client::getProductImages(1);
402402
$this->assertInternalType('array', $collection);
403-
foreach ($collection as $resource) {
404-
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\ProductImage', $resource);
405-
}
403+
$this->assertContainsOnlyInstancesOf('Bigcommerce\\Api\\Resources\\ProductImage', $collection);
406404
}
407405

408406
public function testGettingProductCustomFieldsReturnsCollectionOfProductCustomFields()
@@ -419,6 +417,17 @@ public function testGettingProductCustomFieldsReturnsCollectionOfProductCustomFi
419417
}
420418
}
421419

420+
public function testGettingASpecifiedProductImageReturnsThatProductImage()
421+
{
422+
$this->connection->expects($this->once())
423+
->method('get')
424+
->with($this->basePath . '/products/1/images/1', false)
425+
->will($this->returnValue(array(array(), array())));
426+
427+
$resource = Client::getProductImage(1, 1);
428+
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\ProductImage', $resource);
429+
}
430+
422431
public function testGettingASpecifiedProductCustomFieldReturnsThatProductCustomField()
423432
{
424433
$this->connection->expects($this->once())
@@ -496,6 +505,15 @@ public function testCreatingAnOptionSetOptionPostsToTheOptionSetsOptionsResource
496505
Client::createOptionSetOption(array(), 1);
497506
}
498507

508+
public function testCreatingAProductImagePostsToTheProductImageResource()
509+
{
510+
$this->connection->expects($this->once())
511+
->method('post')
512+
->with($this->basePath . '/products/1/images', (object)array());
513+
514+
Client::createProductImage(1, array());
515+
}
516+
499517
public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResource()
500518
{
501519
$this->connection->expects($this->once())
@@ -505,6 +523,15 @@ public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResou
505523
Client::createProductCustomField(1, array());
506524
}
507525

526+
public function testUpdatingAProductImagePutsToTheProductImageResource()
527+
{
528+
$this->connection->expects($this->once())
529+
->method('put')
530+
->with($this->basePath . '/products/1/images/1', (object)array());
531+
532+
Client::updateProductImage(1, 1, array());
533+
}
534+
508535
public function testUpdatingAProductCustomFieldPutsToTheProductCustomFieldResource()
509536
{
510537
$this->connection->expects($this->once())
@@ -514,6 +541,15 @@ public function testUpdatingAProductCustomFieldPutsToTheProductCustomFieldResour
514541
Client::updateProductCustomField(1, 1, array());
515542
}
516543

544+
public function testDeletingAProductImageDeletesToTheProductImageResource()
545+
{
546+
$this->connection->expects($this->once())
547+
->method('delete')
548+
->with($this->basePath . '/products/1/images/1');
549+
550+
Client::deleteProductImage(1, 1);
551+
}
552+
517553
public function testDeletingAProductCustomFieldDeletesToTheProductCustomFieldResource()
518554
{
519555
$this->connection->expects($this->once())

test/Unit/Api/Resources/ProductImageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ public function testUpdatePassesThroughToConnection()
2525

2626
$productImage->update();
2727
}
28+
29+
public function testDeletePassesThroughToConnection()
30+
{
31+
$productImage = new ProductImage((object)(array('id' => 1, 'product_id' => 1)));
32+
$this->connection->expects($this->once())
33+
->method('delete')
34+
->with($this->basePath . '/products/1/images/1');
35+
36+
$productImage->delete();
37+
}
2838
}

0 commit comments

Comments
 (0)