AttributeLoaderTest.php 3.57 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Eav\Model\Entity;

use Magento\Framework\DataObject;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;

/**
 * @magentoAppIsolation enabled
 * @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
 */
class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var AttributeLoader
     */
    private $attributeLoader;

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

    /**
     * @var \Magento\Eav\Model\Entity\AbstractEntity
     */
    private $resource;

    protected function setUp()
    {
        CacheCleaner::cleanAll();
        $this->objectManager = Bootstrap::getObjectManager();
        $this->attributeLoader = $this->objectManager->get(AttributeLoader::class);
        $entityType = $this->objectManager->create(\Magento\Eav\Model\Entity\Type::class)
            ->loadByCode('test');
        $context = $this->objectManager->get(\Magento\Eav\Model\Entity\Context::class);
        $this->resource = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
            ->setConstructorArgs([$context])
            ->setMethods(['getEntityType', 'getLinkField'])
            ->getMock();
        $this->resource->method('getEntityType')
            ->willReturn($entityType);
        $this->resource->method('getLinkField')
            ->willReturn('link_field');
    }

    /**
     * @param int $expectedNumOfAttributesByCode
     * @param int $expectedNumOfAttributesByTable
     * @param DataObject|null $object
     * @dataProvider loadAllAttributesDataProvider
     */
    public function testLoadAllAttributesTheFirstTime(
        $expectedNumOfAttributesByCode,
        $expectedNumOfAttributesByTable,
        $object
    ) {
        // Before load all attributes
        $attributesByCode = $this->resource->getAttributesByCode();
        $attributesByTable = $this->resource->getAttributesByTable();
        $this->assertEquals(0, count($attributesByCode));
        $this->assertEquals(0, count($attributesByTable));

        // Load all attributes
        $resource2 = $this->attributeLoader->loadAllAttributes(
            $this->resource,
            $object
        );
        $attributesByCode2 = $resource2->getAttributesByCode();
        $attributesByTable2 = $resource2->getAttributesByTable();
        $this->assertEquals($expectedNumOfAttributesByCode, count($attributesByCode2));
        $this->assertEquals($expectedNumOfAttributesByTable, count($attributesByTable2));
    }

    public function loadAllAttributesDataProvider()
    {
        /** @var \Magento\Eav\Model\Entity\Type $entityType */
        $entityType = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Entity\Type::class)
            ->loadByCode('order');
        $attributeSetId = $entityType->getDefaultAttributeSetId();
        return [
            [
                13,
                2,
                null
            ],
            [
                10,
                1,
                new DataObject(
                    [
                        'attribute_set_id' => $attributeSetId,
                        'store_id' => 0
                    ]
                ),
            ],
            [
                10,
                1,
                new DataObject(
                    [
                        'attribute_set_id' => $attributeSetId,
                        'store_id' => 10
                    ]
                ),
            ],
        ];
    }
}