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

Commit 52932d8

Browse files
author
Jens Schulze
committed
feat(ProductType): support changeAttributeConstraint update action
Closes #370
1 parent 30cd045 commit 52932d8

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-attributedefinition-attributeconstraint
14+
* @method string getAction()
15+
* @method ProductTypeChangeAttributeConstraintAction setAction(string $action = null)
16+
* @method string getAttributeName()
17+
* @method ProductTypeChangeAttributeConstraintAction setAttributeName(string $attributeName = null)
18+
* @method string getNewValue()
19+
* @method ProductTypeChangeAttributeConstraintAction setNewValue(string $newValue = null)
20+
*/
21+
class ProductTypeChangeAttributeConstraintAction extends AbstractAction
22+
{
23+
public function fieldDefinitions()
24+
{
25+
return [
26+
'action' => [static::TYPE => 'string'],
27+
'attributeName' => [static::TYPE => 'string'],
28+
'newValue' => [static::TYPE => 'string']
29+
];
30+
}
31+
32+
/**
33+
* @param array $data
34+
* @param Context|callable $context
35+
*/
36+
public function __construct(array $data = [], $context = null)
37+
{
38+
parent::__construct($data, $context);
39+
$this->setAction('changeAttributeConstraint');
40+
}
41+
42+
/**
43+
* @param string $attributeName
44+
* @param string $attributeConstraint
45+
* @param Context|callable $context
46+
* @return ProductTypeChangeAttributeConstraintAction
47+
*/
48+
public static function ofAttributeNameAndAttributeConstraint($attributeName, $attributeConstraint, $context = null)
49+
{
50+
return static::of($context)->setAttributeName($attributeName)->setNewValue($attributeConstraint);
51+
}
52+
}

tests/integration/ProductType/ProductTypeUpdateRequestTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddAttributeDefinitionAction;
4141
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddLocalizedEnumValueAction;
4242
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddPlainEnumValueAction;
43+
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
4344
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
4445
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
4546
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeIsSearchableAction;
@@ -647,4 +648,50 @@ public function testChangeInputHint()
647648
$this->assertSame($inputHint, $result->getAttributes()->current()->getInputHint());
648649
$this->assertNotSame($productType->getVersion(), $result->getVersion());
649650
}
651+
652+
public function testChangeConstraint()
653+
{
654+
$draft = $this->getDraft('change-constraint');
655+
$productType = $this->createProductType($draft);
656+
657+
$definition = AttributeDefinition::of()
658+
->setName('testConstraintField')
659+
->setLabel(LocalizedString::ofLangAndText('en', 'testConstraintField'))
660+
->setIsRequired(false)
661+
->setIsSearchable(false)
662+
->setAttributeConstraint('SameForAll')
663+
->setType(StringType::of())
664+
;
665+
$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
666+
->addAction(
667+
ProductTypeAddAttributeDefinitionAction::ofAttribute($definition)
668+
)
669+
;
670+
$response = $request->executeWithClient($this->getClient());
671+
$result = $request->mapResponse($response);
672+
$this->productTypeDeleteRequest->setVersion($result->getVersion());
673+
674+
$this->assertInstanceOf(ProductType::class, $result);
675+
$this->assertSame($definition->getName(), $result->getAttributes()->current()->getName());
676+
$this->assertSame('SameForAll', $result->getAttributes()->current()->getAttributeConstraint());
677+
$this->assertNotSame($productType->getVersion(), $result->getVersion());
678+
$productType = $result;
679+
680+
$constraint = 'None';
681+
$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
682+
->addAction(
683+
ProductTypeChangeAttributeConstraintAction::ofAttributeNameAndAttributeConstraint(
684+
'testConstraintField',
685+
$constraint
686+
)
687+
)
688+
;
689+
$response = $request->executeWithClient($this->getClient());
690+
$result = $request->mapResponse($response);
691+
$this->productTypeDeleteRequest->setVersion($result->getVersion());
692+
693+
$this->assertInstanceOf(ProductType::class, $result);
694+
$this->assertSame($constraint, $result->getAttributes()->current()->getAttributeConstraint());
695+
$this->assertNotSame($productType->getVersion(), $result->getVersion());
696+
}
650697
}

0 commit comments

Comments
 (0)