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

Commit 3ba9596

Browse files
author
Jens Schulze
committed
feat(ShippingMethod): support shipping method by key functionality
Closes #329
1 parent f7291ae commit 3ba9596

File tree

7 files changed

+160
-1
lines changed

7 files changed

+160
-1
lines changed

src/Core/Model/ShippingMethod/ShippingMethod.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* @method ShippingMethod setZoneRates(ZoneRateCollection $zoneRates = null)
3232
* @method bool getIsDefault()
3333
* @method ShippingMethod setIsDefault(bool $isDefault = null)
34+
* @method string getKey()
35+
* @method ShippingMethod setKey(string $key = null)
3436
* @method ShippingMethodReference getReference()
3537
*/
3638
class ShippingMethod extends Resource
@@ -52,7 +54,8 @@ public function fieldDefinitions()
5254
'description' => [static::TYPE => 'string'],
5355
'taxCategory' => [static::TYPE => TaxCategoryReference::class],
5456
'zoneRates' => [static::TYPE => ZoneRateCollection::class],
55-
'isDefault' => [static::TYPE => 'bool']
57+
'isDefault' => [static::TYPE => 'bool'],
58+
'key' => [static::TYPE => 'string'],
5659
];
5760
}
5861
}

src/Core/Model/ShippingMethod/ShippingMethodDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @method ShippingMethodDraft setZoneRates(ZoneRateCollection $zoneRates = null)
2323
* @method bool getIsDefault()
2424
* @method ShippingMethodDraft setIsDefault(bool $isDefault = null)
25+
* @method string getKey()
26+
* @method ShippingMethodDraft setKey(string $key = null)
2527
*/
2628
class ShippingMethodDraft extends JsonObject
2729
{
@@ -33,6 +35,7 @@ public function fieldDefinitions()
3335
'taxCategory' => [static::TYPE => TaxCategoryReference::class],
3436
'zoneRates' => [static::TYPE => ZoneRateCollection::class],
3537
'isDefault' => [static::TYPE => 'bool'],
38+
'key' => [static::TYPE => 'string'],
3639
];
3740
}
3841

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\ShippingMethods;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
10+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\MapperInterface;
13+
14+
/**
15+
* @package Commercetools\Core\Request\ShippingMethods
16+
* @link https://dev.commercetools.com/http-api-projects-shippingMethods.html#get-shippingmethod-by-key
17+
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
18+
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
19+
*/
20+
class ShippingMethodByKeyGetRequest extends AbstractByKeyGetRequest
21+
{
22+
protected $resultClass = ShippingMethod::class;
23+
24+
/**
25+
* @param string $key
26+
* @param Context $context
27+
*/
28+
public function __construct($key, Context $context = null)
29+
{
30+
parent::__construct(ShippingMethodsEndpoint::endpoint(), $key, $context);
31+
}
32+
33+
/**
34+
* @param string $key
35+
* @param Context $context
36+
* @return static
37+
*/
38+
public static function ofKey($key, Context $context = null)
39+
{
40+
return new static($key, $context);
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\ShippingMethods;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
10+
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\MapperInterface;
13+
14+
/**
15+
* @package Commercetools\Core\Request\ShippingMethods
16+
* @link http://dev.commercetools.com/http-api-projects-shippingMethods.html#delete-shippingmethod-by-key
17+
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
18+
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
19+
*/
20+
class ShippingMethodDeleteByKeyRequest extends AbstractDeleteByKeyRequest
21+
{
22+
protected $resultClass = ShippingMethod::class;
23+
24+
/**
25+
* @param string $id
26+
* @param int $version
27+
* @param Context $context
28+
*/
29+
public function __construct($id, $version, Context $context = null)
30+
{
31+
parent::__construct(ShippingMethodsEndpoint::endpoint(), $id, $version, $context);
32+
}
33+
34+
/**
35+
* @param string $key
36+
* @param int $version
37+
* @param Context $context
38+
* @return static
39+
*/
40+
public static function ofKeyAndVersion($key, $version, Context $context = null)
41+
{
42+
return new static($key, $version, $context);
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\ShippingMethods;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\ShippingMethod\ShippingMethod;
10+
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\MapperInterface;
13+
14+
/**
15+
* @package Commercetools\Core\Request\ShippingMethods
16+
* @link http://dev.commercetools.com/http-api-projects-shippingMethods.html#update-shippingmethod-by-key
17+
* @method ShippingMethod mapResponse(ApiResponseInterface $response)
18+
* @method ShippingMethod mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
19+
*/
20+
class ShippingMethodUpdateByKeyRequest extends AbstractUpdateByKeyRequest
21+
{
22+
protected $resultClass = ShippingMethod::class;
23+
24+
/**
25+
* @param string $key
26+
* @param string $version
27+
* @param array $actions
28+
* @param Context $context
29+
*/
30+
public function __construct($key, $version, array $actions = [], Context $context = null)
31+
{
32+
parent::__construct(ShippingMethodsEndpoint::endpoint(), $key, $version, $actions, $context);
33+
}
34+
35+
/**
36+
* @param string $key
37+
* @param int $version
38+
* @param Context $context
39+
* @return static
40+
*/
41+
public static function ofKeyAndVersion($key, $version, Context $context = null)
42+
{
43+
return new static($key, $version, [], $context);
44+
}
45+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ shippingMethod:
800800
- taxCategory
801801
- zoneRates
802802
- isDefault
803+
- key
803804

804805
zoneRate:
805806
domain: shippingMethod

tests/integration/ShippingMethod/ShippingMethodUpdateRequestTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Commercetools\Core\Request\ShippingMethods\Command\ShippingMethodSetDescriptionAction;
3131
use Commercetools\Core\Request\ShippingMethods\ShippingMethodCreateRequest;
3232
use Commercetools\Core\Request\ShippingMethods\ShippingMethodDeleteRequest;
33+
use Commercetools\Core\Request\ShippingMethods\ShippingMethodUpdateByKeyRequest;
3334
use Commercetools\Core\Request\ShippingMethods\ShippingMethodUpdateRequest;
3435
use Commercetools\Core\Request\TaxCategories\TaxCategoryCreateRequest;
3536
use Commercetools\Core\Request\TaxCategories\TaxCategoryDeleteRequest;
@@ -75,6 +76,26 @@ protected function createShippingMethod(ShippingMethodDraft $draft)
7576
return $shippingMethod;
7677
}
7778

79+
public function testUpdateByKey()
80+
{
81+
$draft = $this->getDraft('update-by-key');
82+
$draft->setKey('test-' . $this->getTestRun() . '-update-by-key');
83+
$shippingMethod = $this->createShippingMethod($draft);
84+
85+
$text = 'test-' . $this->getTestRun() . '-new-name';
86+
$request = ShippingMethodUpdateByKeyRequest::ofKeyAndVersion($shippingMethod->getKey(), $shippingMethod->getVersion())
87+
->addAction(
88+
ShippingMethodChangeNameAction::ofName($text)
89+
)
90+
;
91+
$response = $request->executeWithClient($this->getClient());
92+
$result = $request->mapResponse($response);
93+
$this->deleteRequest->setVersion($result->getVersion());
94+
95+
$this->assertInstanceOf(ShippingMethod::class, $result);
96+
$this->assertSame($text, $result->getName());
97+
}
98+
7899
public function testChangeName()
79100
{
80101
$draft = $this->getDraft('change-name');

0 commit comments

Comments
 (0)