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

Commit

Permalink
Merge pull request #470 from commercetools/ProductTypes-changeAttribu…
Browse files Browse the repository at this point in the history
…teOrderByName-#453

feat(ProductType): support changeAttributeOrderByName action
  • Loading branch information
Jens Schulze committed Mar 11, 2019
2 parents 8edde8a + dd52af8 commit b01bd70
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Core/Builder/Update/ProductTypesActionBuilder.php
Expand Up @@ -10,6 +10,7 @@
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderByNameAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function changeAttributeName($action = null)
}

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

/**
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
* @param ProductTypeChangeAttributeOrderByNameAction|callable $action
* @return $this
*/
public function changeAttributeOrderByName($action = null)
{
$this->addAction($this->resolveAction(ProductTypeChangeAttributeOrderByNameAction::class, $action));
return $this;
}

/**
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-description
* @param ProductTypeChangeDescriptionAction|callable $action
Expand Down
Expand Up @@ -11,7 +11,8 @@

/**
* @package Commercetools\Core\Request\ProductTypes\Command
* @link https://docs.commercetools.com/http-api-projects-productTypes.html#change-the-order-of-attributedefinitions
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
* @deprecated
* @method string getAction()
* @method ProductTypeChangeAttributeOrderAction setAction(string $action = null)
* @method AttributeDefinitionCollection getAttributes()
Expand Down
@@ -0,0 +1,50 @@
<?php
/**
*/

namespace Commercetools\Core\Request\ProductTypes\Command;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\ProductType\AttributeDefinitionCollection;
use Commercetools\Core\Request\AbstractAction;

/**
* @package Commercetools\Core\Request\ProductTypes\Command
* @link https://docs.commercetools.com/http-api-projects-productTypes#change-the-order-of-attributedefinitions
* @method string getAction()
* @method ProductTypeChangeAttributeOrderByNameAction setAction(string $action = null)
* @method AttributeDefinitionCollection getAttributes()
* @method ProductTypeChangeAttributeOrderByNameAction setAttributes(AttributeDefinitionCollection $attributes = null)
* @method array getAttributeNames()
* @method ProductTypeChangeAttributeOrderByNameAction setAttributeNames(array $attributeNames = null)
*/
class ProductTypeChangeAttributeOrderByNameAction extends AbstractAction
{
public function fieldDefinitions()
{
return [
'action' => [static::TYPE => 'string'],
'attributeNames' => [static::TYPE => 'array'],
];
}

/**
* @param array $data
* @param Context|callable $context
*/
public function __construct(array $data = [], $context = null)
{
parent::__construct($data, $context);
$this->setAction('changeAttributeOrderByName');
}

/**
* @param array $attributeNames
* @param Context|callable $context
* @return ProductTypeChangeAttributeOrderByNameAction
*/
public static function ofAttributeNames(array $attributeNames, $context = null)
{
return static::of($context)->setAttributeNames($attributeNames);
}
}
46 changes: 46 additions & 0 deletions tests/integration/ProductType/ProductTypeUpdateRequestTest.php
Expand Up @@ -42,6 +42,7 @@
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddPlainEnumValueAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeOrderByNameAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction;
use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction;
Expand Down Expand Up @@ -778,6 +779,51 @@ public function testChangeAttributeName()
$this->assertNull($result->getAttributes()->getByName($name));
}

public function testChangeAttributeOrderByName()
{
$draft = $this->getDraft('change-attribute-order');

$name = 'testNameField-1-' . $this->getTestRun();
$definition = AttributeDefinition::of()
->setName($name)
->setLabel(LocalizedString::ofLangAndText('en', $name))
->setIsRequired(false)
->setIsSearchable(false)
->setType(StringType::of())
;

$name2 = 'testNameField-2-' . $this->getTestRun();
$definition2 = AttributeDefinition::of()
->setName($name2)
->setLabel(LocalizedString::ofLangAndText('en', $name2))
->setIsRequired(false)
->setIsSearchable(false)
->setType(StringType::of())
;

$attributeCollection = AttributeDefinitionCollection::of()->add($definition)->add($definition2);

$draft->setAttributes($attributeCollection);
$productType = $this->createProductType($draft);

$this->assertInstanceOf(ProductType::class, $productType);
$this->assertSame($name, $productType->getAttributes()->getAt(0)->getName());
$this->assertSame($name2, $productType->getAttributes()->getAt(1)->getName());

$request = ProductTypeUpdateRequest::ofIdAndVersion($productType->getId(), $productType->getVersion())
->addAction(
ProductTypeChangeAttributeOrderByNameAction::ofAttributeNames([$name2, $name])
)
;
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->productTypeDeleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(ProductType::class, $result);
$this->assertSame($name2, $result->getAttributes()->getAt(0)->getName());
$this->assertSame($name, $result->getAttributes()->getAt(1)->getName());
}

public function testChangeEnumKey()
{
$draft = $this->getDraft('change-enum-key');
Expand Down

0 comments on commit b01bd70

Please sign in to comment.