11 use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
15 use Magento\Eav\Model\ConfigFactory;
28 private $attributeManagement;
33 private $setRepositoryMock;
38 private $attributeCollectionMock;
43 private $eavConfigMock;
48 private $entityTypeFactoryMock;
53 private $groupRepositoryMock;
58 private $attributeRepositoryMock;
63 private $attributeResourceMock;
68 private $attributeCollectionFactoryMock;
72 $this->setRepositoryMock =
73 $this->createMock(AttributeSetRepositoryInterface::class);
74 $this->attributeCollectionMock =
75 $this->createMock(Collection::class);
76 $this->eavConfigMock =
77 $this->createMock(Config::class);
78 $this->entityTypeFactoryMock =
79 $this->createPartialMock(ConfigFactory::class, [
'create',
'__wakeup']);
80 $this->groupRepositoryMock =
81 $this->createMock(AttributeGroupRepositoryInterface::class);
82 $this->attributeRepositoryMock =
83 $this->createMock(AttributeRepositoryInterface::class);
84 $this->attributeResourceMock =
85 $this->createMock(Attribute::class);
86 $this->attributeCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
87 ->disableOriginalConstructor()
91 $this->setRepositoryMock,
92 $this->attributeCollectionMock,
94 $this->entityTypeFactoryMock,
95 $this->groupRepositoryMock,
96 $this->attributeRepositoryMock,
97 $this->attributeResourceMock,
98 $this->attributeCollectionFactoryMock
115 $this->setRepositoryMock->expects($this->once())
120 $this->attributeManagement->assign(
141 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
142 $this->setRepositoryMock->expects($this->once())
145 ->willReturn($attributeSetMock);
146 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
147 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
148 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
149 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
150 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
$entityTypeCode+1);
152 $this->attributeManagement->assign(
173 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
174 $this->setRepositoryMock->expects($this->once())
177 ->willReturn($attributeSetMock);
178 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
179 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
180 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
181 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
182 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
$entityTypeCode);
185 ->setMethods([
'getAttributeSetId'])
186 ->disableOriginalConstructor()
187 ->getMockForAbstractClass();
189 $this->groupRepositoryMock->expects($this->once())->method(
'get')->willReturn(
$attributeGroup);
192 $this->attributeManagement->assign(
208 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
209 $this->setRepositoryMock->expects($this->once())
212 ->willReturn($attributeSetMock);
213 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
214 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
215 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
216 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
217 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
$entityTypeCode);
218 $attributeMock = $this->createMock(\
Magento\Eav\Model\Attribute::class);
219 $this->attributeRepositoryMock->expects($this->once())
222 ->willReturn($attributeMock);
223 $attributeMock->expects($this->once())->method(
'getAttributeId')->willReturn(16);
224 $this->attributeResourceMock->expects($this->once())->method(
'saveInSetIncluding')
232 ->willReturn($attributeMock);
233 $attributeMock->expects($this->once())->method(
'setAttributeSetId')->with(
$attributeSetId)->willReturnSelf();
234 $attributeMock->expects($this->once())->method(
'loadEntityAttributeIdBySet')->willReturnSelf();
235 $attributeMock->expects($this->once())->method(
'getData')->with(
'entity_attribute_id')->willReturnSelf();
238 ->setMethods([
'getAttributeSetId'])
239 ->disableOriginalConstructor()
240 ->getMockForAbstractClass();
242 $this->groupRepositoryMock->expects($this->once())->method(
'get')->willReturn(
$attributeGroup);
247 $this->attributeManagement->assign(
262 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
263 $this->setRepositoryMock->expects($this->once())
266 ->willReturn($attributeSetMock);
267 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
268 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
269 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
270 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
271 $attributeMock = $this->createPartialMock(
272 \
Magento\Eav\Model\Entity\Attribute::class,
274 'getEntityAttributeId',
276 'loadEntityAttributeIdBySet',
282 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
'entity type code');
283 $this->attributeRepositoryMock->expects($this->once())
286 ->willReturn($attributeMock);
287 $attributeSetMock->expects($this->once())->method(
'getAttributeSetId')->willReturn(33);
288 $attributeMock->expects($this->once())->method(
'setAttributeSetId')->with(33)->willReturnSelf();
289 $attributeMock->expects($this->once())->method(
'loadEntityAttributeIdBySet')->willReturnSelf();
290 $attributeMock->expects($this->once())->method(
'getEntityAttributeId')->willReturn(12);
291 $attributeMock->expects($this->once())->method(
'getIsUserDefined')->willReturn(
true);
292 $attributeMock->expects($this->once())->method(
'deleteEntity')->willReturnSelf();
305 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
306 $this->setRepositoryMock->expects($this->once())
309 ->willReturn($attributeSetMock);
310 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
311 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
312 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
313 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
314 $attributeMock = $this->createPartialMock(
315 \
Magento\Eav\Model\Entity\Attribute::class,
317 'getEntityAttributeId',
319 'loadEntityAttributeIdBySet',
325 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
'entity type code');
326 $this->attributeRepositoryMock->expects($this->once())
329 ->willReturn($attributeMock);
330 $attributeSetMock->expects($this->once())->method(
'getAttributeSetId')->willReturn(
$attributeSetId);
331 $attributeMock->expects($this->once())->method(
'setAttributeSetId')->with(
$attributeSetId)->willReturnSelf();
332 $attributeMock->expects($this->once())->method(
'loadEntityAttributeIdBySet')->willReturnSelf();
333 $attributeMock->expects($this->once())->method(
'getEntityAttributeId')->willReturn(
null);
334 $attributeMock->expects($this->never())->method(
'getIsUserDefined');
335 $attributeMock->expects($this->never())->method(
'deleteEntity');
339 $this->expectExceptionMessage(
340 'The "code" attribute wasn\'t found in the "1" attribute set. Enter the attribute and try again.' 353 $this->setRepositoryMock->expects($this->once())
370 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
371 $this->setRepositoryMock->expects($this->once())
374 ->willReturn($attributeSetMock);
375 $this->entityTypeFactoryMock->expects($this->once())->method(
'create')->willReturn($this->eavConfigMock);
376 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(66);
377 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
378 $this->eavConfigMock->expects($this->once())->method(
'getEntityType')->with(66)->willReturn($entityTypeMock);
379 $attributeMock = $this->createPartialMock(
380 \
Magento\Eav\Model\Entity\Attribute::class,
382 'getEntityAttributeId',
384 'loadEntityAttributeIdBySet',
390 $entityTypeMock->expects($this->once())->method(
'getEntityTypeCode')->willReturn(
'entity type code');
391 $this->attributeRepositoryMock->expects($this->once())
394 ->willReturn($attributeMock);
395 $attributeSetMock->expects($this->once())->method(
'getAttributeSetId')->willReturn(
$attributeSetId);
396 $attributeMock->expects($this->once())->method(
'setAttributeSetId')->with(
$attributeSetId)->willReturnSelf();
397 $attributeMock->expects($this->once())->method(
'loadEntityAttributeIdBySet')->willReturnSelf();
398 $attributeMock->expects($this->once())->method(
'getEntityAttributeId')->willReturn(12);
399 $attributeMock->expects($this->once())->method(
'getIsUserDefined')->willReturn(
null);
400 $attributeMock->expects($this->never())->method(
'deleteEntity');
410 $attributeCollectionFactoryMock = $this->createPartialMock(
414 $attributeCollectionFactoryMock->expects($this->once())
416 ->willReturn($this->attributeCollectionMock);
418 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
420 $this->attributeManagement,
421 'attributeCollectionFactory',
422 $attributeCollectionFactoryMock
425 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
426 $this->setRepositoryMock->expects($this->once())
429 ->willReturn($attributeSetMock);
430 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
431 $this->eavConfigMock->expects($this->once())
432 ->method(
'getEntityType')
434 ->willReturn($entityTypeMock);
435 $entityTypeMock->expects($this->once())->method(
'getId')->willReturn(88);
436 $attributeSetMock->expects($this->exactly(2))->method(
'getAttributeSetId')->willReturn(88);
437 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(88);
438 $this->attributeCollectionMock->expects($this->once())
439 ->method(
'setAttributeSetFilter')
442 $attributeMock = $this->createMock(\
Magento\Eav\Model\Entity\Attribute::class);
443 $this->attributeCollectionMock->expects($this->once())->method(
'load')->willReturnSelf();
444 $this->attributeCollectionMock->expects($this->once())->method(
'getItems')->willReturn([$attributeMock]);
458 $attributeSetMock = $this->createMock(\
Magento\Eav\Api\Data\AttributeSetInterface::class);
459 $this->setRepositoryMock->expects($this->once())
462 ->willReturn($attributeSetMock);
463 $entityTypeMock = $this->createMock(\
Magento\Eav\Model\Entity\Type::class);
464 $this->eavConfigMock->expects($this->once())
465 ->method(
'getEntityType')
467 ->willReturn($entityTypeMock);
468 $entityTypeMock->expects($this->once())->method(
'getId')->willReturn(77);
469 $attributeSetMock->expects($this->once())->method(
'getAttributeSetId')->willReturn(88);
470 $attributeSetMock->expects($this->once())->method(
'getEntityTypeId')->willReturn(88);
testAssignInputException()
testUnassignStateException()
testAssignNoSuchEntityException()
testUnassignWithWrongAttributeSet()
testUnassignInputException()
testAssignInputExceptionGroupInSet()
testGetAttributesNoSuchEntityException()