|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ADOBE CONFIDENTIAL |
| 4 | + * |
| 5 | + * Copyright 2024 Adobe |
| 6 | + * All Rights Reserved. |
| 7 | + * |
| 8 | + * NOTICE: All information contained herein is, and remains |
| 9 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 10 | + * and technical concepts contained herein are proprietary to Adobe |
| 11 | + * and its suppliers and are protected by all applicable intellectual |
| 12 | + * property laws, including trade secret and copyright laws. |
| 13 | + * Dissemination of this information or reproduction of this material |
| 14 | + * is strictly forbidden unless prior written permission is obtained |
| 15 | + * from Adobe. |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\CustomerGraphQl\Model\Customer; |
| 20 | + |
| 21 | +use Magento\Customer\Api\CustomerMetadataInterface; |
| 22 | +use Magento\Eav\Api\Data\AttributeInterface; |
| 23 | +use Magento\Eav\Api\Data\AttributeOptionInterface; |
| 24 | +use Magento\Eav\Test\Fixture\Attribute; |
| 25 | +use Magento\Eav\Test\Fixture\AttributeOption; |
| 26 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 27 | +use Magento\TestFramework\Fixture\DataFixture; |
| 28 | +use Magento\TestFramework\Helper\Bootstrap; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | + |
| 31 | +#[ |
| 32 | + DataFixture( |
| 33 | + Attribute::class, |
| 34 | + [ |
| 35 | + 'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 36 | + 'attribute_code' => 'select_attr', |
| 37 | + 'frontend_input' => 'select' |
| 38 | + ], |
| 39 | + 'attribute' |
| 40 | + ), |
| 41 | + DataFixture( |
| 42 | + AttributeOption::class, |
| 43 | + [ |
| 44 | + 'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 45 | + 'attribute_code' => '$attribute.attribute_code$', |
| 46 | + 'label' => 'option1 label', |
| 47 | + 'sort_order' => 10 |
| 48 | + ], |
| 49 | + 'option1' |
| 50 | + ), |
| 51 | + DataFixture( |
| 52 | + AttributeOption::class, |
| 53 | + [ |
| 54 | + 'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 55 | + 'attribute_code' => '$attribute.attribute_code$', |
| 56 | + 'label' => 'option2 label', |
| 57 | + 'sort_order' => 20 |
| 58 | + ], |
| 59 | + 'option2' |
| 60 | + ) |
| 61 | +] |
| 62 | +class GetCustomSelectedOptionAttributesTest extends TestCase |
| 63 | +{ |
| 64 | + /** |
| 65 | + * @var GetCustomSelectedOptionAttributes |
| 66 | + */ |
| 67 | + private $getCustomSelectedOptionAttributes; |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + public function setUp(): void |
| 73 | + { |
| 74 | + $this->getCustomSelectedOptionAttributes = Bootstrap::getObjectManager() |
| 75 | + ->create(GetCustomSelectedOptionAttributes::class); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return void |
| 80 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 81 | + */ |
| 82 | + public function testCustomerCustomOptionAttributeWithIntegerValue(): void |
| 83 | + { |
| 84 | + /** @var AttributeInterface $attribute */ |
| 85 | + $attribute = DataFixtureStorageManager::getStorage()->get('attribute'); |
| 86 | + /** @var AttributeOptionInterface $option1 */ |
| 87 | + $option1 = DataFixtureStorageManager::getStorage()->get('option1'); |
| 88 | + $customAttribute['attribute_code'] = $attribute->getAttributeCode(); |
| 89 | + $customAttribute['value'] = (int)$option1->getValue(); |
| 90 | + $result = $this->getCustomSelectedOptionAttributes->execute('customer', $customAttribute); |
| 91 | + unset($result[0]['uid']); |
| 92 | + $this->assertEquals( |
| 93 | + [ |
| 94 | + [ |
| 95 | + 'value' => $option1->getValue(), |
| 96 | + 'label' => $option1->getLabel() |
| 97 | + ] |
| 98 | + ], |
| 99 | + $result |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return void |
| 105 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 106 | + */ |
| 107 | + public function testCustomerCustomOptionAttributeWithStringValue(): void |
| 108 | + { |
| 109 | + /** @var AttributeInterface $attribute */ |
| 110 | + $attribute = DataFixtureStorageManager::getStorage()->get('attribute'); |
| 111 | + |
| 112 | + /** @var AttributeOptionInterface $option1 */ |
| 113 | + $option1 = DataFixtureStorageManager::getStorage()->get('option1'); |
| 114 | + |
| 115 | + /** @var AttributeOptionInterface $option2 */ |
| 116 | + $option2 = DataFixtureStorageManager::getStorage()->get('option2'); |
| 117 | + |
| 118 | + $customAttribute['attribute_code'] = $attribute->getAttributeCode(); |
| 119 | + |
| 120 | + $customAttribute['value'] = $option1->getValue().",".$option2->getValue(); |
| 121 | + |
| 122 | + $result = $this->getCustomSelectedOptionAttributes->execute('customer', $customAttribute); |
| 123 | + |
| 124 | + foreach ($result as &$tmp) { |
| 125 | + unset($tmp['uid']); |
| 126 | + } |
| 127 | + $this->assertEquals( |
| 128 | + [ |
| 129 | + [ |
| 130 | + 'value' => $option1->getValue(), |
| 131 | + 'label' => $option1->getLabel() |
| 132 | + ], |
| 133 | + [ |
| 134 | + 'value' => $option2->getValue(), |
| 135 | + 'label' => $option2->getLabel() |
| 136 | + ] |
| 137 | + ], |
| 138 | + $result |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
0 commit comments