AttributeManagementTest.php 2.44 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Eav\Model;

class AttributeManagementTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Eav\Api\AttributeManagementInterface
     */
    private $model;

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    private $objectManager;

    protected function setUp()
    {
        $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
        $this->model = $this->objectManager->create(\Magento\Eav\Api\AttributeManagementInterface::class);
    }

    /**
     * Verify that collection in service used correctly
     */
    public function testGetList()
    {
        $productAttributeSetId = $this->getAttributeSetId(
            \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
        );
        $productAttributes = $this->model->getAttributes(
            \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
            $productAttributeSetId
        );
        // Verify that result contains only product attributes
        $this->verifyAttributeSetIds($productAttributes, $productAttributeSetId);

        $categoryAttributeSetId = $this->getAttributeSetId(
            \Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE
        );
        $categoryAttributes = $this->model->getAttributes(
            \Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE,
            $categoryAttributeSetId
        );
        // Verify that result contains only category attributes
        $this->verifyAttributeSetIds($categoryAttributes, $categoryAttributeSetId);
    }

    /**
     * @param string $entityTypeCode
     * @return int
     */
    private function getAttributeSetId($entityTypeCode)
    {
        /** @var \Magento\Eav\Model\Config $eavConfig */
        $eavConfig = $this->objectManager->create(\Magento\Eav\Model\Config::class);
        return $eavConfig->getEntityType($entityTypeCode)->getDefaultAttributeSetId();
    }

    /**
     * @param array $items
     * @param string $attributeSetId
     * @return void
     */
    private function verifyAttributeSetIds(array $items, $attributeSetId)
    {
        /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $item */
        foreach ($items as $item) {
            $this->assertEquals($attributeSetId, $item->getAttributeSetId());
        }
    }
}