From 3ba959637f2d3be5ae1b2259b1c6939039e26f14 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 9 Aug 2017 16:33:05 +0200 Subject: [PATCH] feat(ShippingMethod): support shipping method by key functionality Closes #329 --- .../Model/ShippingMethod/ShippingMethod.php | 5 ++- .../ShippingMethod/ShippingMethodDraft.php | 3 ++ .../ShippingMethodByKeyGetRequest.php | 42 +++++++++++++++++ .../ShippingMethodDeleteByKeyRequest.php | 44 ++++++++++++++++++ .../ShippingMethodUpdateByKeyRequest.php | 45 +++++++++++++++++++ tests/fixtures/models.yaml | 1 + .../ShippingMethodUpdateRequestTest.php | 21 +++++++++ 7 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/Core/Request/ShippingMethods/ShippingMethodByKeyGetRequest.php create mode 100644 src/Core/Request/ShippingMethods/ShippingMethodDeleteByKeyRequest.php create mode 100644 src/Core/Request/ShippingMethods/ShippingMethodUpdateByKeyRequest.php diff --git a/src/Core/Model/ShippingMethod/ShippingMethod.php b/src/Core/Model/ShippingMethod/ShippingMethod.php index d5808272e4..13cdf5ff72 100644 --- a/src/Core/Model/ShippingMethod/ShippingMethod.php +++ b/src/Core/Model/ShippingMethod/ShippingMethod.php @@ -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 @@ -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'], ]; } } diff --git a/src/Core/Model/ShippingMethod/ShippingMethodDraft.php b/src/Core/Model/ShippingMethod/ShippingMethodDraft.php index e521ad6b31..5d8c46c574 100644 --- a/src/Core/Model/ShippingMethod/ShippingMethodDraft.php +++ b/src/Core/Model/ShippingMethod/ShippingMethodDraft.php @@ -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 { @@ -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'], ]; } diff --git a/src/Core/Request/ShippingMethods/ShippingMethodByKeyGetRequest.php b/src/Core/Request/ShippingMethods/ShippingMethodByKeyGetRequest.php new file mode 100644 index 0000000000..eec3601e23 --- /dev/null +++ b/src/Core/Request/ShippingMethods/ShippingMethodByKeyGetRequest.php @@ -0,0 +1,42 @@ + + */ + +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); + } +} diff --git a/src/Core/Request/ShippingMethods/ShippingMethodDeleteByKeyRequest.php b/src/Core/Request/ShippingMethods/ShippingMethodDeleteByKeyRequest.php new file mode 100644 index 0000000000..fa7db31dcc --- /dev/null +++ b/src/Core/Request/ShippingMethods/ShippingMethodDeleteByKeyRequest.php @@ -0,0 +1,44 @@ + + */ + +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); + } +} diff --git a/src/Core/Request/ShippingMethods/ShippingMethodUpdateByKeyRequest.php b/src/Core/Request/ShippingMethods/ShippingMethodUpdateByKeyRequest.php new file mode 100644 index 0000000000..adaf1592fa --- /dev/null +++ b/src/Core/Request/ShippingMethods/ShippingMethodUpdateByKeyRequest.php @@ -0,0 +1,45 @@ + + */ + +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); + } +} diff --git a/tests/fixtures/models.yaml b/tests/fixtures/models.yaml index 2fe03c6f1f..b8439896f2 100644 --- a/tests/fixtures/models.yaml +++ b/tests/fixtures/models.yaml @@ -800,6 +800,7 @@ shippingMethod: - taxCategory - zoneRates - isDefault + - key zoneRate: domain: shippingMethod diff --git a/tests/integration/ShippingMethod/ShippingMethodUpdateRequestTest.php b/tests/integration/ShippingMethod/ShippingMethodUpdateRequestTest.php index 8cb7b3ceed..014ab6a463 100644 --- a/tests/integration/ShippingMethod/ShippingMethodUpdateRequestTest.php +++ b/tests/integration/ShippingMethod/ShippingMethodUpdateRequestTest.php @@ -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; @@ -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');