|
41 | 41 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddLocalizedEnumValueAction; |
42 | 42 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeAddPlainEnumValueAction; |
43 | 43 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeConstraintAction; |
| 44 | +use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeAttributeNameAction; |
44 | 45 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeDescriptionAction; |
| 46 | +use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeEnumKeyAction; |
45 | 47 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeInputHintAction; |
46 | 48 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeIsSearchableAction; |
47 | 49 | use Commercetools\Core\Request\ProductTypes\Command\ProductTypeChangeLabelAction; |
@@ -736,4 +738,94 @@ public function testRemoveEnumValues() |
736 | 738 | $this->assertCount(1, $type->getValues()); |
737 | 739 | $this->assertSame('bar', $type->getValues()->current()->getKey()); |
738 | 740 | } |
| 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 | + } |
739 | 831 | } |
0 commit comments