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

Commit 0e5774c

Browse files
committed
feat(Type): add new type update actions
Closes #489
1 parent 298c141 commit 0e5774c

File tree

5 files changed

+300
-0
lines changed

5 files changed

+300
-0
lines changed

src/Core/Builder/Update/TypesActionBuilder.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Commercetools\Core\Request\Types\Command\TypeAddEnumValueAction;
88
use Commercetools\Core\Request\Types\Command\TypeAddFieldDefinitionAction;
99
use Commercetools\Core\Request\Types\Command\TypeAddLocalizedEnumValueAction;
10+
use Commercetools\Core\Request\Types\Command\TypeChangeEnumValueLabelAction;
1011
use Commercetools\Core\Request\Types\Command\TypeChangeEnumValueOrderAction;
1112
use Commercetools\Core\Request\Types\Command\TypeChangeFieldDefinitionOrderAction;
13+
use Commercetools\Core\Request\Types\Command\TypeChangeInputHintAction;
1214
use Commercetools\Core\Request\Types\Command\TypeChangeKeyAction;
1315
use Commercetools\Core\Request\Types\Command\TypeChangeLabelAction;
16+
use Commercetools\Core\Request\Types\Command\TypeChangeLocalizedEnumValueLabelAction;
1417
use Commercetools\Core\Request\Types\Command\TypeChangeLocalizedEnumValueOrderAction;
1518
use Commercetools\Core\Request\Types\Command\TypeChangeNameAction;
1619
use Commercetools\Core\Request\Types\Command\TypeRemoveFieldDefinitionAction;
@@ -53,6 +56,17 @@ public function addLocalizedEnumValue($action = null)
5356
return $this;
5457
}
5558

59+
/**
60+
* @link https://docs.commercetools.com/http-api-projects-types#change-enumvalue-label
61+
* @param TypeChangeEnumValueLabelAction|callable $action
62+
* @return $this
63+
*/
64+
public function changeEnumValueLabel($action = null)
65+
{
66+
$this->addAction($this->resolveAction(TypeChangeEnumValueLabelAction::class, $action));
67+
return $this;
68+
}
69+
5670
/**
5771
* @link https://docs.commercetools.com/http-api-projects-types.html#change-the-order-of-enumvalues
5872
* @param TypeChangeEnumValueOrderAction|callable $action
@@ -75,6 +89,17 @@ public function changeFieldDefinitionOrder($action = null)
7589
return $this;
7690
}
7791

92+
/**
93+
* @link https://docs.commercetools.com/http-api-projects-types#change-inputhint
94+
* @param TypeChangeInputHintAction|callable $action
95+
* @return $this
96+
*/
97+
public function changeInputHint($action = null)
98+
{
99+
$this->addAction($this->resolveAction(TypeChangeInputHintAction::class, $action));
100+
return $this;
101+
}
102+
78103
/**
79104
* @link https://docs.commercetools.com/http-api-projects-types.html#change-key
80105
* @param TypeChangeKeyAction|callable $action
@@ -97,6 +122,17 @@ public function changeLabel($action = null)
97122
return $this;
98123
}
99124

125+
/**
126+
* @link https://docs.commercetools.com/http-api-projects-types#change-localizedenumvalue-label
127+
* @param TypeChangeLocalizedEnumValueLabelAction|callable $action
128+
* @return $this
129+
*/
130+
public function changeLocalizedEnumValueLabel($action = null)
131+
{
132+
$this->addAction($this->resolveAction(TypeChangeLocalizedEnumValueLabelAction::class, $action));
133+
return $this;
134+
}
135+
100136
/**
101137
* @link https://docs.commercetools.com/http-api-projects-types.html#change-the-order-of-localizedenumvalues
102138
* @param TypeChangeLocalizedEnumValueOrderAction|callable $action
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Request\Types\Command;
4+
5+
use Commercetools\Core\Model\Common\Context;
6+
use Commercetools\Core\Model\Common\Enum;
7+
use Commercetools\Core\Request\AbstractAction;
8+
9+
/**
10+
* @package Commercetools\Core\Request\Types\Command
11+
* @link https://docs.commercetools.com/http-api-projects-types#change-enumvalue-label
12+
* @method string getAction()
13+
* @method TypeChangeEnumValueLabelAction setAction(string $action = null)
14+
* @method string getFieldName()
15+
* @method TypeChangeEnumValueLabelAction setFieldName(string $fieldName = null)
16+
* @method Enum getValue()
17+
* @method TypeChangeEnumValueLabelAction setValue(Enum $value = null)
18+
*/
19+
class TypeChangeEnumValueLabelAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'fieldName' => [static::TYPE => 'string'],
26+
'value' => [static::TYPE => Enum::class]
27+
];
28+
}
29+
30+
/**
31+
* @param array $data
32+
* @param Context|callable $context
33+
*/
34+
public function __construct(array $data = [], $context = null)
35+
{
36+
parent::__construct($data, $context);
37+
$this->setAction('changeEnumValueLabel');
38+
}
39+
40+
/**
41+
* @param string $fieldName
42+
* @param Enum $enum
43+
* @param Context|callable $context
44+
* @return TypeChangeEnumValueLabelAction
45+
*/
46+
public static function ofNameAndEnum($fieldName, Enum $enum, $context = null)
47+
{
48+
return static::of($context)->setFieldName($fieldName)->setValue($enum);
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Request\Types\Command;
4+
5+
use Commercetools\Core\Model\Common\Context;
6+
use Commercetools\Core\Model\Common\Enum;
7+
use Commercetools\Core\Request\AbstractAction;
8+
9+
/**
10+
* @package Commercetools\Core\Request\Types\Command
11+
* @link https://docs.commercetools.com/http-api-projects-types#change-inputhint
12+
* @method string getAction()
13+
* @method TypeChangeInputHintAction setAction(string $action = null)
14+
* @method string getFieldName()
15+
* @method TypeChangeInputHintAction setFieldName(string $fieldName = null)
16+
* @method string getInputHint()
17+
* @method TypeChangeInputHintAction setInputHint(string $inputHint = null)
18+
*/
19+
class TypeChangeInputHintAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'fieldName' => [static::TYPE => 'string'],
26+
'inputHint' => [static::TYPE => 'string']
27+
];
28+
}
29+
30+
/**
31+
* @param array $data
32+
* @param Context|callable $context
33+
*/
34+
public function __construct(array $data = [], $context = null)
35+
{
36+
parent::__construct($data, $context);
37+
$this->setAction('changeInputHint');
38+
}
39+
40+
/**
41+
* @param string $fieldName
42+
* @param string $inputHint
43+
* @param Context|callable $context
44+
* @return TypeChangeInputHintAction
45+
*/
46+
public static function ofNameAndInputHint($fieldName, $inputHint, $context = null)
47+
{
48+
return static::of($context)->setFieldName($fieldName)->setInputHint($inputHint);
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Request\Types\Command;
4+
5+
use Commercetools\Core\Model\Common\Context;
6+
use Commercetools\Core\Model\Common\LocalizedEnum;
7+
use Commercetools\Core\Request\AbstractAction;
8+
9+
/**
10+
* @package Commercetools\Core\Request\Types\Command
11+
* @link https://docs.commercetools.com/http-api-projects-types#change-localizedenumvalue-label
12+
* @method string getAction()
13+
* @method TypeChangeLocalizedEnumValueLabelAction setAction(string $action = null)
14+
* @method string getFieldName()
15+
* @method TypeChangeLocalizedEnumValueLabelAction setFieldName(string $fieldName = null)
16+
* @method LocalizedEnum getValue()
17+
* @method TypeChangeLocalizedEnumValueLabelAction setValue(LocalizedEnum $value = null)
18+
*/
19+
class TypeChangeLocalizedEnumValueLabelAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'fieldName' => [static::TYPE => 'string'],
26+
'value' => [static::TYPE => LocalizedEnum::class]
27+
];
28+
}
29+
30+
/**
31+
* @param array $data
32+
* @param Context|callable $context
33+
*/
34+
public function __construct(array $data = [], $context = null)
35+
{
36+
parent::__construct($data, $context);
37+
$this->setAction('changeLocalizedEnumValueLabel');
38+
}
39+
40+
/**
41+
* @param string $fieldName
42+
* @param LocalizedEnum $enum
43+
* @param Context|callable $context
44+
* @return TypeChangeLocalizedEnumValueLabelAction
45+
*/
46+
public static function ofNameAndEnum($fieldName, LocalizedEnum $enum, $context = null)
47+
{
48+
return static::of($context)->setFieldName($fieldName)->setValue($enum);
49+
}
50+
}

tests/integration/Type/TypeUpdateRequestTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
use Commercetools\Core\Request\Types\Command\TypeAddEnumValueAction;
2222
use Commercetools\Core\Request\Types\Command\TypeAddFieldDefinitionAction;
2323
use Commercetools\Core\Request\Types\Command\TypeAddLocalizedEnumValueAction;
24+
use Commercetools\Core\Request\Types\Command\TypeChangeEnumValueLabelAction;
25+
use Commercetools\Core\Request\Types\Command\TypeChangeInputHintAction;
2426
use Commercetools\Core\Request\Types\Command\TypeChangeKeyAction;
2527
use Commercetools\Core\Request\Types\Command\TypeChangeLabelAction;
28+
use Commercetools\Core\Request\Types\Command\TypeChangeLocalizedEnumValueLabelAction;
2629
use Commercetools\Core\Request\Types\Command\TypeChangeNameAction;
2730
use Commercetools\Core\Request\Types\Command\TypeRemoveFieldDefinitionAction;
2831
use Commercetools\Core\Request\Types\Command\TypeSetDescriptionAction;
@@ -51,6 +54,7 @@ protected function getDraft($name)
5154
->setName('testField')
5255
->setLabel(LocalizedString::ofLangAndText('en', 'testField'))
5356
->setRequired(false)
57+
->setInputHint('SingleLine')
5458
->setType(StringType::of())
5559
)
5660
);
@@ -302,4 +306,114 @@ public function testAddLocalizedEnumValue()
302306
$fieldType = $result->getFieldDefinitions()->getByName('newLEnumField')->getType();
303307
$this->assertSame($enum->getKey(), $fieldType->getValues()->current()->getKey());
304308
}
309+
310+
public function testChangeEnumValueLabel()
311+
{
312+
$draft = $this->getDraft('change-enum-value-label');
313+
$type = $this->createType($draft);
314+
315+
$fieldDefinition = FieldDefinition::of()
316+
->setName('newEnumField')
317+
->setLabel(LocalizedString::ofLangAndText('en', 'newEnumField'))
318+
->setRequired(false)
319+
->setType(
320+
EnumType::of()
321+
->setValues(EnumCollection::of()->add(Enum::of()->setKey('test')->setLabel('test')))
322+
)
323+
;
324+
$request = TypeUpdateRequest::ofIdAndVersion($type->getId(), $type->getVersion())
325+
->addAction(
326+
TypeAddFieldDefinitionAction::ofFieldDefinition($fieldDefinition)
327+
)
328+
;
329+
$response = $request->executeWithClient($this->getClient());
330+
$result = $request->mapResponse($response);
331+
$this->deleteRequest->setVersion($result->getVersion());
332+
$type = $result;
333+
334+
$enum = Enum::of()->setKey('test')->setLabel('new-label');
335+
$request = TypeUpdateRequest::ofIdAndVersion($type->getId(), $type->getVersion())
336+
->addAction(
337+
TypeChangeEnumValueLabelAction::ofNameAndEnum('newEnumField', $enum)
338+
)
339+
;
340+
$response = $request->executeWithClient($this->getClient());
341+
$result = $request->mapResponse($response);
342+
$this->deleteRequest->setVersion($result->getVersion());
343+
344+
$this->assertInstanceOf(Type::class, $result);
345+
/**
346+
* @var EnumType $fieldType
347+
*/
348+
$fieldType = $result->getFieldDefinitions()->getByName('newEnumField')->getType();
349+
$this->assertSame($enum->getLabel(), $fieldType->getValues()->current()->getLabel());
350+
}
351+
352+
public function testChangeLocalizedEnumValueLabel()
353+
{
354+
$draft = $this->getDraft('change-localized-enum-value-label');
355+
$type = $this->createType($draft);
356+
357+
$fieldDefinition = FieldDefinition::of()
358+
->setName('newEnumField')
359+
->setLabel(LocalizedString::ofLangAndText('en', 'newEnumField'))
360+
->setRequired(false)
361+
->setType(
362+
LocalizedEnumType::of()
363+
->setValues(
364+
LocalizedEnumCollection::of()->add(
365+
LocalizedEnum::of()->setKey('test')
366+
->setLabel(LocalizedString::ofLangAndText('en', 'test'))
367+
)
368+
)
369+
)
370+
;
371+
$request = TypeUpdateRequest::ofIdAndVersion($type->getId(), $type->getVersion())
372+
->addAction(
373+
TypeAddFieldDefinitionAction::ofFieldDefinition($fieldDefinition)
374+
)
375+
;
376+
$response = $request->executeWithClient($this->getClient());
377+
$result = $request->mapResponse($response);
378+
$this->deleteRequest->setVersion($result->getVersion());
379+
$type = $result;
380+
381+
$enum = LocalizedEnum::of()->setKey('test')
382+
->setLabel(LocalizedString::ofLangAndText('en', 'new-label'));
383+
$request = TypeUpdateRequest::ofIdAndVersion($type->getId(), $type->getVersion())
384+
->addAction(
385+
TypeChangeLocalizedEnumValueLabelAction::ofNameAndEnum('newEnumField', $enum)
386+
)
387+
;
388+
$response = $request->executeWithClient($this->getClient());
389+
$result = $request->mapResponse($response);
390+
$this->deleteRequest->setVersion($result->getVersion());
391+
392+
$this->assertInstanceOf(Type::class, $result);
393+
/**
394+
* @var LocalizedEnumType $fieldType
395+
*/
396+
$fieldType = $result->getFieldDefinitions()->getByName('newEnumField')->getType();
397+
$this->assertSame($enum->getLabel()->en, $fieldType->getValues()->current()->getLabel()->en);
398+
}
399+
400+
public function testChangeInputHint()
401+
{
402+
$draft = $this->getDraft('change-input-hint');
403+
$type = $this->createType($draft);
404+
405+
$inputHint = 'MultiLine';
406+
$request = TypeUpdateRequest::ofIdAndVersion($type->getId(), $type->getVersion())
407+
->addAction(
408+
TypeChangeInputHintAction::ofNameAndInputHint('testField', $inputHint)
409+
)
410+
;
411+
$response = $request->executeWithClient($this->getClient());
412+
$result = $request->mapResponse($response);
413+
$this->deleteRequest->setVersion($result->getVersion());
414+
415+
$this->assertInstanceOf(Type::class, $result);
416+
$field = $result->getFieldDefinitions()->getByName('testField');
417+
$this->assertSame($inputHint, $field->getInputHint());
418+
}
305419
}

0 commit comments

Comments
 (0)