Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
feat(ShippingMethod): support shipping method by key functionality
Browse files Browse the repository at this point in the history
Closes #329
  • Loading branch information
Jens Schulze committed Aug 9, 2017
1 parent f7291ae commit 3ba9596
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Core/Model/ShippingMethod/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* @method ShippingMethod setZoneRates(ZoneRateCollection $zoneRates = null)
* @method bool getIsDefault()
* @method ShippingMethod setIsDefault(bool $isDefault = null)
* @method string getKey()
* @method ShippingMethod setKey(string $key = null)
* @method ShippingMethodReference getReference()
*/
class ShippingMethod extends Resource
Expand All @@ -52,7 +54,8 @@ public function fieldDefinitions()
'description' => [static::TYPE => 'string'],
'taxCategory' => [static::TYPE => TaxCategoryReference::class],
'zoneRates' => [static::TYPE => ZoneRateCollection::class],
'isDefault' => [static::TYPE => 'bool']
'isDefault' => [static::TYPE => 'bool'],
'key' => [static::TYPE => 'string'],
];
}
}
3 changes: 3 additions & 0 deletions src/Core/Model/ShippingMethod/ShippingMethodDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @method ShippingMethodDraft setZoneRates(ZoneRateCollection $zoneRates = null)
* @method bool getIsDefault()
* @method ShippingMethodDraft setIsDefault(bool $isDefault = null)
* @method string getKey()
* @method ShippingMethodDraft setKey(string $key = null)
*/
class ShippingMethodDraft extends JsonObject
{
Expand All @@ -33,6 +35,7 @@ public function fieldDefinitions()
'taxCategory' => [static::TYPE => TaxCategoryReference::class],
'zoneRates' => [static::TYPE => ZoneRateCollection::class],
'isDefault' => [static::TYPE => 'bool'],
'key' => [static::TYPE => 'string'],
];
}

Expand Down
42 changes: 42 additions & 0 deletions src/Core/Request/ShippingMethods/ShippingMethodByKeyGetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\ShippingMethods;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
use Commercetools\Core\Request\AbstractByKeyGetRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\ShippingMethods
* @link https://dev.commercetools.com/http-api-projects-shippingMethods.html#get-shippingmethod-by-key
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class ShippingMethodByKeyGetRequest extends AbstractByKeyGetRequest
{
protected $resultClass = ShippingMethod::class;

/**
* @param string $key
* @param Context $context
*/
public function __construct($key, Context $context = null)
{
parent::__construct(ShippingMethodsEndpoint::endpoint(), $key, $context);
}

/**
* @param string $key
* @param Context $context
* @return static
*/
public static function ofKey($key, Context $context = null)
{
return new static($key, $context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\ShippingMethods;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\ShippingMethods
* @link http://dev.commercetools.com/http-api-projects-shippingMethods.html#delete-shippingmethod-by-key
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class ShippingMethodDeleteByKeyRequest extends AbstractDeleteByKeyRequest
{
protected $resultClass = ShippingMethod::class;

/**
* @param string $id
* @param int $version
* @param Context $context
*/
public function __construct($id, $version, Context $context = null)
{
parent::__construct(ShippingMethodsEndpoint::endpoint(), $id, $version, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, $context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\ShippingMethods;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\ShippingMethods
* @link http://dev.commercetools.com/http-api-projects-shippingMethods.html#update-shippingmethod-by-key
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class ShippingMethodUpdateByKeyRequest extends AbstractUpdateByKeyRequest
{
protected $resultClass = ShippingMethod::class;

/**
* @param string $key
* @param string $version
* @param array $actions
* @param Context $context
*/
public function __construct($key, $version, array $actions = [], Context $context = null)
{
parent::__construct(ShippingMethodsEndpoint::endpoint(), $key, $version, $actions, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, [], $context);
}
}
1 change: 1 addition & 0 deletions tests/fixtures/models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ shippingMethod:
- taxCategory
- zoneRates
- isDefault
- key

zoneRate:
domain: shippingMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetDescriptionAction;
use Commercetools\Core\Request\ShippingMethods\ShippingMethodCreateRequest;
use Commercetools\Core\Request\ShippingMethods\ShippingMethodDeleteRequest;
use Commercetools\Core\Request\ShippingMethods\ShippingMethodUpdateByKeyRequest;
use Commercetools\Core\Request\ShippingMethods\ShippingMethodUpdateRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryCreateRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryDeleteRequest;
Expand Down Expand Up @@ -75,6 +76,26 @@ protected function createShippingMethod(ShippingMethodDraft $draft)
return $shippingMethod;
}

public function testUpdateByKey()
{
$draft = $this->getDraft('update-by-key');
$draft->setKey('test-' . $this->getTestRun() . '-update-by-key');
$shippingMethod = $this->createShippingMethod($draft);

$text = 'test-' . $this->getTestRun() . '-new-name';
$request = ShippingMethodUpdateByKeyRequest::ofKeyAndVersion($shippingMethod->getKey(), $shippingMethod->getVersion())
->addAction(
ShippingMethodChangeNameAction::ofName($text)
)
;
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->deleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(ShippingMethod::class, $result);
$this->assertSame($text, $result->getName());
}

public function testChangeName()
{
$draft = $this->getDraft('change-name');
Expand Down

0 comments on commit 3ba9596

Please # to comment.