Skip to content

Commit

Permalink
Merge pull request #200 from bc-fabiob/SHIPPING-249
Browse files Browse the repository at this point in the history
Add shipping zone and method to client api
  • Loading branch information
zvuki authored May 19, 2017
2 parents a03188a + 222560e commit 94e5618
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Bigcommerce/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1943,4 +1943,73 @@ public static function deleteWebhook($id)
{
return self::deleteResource('/hooks/' . $id);
}

/**
* Return a collection of shipping-zones
*
* @return mixed
*/
public static function getShippingZones()
{
return self::getCollection('/shipping/zones/', 'ShippingZone');
}

/**
* Return a shipping-zone by id
*
* @param int $id shipping-zone id
* @return mixed
*/
public static function getShippingZone($id)
{
return self::getResource('/shipping/zones/' . $id, 'ShippingZone');
}


/**
* Delete the given shipping-zone
*
* @param int $id shipping-zone id
* @return mixed
*/
public static function deleteShippingZone($id)
{
return self::deleteResource('/shipping/zones/' . $id);
}

/**
* Return a shipping-method by id
*
* @param $zoneId
* @param $methodId
* @return mixed
*/
public static function getShippingMethod($zoneId, $methodId)
{
return self::getResource('/shipping/zones/'. $zoneId . '/methods/'. $methodId, 'ShippingMethod');
}

/**
* Return a collection of shipping-methods
*
* @param $zoneId
* @return mixed
*/
public static function getShippingMethods($zoneId)
{
return self::getCollection('/shipping/zones/' . $zoneId . '/methods', 'ShippingMethod');
}


/**
* Delete the given shipping-method by id
*
* @param $zoneId
* @param $methodId
* @return mixed
*/
public static function deleteShippingMethod($zoneId, $methodId)
{
return self::deleteResource('/shipping/zones/'. $zoneId . '/methods/'. $methodId);
}
}
27 changes: 27 additions & 0 deletions src/Bigcommerce/Api/Resources/ShippingMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bigcommerce\Api\Resources;

use Bigcommerce\Api\Client;
use Bigcommerce\Api\Resource;

class ShippingMethod extends Resource
{
protected $ignoreOnCreate = array(
'id',
);

protected $ignoreOnUpdate = array(
'id',
);

public function create($zoneId)
{
return Client::createResource('/shipping/zones/' . $zoneId . '/methods', $this->getCreateFields());
}

public function update($zoneId)
{
return Client::updateResource('/shipping/zones/' . $zoneId . '/methods/' . $this->id, $this->getUpdateFields());
}
}
27 changes: 27 additions & 0 deletions src/Bigcommerce/Api/Resources/ShippingZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bigcommerce\Api\Resources;

use Bigcommerce\Api\Client;
use Bigcommerce\Api\Resource;

class ShippingZone extends Resource
{
protected $ignoreOnCreate = array(
'id',
);

protected $ignoreOnUpdate = array(
'id',
);

public function create()
{
return Client::createResource('/shipping/zones/', $this->getCreateFields());
}

public function update()
{
return Client::updateResource('/shipping/zones/'. $this->id, $this->getUpdateFields());
}
}
Empty file.
105 changes: 105 additions & 0 deletions test/Unit/Api/Resources/ShippingMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Bigcommerce\Test\Unit\Api\Resources;

use Bigcommerce\Api\Client;
use Bigcommerce\Api\Resources\ShippingMethod;

class ShippingMethodTest extends ResourceTestBase
{
public function testCreateShippingMethod()
{
$input = array(
"name" => "USPS Endicia",
"type" => "endicia",
"settings" => array(
"carrier_options" => array(
"delivery_services" => array(
"PriorityExpress",
"PriorityMailExpressInternational",
"FirstClassPackageInternationalService",
"Priority",
"PriorityMailInternational",
"First",
"ParcelSelect",
"MediaMail"
),
"packaging" => array(
"FlatRateLegalEnvelope",
"FlatRatePaddedEnvelope",
"Parcel",
"SmallFlatRateBox",
"MediumFlatRateBox",
"LargeFlatRateBox",
"FlatRateEnvelope",
"RegionalRateBoxA",
"RegionalRateBoxB"
),
"show_transit_time" => true,
)
),
"enabled" => true
);
$method = new ShippingMethod((object)$input);
$this->connection->expects($this->once())
->method('post')
->with($this->basePath . '/shipping/zones/1/methods', (object)$input);

$method->create(1);
}

public function testUpdateShippingMethod()
{
$input = array(
"name" => "Ship by Weight",
"type" => "weight",
"settings" => array(
"default_cost" => 1,
"default_cost_type" => "fixed_amount",
"range" => array(
array(
"lower_limit" => 0,
"upper_limit" => 20,
"shipping_cost" => 8
)
)
),
"enabled" => true
);
$updateResource = array_merge(array('id' => 1), $input);
$method = new ShippingMethod((object)$updateResource);
$this->connection->expects($this->once())
->method('put')
->with($this->basePath . '/shipping/zones/1/methods/1', (object)$input);

$method->update(1);
}

public function testGetShippingMethod()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/shipping/zones/1/methods/2');

Client::getShippingMethod(1, 2);
}

public function testGetShippingMethods()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/shipping/zones/1/methods');

Client::getShippingMethods(1);
}

public function testDeleteShippingMethod()
{

$this->connection->expects($this->once())
->method('delete')
->with($this->basePath . '/shipping/zones/1/methods/2');

Client::deleteShippingMethod(1, 2);
}
}
73 changes: 73 additions & 0 deletions test/Unit/Api/Resources/ShippingZoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Bigcommerce\Test\Unit\Api\Resources;

use Bigcommerce\Api\Client;
use Bigcommerce\Api\Resources\ShippingZone;

class ShippingZoneTest extends ResourceTestBase
{
public function testCreateShippingZone()
{
$input = array(
'name' => 'United States',
'type' => 'country',
'locations' => array(
array('country_iso2' => 'US'),
),
);
$zone = new ShippingZone((object)$input);
$this->connection->expects($this->once())
->method('post')
->with($this->basePath . '/shipping/zones/', (object)$input);

$zone->create();
}

public function testUpdateShippingZone()
{
$input = array(
'name' => 'United States',
'type' => 'country',
'locations' => array(
array('country_iso2' => 'US'),
),
);
$updateResource = array_merge(array('id' => 1), $input);
$zone = new ShippingZone((object)$updateResource);

$this->connection->expects($this->once())
->method('put')
->with($this->basePath . '/shipping/zones/1', (object)$input);

$zone->update();
}

public function testGetShippingZone()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/shipping/zones/1');

Client::getShippingZone(1);
}

public function testGetShippingZones()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/shipping/zones/');

Client::getShippingZones();
}

public function testDeleteShippingZone()
{

$this->connection->expects($this->once())
->method('delete')
->with($this->basePath . '/shipping/zones/1');

Client::deleteShippingZone(1);
}
}

0 comments on commit 94e5618

Please sign in to comment.