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

Commit dd52af8

Browse files
committed
feat(ProductType): support changeAttributeOrderByName action
closes #453
1 parent 2e9d74b commit dd52af8

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

src/Core/Builder/Update/ProductTypesActionBuilder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
1111
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction;
1212
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderAction;
13+
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderByNameAction;
1314
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
1415
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction;
1516
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
@@ -85,7 +86,7 @@ public function changeAttributeName($action = null)
8586
}
8687

8788
/**
88-
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-the-order-of-attributedefinitions
89+
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
8990
* @param ProductTypeChangeAttributeOrderAction|callable $action
9091
* @return $this
9192
*/
@@ -95,6 +96,17 @@ public function changeAttributeOrder($action = null)
9596
return $this;
9697
}
9798

99+
/**
100+
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
101+
* @param ProductTypeChangeAttributeOrderByNameAction|callable $action
102+
* @return $this
103+
*/
104+
public function changeAttributeOrderByName($action = null)
105+
{
106+
$this->addAction($this->resolveAction(ProductTypeChangeAttributeOrderByNameAction::class, $action));
107+
return $this;
108+
}
109+
98110
/**
99111
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-description
100112
* @param ProductTypeChangeDescriptionAction|callable $action

src/Core/Request/ProductTypes/Command/ProductTypeChangeAttributeOrderAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
/**
1313
* @package Commercetools\Core\Request\ProductTypes\Command
14-
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-the-order-of-attributedefinitions
14+
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
15+
* @deprecated
1516
* @method string getAction()
1617
* @method ProductTypeChangeAttributeOrderAction setAction(string $action = null)
1718
* @method AttributeDefinitionCollection getAttributes()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\ProductTypes\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Model\ProductType\AttributeDefinitionCollection;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\ProductTypes\Command
13+
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
14+
* @method string getAction()
15+
* @method ProductTypeChangeAttributeOrderByNameAction setAction(string $action = null)
16+
* @method AttributeDefinitionCollection getAttributes()
17+
* @method ProductTypeChangeAttributeOrderByNameAction setAttributes(AttributeDefinitionCollection $attributes = null)
18+
* @method array getAttributeNames()
19+
* @method ProductTypeChangeAttributeOrderByNameAction setAttributeNames(array $attributeNames = null)
20+
*/
21+
class ProductTypeChangeAttributeOrderByNameAction extends AbstractAction
22+
{
23+
public function fieldDefinitions()
24+
{
25+
return [
26+
'action' => [static::TYPE => 'string'],
27+
'attributeNames' => [static::TYPE => 'array'],
28+
];
29+
}
30+
31+
/**
32+
* @param array $data
33+
* @param Context|callable $context
34+
*/
35+
public function __construct(array $data = [], $context = null)
36+
{
37+
parent::__construct($data, $context);
38+
$this->setAction('changeAttributeOrderByName');
39+
}
40+
41+
/**
42+
* @param array $attributeNames
43+
* @param Context|callable $context
44+
* @return ProductTypeChangeAttributeOrderByNameAction
45+
*/
46+
public static function ofAttributeNames(array $attributeNames, $context = null)
47+
{
48+
return static::of($context)->setAttributeNames($attributeNames);
49+
}
50+
}

tests/integration/ProductType/ProductTypeUpdateRequestTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddPlainEnumValueAction;
4343
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
4444
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction;
45+
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderByNameAction;
4546
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
4647
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction;
4748
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
@@ -778,6 +779,51 @@ public function testChangeAttributeName()
778779
$this->assertNull($result->getAttributes()->getByName($name));
779780
}
780781

782+
public function testChangeAttributeOrderByName()
783+
{
784+
$draft = $this->getDraft('change-attribute-order');
785+
786+
$name = 'testNameField-1-' . $this->getTestRun();
787+
$definition = AttributeDefinition::of()
788+
->setName($name)
789+
->setLabel(LocalizedString::ofLangAndText('en', $name))
790+
->setIsRequired(false)
791+
->setIsSearchable(false)
792+
->setType(StringType::of())
793+
;
794+
795+
$name2 = 'testNameField-2-' . $this->getTestRun();
796+
$definition2 = AttributeDefinition::of()
797+
->setName($name2)
798+
->setLabel(LocalizedString::ofLangAndText('en', $name2))
799+
->setIsRequired(false)
800+
->setIsSearchable(false)
801+
->setType(StringType::of())
802+
;
803+
804+
$attributeCollection = AttributeDefinitionCollection::of()->add($definition)->add($definition2);
805+
806+
$draft->setAttributes($attributeCollection);
807+
$productType = $this->createProductType($draft);
808+
809+
$this->assertInstanceOf(ProductType::class, $productType);
810+
$this->assertSame($name, $productType->getAttributes()->getAt(0)->getName());
811+
$this->assertSame($name2, $productType->getAttributes()->getAt(1)->getName());
812+
813+
$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
814+
->addAction(
815+
ProductTypeChangeAttributeOrderByNameAction::ofAttributeNames([$name2, $name])
816+
)
817+
;
818+
$response = $request->executeWithClient($this->getClient());
819+
$result = $request->mapResponse($response);
820+
$this->productTypeDeleteRequest->setVersion($result->getVersion());
821+
822+
$this->assertInstanceOf(ProductType::class, $result);
823+
$this->assertSame($name2, $result->getAttributes()->getAt(0)->getName());
824+
$this->assertSame($name, $result->getAttributes()->getAt(1)->getName());
825+
}
826+
781827
public function testChangeEnumKey()
782828
{
783829
$draft = $this->getDraft('change-enum-key');

0 commit comments

Comments
 (0)