Skip to content

Commit

Permalink
Merge pull request #173 from bc-ruth/OMNI-1259
Browse files Browse the repository at this point in the history
OMNI-1259 Expose product image resource to Client API
  • Loading branch information
philipmuir committed Jun 9, 2016
2 parents d261a4f + 2d8d2ee commit 345d27d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
51 changes: 50 additions & 1 deletion src/Bigcommerce/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ public static function getRequestsRemaining()
$limit = self::connection()->getHeader('X-BC-ApiLimit-Remaining');
}

return intval($limit);
return (int)$limit;
}

/**
Expand Down Expand Up @@ -1471,4 +1471,53 @@ public static function getCurrenciesCount($filter = array())
$filter = Filter::create($filter);
return self::getCount('/currencies/count' . $filter->toQuery());
}

/**
* Create a new product image.
*
* @param string $productId
* @param mixed $object
* @return mixed
*/
public static function createProductImage($productId, $object)
{
return self::createResource('/products/' . $productId . '/images', $object);
}

/**
* Update a product image.
*
* @param string $productId
* @param string $imageId
* @param mixed $object
* @return mixed
*/
public static function updateProductImage($productId, $imageId, $object)
{
return self::updateResource('/products/' . $productId . '/images/' . $imageId, $object);
}

/**
* Returns a product image resource by the given product id.
*
* @param int $productId
* @param int $imageId
* @return Resources\ProductImage|string
*/
public static function getProductImage($productId, $imageId)
{
return self::getResource('/products/' . $productId . '/images/' . $imageId, 'ProductImage');
}

/**
* Delete the given product image.
*
* @param int $productId
* @param int $imageId
* @return mixed
*/
public static function deleteProductImage($productId, $imageId)
{
return self::deleteResource('/products/' . $productId . '/images/' . $imageId);
}
}
11 changes: 4 additions & 7 deletions src/Bigcommerce/Api/Resources/ProductImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ class ProductImage extends Resource

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

public function update()
{
Client::updateResource(
'/products/' . $this->fields->product_id . '/images/' . $this->id,
$this->getUpdateFields()
);
return Client::updateProductImage($this->product_id, $this->id, $this->getUpdateFields());
}

public function delete()
{
Client::deleteResource('/products/' . $this->product_id . '/images/' . $this->id);
return Client::deleteProductImage($this->product_id, $this->id);
}
}
42 changes: 39 additions & 3 deletions test/Unit/Api/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ public function testGettingProductImagesReturnsCollectionOfProductImages()

$collection = Client::getProductImages(1);
$this->assertInternalType('array', $collection);
foreach ($collection as $resource) {
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\ProductImage', $resource);
}
$this->assertContainsOnlyInstancesOf('Bigcommerce\\Api\\Resources\\ProductImage', $collection);
}

public function testGettingProductCustomFieldsReturnsCollectionOfProductCustomFields()
Expand All @@ -419,6 +417,17 @@ public function testGettingProductCustomFieldsReturnsCollectionOfProductCustomFi
}
}

public function testGettingASpecifiedProductImageReturnsThatProductImage()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/products/1/images/1', false)
->will($this->returnValue(array(array(), array())));

$resource = Client::getProductImage(1, 1);
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\ProductImage', $resource);
}

public function testGettingASpecifiedProductCustomFieldReturnsThatProductCustomField()
{
$this->connection->expects($this->once())
Expand Down Expand Up @@ -496,6 +505,15 @@ public function testCreatingAnOptionSetOptionPostsToTheOptionSetsOptionsResource
Client::createOptionSetOption(array(), 1);
}

public function testCreatingAProductImagePostsToTheProductImageResource()
{
$this->connection->expects($this->once())
->method('post')
->with($this->basePath . '/products/1/images', (object)array());

Client::createProductImage(1, array());
}

public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResource()
{
$this->connection->expects($this->once())
Expand All @@ -505,6 +523,15 @@ public function testCreatingAProductCustomFieldPostsToTheProductCustomFieldResou
Client::createProductCustomField(1, array());
}

public function testUpdatingAProductImagePutsToTheProductImageResource()
{
$this->connection->expects($this->once())
->method('put')
->with($this->basePath . '/products/1/images/1', (object)array());

Client::updateProductImage(1, 1, array());
}

public function testUpdatingAProductCustomFieldPutsToTheProductCustomFieldResource()
{
$this->connection->expects($this->once())
Expand All @@ -514,6 +541,15 @@ public function testUpdatingAProductCustomFieldPutsToTheProductCustomFieldResour
Client::updateProductCustomField(1, 1, array());
}

public function testDeletingAProductImageDeletesToTheProductImageResource()
{
$this->connection->expects($this->once())
->method('delete')
->with($this->basePath . '/products/1/images/1');

Client::deleteProductImage(1, 1);
}

public function testDeletingAProductCustomFieldDeletesToTheProductCustomFieldResource()
{
$this->connection->expects($this->once())
Expand Down
10 changes: 10 additions & 0 deletions test/Unit/Api/Resources/ProductImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ public function testUpdatePassesThroughToConnection()

$productImage->update();
}

public function testDeletePassesThroughToConnection()
{
$productImage = new ProductImage((object)(array('id' => 1, 'product_id' => 1)));
$this->connection->expects($this->once())
->method('delete')
->with($this->basePath . '/products/1/images/1');

$productImage->delete();
}
}

0 comments on commit 345d27d

Please sign in to comment.