Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeManagementTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory;
15 use Magento\Eav\Model\ConfigFactory;
19 
23 class AttributeManagementTest extends \PHPUnit\Framework\TestCase
24 {
28  private $attributeManagement;
29 
33  private $setRepositoryMock;
34 
38  private $attributeCollectionMock;
39 
43  private $eavConfigMock;
44 
48  private $entityTypeFactoryMock;
49 
53  private $groupRepositoryMock;
54 
58  private $attributeRepositoryMock;
59 
63  private $attributeResourceMock;
64 
68  private $attributeCollectionFactoryMock;
69 
70  protected function setUp()
71  {
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()
88  ->getMock();
89 
90  $this->attributeManagement = new AttributeManagement(
91  $this->setRepositoryMock,
92  $this->attributeCollectionMock,
93  $this->eavConfigMock,
94  $this->entityTypeFactoryMock,
95  $this->groupRepositoryMock,
96  $this->attributeRepositoryMock,
97  $this->attributeResourceMock,
98  $this->attributeCollectionFactoryMock
99  );
100  }
101 
108  {
109  $entityTypeCode = 1;
110  $attributeSetId = 2;
111  $attributeGroupId = 3;
112  $attributeCode = 4;
113  $sortOrder = 5;
114 
115  $this->setRepositoryMock->expects($this->once())
116  ->method('get')
117  ->with($attributeSetId)
118  ->will($this->throwException(new \Magento\Framework\Exception\NoSuchEntityException()));
119 
120  $this->attributeManagement->assign(
125  $sortOrder
126  );
127  }
128 
134  public function testAssignInputException()
135  {
136  $entityTypeCode = 1;
137  $attributeSetId = 2;
138  $attributeGroupId = 3;
139  $attributeCode = 4;
140  $sortOrder = 5;
141  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
142  $this->setRepositoryMock->expects($this->once())
143  ->method('get')
144  ->with($attributeSetId)
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);
151 
152  $this->attributeManagement->assign(
157  $sortOrder
158  );
159  }
160 
167  {
168  $entityTypeCode = 1;
169  $attributeSetId = 2;
170  $attributeGroupId = 3;
171  $attributeCode = 4;
172  $sortOrder = 5;
173  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
174  $this->setRepositoryMock->expects($this->once())
175  ->method('get')
176  ->with($attributeSetId)
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);
183 
184  $attributeGroup = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterface::class)
185  ->setMethods(['getAttributeSetId'])
186  ->disableOriginalConstructor()
187  ->getMockForAbstractClass();
188 
189  $this->groupRepositoryMock->expects($this->once())->method('get')->willReturn($attributeGroup);
190  $attributeGroup->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId + 1);
191 
192  $this->attributeManagement->assign(
197  $sortOrder
198  );
199  }
200 
201  public function testAssign()
202  {
203  $entityTypeCode = 1;
204  $attributeSetId = 2;
205  $attributeGroupId = 3;
206  $attributeCode = 4;
207  $sortOrder = 5;
208  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
209  $this->setRepositoryMock->expects($this->once())
210  ->method('get')
211  ->with($attributeSetId)
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())
220  ->method('get')
222  ->willReturn($attributeMock);
223  $attributeMock->expects($this->once())->method('getAttributeId')->willReturn(16);
224  $this->attributeResourceMock->expects($this->once())->method('saveInSetIncluding')
225  ->with(
226  $attributeMock,
227  16,
230  $sortOrder
231  )
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();
236 
237  $attributeGroup = $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeGroupInterface::class)
238  ->setMethods(['getAttributeSetId'])
239  ->disableOriginalConstructor()
240  ->getMockForAbstractClass();
241 
242  $this->groupRepositoryMock->expects($this->once())->method('get')->willReturn($attributeGroup);
243  $attributeGroup->expects($this->once())->method('getAttributeSetId')->willReturn($attributeSetId);
244 
245  $this->assertEquals(
246  $attributeMock,
247  $this->attributeManagement->assign(
252  $sortOrder
253  )
254  );
255  }
256 
257  public function testUnassign()
258  {
259  $attributeSetId = 1;
260  $attributeCode = 'code';
261 
262  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
263  $this->setRepositoryMock->expects($this->once())
264  ->method('get')
265  ->with($attributeSetId)
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,
273  [
274  'getEntityAttributeId',
275  'setAttributeSetId',
276  'loadEntityAttributeIdBySet',
277  'getIsUserDefined',
278  'deleteEntity',
279  '__wakeup'
280  ]
281  );
282  $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
283  $this->attributeRepositoryMock->expects($this->once())
284  ->method('get')
285  ->with('entity type code', $attributeCode)
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();
293 
294  $this->assertTrue($this->attributeManagement->unassign($attributeSetId, $attributeCode));
295  }
296 
300  public function testUnassignInputException()
301  {
302  $attributeSetId = 1;
303  $attributeCode = 'code';
304 
305  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
306  $this->setRepositoryMock->expects($this->once())
307  ->method('get')
308  ->with($attributeSetId)
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,
316  [
317  'getEntityAttributeId',
318  'setAttributeSetId',
319  'loadEntityAttributeIdBySet',
320  'getIsUserDefined',
321  'deleteEntity',
322  '__wakeup'
323  ]
324  );
325  $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
326  $this->attributeRepositoryMock->expects($this->once())
327  ->method('get')
328  ->with('entity type code', $attributeCode)
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');
336 
337  $this->attributeManagement->unassign($attributeSetId, $attributeCode);
338 
339  $this->expectExceptionMessage(
340  'The "code" attribute wasn\'t found in the "1" attribute set. Enter the attribute and try again.'
341  );
342  }
343 
349  {
350  $attributeSetId = 1234567;
351  $attributeCode = 'code';
352 
353  $this->setRepositoryMock->expects($this->once())
354  ->method('get')
355  ->with($attributeSetId)
356  ->willThrowException(new NoSuchEntityException(__('hello')));
357 
358  $this->attributeManagement->unassign($attributeSetId, $attributeCode);
359  }
360 
365  public function testUnassignStateException()
366  {
367  $attributeSetId = 1;
368  $attributeCode = 'code';
369 
370  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
371  $this->setRepositoryMock->expects($this->once())
372  ->method('get')
373  ->with($attributeSetId)
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,
381  [
382  'getEntityAttributeId',
383  'setAttributeSetId',
384  'loadEntityAttributeIdBySet',
385  'getIsUserDefined',
386  'deleteEntity',
387  '__wakeup'
388  ]
389  );
390  $entityTypeMock->expects($this->once())->method('getEntityTypeCode')->willReturn('entity type code');
391  $this->attributeRepositoryMock->expects($this->once())
392  ->method('get')
393  ->with('entity type code', $attributeCode)
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');
401 
402  $this->attributeManagement->unassign($attributeSetId, $attributeCode);
403  }
404 
405  public function testGetAttributes()
406  {
407  $entityType = 'type';
408  $attributeSetId = 148;
409 
410  $attributeCollectionFactoryMock = $this->createPartialMock(
411  \Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory::class,
412  ['create']
413  );
414  $attributeCollectionFactoryMock->expects($this->once())
415  ->method('create')
416  ->willReturn($this->attributeCollectionMock);
417 
418  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
419  $objectManager->setBackwardCompatibleProperty(
420  $this->attributeManagement,
421  'attributeCollectionFactory',
422  $attributeCollectionFactoryMock
423  );
424 
425  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
426  $this->setRepositoryMock->expects($this->once())
427  ->method('get')
428  ->with($attributeSetId)
429  ->willReturn($attributeSetMock);
430  $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
431  $this->eavConfigMock->expects($this->once())
432  ->method('getEntityType')
433  ->with($entityType)
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')
440  ->with(88)
441  ->willReturnSelf();
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]);
445 
446  $this->assertEquals([$attributeMock], $this->attributeManagement->getAttributes($entityType, $attributeSetId));
447  }
448 
454  {
455  $entityType = 'type';
456  $attributeSetId = 148;
457 
458  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
459  $this->setRepositoryMock->expects($this->once())
460  ->method('get')
461  ->with($attributeSetId)
462  ->willReturn($attributeSetMock);
463  $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
464  $this->eavConfigMock->expects($this->once())
465  ->method('getEntityType')
466  ->with($entityType)
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);
471 
472  $this->attributeManagement->getAttributes($entityType, $attributeSetId);
473  }
474 }
$objectManager
Definition: bootstrap.php:17
$attributeGroupId
__()
Definition: __.php:13
$attributeCode
Definition: extend.phtml:12