|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Product\Attribute\Save; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 13 | +use Magento\Eav\Api\AttributeRepositoryInterface; |
| 14 | +use Magento\Eav\Model\Entity\Attribute\Exception; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Base class for text product attributes |
| 21 | + */ |
| 22 | +abstract class AbstractAttributeTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var ObjectManagerInterface */ |
| 25 | + protected $objectManager; |
| 26 | + |
| 27 | + /** @var AttributeRepositoryInterface */ |
| 28 | + protected $attributeRepository; |
| 29 | + |
| 30 | + /** @var ProductRepositoryInterface */ |
| 31 | + protected $productRepository; |
| 32 | + |
| 33 | + /** @var ProductAttributeInterface */ |
| 34 | + protected $attribute; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritdoc |
| 38 | + */ |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + parent::setUp(); |
| 42 | + |
| 43 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 44 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 45 | + $this->attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider productProvider |
| 50 | + * @param $productSku |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function testSaveAttribute(string $productSku): void |
| 54 | + { |
| 55 | + $product = $this->setAttributeValueAndValidate($productSku, $this->getDefaultAttributeValue()); |
| 56 | + $product = $this->productRepository->save($product); |
| 57 | + $this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode())); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider productProvider |
| 62 | + * @param string $productSku |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function testRequiredAttribute(string $productSku): void |
| 66 | + { |
| 67 | + $this->expectException(Exception::class); |
| 68 | + $messageFormat = 'The "%s" attribute value is empty. Set the attribute and try again.'; |
| 69 | + $this->expectExceptionMessage( |
| 70 | + (string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel())) |
| 71 | + ); |
| 72 | + $this->prepareAttribute(['is_required' => true]); |
| 73 | + $this->unsetAttributeValueAndValidate($productSku); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @dataProvider productProvider |
| 78 | + * @param string $productSku |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function testDefaultValue(string $productSku): void |
| 82 | + { |
| 83 | + $this->prepareAttribute(['default_value' => $this->getDefaultAttributeValue()]); |
| 84 | + $product = $this->unsetAttributeValueAndValidate($productSku); |
| 85 | + $product = $this->productRepository->save($product); |
| 86 | + $this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode())); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @dataProvider uniqueAttributeValueProvider |
| 91 | + * @param string $firstSku |
| 92 | + * @param string $secondSku |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + public function testUniqueAttribute(string $firstSku, string $secondSku): void |
| 96 | + { |
| 97 | + $this->expectException(Exception::class); |
| 98 | + $messageFormat = 'The value of the "%s" attribute isn\'t unique. Set a unique value and try again.'; |
| 99 | + $this->expectExceptionMessage( |
| 100 | + (string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel())) |
| 101 | + ); |
| 102 | + $this->prepareAttribute(['is_unique' => 1]); |
| 103 | + $product = $this->setAttributeValueAndValidate($firstSku, $this->getDefaultAttributeValue()); |
| 104 | + $this->productRepository->save($product); |
| 105 | + $this->setAttributeValueAndValidate($secondSku, $this->getDefaultAttributeValue()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get attribute |
| 110 | + * |
| 111 | + * @return ProductAttributeInterface |
| 112 | + */ |
| 113 | + protected function getAttribute(): ProductAttributeInterface |
| 114 | + { |
| 115 | + if ($this->attribute === null) { |
| 116 | + $this->attribute = $this->attributeRepository->get( |
| 117 | + ProductAttributeInterface::ENTITY_TYPE_CODE, |
| 118 | + $this->getAttributeCode() |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return $this->attribute; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Set attribute value to product and validate the product |
| 127 | + * |
| 128 | + * @param string $attributeValue |
| 129 | + * @param string $productSku |
| 130 | + * @return ProductInterface |
| 131 | + */ |
| 132 | + protected function setAttributeValueAndValidate(string $productSku, string $attributeValue): ProductInterface |
| 133 | + { |
| 134 | + $product = $this->productRepository->get($productSku); |
| 135 | + $product->addData([$this->getAttributeCode() => $attributeValue]); |
| 136 | + $product->validate(); |
| 137 | + |
| 138 | + return $product; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Unset attribute value of the product and validate the product |
| 143 | + * |
| 144 | + * @param string $productSku |
| 145 | + * @return ProductInterface |
| 146 | + */ |
| 147 | + private function unsetAttributeValueAndValidate(string $productSku): ProductInterface |
| 148 | + { |
| 149 | + $product = $this->productRepository->get($productSku); |
| 150 | + $product->unsetData($this->getAttributeCode()); |
| 151 | + $product->validate(); |
| 152 | + |
| 153 | + return $product; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Prepare attribute to test |
| 158 | + * |
| 159 | + * @param array $data |
| 160 | + * @return void |
| 161 | + */ |
| 162 | + private function prepareAttribute(array $data): void |
| 163 | + { |
| 164 | + $attribute = $this->getAttribute(); |
| 165 | + $attribute->addData($data); |
| 166 | + $this->attributeRepository->save($attribute); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns attribute code for current test |
| 171 | + * |
| 172 | + * @return string |
| 173 | + */ |
| 174 | + abstract protected function getAttributeCode(): string; |
| 175 | + |
| 176 | + /** |
| 177 | + * Get default value for current attribute |
| 178 | + * |
| 179 | + * @return string |
| 180 | + */ |
| 181 | + abstract protected function getDefaultAttributeValue(): string; |
| 182 | + |
| 183 | + /** |
| 184 | + * Products provider for tests |
| 185 | + * |
| 186 | + * @return array |
| 187 | + */ |
| 188 | + abstract public function productProvider(): array; |
| 189 | + |
| 190 | + /** |
| 191 | + * Provider for unique attribute tests |
| 192 | + * |
| 193 | + * @return array |
| 194 | + */ |
| 195 | + abstract public function uniqueAttributeValueProvider(): array; |
| 196 | +} |
0 commit comments