Skip to content

Commit

Permalink
Merge pull request #162 from zvuki/API-16
Browse files Browse the repository at this point in the history
API-16 fix for SkuOption error on Sku resource
  • Loading branch information
zvuki committed Jan 27, 2016
2 parents 0589592 + 7a90396 commit a7a892d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require-dev": {
"phpunit/phpunit": "4.5.*",
"satooshi/php-coveralls": "dev-master",
"satooshi/php-coveralls": "1.0",
"codeless/jugglecode": "1.0"
},
"autoload": {
Expand Down
10 changes: 7 additions & 3 deletions src/Bigcommerce/Api/Resources/Sku.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class Sku extends Resource

public function options()
{
$options = Client::getCollection($this->fields->options->resource, 'SkuOption');
$options = array();

foreach ($options as $option) {
$option->product_id = $this->product_id;
if (!isset($this->fields->options)) {
return $options;
}

foreach ($this->fields->options as $option) {
$options[] = new SkuOption((object)$option);
}

return $options;
Expand Down
42 changes: 23 additions & 19 deletions test/Unit/Api/Resources/SkuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

use Bigcommerce\Api\Resources\Sku;
use Bigcommerce\Api\Client;
use Bigcommerce\Api\Resources\SkuOption;

class SkuTest extends ResourceTestBase
{
public function testCreatePassesThroughToConnection()
{
$sku = new Sku((object)array('id' => 1, 'product_id' => 1));
$sku = $this->getSimpleSku();
$this->connection->expects($this->once())
->method('post')
->with($this->basePath . '/products/1/skus', (object)array('id' => 1));
Expand All @@ -18,31 +19,34 @@ public function testCreatePassesThroughToConnection()

public function testUpdatePassesThroughToConnection()
{
$sku = new Sku((object)array('id' => 1, 'product_id' => 1));
$sku = $sku = $this->getSimpleSku();
$this->connection->expects($this->once())
->method('put')
->with($this->basePath . '/products/1/skus/1', (object)array());

$sku->update();
}

public function testOptionsPassesThroughToConnection()
public function testSkuHasOptions()
{
$sku = new Sku((object)array(
'product_id' => 1,
'options' => (object)array(
'resource' => '/products/1/skus/1/options'
)
));
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/products/1/skus/1/options')
->will($this->returnValue(array(array(), array())));

$collection = $sku->options;
$this->assertInternalType('array', $collection);
foreach ($collection as $condition) {
$this->assertInstanceOf('Bigcommerce\\Api\\Resources\\SkuOption', $condition);
}
$sku = new Sku((object)array('id' => 1, 'product_id' => 1, 'options' => array(
array('option_value_id' => 1, 'product_option_id' => 1)
)));

$this->assertInstanceOf('Bigcommerce\Api\Resources\SkuOption', $sku->options[0]);

$this->assertEquals(1, $sku->options[0]->option_value_id);
$this->assertEquals(1, $sku->options[0]->product_option_id);
}

public function testSkuHasNoOptions()
{
$sku = $this->getSimpleSku();
$this->assertEmpty($sku->options);
}

private function getSimpleSku()
{
return new Sku((object)array('id' => 1, 'product_id' => 1));
}
}

0 comments on commit a7a892d

Please sign in to comment.