From 08ab5098da260305b3318c5313b30f0af24156a6 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 09:28:47 +0100 Subject: [PATCH 01/11] Refactor: move interface attributes array to interface itself --- .../Catalog/Api/Data/ProductInterface.php | 18 +++++++++++++++++ app/code/Magento/Catalog/Model/Product.php | 20 +++++-------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/Catalog/Api/Data/ProductInterface.php b/app/code/Magento/Catalog/Api/Data/ProductInterface.php index a79c76fd8e2b6..4968f49fd20dc 100644 --- a/app/code/Magento/Catalog/Api/Data/ProductInterface.php +++ b/app/code/Magento/Catalog/Api/Data/ProductInterface.php @@ -36,6 +36,24 @@ interface ProductInterface extends \Magento\Framework\Api\CustomAttributesDataIn const UPDATED_AT = 'updated_at'; + const MEDIA_GALLERY = 'media_gallery'; + + const TIER_PRICE = 'tier_price'; + + const ATTRIBUTES = [ + self::SKU, + self::NAME, + self::PRICE, + self::WEIGHT, + self::STATUS, + self::VISIBILITY, + self::ATTRIBUTE_SET_ID, + self::TYPE_ID, + self::CREATED_AT, + self::UPDATED_AT, + self::MEDIA_GALLERY, + self::TIER_PRICE, + ]; /**#@-*/ /** diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index cf1392a7e9e8c..55f61d1757dc0 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -305,22 +305,12 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements /** * List of attributes in ProductInterface + * + * @deprecated + * @see ProductInterface::ATTRIBUTES * @var array */ - protected $interfaceAttributes = [ - ProductInterface::SKU, - ProductInterface::NAME, - ProductInterface::PRICE, - ProductInterface::WEIGHT, - ProductInterface::STATUS, - ProductInterface::VISIBILITY, - ProductInterface::ATTRIBUTE_SET_ID, - ProductInterface::TYPE_ID, - ProductInterface::CREATED_AT, - ProductInterface::UPDATED_AT, - 'media_gallery', - 'tier_price', - ]; + protected $interfaceAttributes = ProductInterface::ATTRIBUTES; /** * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface @@ -480,7 +470,7 @@ protected function getCustomAttributesCodes() { if ($this->customAttributesCodes === null) { $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes); + $this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES); } return $this->customAttributesCodes; } From 74deee43ff0c76cb2f841d700d0673e693703f99 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 10:05:17 +0100 Subject: [PATCH 02/11] Move getCustomAttributeCodes from product model to resource model --- app/code/Magento/Catalog/Model/Product.php | 12 +++++---- .../Model/ResourceModel/AbstractResource.php | 1 + .../Catalog/Model/ResourceModel/Product.php | 27 ++++++++++++++++++- .../Eav/Model/Entity/AbstractEntity.php | 22 +++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index 55f61d1757dc0..1fd63075a44e5 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -117,6 +117,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements */ protected $_urlModel = null; + /** + * @var ResourceModel\Product + */ + protected $_resource; + /** * @var string */ @@ -269,6 +274,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements protected $imageCacheFactory; /** + * @deprecated not used anymore, related functionality has been moved to resource model * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface */ protected $metadataService; @@ -468,11 +474,7 @@ protected function _construct() */ protected function getCustomAttributesCodes() { - if ($this->customAttributesCodes === null) { - $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES); - } - return $this->customAttributesCodes; + return $this->_resource->getCustomAttributesCodes(); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php b/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php index a97a3dcdd2164..099fcf3c50ed7 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php @@ -577,4 +577,5 @@ public function getAttributeRawValue($entityId, $attribute, $store) return $attributesData === false ? false : $attributesData; } + } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index a5fdc264aa19a..baa58eb3aeaec 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -5,6 +5,8 @@ */ namespace Magento\Catalog\Model\ResourceModel; +use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink; use Magento\Framework\App\ObjectManager; @@ -83,6 +85,16 @@ class Product extends AbstractResource */ private $productCategoryLink; + /** + * @var ProductAttributeRepositoryInterface + */ + protected $metadataService; + + /** + * @var string[] + */ + private $customAttributesCodes; + /** * @param \Magento\Eav\Model\Entity\Context $context * @param \Magento\Store\Model\StoreManagerInterface $storeManager @@ -94,6 +106,7 @@ class Product extends AbstractResource * @param \Magento\Eav\Model\Entity\TypeFactory $typeFactory * @param \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes * @param array $data + * @param ProductAttributeRepositoryInterface|null $metadataService * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -107,7 +120,8 @@ public function __construct( \Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory, \Magento\Eav\Model\Entity\TypeFactory $typeFactory, \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes, - $data = [] + $data = [], + ProductAttributeRepositoryInterface $metadataService = null ) { $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_catalogCategory = $catalogCategory; @@ -115,6 +129,9 @@ public function __construct( $this->setFactory = $setFactory; $this->typeFactory = $typeFactory; $this->defaultAttributes = $defaultAttributes; + $this->metadataService = $metadataService ?? ObjectManager::getInstance( + ProductAttributeRepositoryInterface::class + ); parent::__construct( $context, $storeManager, @@ -232,6 +249,14 @@ public function getCategoryIds($product) return array_column($result, 'category_id'); } + public function getCustomAttributesCodes() + { + if ($this->customAttributesCodes === null) { + $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); + $this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES); + } + } + /** * Get product identifier by sku * diff --git a/app/code/Magento/Eav/Model/Entity/AbstractEntity.php b/app/code/Magento/Eav/Model/Entity/AbstractEntity.php index c94d78238a1ab..045492e3ef24d 100644 --- a/app/code/Magento/Eav/Model/Entity/AbstractEntity.php +++ b/app/code/Magento/Eav/Model/Entity/AbstractEntity.php @@ -10,6 +10,7 @@ use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend; use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource; +use Magento\Framework\Api\MetadataServiceInterface; use Magento\Framework\App\Config\Element; use Magento\Framework\DB\Adapter\DuplicateException; use Magento\Framework\Exception\AlreadyExistsException; @@ -1958,4 +1959,25 @@ protected function loadAttributesForObject($attributes, $object = null) } } } + + /** + * Receive a list of EAV attributes using provided metadata service. + * + * @param MetadataServiceInterface $metadataService + * @return string[] + */ + protected function getEavAttributesCodes(MetadataServiceInterface $metadataService) + { + $attributeCodes = []; + $customAttributesMetadata = $metadataService->getCustomAttributesMetadata( + $this->getEntityType()->getEntityModel() + ); + if (is_array($customAttributesMetadata)) { + /** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */ + foreach ($customAttributesMetadata as $attribute) { + $attributeCodes[] = $attribute->getAttributeCode(); + } + } + return $attributeCodes; + } } From 53e03b25c0eabf844d8ca36e2d51465c99fef8b3 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 11:31:40 +0100 Subject: [PATCH 03/11] Update unit tests --- .../Model/ResourceModel/AbstractResource.php | 1 - .../Catalog/Model/ResourceModel/Product.php | 7 ++-- .../Catalog/Test/Unit/Model/ProductTest.php | 30 ++++------------ .../Unit/Model/ResourceModel/ProductTest.php | 34 +++++++++++++++++++ .../Model/AbstractExtensibleModel.php | 3 ++ 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php b/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php index 099fcf3c50ed7..a97a3dcdd2164 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php @@ -577,5 +577,4 @@ public function getAttributeRawValue($entityId, $attribute, $store) return $attributesData === false ? false : $attributesData; } - } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index baa58eb3aeaec..c75eba4513697 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -129,7 +129,7 @@ public function __construct( $this->setFactory = $setFactory; $this->typeFactory = $typeFactory; $this->defaultAttributes = $defaultAttributes; - $this->metadataService = $metadataService ?? ObjectManager::getInstance( + $this->metadataService = $metadataService ?? ObjectManager::getInstance()->get( ProductAttributeRepositoryInterface::class ); parent::__construct( @@ -253,8 +253,11 @@ public function getCustomAttributesCodes() { if ($this->customAttributesCodes === null) { $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES); + $this->customAttributesCodes = array_values( + array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES) + ); } + return $this->customAttributesCodes; } /** diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php index 8d7e3dfb3f2fd..420615f2e92c3 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php @@ -8,6 +8,7 @@ use Magento\Catalog\Api\Data\ProductExtensionFactory; use Magento\Catalog\Api\Data\ProductExtensionInterface; +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\Catalog\Model\Product; use Magento\Framework\Api\Data\ImageContentInterface; use Magento\Framework\Api\ExtensibleDataInterface; @@ -140,11 +141,6 @@ class ProductTest extends \PHPUnit\Framework\TestCase */ protected $dataObjectHelperMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $metadataServiceMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -274,9 +270,7 @@ protected function setUp() ); $optionFactory->expects($this->any())->method('create')->willReturn($this->optionInstanceMock); - $this->resource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class) - ->disableOriginalConstructor() - ->getMock(); + $this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class); $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class) ->disableOriginalConstructor() @@ -328,7 +322,6 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->metadataServiceMock = $this->createMock(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class); $this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class) ->disableOriginalConstructor()->getMock(); @@ -391,7 +384,7 @@ protected function setUp() 'catalogProduct' => $this->_catalogProduct, 'imageCacheFactory' => $this->imageCacheFactory, 'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock, - 'metadataService' => $this->metadataServiceMock, + 'metadataService' => $this->createMock(ProductAttributeRepositoryInterface::class), 'customAttributeFactory' => $this->attributeValueFactory, 'mediaGalleryEntryConverterPool' => $this->mediaGalleryEntryConverterPoolMock, 'linkRepository' => $this->productLinkRepositoryMock, @@ -1269,19 +1262,10 @@ public function testGetCustomAttributes() { $priceCode = 'price'; $colorAttributeCode = 'color'; - $interfaceAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class); - $interfaceAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($priceCode); - $colorAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class); - $colorAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($colorAttributeCode); - $customAttributesMetadata = [$interfaceAttribute, $colorAttribute]; - - $this->metadataServiceMock->expects($this->once()) - ->method('getCustomAttributesMetadata') - ->willReturn($customAttributesMetadata); + + $this->resource + ->method('getCustomAttributesCodes') + ->willReturn([$colorAttributeCode]); $this->model->setData($priceCode, 10); //The color attribute is not set, expect empty custom attribute array diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php index 0606a31befcb7..6c8fc92e54ca2 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php @@ -6,10 +6,14 @@ namespace Magento\Catalog\Test\Unit\Model\ResourceModel; +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; +use Magento\Catalog\Model\Product; +use Magento\Framework\Api\MetadataObjectInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class ProductTest extends \PHPUnit\Framework\TestCase { + private $metadataService; /** * @var \Magento\Catalog\Model\ResourceModel\Product */ @@ -38,11 +42,20 @@ protected function setUp() ['create', '__wakeup'] ); + $this->metadataService = $this->createMock(ProductAttributeRepositoryInterface::class); + + $entityTypeMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['getEntityModel']); + $entityTypeMock->method('getEntityModel')->willReturn(Product::class); + $eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class); + $eavConfigMock->method('getEntityType')->willReturn($entityTypeMock); + $this->model = $objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product::class, [ 'setFactory' => $this->setFactoryMock, 'typeFactory' => $this->typeFactoryMock, + 'eavConfig' => $eavConfigMock, + 'metadataService' => $this->metadataService, ] ); } @@ -78,4 +91,25 @@ public function testValidateWrongAttributeSet() $this->assertEquals($expectedErrorMessage, $this->model->validate($productMock)); } + + public function testGetCustomAttributes() + { + $priceCode = 'price'; + $colorAttributeCode = 'color'; + $interfaceAttribute = $this->createMock(MetadataObjectInterface::class); + $interfaceAttribute->expects($this->once()) + ->method('getAttributeCode') + ->willReturn($priceCode); + $colorAttribute = $this->createMock(MetadataObjectInterface::class); + $colorAttribute->expects($this->once()) + ->method('getAttributeCode') + ->willReturn($colorAttributeCode); + $customAttributesMetadata = [$interfaceAttribute, $colorAttribute]; + + $this->metadataService->expects($this->once()) + ->method('getCustomAttributesMetadata') + ->willReturn($customAttributesMetadata); + + $this->assertEquals([$colorAttributeCode], $this->model->getCustomAttributesCodes()); + } } diff --git a/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php b/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php index 6966d9711cb56..477aa085a8d7b 100644 --- a/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php +++ b/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php @@ -35,6 +35,7 @@ abstract class AbstractExtensibleModel extends AbstractModel implements protected $customAttributeFactory; /** + * @deprecated Attribute codes are the same for all entities of the same type and should be saved in resource model * @var string[] */ protected $customAttributesCodes = null; @@ -286,6 +287,8 @@ protected function getCustomAttributesCodes() * * Can be used in child classes, which represent EAV entities. * + * @deprecated attribute codes should be managed centrally in resource model + * @see \Magento\Eav\Model\Entity\AbstractEntity::getEavAttributesCodes() * @param \Magento\Framework\Api\MetadataServiceInterface $metadataService * @return string[] */ From ddc1b03fc302322bbabf3744f4e8048bbd2ff707 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 11:51:36 +0100 Subject: [PATCH 04/11] Move getCustomAttributeCodes from category model to resource model --- .../Catalog/Api/Data/CategoryInterface.php | 31 ++++++++++ app/code/Magento/Catalog/Model/Category.php | 46 ++++----------- .../Catalog/Model/ResourceModel/Category.php | 36 +++++++++-- .../Catalog/Test/Unit/Model/CategoryTest.php | 59 ++++++++----------- .../Catalog/Test/Unit/Model/ProductTest.php | 28 +++++---- .../Unit/Model/ResourceModel/CategoryTest.php | 45 ++++++++++++-- .../Unit/Model/ResourceModel/ProductTest.php | 20 ++++--- 7 files changed, 168 insertions(+), 97 deletions(-) diff --git a/app/code/Magento/Catalog/Api/Data/CategoryInterface.php b/app/code/Magento/Catalog/Api/Data/CategoryInterface.php index b65cdafbe26f4..b9a23e9d08ec3 100644 --- a/app/code/Magento/Catalog/Api/Data/CategoryInterface.php +++ b/app/code/Magento/Catalog/Api/Data/CategoryInterface.php @@ -14,6 +14,37 @@ */ interface CategoryInterface extends \Magento\Framework\Api\CustomAttributesDataInterface { + /**#@+ + * Constants defined for keys of data array + */ + const KEY_PARENT_ID = 'parent_id'; + const KEY_NAME = 'name'; + const KEY_IS_ACTIVE = 'is_active'; + const KEY_POSITION = 'position'; + const KEY_LEVEL = 'level'; + const KEY_UPDATED_AT = 'updated_at'; + const KEY_CREATED_AT = 'created_at'; + const KEY_PATH = 'path'; + const KEY_AVAILABLE_SORT_BY = 'available_sort_by'; + const KEY_INCLUDE_IN_MENU = 'include_in_menu'; + const KEY_PRODUCT_COUNT = 'product_count'; + const KEY_CHILDREN_DATA = 'children_data'; + + const ATTRIBUTES = [ + 'id', + self::KEY_PARENT_ID, + self::KEY_NAME, + self::KEY_IS_ACTIVE, + self::KEY_POSITION, + self::KEY_LEVEL, + self::KEY_UPDATED_AT, + self::KEY_CREATED_AT, + self::KEY_AVAILABLE_SORT_BY, + self::KEY_INCLUDE_IN_MENU, + self::KEY_CHILDREN_DATA, + ]; + /**#@-*/ + /** * @return int|null */ diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index c4d6742a02dd3..d7c63f9963994 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -6,6 +6,7 @@ namespace Magento\Catalog\Model; use Magento\Catalog\Api\CategoryRepositoryInterface; +use Magento\Catalog\Api\Data\CategoryInterface; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; use Magento\Framework\Api\AttributeValueFactory; use Magento\Framework\Convert\ConvertArray; @@ -69,23 +70,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements const CACHE_TAG = 'cat_c'; - /**#@+ - * Constants - */ - const KEY_PARENT_ID = 'parent_id'; - const KEY_NAME = 'name'; - const KEY_IS_ACTIVE = 'is_active'; - const KEY_POSITION = 'position'; - const KEY_LEVEL = 'level'; - const KEY_UPDATED_AT = 'updated_at'; - const KEY_CREATED_AT = 'created_at'; - const KEY_PATH = 'path'; - const KEY_AVAILABLE_SORT_BY = 'available_sort_by'; - const KEY_INCLUDE_IN_MENU = 'include_in_menu'; - const KEY_PRODUCT_COUNT = 'product_count'; - const KEY_CHILDREN_DATA = 'children_data'; - /**#@-*/ - /**#@-*/ protected $_eventPrefix = 'catalog_category'; @@ -118,6 +102,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements */ protected $_urlRewrite; + /** + * @var ResourceModel\Category + */ + protected $_resource; + /** * Use flat resource model flag * @@ -142,21 +131,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements /** * Attributes are that part of interface * + * @deprecated + * @see CategoryInterface::ATTRIBUTES * @var array */ - protected $interfaceAttributes = [ - 'id', - self::KEY_PARENT_ID, - self::KEY_NAME, - self::KEY_IS_ACTIVE, - self::KEY_POSITION, - self::KEY_LEVEL, - self::KEY_UPDATED_AT, - self::KEY_CREATED_AT, - self::KEY_AVAILABLE_SORT_BY, - self::KEY_INCLUDE_IN_MENU, - self::KEY_CHILDREN_DATA, - ]; + protected $interfaceAttributes = CategoryInterface::ATTRIBUTES; /** * Category tree model @@ -226,6 +205,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements protected $categoryRepository; /** + * @deprecated not used anymore, related functionality has been moved to resource model * @var \Magento\Framework\Api\MetadataServiceInterface */ protected $metadataService; @@ -323,11 +303,7 @@ protected function _construct() */ protected function getCustomAttributesCodes() { - if ($this->customAttributesCodes === null) { - $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes); - } - return $this->customAttributesCodes; + return $this->_resource->getCustomAttributesCodes(); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index a9c705697b268..949c177bd1d25 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -11,6 +11,9 @@ */ namespace Magento\Catalog\Model\ResourceModel; +use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; +use Magento\Catalog\Api\Data\CategoryInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\EntityManager\EntityManager; /** @@ -82,6 +85,16 @@ class Category extends AbstractResource */ protected $aggregateCount; + /** + * @var CategoryAttributeRepositoryInterface + */ + protected $metadataService; + + /** + * @var string[] + */ + private $customAttributesCodes; + /** * Category constructor. * @param \Magento\Eav\Model\Entity\Context $context @@ -92,6 +105,7 @@ class Category extends AbstractResource * @param Category\CollectionFactory $categoryCollectionFactory * @param array $data * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @param CategoryAttributeRepositoryInterface|null $metaDataService */ public function __construct( \Magento\Eav\Model\Entity\Context $context, @@ -101,7 +115,8 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, $data = [], - \Magento\Framework\Serialize\Serializer\Json $serializer = null + \Magento\Framework\Serialize\Serializer\Json $serializer = null, + CategoryAttributeRepositoryInterface $metaDataService = null ) { parent::__construct( $context, @@ -113,8 +128,21 @@ public function __construct( $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_eventManager = $eventManager; $this->connectionName = 'catalog'; - $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + $this->serializer = $serializer ?: ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); + $this->metadataService = $metaDataService ?? ObjectManager::getInstance() + ->get(CategoryAttributeRepositoryInterface::class); + } + + public function getCustomAttributesCodes() + { + if ($this->customAttributesCodes === null) { + $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); + $this->customAttributesCodes = array_values( + array_diff($this->customAttributesCodes, CategoryInterface::ATTRIBUTES) + ); + } + return $this->customAttributesCodes; } /** @@ -1060,7 +1088,7 @@ public function save(\Magento\Framework\Model\AbstractModel $object) private function getEntityManager() { if (null === $this->entityManager) { - $this->entityManager = \Magento\Framework\App\ObjectManager::getInstance() + $this->entityManager = ObjectManager::getInstance() ->get(\Magento\Framework\EntityManager\EntityManager::class); } return $this->entityManager; @@ -1072,7 +1100,7 @@ private function getEntityManager() private function getAggregateCount() { if (null === $this->aggregateCount) { - $this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance() + $this->aggregateCount = ObjectManager::getInstance() ->get(\Magento\Catalog\Model\ResourceModel\Category\AggregateCount::class); } return $this->aggregateCount; diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php index d96ac4bfaab0a..43e151046ec13 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php @@ -6,6 +6,7 @@ namespace Magento\Catalog\Test\Unit\Model; +use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; use Magento\Catalog\Model\Indexer; use Magento\Catalog\Model\Category; @@ -105,11 +106,6 @@ class CategoryTest extends \PHPUnit\Framework\TestCase */ private $indexerRegistry; - /** - * @var \Magento\Catalog\Api\CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $metadataServiceMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -155,9 +151,6 @@ protected function setUp() $this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class); $this->indexerRegistry = $this->createPartialMock(\Magento\Framework\Indexer\IndexerRegistry::class, ['get']); - $this->metadataServiceMock = $this->createMock( - \Magento\Catalog\Api\CategoryAttributeRepositoryInterface::class - ); $this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class) ->disableOriginalConstructor()->getMock(); @@ -307,7 +300,7 @@ protected function getCategoryModel() 'urlFinder' => $this->urlFinder, 'resource' => $this->resource, 'indexerRegistry' => $this->indexerRegistry, - 'metadataService' => $this->metadataServiceMock, + 'metadataService' => $this->createMock(CategoryAttributeRepositoryInterface::class), 'customAttributeFactory' => $this->attributeValueFactory, ] ); @@ -431,43 +424,39 @@ public function testReindexFlatDisabled( public function testGetCustomAttributes() { - $nameAttributeCode = 'name'; - $descriptionAttributeCode = 'description'; - $interfaceAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class); - $interfaceAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($nameAttributeCode); - $descriptionAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class); - $descriptionAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($descriptionAttributeCode); - $customAttributesMetadata = [$interfaceAttribute, $descriptionAttribute]; - - $this->metadataServiceMock->expects($this->once()) - ->method('getCustomAttributesMetadata') - ->willReturn($customAttributesMetadata); - $this->category->setData($nameAttributeCode, "sub"); - - //The color attribute is not set, expect empty custom attribute array + $interfaceAttributeCode = 'name'; + $customAttributeCode = 'description'; + + $this->resource + ->method('getCustomAttributesCodes') + ->willReturn([$customAttributeCode]); + $this->category->setData($interfaceAttributeCode, "sub"); + + //The description attribute is not set, expect empty custom attribute array $this->assertEquals([], $this->category->getCustomAttributes()); - //Set the color attribute; - $this->category->setData($descriptionAttributeCode, "description"); + //Set the description attribute; + $initialCustomAttributeValue = "initial description"; + $this->category->setData($customAttributeCode, $initialCustomAttributeValue); $attributeValue = new \Magento\Framework\Api\AttributeValue(); $attributeValue2 = new \Magento\Framework\Api\AttributeValue(); $this->attributeValueFactory->expects($this->exactly(2))->method('create') ->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2); $this->assertEquals(1, count($this->category->getCustomAttributes())); - $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); - $this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue()); + $this->assertNotNull($this->category->getCustomAttribute($customAttributeCode)); + $this->assertEquals( + $initialCustomAttributeValue, + $this->category->getCustomAttribute($customAttributeCode)->getValue() + ); //Change the attribute value, should reflect in getCustomAttribute - $this->category->setData($descriptionAttributeCode, "new description"); + $newCustomAttributeValue = "new description"; + $this->category->setData($customAttributeCode, $newCustomAttributeValue); $this->assertEquals(1, count($this->category->getCustomAttributes())); - $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); + $this->assertNotNull($this->category->getCustomAttribute($customAttributeCode)); $this->assertEquals( - "new description", - $this->category->getCustomAttribute($descriptionAttributeCode)->getValue() + $newCustomAttributeValue, + $this->category->getCustomAttribute($customAttributeCode)->getValue() ); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php index 420615f2e92c3..af0be648450ff 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php @@ -1260,32 +1260,40 @@ public function testGetMediaGalleryImagesMerging() public function testGetCustomAttributes() { - $priceCode = 'price'; - $colorAttributeCode = 'color'; + $interfaceAttributeCode = 'price'; + $customAttributeCode = 'color'; $this->resource ->method('getCustomAttributesCodes') - ->willReturn([$colorAttributeCode]); - $this->model->setData($priceCode, 10); + ->willReturn([$customAttributeCode]); + $this->model->setData($interfaceAttributeCode, 10); //The color attribute is not set, expect empty custom attribute array $this->assertEquals([], $this->model->getCustomAttributes()); //Set the color attribute; - $this->model->setData($colorAttributeCode, "red"); + $initialCustomAttribueValue = "red"; + $this->model->setData($customAttributeCode, $initialCustomAttribueValue); $attributeValue = new \Magento\Framework\Api\AttributeValue(); $attributeValue2 = new \Magento\Framework\Api\AttributeValue(); $this->attributeValueFactory->expects($this->exactly(2))->method('create') ->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2); $this->assertEquals(1, count($this->model->getCustomAttributes())); - $this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode)); - $this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue()); + $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode)); + $this->assertEquals( + $initialCustomAttribueValue, + $this->model->getCustomAttribute($customAttributeCode)->getValue() + ); //Change the attribute value, should reflect in getCustomAttribute - $this->model->setData($colorAttributeCode, "blue"); + $newCustomAttributeValue = "blue"; + $this->model->setData($customAttributeCode, $newCustomAttributeValue); $this->assertEquals(1, count($this->model->getCustomAttributes())); - $this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode)); - $this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue()); + $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode)); + $this->assertEquals( + $newCustomAttributeValue, + $this->model->getCustomAttribute($customAttributeCode)->getValue() + ); } /** diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php index 4812751792f18..bd8f139b83cc1 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php @@ -6,6 +6,7 @@ namespace Magento\Catalog\Test\Unit\Model\ResourceModel; +use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; use Magento\Catalog\Model\Factory; use Magento\Catalog\Model\ResourceModel\Category; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; @@ -14,6 +15,7 @@ use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; use Magento\Eav\Model\Entity\Context; use Magento\Eav\Model\Entity\Type; +use Magento\Framework\Api\MetadataObjectInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Adapter\AdapterInterface as Adapter; use Magento\Framework\DB\Select; @@ -51,6 +53,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase */ private $resourceMock; + /** + * @var CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $metadataService; + /** * @var Config|\PHPUnit_Framework_MockObject_MockObject */ @@ -97,11 +104,8 @@ class CategoryTest extends \PHPUnit\Framework\TestCase protected function setUp() { $this->selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock(); - $this->selectMock->expects($this->at(2))->method('where')->willReturnSelf(); - $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); - $this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf(); $this->connectionMock = $this->getMockBuilder(Adapter::class)->getMockForAbstractClass(); - $this->connectionMock->expects($this->once())->method('select')->willReturn($this->selectMock); + $this->connectionMock->method('select')->willReturn($this->selectMock); $this->resourceMock = $this->getMockBuilder(ResourceConnection::class)->disableOriginalConstructor()->getMock(); $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock); $this->connectionMock->expects($this->any())->method('getTableName')->willReturn('TableName'); @@ -124,6 +128,11 @@ protected function setUp() $this->serializerMock = $this->getMockBuilder(Json::class)->getMock(); + $this->metadataService = $this->createMock(CategoryAttributeRepositoryInterface::class); + $entityTypeMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['getEntityModel']); + $entityTypeMock->method('getEntityModel')->willReturn(\Magento\Catalog\Model\Category::class); + $this->eavConfigMock->method('getEntityType')->willReturn($entityTypeMock); + $this->category = new Category( $this->contextMock, $this->storeManagerMock, @@ -132,7 +141,8 @@ protected function setUp() $this->treeFactoryMock, $this->collectionFactoryMock, [], - $this->serializerMock + $this->serializerMock, + $this->metadataService ); } @@ -146,6 +156,10 @@ public function testFindWhereAttributeIs() $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->getMock(); $backendModel = $this->getMockBuilder(AbstractBackend::class)->disableOriginalConstructor()->getMock(); + $this->selectMock->expects($this->at(2))->method('where')->willReturnSelf(); + $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); + $this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf(); + $attribute->expects($this->any())->method('getBackend')->willReturn($backendModel); $this->connectionMock->expects($this->once())->method('fetchCol')->willReturn(['result']); $this->serializerMock->expects($this->once()) @@ -161,4 +175,25 @@ function ($value) { $result = $this->category->findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue); $this->assertEquals(['result'], $result); } + + public function testGetCustomAttributes() + { + $interfaceAttributeCode = 'name'; + $customAttributeCode = 'description'; + $interfaceAttribute = $this->createMock(MetadataObjectInterface::class); + $interfaceAttribute->expects($this->once()) + ->method('getAttributeCode') + ->willReturn($interfaceAttributeCode); + $customAttribute = $this->createMock(MetadataObjectInterface::class); + $customAttribute->expects($this->once()) + ->method('getAttributeCode') + ->willReturn($customAttributeCode); + $customAttributesMetadata = [$interfaceAttribute, $customAttribute]; + + $this->metadataService->expects($this->once()) + ->method('getCustomAttributesMetadata') + ->willReturn($customAttributesMetadata); + + $this->assertEquals([$customAttributeCode], $this->category->getCustomAttributesCodes()); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php index 6c8fc92e54ca2..b6bd27d6ac8d8 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php @@ -13,7 +13,11 @@ class ProductTest extends \PHPUnit\Framework\TestCase { + /** + * @var ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject + */ private $metadataService; + /** * @var \Magento\Catalog\Model\ResourceModel\Product */ @@ -94,22 +98,22 @@ public function testValidateWrongAttributeSet() public function testGetCustomAttributes() { - $priceCode = 'price'; - $colorAttributeCode = 'color'; + $interfaceAttributeCode = 'price'; + $customAttributeCode = 'color'; $interfaceAttribute = $this->createMock(MetadataObjectInterface::class); $interfaceAttribute->expects($this->once()) ->method('getAttributeCode') - ->willReturn($priceCode); - $colorAttribute = $this->createMock(MetadataObjectInterface::class); - $colorAttribute->expects($this->once()) + ->willReturn($interfaceAttributeCode); + $customAttribute = $this->createMock(MetadataObjectInterface::class); + $customAttribute->expects($this->once()) ->method('getAttributeCode') - ->willReturn($colorAttributeCode); - $customAttributesMetadata = [$interfaceAttribute, $colorAttribute]; + ->willReturn($customAttributeCode); + $customAttributesMetadata = [$interfaceAttribute, $customAttribute]; $this->metadataService->expects($this->once()) ->method('getCustomAttributesMetadata') ->willReturn($customAttributesMetadata); - $this->assertEquals([$colorAttributeCode], $this->model->getCustomAttributesCodes()); + $this->assertEquals([$customAttributeCode], $this->model->getCustomAttributesCodes()); } } From e69be4466fd947e28aedbb0b67e075aa862591e7 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 13:58:41 +0100 Subject: [PATCH 05/11] Add missing method call for reflection class name Without getName(), __toString() is triggered, which creates a string representation of the whole class and apparently also can lead to errors --- .../Magento/Framework/Api/ExtensionAttributesFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php index d7a92460a7f4d..b0263aa1a05e5 100644 --- a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php +++ b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php @@ -49,7 +49,7 @@ public function create($extensibleClassName, $data = []) $interfaceReflection = new \ReflectionClass($this->getExtensibleInterfaceName($extensibleClassName)); $methodReflection = $interfaceReflection->getMethod('getExtensionAttributes'); - if ($methodReflection->getDeclaringClass() == self::EXTENSIBLE_INTERFACE_NAME) { + if ($methodReflection->getDeclaringClass()->getName() === self::EXTENSIBLE_INTERFACE_NAME) { throw new \LogicException( "Method 'getExtensionAttributes' must be overridden in the interfaces " . "which extend '" . self::EXTENSIBLE_INTERFACE_NAME . "'. " From ab558bdc08fb0f4610682218acc3a427fb5cb5e3 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 22 Jan 2018 14:30:56 +0100 Subject: [PATCH 06/11] Fix error with uninstantiated resource model --- app/code/Magento/Catalog/Model/Category.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index d7c63f9963994..67a3627db9349 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -303,7 +303,17 @@ protected function _construct() */ protected function getCustomAttributesCodes() { - return $this->_resource->getCustomAttributesCodes(); + return $this->_getResource()->getCustomAttributesCodes(); + } + + /** + * @throws \Magento\Framework\Exception\LocalizedException + * @return \Magento\Catalog\Model\ResourceModel\Category + * @deprecated because resource models should be used directly + */ + protected function _getResource() + { + return parent::_getResource(); } /** From 7d4523c1fa108755792f1867d5172276648eb9f0 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Thu, 8 Feb 2018 17:15:04 +0100 Subject: [PATCH 07/11] Make new properties private --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 2 +- app/code/Magento/Catalog/Model/ResourceModel/Product.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 949c177bd1d25..f2cafd68f43da 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -88,7 +88,7 @@ class Category extends AbstractResource /** * @var CategoryAttributeRepositoryInterface */ - protected $metadataService; + private $metadataService; /** * @var string[] diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index c75eba4513697..b42e7d850d30a 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -88,7 +88,7 @@ class Product extends AbstractResource /** * @var ProductAttributeRepositoryInterface */ - protected $metadataService; + private $metadataService; /** * @var string[] From 98a9c36cd0b6826e161fbc69f23260460c1076ac Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Fri, 9 Feb 2018 14:55:15 +0100 Subject: [PATCH 08/11] WIP: GetCustomAttributeCodes service --- .../Model/Entity/GetCustomAttributeCodes.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php diff --git a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php new file mode 100644 index 0000000000000..cd93914f42e90 --- /dev/null +++ b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php @@ -0,0 +1,63 @@ +customAttributesCodes[$cacheKey])) { + $customAttributesCodes = $this->getEavAttributesCodes($metadataService, $entityType); + $this->customAttributesCodes[$cacheKey] = array_values( + array_diff($customAttributesCodes, $interfaceAttributes) + ); + } + return $this->customAttributesCodes; + } + + /** + * Receive a list of EAV attributes using provided metadata service. + * + * @param MetadataServiceInterface $metadataService + * @param string|null $entityType + * @return string[] + */ + private function getEavAttributesCodes(MetadataServiceInterface $metadataService, string $entityType = null) + { + $attributeCodes = []; + $customAttributesMetadata = $metadataService->getCustomAttributesMetadata($entityType); + if (is_array($customAttributesMetadata)) { + /** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */ + foreach ($customAttributesMetadata as $attribute) { + $attributeCodes[] = $attribute->getAttributeCode(); + } + } + return $attributeCodes; + } +} From 8378eff211f9e91f831159f652c0d190ccbf1eba Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Thu, 15 Feb 2018 16:21:53 +0100 Subject: [PATCH 09/11] Move retrieval of custom attributes to dedicated services --- app/code/Magento/Catalog/Model/Category.php | 17 +++++++-- .../GetCategoryCustomAttributeCodes.php | 35 +++++++++++++++++++ .../Entity/GetProductCustomAttributeCodes.php | 35 +++++++++++++++++++ app/code/Magento/Catalog/Model/Product.php | 17 +++++++-- .../Catalog/Model/ResourceModel/Category.php | 26 +------------- .../Catalog/Model/ResourceModel/Product.php | 28 +-------------- app/code/Magento/Catalog/etc/di.xml | 6 ++++ .../Model/Entity/GetCustomAttributeCodes.php | 24 ++++--------- .../GetCustomAttributeCodesInterface.php | 20 +++++++++++ app/code/Magento/Eav/etc/di.xml | 1 + 10 files changed, 134 insertions(+), 75 deletions(-) create mode 100644 app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php create mode 100644 app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php create mode 100644 app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodesInterface.php diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 67a3627db9349..9092b49ef9a75 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -7,8 +7,11 @@ use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Api\Data\CategoryInterface; +use Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; +use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface; use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Convert\ConvertArray; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Profiler; @@ -205,11 +208,15 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements protected $categoryRepository; /** - * @deprecated not used anymore, related functionality has been moved to resource model * @var \Magento\Framework\Api\MetadataServiceInterface */ protected $metadataService; + /** + * @var GetCustomAttributeCodesInterface + */ + private $getCustomAttributeCodes; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -255,7 +262,8 @@ public function __construct( CategoryRepositoryInterface $categoryRepository, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + GetCustomAttributeCodesInterface $getCustomAttributeCodes = null ) { $this->metadataService = $metadataService; $this->_treeModel = $categoryTreeResource; @@ -270,6 +278,9 @@ public function __construct( $this->urlFinder = $urlFinder; $this->indexerRegistry = $indexerRegistry; $this->categoryRepository = $categoryRepository; + $this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get( + GetCategoryCustomAttributeCodes::class + ); parent::__construct( $context, $registry, @@ -303,7 +314,7 @@ protected function _construct() */ protected function getCustomAttributesCodes() { - return $this->_getResource()->getCustomAttributesCodes(); + return $this->getCustomAttributeCodes->execute($this->metadataService); } /** diff --git a/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php b/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php new file mode 100644 index 0000000000000..710e155e4a392 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php @@ -0,0 +1,35 @@ +baseCustomAttributeCodes = $baseCustomAttributeCodes; + } + + + public function execute(MetadataServiceInterface $metadataService): array + { + $customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService); + return array_diff($customAttributesCodes, CategoryInterface::ATTRIBUTES); + } +} diff --git a/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php b/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php new file mode 100644 index 0000000000000..76fec4a036ad0 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php @@ -0,0 +1,35 @@ +baseCustomAttributeCodes = $baseCustomAttributeCodes; + } + + + public function execute(MetadataServiceInterface $metadataService): array + { + $customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService); + return array_diff($customAttributesCodes, ProductInterface::ATTRIBUTES); + } +} diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index 1fd63075a44e5..79f0331136d4a 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -9,9 +9,12 @@ use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductLinkRepositoryInterface; +use Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes; use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool; +use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface; use Magento\Framework\Api\AttributeValueFactory; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject\IdentityInterface; use Magento\Framework\Pricing\SaleableInterface; @@ -274,7 +277,6 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements protected $imageCacheFactory; /** - * @deprecated not used anymore, related functionality has been moved to resource model * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface */ protected $metadataService; @@ -342,6 +344,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements */ protected $linkTypeProvider; + /** + * @var GetCustomAttributeCodesInterface + */ + private $getCustomAttributeCodes; + /** * Product constructor. * @param \Magento\Framework\Model\Context $context @@ -418,7 +425,8 @@ public function __construct( EntryConverterPool $mediaGalleryEntryConverterPool, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, - array $data = [] + array $data = [], + GetCustomAttributeCodesInterface $getCustomAttributeCodes = null ) { $this->metadataService = $metadataService; $this->_itemOptionFactory = $itemOptionFactory; @@ -447,6 +455,9 @@ public function __construct( $this->mediaGalleryEntryConverterPool = $mediaGalleryEntryConverterPool; $this->dataObjectHelper = $dataObjectHelper; $this->joinProcessor = $joinProcessor; + $this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get( + GetProductCustomAttributeCodes::class + ); parent::__construct( $context, $registry, @@ -474,7 +485,7 @@ protected function _construct() */ protected function getCustomAttributesCodes() { - return $this->_resource->getCustomAttributesCodes(); + return $this->getCustomAttributeCodes->execute($this->metadataService); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index f2cafd68f43da..753eab3bd92d7 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -85,16 +85,6 @@ class Category extends AbstractResource */ protected $aggregateCount; - /** - * @var CategoryAttributeRepositoryInterface - */ - private $metadataService; - - /** - * @var string[] - */ - private $customAttributesCodes; - /** * Category constructor. * @param \Magento\Eav\Model\Entity\Context $context @@ -115,8 +105,7 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, $data = [], - \Magento\Framework\Serialize\Serializer\Json $serializer = null, - CategoryAttributeRepositoryInterface $metaDataService = null + \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { parent::__construct( $context, @@ -130,19 +119,6 @@ public function __construct( $this->connectionName = 'catalog'; $this->serializer = $serializer ?: ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); - $this->metadataService = $metaDataService ?? ObjectManager::getInstance() - ->get(CategoryAttributeRepositoryInterface::class); - } - - public function getCustomAttributesCodes() - { - if ($this->customAttributesCodes === null) { - $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_values( - array_diff($this->customAttributesCodes, CategoryInterface::ATTRIBUTES) - ); - } - return $this->customAttributesCodes; } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index b42e7d850d30a..2c2ea9e9bbbc4 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -85,16 +85,6 @@ class Product extends AbstractResource */ private $productCategoryLink; - /** - * @var ProductAttributeRepositoryInterface - */ - private $metadataService; - - /** - * @var string[] - */ - private $customAttributesCodes; - /** * @param \Magento\Eav\Model\Entity\Context $context * @param \Magento\Store\Model\StoreManagerInterface $storeManager @@ -106,7 +96,6 @@ class Product extends AbstractResource * @param \Magento\Eav\Model\Entity\TypeFactory $typeFactory * @param \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes * @param array $data - * @param ProductAttributeRepositoryInterface|null $metadataService * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -120,8 +109,7 @@ public function __construct( \Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory, \Magento\Eav\Model\Entity\TypeFactory $typeFactory, \Magento\Catalog\Model\Product\Attribute\DefaultAttributes $defaultAttributes, - $data = [], - ProductAttributeRepositoryInterface $metadataService = null + $data = [] ) { $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_catalogCategory = $catalogCategory; @@ -129,9 +117,6 @@ public function __construct( $this->setFactory = $setFactory; $this->typeFactory = $typeFactory; $this->defaultAttributes = $defaultAttributes; - $this->metadataService = $metadataService ?? ObjectManager::getInstance()->get( - ProductAttributeRepositoryInterface::class - ); parent::__construct( $context, $storeManager, @@ -249,17 +234,6 @@ public function getCategoryIds($product) return array_column($result, 'category_id'); } - public function getCustomAttributesCodes() - { - if ($this->customAttributesCodes === null) { - $this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService); - $this->customAttributesCodes = array_values( - array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES) - ); - } - return $this->customAttributesCodes; - } - /** * Get product identifier by sku * diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index f28d2cbcdff3e..d6e40fad3a327 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -143,6 +143,12 @@ Magento\Catalog\Model\Product\Attribute\Source\Status\Proxy Magento\Catalog\Model\Product\Link\Proxy + Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes + + + + + Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes diff --git a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php index cd93914f42e90..231d53bd8ed52 100644 --- a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php +++ b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php @@ -9,10 +9,10 @@ use Magento\Framework\Api\MetadataServiceInterface; -class GetCustomAttributeCodes +class GetCustomAttributeCodes implements GetCustomAttributeCodesInterface { /** - * @var string[] + * @var string[][] */ private $customAttributesCodes; @@ -20,25 +20,15 @@ class GetCustomAttributeCodes * Receive a list of custom EAV attributes using provided metadata service. The results are cached per entity type * * @param MetadataServiceInterface $metadataService Custom attribute metadata service to be used - * @param string[] $interfaceAttributes Attribute codes that are part of the interface and should not be - * considered custom - * @param string|null $entityType Entity type (class name), only needed if metadata service handles different - * entities * @return string[] */ - public function execute( - MetadataServiceInterface $metadataService, - array $interfaceAttributes, - string $entityType = null - ): array { - $cacheKey = get_class($metadataService) . '|' . $entityType; + public function execute(MetadataServiceInterface $metadataService): array + { + $cacheKey = get_class($metadataService); if (!isset($this->customAttributesCodes[$cacheKey])) { - $customAttributesCodes = $this->getEavAttributesCodes($metadataService, $entityType); - $this->customAttributesCodes[$cacheKey] = array_values( - array_diff($customAttributesCodes, $interfaceAttributes) - ); + $this->customAttributesCodes[$cacheKey] = $this->getEavAttributesCodes($metadataService); } - return $this->customAttributesCodes; + return $this->customAttributesCodes[$cacheKey]; } /** diff --git a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodesInterface.php b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodesInterface.php new file mode 100644 index 0000000000000..c73d626e7364d --- /dev/null +++ b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodesInterface.php @@ -0,0 +1,20 @@ + + From 208b1ecbe9b01aac56db2976b6ef6cb9b621088e Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 21 Feb 2018 14:37:53 +0200 Subject: [PATCH 10/11] Test stabilization. --- app/code/Magento/Catalog/Model/Category.php | 16 +---- .../GetCategoryCustomAttributeCodes.php | 4 +- .../Entity/GetProductCustomAttributeCodes.php | 4 +- app/code/Magento/Catalog/Model/Product.php | 7 +- .../Catalog/Model/ResourceModel/Category.php | 10 +-- .../Catalog/Model/ResourceModel/Product.php | 2 - .../Catalog/Test/Unit/Model/CategoryTest.php | 55 +++++++++------- .../GetCategoryCustomAttributeCodesTest.php | 66 +++++++++++++++++++ .../GetProductCustomAttributeCodesTest.php | 66 +++++++++++++++++++ .../Catalog/Test/Unit/Model/ProductTest.php | 59 ++++++++++------- .../Unit/Model/ResourceModel/CategoryTest.php | 45 ++----------- .../Unit/Model/ResourceModel/ProductTest.php | 38 ----------- .../Eav/Model/Entity/AbstractEntity.php | 22 ------- .../Model/Entity/GetCustomAttributeCodes.php | 1 - .../Entity/GetCustomAttributeCodesTest.php | 59 +++++++++++++++++ .../Api/ExtensionAttributesFactory.php | 2 +- .../Model/AbstractExtensibleModel.php | 3 - 17 files changed, 275 insertions(+), 184 deletions(-) create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/Entity/GetCategoryCustomAttributeCodesTest.php create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/Entity/GetProductCustomAttributeCodesTest.php create mode 100644 app/code/Magento/Eav/Test/Unit/Model/Entity/GetCustomAttributeCodesTest.php diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 453128bee8020..e35846e69a29e 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -105,11 +105,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements */ protected $_urlRewrite; - /** - * @var ResourceModel\Category - */ - protected $_resource; - /** * Use flat resource model flag * @@ -239,6 +234,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data + * @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -317,16 +313,6 @@ protected function getCustomAttributesCodes() return $this->getCustomAttributeCodes->execute($this->metadataService); } - /** - * @throws \Magento\Framework\Exception\LocalizedException - * @return \Magento\Catalog\Model\ResourceModel\Category - * @deprecated because resource models should be used directly - */ - protected function _getResource() - { - return parent::_getResource(); - } - /** * Get flat resource model flag * diff --git a/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php b/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php index 710e155e4a392..b2b9199cc56b4 100644 --- a/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php +++ b/app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php @@ -26,7 +26,9 @@ public function __construct( $this->baseCustomAttributeCodes = $baseCustomAttributeCodes; } - + /** + * @inheritdoc + */ public function execute(MetadataServiceInterface $metadataService): array { $customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService); diff --git a/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php b/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php index 76fec4a036ad0..23678ffcf48b7 100644 --- a/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php +++ b/app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php @@ -26,7 +26,9 @@ public function __construct( $this->baseCustomAttributeCodes = $baseCustomAttributeCodes; } - + /** + * @inheritdoc + */ public function execute(MetadataServiceInterface $metadataService): array { $customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService); diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index 87a62a215ace2..db16c34f123f2 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -120,11 +120,6 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements */ protected $_urlModel = null; - /** - * @var ResourceModel\Product - */ - protected $_resource; - /** * @var string */ @@ -386,7 +381,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor * @param array $data - * + * @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 753eab3bd92d7..a9c705697b268 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -11,9 +11,6 @@ */ namespace Magento\Catalog\Model\ResourceModel; -use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; -use Magento\Catalog\Api\Data\CategoryInterface; -use Magento\Framework\App\ObjectManager; use Magento\Framework\EntityManager\EntityManager; /** @@ -95,7 +92,6 @@ class Category extends AbstractResource * @param Category\CollectionFactory $categoryCollectionFactory * @param array $data * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer - * @param CategoryAttributeRepositoryInterface|null $metaDataService */ public function __construct( \Magento\Eav\Model\Entity\Context $context, @@ -117,7 +113,7 @@ public function __construct( $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_eventManager = $eventManager; $this->connectionName = 'catalog'; - $this->serializer = $serializer ?: ObjectManager::getInstance() + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\Serialize\Serializer\Json::class); } @@ -1064,7 +1060,7 @@ public function save(\Magento\Framework\Model\AbstractModel $object) private function getEntityManager() { if (null === $this->entityManager) { - $this->entityManager = ObjectManager::getInstance() + $this->entityManager = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Framework\EntityManager\EntityManager::class); } return $this->entityManager; @@ -1076,7 +1072,7 @@ private function getEntityManager() private function getAggregateCount() { if (null === $this->aggregateCount) { - $this->aggregateCount = ObjectManager::getInstance() + $this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance() ->get(\Magento\Catalog\Model\ResourceModel\Category\AggregateCount::class); } return $this->aggregateCount; diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product.php b/app/code/Magento/Catalog/Model/ResourceModel/Product.php index 2c2ea9e9bbbc4..a5fdc264aa19a 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product.php @@ -5,8 +5,6 @@ */ namespace Magento\Catalog\Model\ResourceModel; -use Magento\Catalog\Api\Data\ProductInterface; -use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink; use Magento\Framework\App\ObjectManager; diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php index 43e151046ec13..9f5f3313c6859 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php @@ -6,9 +6,8 @@ namespace Magento\Catalog\Test\Unit\Model; -use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; use Magento\Catalog\Model\Indexer; -use Magento\Catalog\Model\Category; +use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface; /** * @SuppressWarnings(PHPMD.TooManyFields) @@ -106,6 +105,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase */ private $indexerRegistry; + /** + * @var \Magento\Catalog\Api\CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $metadataServiceMock; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -116,6 +120,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase */ private $objectManager; + /** + * @var GetCustomAttributeCodesInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $getCustomAttributeCodes; + protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -151,8 +160,15 @@ protected function setUp() $this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class); $this->indexerRegistry = $this->createPartialMock(\Magento\Framework\Indexer\IndexerRegistry::class, ['get']); + $this->metadataServiceMock = $this->createMock( + \Magento\Catalog\Api\CategoryAttributeRepositoryInterface::class + ); $this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class) ->disableOriginalConstructor()->getMock(); + $this->getCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class) + ->setMethods(['execute']) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); $this->category = $this->getCategoryModel(); } @@ -300,8 +316,9 @@ protected function getCategoryModel() 'urlFinder' => $this->urlFinder, 'resource' => $this->resource, 'indexerRegistry' => $this->indexerRegistry, - 'metadataService' => $this->createMock(CategoryAttributeRepositoryInterface::class), + 'metadataService' => $this->metadataServiceMock, 'customAttributeFactory' => $this->attributeValueFactory, + 'getCustomAttributeCodes' => $this->getCustomAttributeCodes ] ); } @@ -424,39 +441,33 @@ public function testReindexFlatDisabled( public function testGetCustomAttributes() { - $interfaceAttributeCode = 'name'; - $customAttributeCode = 'description'; - - $this->resource - ->method('getCustomAttributesCodes') - ->willReturn([$customAttributeCode]); - $this->category->setData($interfaceAttributeCode, "sub"); + $nameAttributeCode = 'name'; + $descriptionAttributeCode = 'description'; + $this->getCustomAttributeCodes->expects($this->exactly(3)) + ->method('execute') + ->willReturn([$descriptionAttributeCode]); + $this->category->setData($nameAttributeCode, "sub"); //The description attribute is not set, expect empty custom attribute array $this->assertEquals([], $this->category->getCustomAttributes()); //Set the description attribute; - $initialCustomAttributeValue = "initial description"; - $this->category->setData($customAttributeCode, $initialCustomAttributeValue); + $this->category->setData($descriptionAttributeCode, "description"); $attributeValue = new \Magento\Framework\Api\AttributeValue(); $attributeValue2 = new \Magento\Framework\Api\AttributeValue(); $this->attributeValueFactory->expects($this->exactly(2))->method('create') ->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2); $this->assertEquals(1, count($this->category->getCustomAttributes())); - $this->assertNotNull($this->category->getCustomAttribute($customAttributeCode)); - $this->assertEquals( - $initialCustomAttributeValue, - $this->category->getCustomAttribute($customAttributeCode)->getValue() - ); + $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); + $this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue()); //Change the attribute value, should reflect in getCustomAttribute - $newCustomAttributeValue = "new description"; - $this->category->setData($customAttributeCode, $newCustomAttributeValue); + $this->category->setData($descriptionAttributeCode, "new description"); $this->assertEquals(1, count($this->category->getCustomAttributes())); - $this->assertNotNull($this->category->getCustomAttribute($customAttributeCode)); + $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode)); $this->assertEquals( - $newCustomAttributeValue, - $this->category->getCustomAttribute($customAttributeCode)->getValue() + "new description", + $this->category->getCustomAttribute($descriptionAttributeCode)->getValue() ); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetCategoryCustomAttributeCodesTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetCategoryCustomAttributeCodesTest.php new file mode 100644 index 0000000000000..465063dccd3d5 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetCategoryCustomAttributeCodesTest.php @@ -0,0 +1,66 @@ +baseCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class) + ->disableOriginalConstructor() + ->setMethods(['execute']) + ->getMockForAbstractClass(); + $objectManager = new ObjectManager($this); + $this->getCategoryCustomAttributeCodes = $objectManager->getObject( + GetCategoryCustomAttributeCodes::class, + ['baseCustomAttributeCodes' => $this->baseCustomAttributeCodes] + ); + } + + /** + * Test GetCategoryCustomAttributeCodes::execute() will return only custom category attribute codes. + */ + public function testExecute() + { + /** @var MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject $metadataService */ + $metadataService = $this->getMockBuilder(MetadataServiceInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $this->baseCustomAttributeCodes->expects($this->once()) + ->method('execute') + ->with($this->identicalTo($metadataService)) + ->willReturn(['test_custom_attribute_code', 'name']); + $this->assertEquals( + ['test_custom_attribute_code'], + $this->getCategoryCustomAttributeCodes->execute($metadataService) + ); + } +} diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetProductCustomAttributeCodesTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetProductCustomAttributeCodesTest.php new file mode 100644 index 0000000000000..a37e1c6df0908 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/Entity/GetProductCustomAttributeCodesTest.php @@ -0,0 +1,66 @@ +baseCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class) + ->disableOriginalConstructor() + ->setMethods(['execute']) + ->getMockForAbstractClass(); + $objectManager = new ObjectManager($this); + $this->getProductCustomAttributeCodes = $objectManager->getObject( + GetProductCustomAttributeCodes::class, + ['baseCustomAttributeCodes' => $this->baseCustomAttributeCodes] + ); + } + + /** + * Test GetProductCustomAttributeCodes::execute() will return only custom product attribute codes. + */ + public function testExecute() + { + /** @var MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject $metadataService */ + $metadataService = $this->getMockBuilder(MetadataServiceInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $this->baseCustomAttributeCodes->expects($this->once()) + ->method('execute') + ->with($this->identicalTo($metadataService)) + ->willReturn(['test_custom_attribute_code', 'name']); + $this->assertEquals( + ['test_custom_attribute_code'], + $this->getProductCustomAttributeCodes->execute($metadataService) + ); + } +} diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php index af0be648450ff..74a71a2828e1d 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php @@ -8,8 +8,8 @@ use Magento\Catalog\Api\Data\ProductExtensionFactory; use Magento\Catalog\Api\Data\ProductExtensionInterface; -use Magento\Catalog\Api\ProductAttributeRepositoryInterface; use Magento\Catalog\Model\Product; +use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface; use Magento\Framework\Api\Data\ImageContentInterface; use Magento\Framework\Api\ExtensibleDataInterface; use Magento\Framework\Api\ExtensionAttributesFactory; @@ -141,6 +141,11 @@ class ProductTest extends \PHPUnit\Framework\TestCase */ protected $dataObjectHelperMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $metadataServiceMock; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -194,6 +199,11 @@ class ProductTest extends \PHPUnit\Framework\TestCase */ private $extensionAttributes; + /** + * @var GetCustomAttributeCodesInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $getCustomAttributeCodes; + /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ @@ -270,7 +280,9 @@ protected function setUp() ); $optionFactory->expects($this->any())->method('create')->willReturn($this->optionInstanceMock); - $this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class); + $this->resource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class) + ->disableOriginalConstructor() + ->getMock(); $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class) ->disableOriginalConstructor() @@ -322,6 +334,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->metadataServiceMock = $this->createMock(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class); $this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class) ->disableOriginalConstructor()->getMock(); @@ -362,6 +375,10 @@ protected function setUp() ->expects($this->any()) ->method('create') ->willReturn($this->extensionAttributes); + $this->getCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class) + ->disableOriginalConstructor() + ->setMethods(['execute']) + ->getMockForAbstractClass(); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->model = $this->objectManagerHelper->getObject( @@ -384,14 +401,15 @@ protected function setUp() 'catalogProduct' => $this->_catalogProduct, 'imageCacheFactory' => $this->imageCacheFactory, 'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock, - 'metadataService' => $this->createMock(ProductAttributeRepositoryInterface::class), + 'metadataService' => $this->metadataServiceMock, 'customAttributeFactory' => $this->attributeValueFactory, 'mediaGalleryEntryConverterPool' => $this->mediaGalleryEntryConverterPoolMock, 'linkRepository' => $this->productLinkRepositoryMock, 'catalogProductMediaConfig' => $this->mediaConfig, '_filesystem' => $this->filesystemMock, '_collectionFactory' => $this->collectionFactoryMock, - 'data' => ['id' => 1] + 'data' => ['id' => 1], + 'getCustomAttributeCodes' => $this->getCustomAttributeCodes ] ); } @@ -1260,40 +1278,31 @@ public function testGetMediaGalleryImagesMerging() public function testGetCustomAttributes() { - $interfaceAttributeCode = 'price'; - $customAttributeCode = 'color'; - - $this->resource - ->method('getCustomAttributesCodes') - ->willReturn([$customAttributeCode]); - $this->model->setData($interfaceAttributeCode, 10); + $priceCode = 'price'; + $colorAttributeCode = 'color'; + $this->getCustomAttributeCodes->expects($this->exactly(3)) + ->method('execute') + ->willReturn([$colorAttributeCode]); + $this->model->setData($priceCode, 10); //The color attribute is not set, expect empty custom attribute array $this->assertEquals([], $this->model->getCustomAttributes()); //Set the color attribute; - $initialCustomAttribueValue = "red"; - $this->model->setData($customAttributeCode, $initialCustomAttribueValue); + $this->model->setData($colorAttributeCode, "red"); $attributeValue = new \Magento\Framework\Api\AttributeValue(); $attributeValue2 = new \Magento\Framework\Api\AttributeValue(); $this->attributeValueFactory->expects($this->exactly(2))->method('create') ->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2); $this->assertEquals(1, count($this->model->getCustomAttributes())); - $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode)); - $this->assertEquals( - $initialCustomAttribueValue, - $this->model->getCustomAttribute($customAttributeCode)->getValue() - ); + $this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode)); + $this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue()); //Change the attribute value, should reflect in getCustomAttribute - $newCustomAttributeValue = "blue"; - $this->model->setData($customAttributeCode, $newCustomAttributeValue); + $this->model->setData($colorAttributeCode, "blue"); $this->assertEquals(1, count($this->model->getCustomAttributes())); - $this->assertNotNull($this->model->getCustomAttribute($customAttributeCode)); - $this->assertEquals( - $newCustomAttributeValue, - $this->model->getCustomAttribute($customAttributeCode)->getValue() - ); + $this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode)); + $this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue()); } /** diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php index bd8f139b83cc1..4812751792f18 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/CategoryTest.php @@ -6,7 +6,6 @@ namespace Magento\Catalog\Test\Unit\Model\ResourceModel; -use Magento\Catalog\Api\CategoryAttributeRepositoryInterface; use Magento\Catalog\Model\Factory; use Magento\Catalog\Model\ResourceModel\Category; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; @@ -15,7 +14,6 @@ use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; use Magento\Eav\Model\Entity\Context; use Magento\Eav\Model\Entity\Type; -use Magento\Framework\Api\MetadataObjectInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Adapter\AdapterInterface as Adapter; use Magento\Framework\DB\Select; @@ -53,11 +51,6 @@ class CategoryTest extends \PHPUnit\Framework\TestCase */ private $resourceMock; - /** - * @var CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $metadataService; - /** * @var Config|\PHPUnit_Framework_MockObject_MockObject */ @@ -104,8 +97,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase protected function setUp() { $this->selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock(); + $this->selectMock->expects($this->at(2))->method('where')->willReturnSelf(); + $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); + $this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf(); $this->connectionMock = $this->getMockBuilder(Adapter::class)->getMockForAbstractClass(); - $this->connectionMock->method('select')->willReturn($this->selectMock); + $this->connectionMock->expects($this->once())->method('select')->willReturn($this->selectMock); $this->resourceMock = $this->getMockBuilder(ResourceConnection::class)->disableOriginalConstructor()->getMock(); $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock); $this->connectionMock->expects($this->any())->method('getTableName')->willReturn('TableName'); @@ -128,11 +124,6 @@ protected function setUp() $this->serializerMock = $this->getMockBuilder(Json::class)->getMock(); - $this->metadataService = $this->createMock(CategoryAttributeRepositoryInterface::class); - $entityTypeMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['getEntityModel']); - $entityTypeMock->method('getEntityModel')->willReturn(\Magento\Catalog\Model\Category::class); - $this->eavConfigMock->method('getEntityType')->willReturn($entityTypeMock); - $this->category = new Category( $this->contextMock, $this->storeManagerMock, @@ -141,8 +132,7 @@ protected function setUp() $this->treeFactoryMock, $this->collectionFactoryMock, [], - $this->serializerMock, - $this->metadataService + $this->serializerMock ); } @@ -156,10 +146,6 @@ public function testFindWhereAttributeIs() $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->getMock(); $backendModel = $this->getMockBuilder(AbstractBackend::class)->disableOriginalConstructor()->getMock(); - $this->selectMock->expects($this->at(2))->method('where')->willReturnSelf(); - $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); - $this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf(); - $attribute->expects($this->any())->method('getBackend')->willReturn($backendModel); $this->connectionMock->expects($this->once())->method('fetchCol')->willReturn(['result']); $this->serializerMock->expects($this->once()) @@ -175,25 +161,4 @@ function ($value) { $result = $this->category->findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue); $this->assertEquals(['result'], $result); } - - public function testGetCustomAttributes() - { - $interfaceAttributeCode = 'name'; - $customAttributeCode = 'description'; - $interfaceAttribute = $this->createMock(MetadataObjectInterface::class); - $interfaceAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($interfaceAttributeCode); - $customAttribute = $this->createMock(MetadataObjectInterface::class); - $customAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($customAttributeCode); - $customAttributesMetadata = [$interfaceAttribute, $customAttribute]; - - $this->metadataService->expects($this->once()) - ->method('getCustomAttributesMetadata') - ->willReturn($customAttributesMetadata); - - $this->assertEquals([$customAttributeCode], $this->category->getCustomAttributesCodes()); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php index b6bd27d6ac8d8..0606a31befcb7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php @@ -6,18 +6,10 @@ namespace Magento\Catalog\Test\Unit\Model\ResourceModel; -use Magento\Catalog\Api\ProductAttributeRepositoryInterface; -use Magento\Catalog\Model\Product; -use Magento\Framework\Api\MetadataObjectInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class ProductTest extends \PHPUnit\Framework\TestCase { - /** - * @var ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $metadataService; - /** * @var \Magento\Catalog\Model\ResourceModel\Product */ @@ -46,20 +38,11 @@ protected function setUp() ['create', '__wakeup'] ); - $this->metadataService = $this->createMock(ProductAttributeRepositoryInterface::class); - - $entityTypeMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['getEntityModel']); - $entityTypeMock->method('getEntityModel')->willReturn(Product::class); - $eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class); - $eavConfigMock->method('getEntityType')->willReturn($entityTypeMock); - $this->model = $objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product::class, [ 'setFactory' => $this->setFactoryMock, 'typeFactory' => $this->typeFactoryMock, - 'eavConfig' => $eavConfigMock, - 'metadataService' => $this->metadataService, ] ); } @@ -95,25 +78,4 @@ public function testValidateWrongAttributeSet() $this->assertEquals($expectedErrorMessage, $this->model->validate($productMock)); } - - public function testGetCustomAttributes() - { - $interfaceAttributeCode = 'price'; - $customAttributeCode = 'color'; - $interfaceAttribute = $this->createMock(MetadataObjectInterface::class); - $interfaceAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($interfaceAttributeCode); - $customAttribute = $this->createMock(MetadataObjectInterface::class); - $customAttribute->expects($this->once()) - ->method('getAttributeCode') - ->willReturn($customAttributeCode); - $customAttributesMetadata = [$interfaceAttribute, $customAttribute]; - - $this->metadataService->expects($this->once()) - ->method('getCustomAttributesMetadata') - ->willReturn($customAttributesMetadata); - - $this->assertEquals([$customAttributeCode], $this->model->getCustomAttributesCodes()); - } } diff --git a/app/code/Magento/Eav/Model/Entity/AbstractEntity.php b/app/code/Magento/Eav/Model/Entity/AbstractEntity.php index 12a68c82c2203..7159cedb61cee 100644 --- a/app/code/Magento/Eav/Model/Entity/AbstractEntity.php +++ b/app/code/Magento/Eav/Model/Entity/AbstractEntity.php @@ -10,7 +10,6 @@ use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend; use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend; use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource; -use Magento\Framework\Api\MetadataServiceInterface; use Magento\Framework\App\Config\Element; use Magento\Framework\DataObject; use Magento\Framework\DB\Adapter\DuplicateException; @@ -1999,25 +1998,4 @@ protected function loadAttributesForObject($attributes, $object = null) } } } - - /** - * Receive a list of EAV attributes using provided metadata service. - * - * @param MetadataServiceInterface $metadataService - * @return string[] - */ - protected function getEavAttributesCodes(MetadataServiceInterface $metadataService) - { - $attributeCodes = []; - $customAttributesMetadata = $metadataService->getCustomAttributesMetadata( - $this->getEntityType()->getEntityModel() - ); - if (is_array($customAttributesMetadata)) { - /** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */ - foreach ($customAttributesMetadata as $attribute) { - $attributeCodes[] = $attribute->getAttributeCode(); - } - } - return $attributeCodes; - } } diff --git a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php index 231d53bd8ed52..a77b298f5d209 100644 --- a/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php +++ b/app/code/Magento/Eav/Model/Entity/GetCustomAttributeCodes.php @@ -6,7 +6,6 @@ namespace Magento\Eav\Model\Entity; - use Magento\Framework\Api\MetadataServiceInterface; class GetCustomAttributeCodes implements GetCustomAttributeCodesInterface diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/GetCustomAttributeCodesTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/GetCustomAttributeCodesTest.php new file mode 100644 index 0000000000000..0ba247e1fbb65 --- /dev/null +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/GetCustomAttributeCodesTest.php @@ -0,0 +1,59 @@ +getCustomAttributeCodes = new GetCustomAttributeCodes(); + } + + /** + * Test GetCustomAttributeCodes::execute() will return attribute codes from attributes metadata. + * + * @return void + */ + public function testExecute() + { + $attributeCode = 'testCode'; + $attributeMetadata = $this->getMockBuilder(MetadataObjectInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getAttributeCode']) + ->getMockForAbstractClass(); + $attributeMetadata->expects($this->once()) + ->method('getAttributeCode') + ->willReturn($attributeCode); + /** @var MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject $metadataService */ + $metadataService = $this->getMockBuilder(MetadataServiceInterface::class) + ->setMethods(['getCustomAttributesMetadata']) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $metadataService->expects($this->once()) + ->method('getCustomAttributesMetadata') + ->willReturn([$attributeMetadata]); + $this->assertEquals([$attributeCode], $this->getCustomAttributeCodes->execute($metadataService)); + } +} diff --git a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php index e1a4431783da6..dab0650fc7f6e 100644 --- a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php +++ b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php @@ -49,7 +49,7 @@ public function create($extensibleClassName, $data = []) $interfaceReflection = new \ReflectionClass($this->getExtensibleInterfaceName($extensibleClassName)); $methodReflection = $interfaceReflection->getMethod('getExtensionAttributes'); - if ($methodReflection->getDeclaringClass()->getName() === self::EXTENSIBLE_INTERFACE_NAME) { + if ($methodReflection->getDeclaringClass() == self::EXTENSIBLE_INTERFACE_NAME) { throw new \LogicException( "Method 'getExtensionAttributes' must be overridden in the interfaces " . "which extend '" . self::EXTENSIBLE_INTERFACE_NAME . "'. " diff --git a/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php b/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php index 477aa085a8d7b..6966d9711cb56 100644 --- a/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php +++ b/lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php @@ -35,7 +35,6 @@ abstract class AbstractExtensibleModel extends AbstractModel implements protected $customAttributeFactory; /** - * @deprecated Attribute codes are the same for all entities of the same type and should be saved in resource model * @var string[] */ protected $customAttributesCodes = null; @@ -287,8 +286,6 @@ protected function getCustomAttributesCodes() * * Can be used in child classes, which represent EAV entities. * - * @deprecated attribute codes should be managed centrally in resource model - * @see \Magento\Eav\Model\Entity\AbstractEntity::getEavAttributesCodes() * @param \Magento\Framework\Api\MetadataServiceInterface $metadataService * @return string[] */ From f286a9857a023d0907874a1f206eba5afa047534 Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 21 Feb 2018 16:58:38 +0200 Subject: [PATCH 11/11] Test stabilization. --- .../Magento/Framework/Api/ExtensionAttributesFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php index dab0650fc7f6e..e1a4431783da6 100644 --- a/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php +++ b/lib/internal/Magento/Framework/Api/ExtensionAttributesFactory.php @@ -49,7 +49,7 @@ public function create($extensibleClassName, $data = []) $interfaceReflection = new \ReflectionClass($this->getExtensibleInterfaceName($extensibleClassName)); $methodReflection = $interfaceReflection->getMethod('getExtensionAttributes'); - if ($methodReflection->getDeclaringClass() == self::EXTENSIBLE_INTERFACE_NAME) { + if ($methodReflection->getDeclaringClass()->getName() === self::EXTENSIBLE_INTERFACE_NAME) { throw new \LogicException( "Method 'getExtensionAttributes' must be overridden in the interfaces " . "which extend '" . self::EXTENSIBLE_INTERFACE_NAME . "'. "