AttributeSetManagementTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  7. use Magento\Eav\Model\AttributeSetManagement;
  8. class AttributeSetManagementTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var AttributeSetManagement
  12. */
  13. private $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $repositoryMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $eavConfigMock;
  22. protected function setUp()
  23. {
  24. $this->repositoryMock = $this->createMock(\Magento\Eav\Api\AttributeSetRepositoryInterface::class);
  25. $this->eavConfigMock = $this->createPartialMock(\Magento\Eav\Model\Config::class, ['getEntityType']);
  26. $this->model = new \Magento\Eav\Model\AttributeSetManagement(
  27. $this->eavConfigMock,
  28. $this->repositoryMock
  29. );
  30. }
  31. public function testCreate()
  32. {
  33. $skeletonId = 1;
  34. $entityTypeCode = 'catalog_product';
  35. $entityTypeId = 4;
  36. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  37. $entityTypeMock->expects($this->any())->method('getId')->will($this->returnValue($entityTypeId));
  38. $this->eavConfigMock->expects($this->once())
  39. ->method('getEntityType')
  40. ->with($entityTypeCode)
  41. ->will($this->returnValue($entityTypeMock));
  42. $attributeSetMock = $this->createPartialMock(
  43. \Magento\Eav\Model\Entity\Attribute\Set::class,
  44. ['validate', 'getId', 'setEntityTypeId', 'initFromSkeleton']
  45. );
  46. $attributeSetMock->expects($this->once())->method('validate');
  47. $attributeSetMock->expects($this->once())->method('setEntityTypeId')->with($entityTypeId);
  48. $this->repositoryMock->expects($this->exactly(2))
  49. ->method('save')
  50. ->with($attributeSetMock)
  51. ->will($this->returnValue($attributeSetMock));
  52. $attributeSetMock->expects($this->once())->method('initFromSkeleton')->with($skeletonId);
  53. $this->assertEquals($attributeSetMock, $this->model->create($entityTypeCode, $attributeSetMock, $skeletonId));
  54. }
  55. /**
  56. * @expectedException \Magento\Framework\Exception\InputException
  57. * @expectedExceptionMessage Invalid value of "1" provided for the id field.
  58. */
  59. public function testCreateThrowsExceptionIfGivenAttributeSetAlreadyHasId()
  60. {
  61. $skeletonId = 1;
  62. $entityTypeCode = 'catalog_product';
  63. $attributeSetMock = $this->createPartialMock(
  64. \Magento\Eav\Model\Entity\Attribute\Set::class,
  65. ['validate', 'getId', 'setEntityTypeId', 'initFromSkeleton']
  66. );
  67. $attributeSetMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  68. $this->repositoryMock->expects($this->never())->method('save')->with($attributeSetMock);
  69. $attributeSetMock->expects($this->never())->method('initFromSkeleton')->with($skeletonId);
  70. $this->model->create($entityTypeCode, $attributeSetMock, $skeletonId);
  71. }
  72. /**
  73. * @expectedException \Magento\Framework\Exception\InputException
  74. * @expectedExceptionMessage Invalid value of "0" provided for the skeletonId field.
  75. */
  76. public function testCreateThrowsExceptionIfGivenSkeletonIdIsInvalid()
  77. {
  78. $skeletonId = 0;
  79. $entityTypeCode = 'catalog_product';
  80. $attributeSetMock = $this->createPartialMock(
  81. \Magento\Eav\Model\Entity\Attribute\Set::class,
  82. ['validate', 'getId', 'setEntityTypeId', 'initFromSkeleton']
  83. );
  84. $this->repositoryMock->expects($this->never())->method('save')->with($attributeSetMock);
  85. $attributeSetMock->expects($this->never())->method('initFromSkeleton')->with($skeletonId);
  86. $this->model->create($entityTypeCode, $attributeSetMock, $skeletonId);
  87. }
  88. /**
  89. * @expectedException \Exception
  90. * @expectedExceptionMessage Wrong attribute properties
  91. */
  92. public function testCreateThrowsExceptionIfAttributeSetNotValid()
  93. {
  94. $entityTypeId = 4;
  95. $skeletonId = 5;
  96. $entityTypeCode = 'catalog_product';
  97. $attributeSetMock = $this->createPartialMock(
  98. \Magento\Eav\Model\Entity\Attribute\Set::class,
  99. ['validate', 'getId', 'setEntityTypeId', 'initFromSkeleton']
  100. );
  101. $entityTypeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
  102. $entityTypeMock->expects($this->any())->method('getId')->will($this->returnValue($entityTypeId));
  103. $this->eavConfigMock->expects($this->once())
  104. ->method('getEntityType')
  105. ->with($entityTypeCode)
  106. ->will($this->returnValue($entityTypeMock));
  107. $attributeSetMock->expects($this->once())->method('setEntityTypeId')->with($entityTypeId);
  108. $attributeSetMock->expects($this->once())
  109. ->method('validate')
  110. ->willThrowException(new \Exception('Wrong attribute properties'));
  111. $this->repositoryMock->expects($this->never())->method('save')->with($attributeSetMock);
  112. $attributeSetMock->expects($this->never())->method('initFromSkeleton')->with($skeletonId);
  113. $this->model->create($entityTypeCode, $attributeSetMock, $skeletonId);
  114. }
  115. }