AttributeLoaderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity;
  7. use Magento\Eav\Model\Attribute;
  8. use Magento\Eav\Model\Config;
  9. use Magento\Eav\Model\Entity\AbstractEntity;
  10. use Magento\Eav\Model\Entity\AttributeLoader;
  11. use Magento\Eav\Model\Entity\Type;
  12. use Magento\Framework\DataObject;
  13. use Magento\Framework\ObjectManagerInterface;
  14. class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $configMock;
  20. /**
  21. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $objectManagerMock;
  24. /**
  25. * @var AbstractEntity|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $entityMock;
  28. /**
  29. * @var Type|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $entityTypeMock;
  32. /**
  33. * @var AttributeLoader
  34. */
  35. private $attributeLoader;
  36. protected function setUp()
  37. {
  38. $this->configMock = $this->createMock(Config::class, [], [], '', false);
  39. $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
  40. ->setMethods(['create'])
  41. ->disableOriginalConstructor()
  42. ->getMockForAbstractClass();
  43. $this->entityMock = $this->createMock(AbstractEntity::class, [], [], '', false);
  44. $this->entityTypeMock = $this->createMock(Type::class, [], [], '', false);
  45. $this->attributeLoader = new AttributeLoader(
  46. $this->configMock,
  47. $this->objectManagerMock
  48. );
  49. }
  50. public function testLoadAllAttributes()
  51. {
  52. $attributeCode = 'bar';
  53. $entityTypeId = 1;
  54. $dataObject = new DataObject();
  55. $this->entityMock->expects($this->atLeastOnce())->method('getEntityType')->willReturn($this->entityTypeMock);
  56. $this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([$attributeCode]);
  57. $this->entityTypeMock->expects($this->atLeastOnce())->method('getId')->willReturn($entityTypeId);
  58. $this->configMock->expects($this->once())->method('getEntityAttributes')->willReturn([]);
  59. $this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf();
  60. $this->entityTypeMock->expects($this->once())
  61. ->method('getAttributeModel')->willReturn(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL);
  62. $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
  63. ->setMethods(['setAttributeCode', 'setBackendType', 'setIsGlobal', 'setEntityType', 'setEntityTypeId'])
  64. ->disableOriginalConstructor()->getMock();
  65. $this->objectManagerMock->expects($this->once())
  66. ->method('create')->with(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL)->willReturn($attributeMock);
  67. $attributeMock->expects($this->once())->method('setAttributeCode')->with($attributeCode)->willReturnSelf();
  68. $attributeMock->expects($this->once())->method('setBackendType')
  69. ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::TYPE_STATIC)->willReturnSelf();
  70. $attributeMock->expects($this->once())->method('setIsGlobal')->with(1)->willReturnSelf();
  71. $attributeMock->expects($this->once())->method('setEntityType')->with($this->entityTypeMock)->willReturnSelf();
  72. $attributeMock->expects($this->once())->method('setEntityTypeId')->with($entityTypeId)->willReturnSelf();
  73. $this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject);
  74. }
  75. public function testLoadAllAttributesAttributeCodesPresentInDefaultAttributes()
  76. {
  77. $attributeMock = $this->createPartialMock(
  78. \Magento\Eav\Model\Attribute::class,
  79. [
  80. 'setAttributeCode',
  81. 'setBackendType',
  82. 'setIsGlobal',
  83. 'setEntityType',
  84. 'setEntityTypeId'
  85. ]
  86. );
  87. $attributeCodes = ['bar' => $attributeMock];
  88. $defaultAttributes = ['bar'];
  89. $dataObject = new DataObject();
  90. $this->entityMock->expects($this->once())->method('getEntityType')->willReturn($this->entityTypeMock);
  91. $this->configMock->expects($this->once())
  92. ->method('getEntityAttributes')->willReturn($attributeCodes);
  93. $this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn($defaultAttributes);
  94. $this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf();
  95. $this->objectManagerMock->expects($this->never())->method('create');
  96. $this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject);
  97. }
  98. }