|
8 | 8 | use Magento\Eav\Model\Attribute; |
9 | 9 | use Magento\Eav\Model\Config; |
10 | 10 | use Magento\Eav\Model\Entity\AbstractEntity; |
11 | | -use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
12 | 11 | use Magento\Eav\Model\Entity\AttributeLoader; |
13 | 12 | use Magento\Eav\Model\Entity\Type; |
14 | 13 | use Magento\Framework\DataObject; |
@@ -41,87 +40,75 @@ class AttributeLoaderTest extends \PHPUnit_Framework_TestCase |
41 | 40 | */ |
42 | 41 | private $attributeLoader; |
43 | 42 |
|
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
44 | 46 | protected function setUp() |
45 | 47 | { |
46 | | - $this->configMock = $this->getMock(Config::class, [], [], '', false); |
47 | | - $this->objectManagerMock = $this->getMock(ObjectManagerInterface::class); |
48 | | - $this->entityMock = $this->getMock(AbstractEntity::class, [], [], '', false); |
49 | | - $this->entityTypeMock = $this->getMock(Type::class, [], [], '', false); |
| 48 | + $this->configMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); |
| 49 | + $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class) |
| 50 | + ->setMethods(['create']) |
| 51 | + ->disableOriginalConstructor()->getMockForAbstractClass(); |
| 52 | + $this->entityMock = $this->getMockBuilder(AbstractEntity::class)->disableOriginalConstructor()->getMock(); |
| 53 | + $this->entityTypeMock = $this->getMockBuilder(Type::class)->disableOriginalConstructor()->getMock(); |
50 | 54 | $this->attributeLoader = new AttributeLoader( |
51 | 55 | $this->configMock, |
52 | 56 | $this->objectManagerMock |
53 | 57 | ); |
54 | 58 | } |
55 | 59 |
|
| 60 | + /** |
| 61 | + * Test for loadAllAttributes method. |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
56 | 65 | public function testLoadAllAttributes() |
57 | 66 | { |
58 | | - $defaultAttributes = ['bar']; |
| 67 | + $attributeCode = 'bar'; |
59 | 68 | $entityTypeId = 1; |
60 | 69 | $dataObject = new DataObject(); |
61 | | - $this->entityMock->expects($this->any()) |
62 | | - ->method('getEntityType') |
63 | | - ->willReturn($this->entityTypeMock); |
64 | | - |
65 | | - $this->entityMock->expects($this->once()) |
66 | | - ->method('getDefaultAttributes') |
67 | | - ->willReturn($defaultAttributes); |
68 | | - $this->entityTypeMock->expects($this->any()) |
69 | | - ->method('getId') |
70 | | - ->willReturn($entityTypeId); |
71 | | - $attributeMock = $this->getMock( |
72 | | - \Magento\Eav\Model\Attribute::class, |
73 | | - [ |
74 | | - 'setAttributeCode', |
75 | | - 'setBackendType', |
76 | | - 'setIsGlobal', |
77 | | - 'setEntityType', |
78 | | - 'setEntityTypeId' |
79 | | - ], |
80 | | - [], |
81 | | - '', |
82 | | - false |
83 | | - ); |
84 | | - $this->configMock->expects($this->once()) |
85 | | - ->method('getEntityAttributes') |
86 | | - ->willReturn(['bar' => $attributeMock]); |
87 | | - $this->entityMock->expects($this->once()) |
88 | | - ->method('addAttribute') |
89 | | - ->with($attributeMock); |
| 70 | + $this->entityMock->expects($this->atLeastOnce())->method('getEntityType')->willReturn($this->entityTypeMock); |
| 71 | + $this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([$attributeCode]); |
| 72 | + $this->entityTypeMock->expects($this->atLeastOnce())->method('getId')->willReturn($entityTypeId); |
| 73 | + $this->configMock->expects($this->once())->method('getEntityAttributes')->willReturn([]); |
| 74 | + $this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf(); |
| 75 | + $this->entityTypeMock->expects($this->once()) |
| 76 | + ->method('getAttributeModel')->willReturn(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL); |
| 77 | + $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class) |
| 78 | + ->setMethods(['setAttributeCode', 'setBackendType', 'setIsGlobal', 'setEntityType', 'setEntityTypeId']) |
| 79 | + ->disableOriginalConstructor()->getMock(); |
| 80 | + $this->objectManagerMock->expects($this->once()) |
| 81 | + ->method('create')->with(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL)->willReturn($attributeMock); |
| 82 | + $attributeMock->expects($this->once())->method('setAttributeCode')->with($attributeCode)->willReturnSelf(); |
| 83 | + $attributeMock->expects($this->once())->method('setBackendType') |
| 84 | + ->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::TYPE_STATIC)->willReturnSelf(); |
| 85 | + $attributeMock->expects($this->once())->method('setIsGlobal')->with(1)->willReturnSelf(); |
| 86 | + $attributeMock->expects($this->once())->method('setEntityType')->with($this->entityTypeMock)->willReturnSelf(); |
| 87 | + $attributeMock->expects($this->once())->method('setEntityTypeId')->with($entityTypeId)->willReturnSelf(); |
| 88 | + $this->entityMock->expects($this->once())->method('addAttribute')->with($attributeMock)->willReturnSelf(); |
90 | 89 | $this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject); |
91 | 90 | } |
92 | 91 |
|
| 92 | + /** |
| 93 | + * Test for loadAllAttributes method with attribute codes present in default attributes. |
| 94 | + * |
| 95 | + * @return void |
| 96 | + */ |
93 | 97 | public function testLoadAllAttributesAttributeCodesPresentInDefaultAttributes() |
94 | 98 | { |
95 | | - $attributeMock = $this->getMock( |
96 | | - \Magento\Eav\Model\Attribute::class, |
97 | | - [ |
98 | | - 'setAttributeCode', |
99 | | - 'setBackendType', |
100 | | - 'setIsGlobal', |
101 | | - 'setEntityType', |
102 | | - 'setEntityTypeId' |
103 | | - ], |
104 | | - [], |
105 | | - '', |
106 | | - false |
107 | | - ); |
108 | | - $attributeCodes = ['bar'=>$attributeMock]; |
| 99 | + $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class) |
| 100 | + ->disableOriginalConstructor()->getMock(); |
| 101 | + $attributeCodes = ['bar' => $attributeMock]; |
109 | 102 | $defaultAttributes = ['bar']; |
110 | 103 | $dataObject = new DataObject(); |
111 | | - $this->entityMock->expects($this->any()) |
112 | | - ->method('getEntityType') |
113 | | - ->willReturn($this->entityTypeMock); |
| 104 | + $this->entityMock->expects($this->once())->method('getEntityType')->willReturn($this->entityTypeMock); |
114 | 105 | $this->configMock->expects($this->once()) |
115 | | - ->method('getEntityAttributes') |
116 | | - ->willReturn($attributeCodes, $dataObject); |
117 | | - $this->entityMock->expects($this->once()) |
118 | | - ->method('getDefaultAttributes') |
119 | | - ->willReturn($defaultAttributes); |
| 106 | + ->method('getEntityAttributes')->willReturn($attributeCodes); |
| 107 | + $this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn($defaultAttributes); |
| 108 | + $this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf(); |
120 | 109 | $this->entityMock->expects($this->atLeastOnce()) |
121 | | - ->method('addAttribute')->with($attributeMock); |
122 | | - |
123 | | - $this->objectManagerMock->expects($this->never()) |
124 | | - ->method('create'); |
| 110 | + ->method('addAttribute')->with($attributeMock)->willReturnSelf(); |
| 111 | + $this->objectManagerMock->expects($this->never())->method('create'); |
125 | 112 | $this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject); |
126 | 113 | } |
127 | 114 | } |
0 commit comments