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

Commit 8994315

Browse files
nikossvnkJens Schulze
authored andcommitted
feat(ProductType): support product type changeAttributeName & changeE… (#397)
* feat(ProductType): support product type changeAttributeName & changeEnumKey action " Closes #393 * WIP: add missing annotations
1 parent c0b367a commit 8994315

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ requests.log
1616
env.list
1717
humbug.*
1818
cache/commercetools_io_access_token*
19+
src/cache/
1920
github_deploy_key
2021
node_modules
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* @author @jenschude <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\ProductTypes\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\ProductTypes\Command
13+
* @codingStandardsIgnoreStart
14+
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-attributedefinition-name
15+
* @codingStandardsIgnoreEnd
16+
* @method string getAction()
17+
* @method ProductTypeChangeAttributeNameAction setAction(string $action = null)
18+
* @method string getAttributeName()
19+
* @method ProductTypeChangeAttributeNameAction setAttributeName(string $attributeName = null)
20+
* @method string getNewAttributeName()
21+
* @method ProductTypeChangeAttributeNameAction setNewAttributeName(string $newAttributeName = null)
22+
*/
23+
class ProductTypeChangeAttributeNameAction extends AbstractAction
24+
{
25+
public function fieldDefinitions()
26+
{
27+
return [
28+
'action' => [static::TYPE => 'string'],
29+
'attributeName' => [static::TYPE => 'string'],
30+
'newAttributeName' => [static::TYPE => 'string']
31+
];
32+
}
33+
34+
/**
35+
* @param array $data
36+
* @param Context|callable $context
37+
*/
38+
public function __construct(array $data = [], $context = null)
39+
{
40+
parent::__construct($data, $context);
41+
$this->setAction('changeAttributeName');
42+
}
43+
44+
/**
45+
* @param string $attributeName
46+
* @param string $newAttributeName
47+
* @param Context|callable $context
48+
* @return ProductTypeChangeAttributeNameAction
49+
*/
50+
public static function ofAttributeName($attributeName, $newAttributeName, $context = null)
51+
{
52+
return static::of($context)->setAttributeName($attributeName)->setNewAttributeName($newAttributeName);
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @author @jenschude <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\ProductTypes\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\ProductTypes\Command
13+
* @codingStandardsIgnoreStart
14+
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-the-key-of-an-enumvalue
15+
* @codingStandardsIgnoreEnd
16+
* @method string getAction()
17+
* @method ProductTypeChangeEnumKeyAction setAction(string $action = null)
18+
* @method string getAttributeName()
19+
* @method ProductTypeChangeEnumKeyAction setAttributeName(string $attributeName = null)
20+
* @method string getKey()
21+
* @method ProductTypeChangeEnumKeyAction setKey(string $key = null)
22+
* @method string getNewKey()
23+
* @method ProductTypeChangeEnumKeyAction setNewKey(string $newKey = null)
24+
*/
25+
class ProductTypeChangeEnumKeyAction extends AbstractAction
26+
{
27+
public function fieldDefinitions()
28+
{
29+
return [
30+
'action' => [static::TYPE => 'string'],
31+
'attributeName' => [static::TYPE => 'string'],
32+
'key' => [static::TYPE => 'string'],
33+
'newKey' => [static::TYPE => 'string']
34+
35+
];
36+
}
37+
38+
/**
39+
* @param array $data
40+
* @param Context|callable $context
41+
*/
42+
public function __construct(array $data = [], $context = null)
43+
{
44+
parent::__construct($data, $context);
45+
$this->setAction('changeEnumKey');
46+
}
47+
48+
/**
49+
* @param string $attributeName
50+
* @param string $enumKey
51+
* @param string $newEnumKey
52+
* @param Context|callable $context
53+
* @return ProductTypeChangeEnumKeyAction
54+
*/
55+
public static function ofAttributeNameAndEnumKey($attributeName, $enumKey, $newEnumKey, $context = null)
56+
{
57+
return static::of($context)->setAttributeName($attributeName)->setkey($enumKey)->setNewKey($newEnumKey);
58+
}
59+
}

tests/integration/ProductType/ProductTypeUpdateRequestTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddLocalizedEnumValueAction;
4242
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddPlainEnumValueAction;
4343
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
44+
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction;
4445
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
46+
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction;
4547
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
4648
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeIsSearchableAction;
4749
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeLabelAction;
@@ -736,4 +738,94 @@ public function testRemoveEnumValues()
736738
$this->assertCount(1, $type->getValues());
737739
$this->assertSame('bar', $type->getValues()->current()->getKey());
738740
}
741+
742+
public function testChangeAttributeName()
743+
{
744+
$draft = $this->getDraft('change-attribute-name');
745+
746+
$name = 'testNameField' . $this->getTestRun();
747+
$definition = AttributeDefinition::of()
748+
->setName($name)
749+
->setLabel(LocalizedString::ofLangAndText('en', $name))
750+
->setIsRequired(false)
751+
->setIsSearchable(false)
752+
->setType(StringType::of())
753+
;
754+
755+
$draft->setAttributes(AttributeDefinitionCollection::of()->add($definition));
756+
$productType = $this->createProductType($draft);
757+
758+
$this->assertInstanceOf(ProductType::class, $productType);
759+
$this->assertSame($name, $productType->getAttributes()->getByName($name)->getName());
760+
761+
762+
$newAttributeName = 'new' . ucfirst($name);
763+
$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
764+
->addAction(
765+
ProductTypeChangeAttributeNameAction::ofAttributeName(
766+
$productType->getAttributes()->getByName($name)->getName(),
767+
$newAttributeName
768+
)
769+
)
770+
;
771+
$response = $request->executeWithClient($this->getClient());
772+
$result = $request->mapResponse($response);
773+
$this->productTypeDeleteRequest->setVersion($result->getVersion());
774+
775+
$this->assertInstanceOf(ProductType::class, $result);
776+
$this->assertSame($newAttributeName, $result->getAttributes()->current()->getName());
777+
$this->assertSame($newAttributeName, $result->getAttributes()->getByName($newAttributeName)->getName());
778+
$this->assertNull($result->getAttributes()->getByName($name));
779+
}
780+
781+
public function testChangeEnumKey()
782+
{
783+
$draft = $this->getDraft('change-enum-key');
784+
785+
$name = 'testNameField' . $this->getTestRun();
786+
$keyName = 'foo';
787+
$definition = AttributeDefinition::of()
788+
->setName($name)
789+
->setLabel(LocalizedString::ofLangAndText('en', $name))
790+
->setIsRequired(false)
791+
->setIsSearchable(false)
792+
->setType(EnumType::of()->setValues(
793+
EnumCollection::of()
794+
->add(Enum::of()->setKey('foo')->setLabel('foo'))
795+
)
796+
);
797+
798+
$draft->setAttributes(AttributeDefinitionCollection::of()->add($definition));
799+
$productType = $this->createProductType($draft);
800+
801+
$this->assertInstanceOf(ProductType::class, $productType);
802+
/**
803+
* @var EnumType $enumType
804+
*/
805+
$enumType = $productType->getAttributes()->getByName($name)->getType();
806+
$this->assertSame($keyName, $enumType->getValues()->getByKey($keyName)->getKey());
807+
808+
809+
$newKeyName = 'new-foo';
810+
$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
811+
->addAction(
812+
ProductTypeChangeEnumKeyAction::ofAttributeNameAndEnumKey(
813+
$productType->getAttributes()->getByName($name)->getName(),
814+
$keyName,
815+
$newKeyName
816+
)
817+
)
818+
;
819+
$response = $request->executeWithClient($this->getClient());
820+
$result = $request->mapResponse($response);
821+
$this->productTypeDeleteRequest->setVersion($result->getVersion());
822+
823+
$this->assertInstanceOf(ProductType::class, $result);
824+
/**
825+
* @var EnumType $enumType
826+
*/
827+
$enumType = $result->getAttributes()->getByName($name)->getType();
828+
$this->assertSame($newKeyName, $enumType->getValues()->getByKey($newKeyName)->getKey());
829+
$this->assertNull($enumType->getValues()->getByKey($keyName));
830+
}
739831
}

0 commit comments

Comments
 (0)