AttributeLoaderTest.php 3.76 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Eav\Test\Unit\Model\ResourceModel;

class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Eav\Api\AttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $attributeRepositoryMock;

    /**
     * @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
     */
    private $metadataPoolMock;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
     */
    private $searchCriteriaBuilderMock;

    /**
     * @var \Magento\Eav\Model\ResourceModel\AttributeLoader
     */
    private $attributeLoader;

    protected function setUp()
    {
        $this->attributeRepositoryMock = $this->createMock(\Magento\Eav\Api\AttributeRepositoryInterface::class);
        $this->metadataPoolMock = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->searchCriteriaBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
            ->disableOriginalConstructor()
            ->getMock();
        $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->attributeLoader = $objectManagerHelper->getObject(
            \Magento\Eav\Model\ResourceModel\AttributeLoader::class,
            [
                'attributeRepository' => $this->attributeRepositoryMock,
                'metadataPool' => $this->metadataPoolMock,
                'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock
            ]
        );
    }

    /**
     * @param string $entityType
     * @param int|null $attributeSetId
     * @param string $expectedCondition
     * @dataProvider getAttributesDataProvider
     */
    public function testGetAttributes($entityType, $attributeSetId, $expectedCondition)
    {
        $metadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
        $metadataMock->expects($this->once())
            ->method('getEavEntityType')
            ->willReturn($entityType);
        $this->metadataPoolMock->expects($this->once())
            ->method('getMetadata')
            ->with($entityType)
            ->willReturn($metadataMock);

        $searchCriteria = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
        $this->searchCriteriaBuilderMock->expects($this->once())
            ->method('addFilter')
            ->with(
                \Magento\Eav\Model\ResourceModel\AttributeLoader::ATTRIBUTE_SET_ID,
                $attributeSetId,
                $expectedCondition
            )->willReturnSelf();
        $this->searchCriteriaBuilderMock->expects($this->once())
            ->method('create')
            ->willReturn($searchCriteria);

        $attributeMock = $this->createMock(\Magento\Eav\Api\Data\AttributeInterface::class);
        $searchResultMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSearchResultsInterface::class);
        $searchResultMock->expects($this->once())
            ->method('getItems')
            ->willReturn([$attributeMock]);
        $this->attributeRepositoryMock->expects($this->once())
            ->method('getList')
            ->with($entityType, $searchCriteria)
            ->willReturn($searchResultMock);

        $this->assertEquals([$attributeMock], $this->attributeLoader->getAttributes($entityType, $attributeSetId));
    }

    /**
     * @return array
     */
    public function getAttributesDataProvider()
    {
        return [
            ['entity-type', null, 'neq'],
            ['entity-type', 1, 'eq']
        ];
    }
}